UNPKG

jaycc

Version:

### the Javascript C Compiler

145 lines (144 loc) 2.81 kB
/* ast.ts */ var Etype; (function (Etype) { Etype[Etype["Int"] = 0] = "Int"; })(Etype || (Etype = {})); ; let test; let log; let numberconstant; let expr; let identifier; let datatype; log = console.log; numberconstant = (input) => { let ret; ret = new Number(input); ret.tag = 'Numberconstant'; return ret; }; expr = (input) => { let ret; ret = new Number(input); ret.tag = 'Expr'; return ret; }; identifier = (input) => { let ret; ret = new String(input); ret.tag = 'Identifier'; return ret; }; datatype = (input) => { let ret; ret = new Number(input); ret.tag = 'Datatype'; return ret; }; class SyntaxException extends Error { constructor(msg) { super(msg); } } class ASE { constructor() { void 0; } } class ASTfunction extends ASE { tag; type; name; body; constructor(type, name, body) { super(); this.tag = 'Function'; this.type = type; this.name = name; this.body = body; } } class Codeblock extends ASE { tag; stms; constructor(input) { super(); this.tag = 'Codeblock'; this.stms = input; } } class Returnstm extends ASE { tag; expr; constructor(input) { super(); this.tag = 'Returnstm'; this.expr = input; } } class Stm { tag; decl; assignment; returnstm; constructor(input) { let ok; this.tag = 'Stm'; ok = false; switch (input.tag) { case 'Decl': this.decl = input; ok = true; break; case 'Assigment': this.assignment = input; ok = true; break; case 'Returnstm': this.returnstm = input; ok = true; break; default: ok = false; break; } if (!ok) throw new SyntaxException('Syntax error in statement'); } } class Program extends ASE { tag; fns; constructor(input) { super(); this.tag = 'Program'; this.fns = input; } } test = () => { let r; let e; let s; let n; let c; let i; let d; let f; let p; let ast; n = numberconstant(5); e = expr(n); r = new Returnstm(e); s = new Stm(r); c = new Codeblock([s]); i = identifier('main'); d = datatype(Etype.Int); f = new ASTfunction(d, i, c); p = new Program([f]); ast = p; log(JSON.stringify(ast, null, 3)); return void 0; }; export { test }; export { numberconstant, expr, identifier, datatype }; export { ASTfunction, Codeblock, Returnstm, Stm, Program, SyntaxException, Etype };