| 26 | |
| 27 | |
| 28 | export default class InterpreterModule { |
| 29 | private static _visitorMap = { |
| 30 | [NodeType.Program]: new Program(), |
| 31 | [NodeType.InitStatement]: new InitStatement(), |
| 32 | [NodeType.PrintStatement]: new PrintStatement(), |
| 33 | [NodeType.EmptyStatement]: new EmptyStatement(), |
| 34 | [NodeType.BlockStatement]: new BlockStatement(), |
| 35 | [NodeType.VariableStatement]: new VariableStatement(), |
| 36 | [NodeType.IdentifierExpression]: new IdentifierExpression(), |
| 37 | [NodeType.VariableDeclaration]: new VariableDeclaration(), |
| 38 | [NodeType.AssignmentExpression]: new AssignmentExpression(), |
| 39 | [NodeType.ExpressionStatement]: new ExpressionStatement(), |
| 40 | [NodeType.BinaryExpression]: new BinaryExpression(), |
| 41 | [NodeType.LogicalExpression]: new BinaryExpression(), |
| 42 | [NodeType.StringLiteral]: new StringLiteral(), |
| 43 | [NodeType.NumericLiteral]: new NumericLiteral(), |
| 44 | [NodeType.BooleanLiteral]: new BooleanLiteral(), |
| 45 | [NodeType.NullLiteral]: new NullLiteral(), |
| 46 | [NodeType.IfStatement]: new IfStatement(), |
| 47 | [NodeType.WhileStatement]: new WhileStatement(), |
| 48 | [NodeType.BreakStatement]: new BreakStatement(), |
| 49 | [NodeType.ContinueStatement]: new ContinueStatement(), |
| 50 | } as Record<string, Visitor>; |
| 51 | |
| 52 | private static _currentScope: Scope; |
| 53 | private static _interpreter: Interpreter; |
| 54 | |
| 55 | static getVisitor(nodeType: string) { |
| 56 | const visitor = InterpreterModule._visitorMap[nodeType]; |
| 57 | |
| 58 | if (!visitor) |
| 59 | throw new InvalidStateException( |
| 60 | `Couldn't find any visitor object for nodeType: ${nodeType}` |
| 61 | ); |
| 62 | |
| 63 | return visitor; |
| 64 | } |
| 65 | |
| 66 | static getInterpreter() { |
| 67 | this._interpreter = |
| 68 | this._interpreter ?? new Interpreter(parser, this.getCurrentScope()); |
| 69 | return this._interpreter; |
| 70 | } |
| 71 | |
| 72 | static getCurrentScope() { |
| 73 | this._currentScope = this._currentScope ?? new Scope(null); |
| 74 | return this._currentScope; |
| 75 | } |
| 76 | |
| 77 | static setCurrentScope(scope: Scope) { |
| 78 | this._currentScope = scope; |
| 79 | } |
| 80 | } |
nothing calls this directly
no outgoing calls
no test coverage detected