UNPKG

68kcounter

Version:
170 lines (169 loc) 5.54 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.StatementNode = exports.CommentNode = exports.MacroInvocationsNode = exports.MacroArgNode = exports.StringNode = exports.EffectiveAddressNode = exports.QualifierNode = exports.MacroNode = exports.DirectiveNode = exports.MnemonicNode = exports.OpcodeNode = exports.LabelNode = exports.Node = void 0; const json5_1 = __importDefault(require("json5")); const syntax_1 = require("../syntax"); const operandMode_1 = __importDefault(require("./operandMode")); const tokenize_1 = __importDefault(require("./tokenize")); class Node { constructor(start, text, type = "") { this.type = type; this.text = text; this.loc = { start, end: start + text.length, }; } } exports.Node = Node; class LabelNode extends Node { constructor(start, text) { super(start, text, "Label"); this.name = text; this.local = text.indexOf(".") === 0; this.macro = text.includes("@"); } } exports.LabelNode = LabelNode; class OpcodeNode extends Node { constructor(start, text) { super(start, text, "Opcode"); let [op, qualifier] = text.toUpperCase().split("."); if (syntax_1.aliases[op]) op = syntax_1.aliases[op]; if (syntax_1.isMnemonic(op)) { this.op = new MnemonicNode(start, op); if (qualifier === syntax_1.Qualifiers.S) { qualifier = syntax_1.Qualifiers.B; } } else if (syntax_1.isDirective(op)) { this.op = new DirectiveNode(start, op); } else { this.op = new MacroNode(start, op); } if (syntax_1.isQualifier(qualifier)) { const sepPos = text.indexOf("."); this.qualifier = new QualifierNode(start + sepPos + 1, qualifier); } } } exports.OpcodeNode = OpcodeNode; class MnemonicNode extends Node { constructor(start, name) { super(start, name, "Mnemonic"); this.name = name; } } exports.MnemonicNode = MnemonicNode; class DirectiveNode extends Node { constructor(start, name) { super(start, name, "Directive"); this.name = name; } } exports.DirectiveNode = DirectiveNode; class MacroNode extends Node { constructor(start, name) { super(start, name, "Macro"); this.name = name; } } exports.MacroNode = MacroNode; class QualifierNode extends Node { constructor(start, name) { super(start, name, "Qualifier"); this.name = name; } } exports.QualifierNode = QualifierNode; class EffectiveAddressNode extends Node { constructor(start, text) { super(start, text, "EffectiveAddress"); this.mode = operandMode_1.default(text); } } exports.EffectiveAddressNode = EffectiveAddressNode; class StringNode extends Node { constructor(start, text) { super(start, text, "String"); try { this.value = json5_1.default.parse(text); } catch (_) { this.value = ""; console.error("Unable to parse text", { text }); } } } exports.StringNode = StringNode; class MacroArgNode extends Node { constructor(start, text) { super(start, text, "MacroArg"); this.index = parseInt(text.substring(1), 10); } } exports.MacroArgNode = MacroArgNode; class MacroInvocationsNode extends Node { constructor(start, text) { super(start, text, "MacroInvocations"); } } exports.MacroInvocationsNode = MacroInvocationsNode; class CommentNode extends Node { constructor(start, text) { super(start, text, "Comment"); } } exports.CommentNode = CommentNode; class StatementNode extends Node { constructor(text) { super(0, text); this.operands = []; const tokens = tokenize_1.default(text); for (const i in tokens) { const [start, text] = tokens[i]; if (text[0] === ";" || (text[0] === "*" && text[1] !== "+")) { this.comment = new CommentNode(start, text); } else if (start === 0) { this.label = new LabelNode(start, text); } else if (!this.opcode) { this.opcode = new OpcodeNode(start, text); } else { // Operands if (["'", '"'].includes(text[0])) { this.operands.push(new StringNode(start, text)); } else if (text === "\\@") { this.operands.push(new MacroInvocationsNode(start, text)); } else if (text[0] === "\\") { this.operands.push(new MacroArgNode(start, text)); } else { this.operands.push(new EffectiveAddressNode(start, text)); } } } } isLabel() { return !this.opcode && this.label !== undefined; } isInstruction() { return this.opcode !== undefined && this.opcode.op instanceof MnemonicNode; } isDirective() { return this.opcode !== undefined && this.opcode.op instanceof DirectiveNode; } isMacro() { return this.opcode !== undefined && this.opcode.op instanceof MacroNode; } } exports.StatementNode = StatementNode;