UNPKG

toylang

Version:

A toy programming language built with TypeScript for learning purposes

185 lines (184 loc) 5.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SExpressionFactory = exports.DefaultASTFactory = void 0; var typings_1 = require("./typings"); exports.DefaultASTFactory = { Program: function (body) { return { type: typings_1.tl.SyntaxKind.Program, body: body }; }, EmptyStatement: function () { return { type: typings_1.tl.SyntaxKind.EmptyStatement }; }, BlockStatement: function (body) { return { type: typings_1.tl.SyntaxKind.BlockStatement, body: body, }; }, ExpressionStatement: function (expression) { return { type: typings_1.tl.SyntaxKind.ExpressionStatement, expression: expression, }; }, IfStatement: function (test, consequent, alternate) { return { type: typings_1.tl.SyntaxKind.IfStatement, test: test, consequent: consequent, alternate: alternate, }; }, ReturnStatement: function (argument) { return { type: typings_1.tl.SyntaxKind.ReturnStatement, argument: argument, }; }, WhileStatement: function (test, body) { return { type: typings_1.tl.SyntaxKind.WhileStatement, test: test, body: body, }; }, DoWhileStatement: function (test, body) { return { type: typings_1.tl.SyntaxKind.DoWhileStatement, test: test, body: body, }; }, ForStatement: function (props) { return { type: typings_1.tl.SyntaxKind.ForStatement, test: props.test, init: props.init, update: props.update, body: props.body, }; }, VariableStatement: function (declarations) { return { type: typings_1.tl.SyntaxKind.VariableStatement, declarations: declarations, }; }, VariableDeclaration: function (id, init) { return { type: typings_1.tl.SyntaxKind.VariableDeclaration, id: id, init: init, }; }, AssignmentExpression: function (_a) { var operator = _a.operator, left = _a.left, right = _a.right; return { type: typings_1.tl.SyntaxKind.AssignmentExpression, operator: operator, left: left, right: right, }; }, BinaryExpression: function (operator, left, right) { return { type: typings_1.tl.SyntaxKind.BinaryExpression, operator: operator, left: left, right: right, }; }, LogicalExpression: function (operator, left, right) { return { type: typings_1.tl.SyntaxKind.LogicalExpression, operator: operator, left: left, right: right, }; }, Identifier: function (name) { return { type: typings_1.tl.SyntaxKind.Identifier, name: name }; }, NullLiteral: function () { return { type: typings_1.tl.SyntaxKind.NullLiteral, value: null, }; }, BooleanLiteral: function (value) { return { type: typings_1.tl.SyntaxKind.BooleanLiteral, value: value, }; }, StringLiteral: function (value) { return { type: typings_1.tl.SyntaxKind.StringLiteral, value: value, }; }, NumericLiteral: function (value) { return { type: typings_1.tl.SyntaxKind.NumericLiteral, value: value, }; }, NewExpression: function (callee, args) { return { type: typings_1.tl.SyntaxKind.NewExpression, callee: callee, arguments: args, }; }, UnaryExpression: function (operator, argument) { return { type: typings_1.tl.SyntaxKind.UnaryExpression, operator: operator, argument: argument, }; }, ThisExpression: function () { return { type: typings_1.tl.SyntaxKind.ThisExpression, }; }, Super: function () { return { type: typings_1.tl.SyntaxKind.Super, }; }, ClassDeclaration: function (id, body, superClass) { return { type: typings_1.tl.SyntaxKind.ClassDeclaration, superClass: superClass, id: id, body: body, }; }, FunctionStatement: function (name, body, params) { return { type: typings_1.tl.SyntaxKind.FunctionDeclaration, name: name, body: body, params: params, }; }, }; exports.SExpressionFactory = { Program: function (body) { return ["begin", body]; }, EmptyStatement: function () { }, BlockStatement: function (body) { return ["begin", body]; }, ExpressionStatement: function (expression) { return expression; }, StringLiteral: function (value) { return "" + value; }, NumericLiteral: function (value) { return value; }, };