Add AST
This commit is contained in:
parent
736ec0f40d
commit
d0c76ef906
11
src/main.ts
11
src/main.ts
@ -1,4 +1,13 @@
|
|||||||
import { Lexer } from './parser/Lexer'
|
import { Lexer } from './parser/Lexer'
|
||||||
|
import { BinaryExpression } from './parser/ast/BinaryExpression'
|
||||||
|
import { Expression } from './parser/ast/Expression'
|
||||||
|
import { Operator } from './parser/ast/Operator'
|
||||||
|
import { ValueExpression } from './parser/ast/ValueExpression'
|
||||||
|
|
||||||
|
let expr1: Expression = new ValueExpression(10)
|
||||||
|
let expr2: Expression = new ValueExpression(20)
|
||||||
|
let exprAdd: Expression = new BinaryExpression(Operator.ADD, expr1, expr2)
|
||||||
|
console.log(exprAdd.eval());
|
||||||
|
|
||||||
const tokens = new Lexer("10 / 2").process().getTokens();
|
const tokens = new Lexer("10 / 2").process().getTokens();
|
||||||
console.log(tokens);
|
console.log(tokens)
|
18
src/parser/ast/BinaryExpression.ts
Normal file
18
src/parser/ast/BinaryExpression.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { Expression } from "./Expression";
|
||||||
|
import { BinaryFunction } from "./Operator";
|
||||||
|
|
||||||
|
export class BinaryExpression implements Expression {
|
||||||
|
constructor(
|
||||||
|
private readonly op: BinaryFunction,
|
||||||
|
private readonly left: Expression,
|
||||||
|
private readonly right: Expression
|
||||||
|
) {
|
||||||
|
this.op = op;
|
||||||
|
this.left = left;
|
||||||
|
this.right = right;
|
||||||
|
}
|
||||||
|
|
||||||
|
public eval(): any {
|
||||||
|
return this.op(this.left.eval(), this.right.eval());
|
||||||
|
}
|
||||||
|
}
|
3
src/parser/ast/Expression.ts
Normal file
3
src/parser/ast/Expression.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export interface Expression {
|
||||||
|
eval(): any
|
||||||
|
}
|
19
src/parser/ast/Operator.ts
Normal file
19
src/parser/ast/Operator.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
export type BinaryFunction = (leftVal: any, rightVal: any) => any;
|
||||||
|
|
||||||
|
export const Operator = {
|
||||||
|
ADD : (leftVal: any, rightVal: any) => leftVal + rightVal,
|
||||||
|
SUBTRACT : (leftVal: any, rightVal: any) => leftVal - rightVal,
|
||||||
|
MULTIPLY : (leftVal: any, rightVal: any) => leftVal * rightVal,
|
||||||
|
DIVIDE : (leftVal: any, rightVal: any) => leftVal / rightVal,
|
||||||
|
|
||||||
|
EQUALS : (leftVal: any, rightVal: any) => (leftVal == rightVal) ? 1 : 0,
|
||||||
|
NOTEQUALS : (leftVal: any, rightVal: any) => (leftVal != rightVal) ? 1 : 0,
|
||||||
|
|
||||||
|
GT : (leftVal: any, rightVal: any) => (leftVal > rightVal) ? 1 : 0,
|
||||||
|
LT : (leftVal: any, rightVal: any) => (leftVal < rightVal) ? 1 : 0,
|
||||||
|
GTEQ : (leftVal: any, rightVal: any) => (leftVal >= rightVal) ? 1 : 0,
|
||||||
|
LTEQ : (leftVal: any, rightVal: any) => (leftVal <= rightVal) ? 1 : 0,
|
||||||
|
|
||||||
|
BOOLEAN_OR : (leftVal: any, rightVal: any) => { ((leftVal != 0) || (rightVal != 0)) ? 1 : 0; },
|
||||||
|
BOOLEAN_AND : (leftVal: any, rightVal: any) => { ((leftVal != 0) && (rightVal != 0)) ? 1 : 0; }
|
||||||
|
};
|
11
src/parser/ast/ValueExpression.ts
Normal file
11
src/parser/ast/ValueExpression.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { Expression } from "./Expression";
|
||||||
|
|
||||||
|
export class ValueExpression implements Expression {
|
||||||
|
constructor(private readonly value: any) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public eval(): any {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
}
|
12
src/parser/ast/VariableExpression.ts
Normal file
12
src/parser/ast/VariableExpression.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { Variables } from "../../runtime/Variables";
|
||||||
|
import { Expression } from "./Expression";
|
||||||
|
|
||||||
|
export class VariableExpression implements Expression {
|
||||||
|
constructor(private readonly variable: string) {
|
||||||
|
this.variable = variable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public eval(): string { // TODO context
|
||||||
|
return Variables.getVariable(this.variable);
|
||||||
|
}
|
||||||
|
}
|
13
src/runtime/Variables.ts
Normal file
13
src/runtime/Variables.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { VariablesHolder } from "./VariablesHolder";
|
||||||
|
|
||||||
|
export class Variables {
|
||||||
|
private static holder: VariablesHolder = new VariablesHolder();
|
||||||
|
|
||||||
|
public static getVariable(name: string): any {
|
||||||
|
return Variables.holder.getVariable(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static setVariable(name: string, value: any): void {
|
||||||
|
Variables.holder.setVariable(name, value);
|
||||||
|
}
|
||||||
|
}
|
26
src/runtime/VariablesHolder.ts
Normal file
26
src/runtime/VariablesHolder.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
export class VariablesHolder {
|
||||||
|
|
||||||
|
private variables: object
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.variables = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(): void {
|
||||||
|
this.variables = {
|
||||||
|
"True": 1,
|
||||||
|
"False": 0
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public getVariable(name: string): any {
|
||||||
|
if (name in this.variables) {
|
||||||
|
return this.variables[name];
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public setVariable(name: string, value: any): void {
|
||||||
|
this.variables[name] = value;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user