UNPKG

cookeylang-ts

Version:
76 lines (75 loc) 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AstPrinter = void 0; const visitor_1 = require("../expr/visitor"); const token_1 = require("../token"); class AstPrinter extends visitor_1.Visitor { constructor(trees) { super(); this.trees = trees; } init() { return `== PARSER == ${this.trees.map(tree => tree.visit(this)).join("\n---\n")} == RESRAP ==`; } ClassDecl(self) { return `class ${self.name.value} {\n\n}`; } FuncDecl(self) { return `function ${self.name.value}(${self.params.map(p => p.value).join(", ")}) { ${self.body.map(stmt => stmt.visit(this)).join("\n---\n")} }`; } VarDecl(self) { return `${token_1.TType[self.mut.type]} ${self.name.value} = ${self.value.visit(this)};`; } ExprStmt(self) { return `${self.expr.visit(this)};`; } IfStmt(self) { return `(if (${self.condition.visit(this)}) ${self.thenBr.visit(this)}${self.elseBr ? ` else ${self.elseBr.visit(this)}` : ""})`; } WhileStmt(self) { return `(while (${self.condition.visit(this)}) ${self.body.visit(this)})`; } ExitStmt(self) { return `(exit ${self.exit.visit(this)})`; } RetStmt(self) { return `ret ${self.value.visit(this)};`; } Block(self) { return `{\n${self.stmts.map(stmt => stmt.visit(this)).join("\n---\n")}\n}`; } Literal(self) { return `${typeof self.value == "string" ? "'" : ""}${String(self.value)}${typeof self.value == "string" ? "'" : ""}`; } Assign(self) { return `(${self.name.value} = ${self.value.visit(this)})`; } Logic(self) { return `(${self.left.visit(this)} ${token_1.TType[self.op.type]} ${self.right.visit(this)})`; } Lambda(self) { return `lambda (${self.params.map(p => p.value).join(", ")}): ${`{ ${self.body.map(stmt => stmt.visit(this)).join("\n---\n")} }`}`; } Binary(self) { return `(${token_1.TType[self.op.type]} ${self.left.visit(this)} ${self.right.visit(this)})`; } Call(self) { return `${self.callee.visit(this)}(${self.args.map(a => a.visit(this)).join(", ")})`; } Unary(self) { return `(${token_1.TType[self.op.type]} ${self.right.visit(this)})`; } Variable(self) { return `${self.name.value}`; } Grouping(self) { return `(${self.value.visit(this)})`; } } exports.AstPrinter = AstPrinter;