Skip to content

Commit

Permalink
ast
Browse files Browse the repository at this point in the history
  • Loading branch information
slince committed Dec 27, 2021
1 parent 0d0d9b2 commit 9a58d5d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 23 deletions.
12 changes: 7 additions & 5 deletions dist/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,13 @@
}
return stmt;
};
Parser.prototype.parseAssignStatement = function () {
var token = this.tokens.current();
var variable = new Identifier(token.value, token.position);
this.tokens.next();
this.tokens.expect(16 /* T_ASSIGN */);
return new AssignStatement(variable, this.parseExpression(), token.position);
};
Parser.prototype.parseBlockStatement = function () {
this.tokens.expect(24 /* T_LBRACE */, 'A block must begin with an opening braces');
var token = this.tokens.current();
Expand All @@ -856,11 +863,6 @@
this.tokens.expect(27 /* T_RBRACE */, 'A block must be closed by a braces');
return new BlockStatement(stmts, token.position);
};
Parser.prototype.parseAssignStatement = function () {
var token = this.tokens.current();
var variable = new Identifier(token.value, token.position);
return new AssignStatement(variable, this.parseExpression(), token.position);
};
Parser.prototype.parseExpression = function () {
var expr = this.parsePrimaryExpression();
if (this.tokens.current().isBinaryOperator()) {
Expand Down
12 changes: 7 additions & 5 deletions dist/expression.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,13 @@ var Parser = /** @class */ (function () {
}
return stmt;
};
Parser.prototype.parseAssignStatement = function () {
var token = this.tokens.current();
var variable = new Identifier(token.value, token.position);
this.tokens.next();
this.tokens.expect(16 /* T_ASSIGN */);
return new AssignStatement(variable, this.parseExpression(), token.position);
};
Parser.prototype.parseBlockStatement = function () {
this.tokens.expect(24 /* T_LBRACE */, 'A block must begin with an opening braces');
var token = this.tokens.current();
Expand All @@ -850,11 +857,6 @@ var Parser = /** @class */ (function () {
this.tokens.expect(27 /* T_RBRACE */, 'A block must be closed by a braces');
return new BlockStatement(stmts, token.position);
};
Parser.prototype.parseAssignStatement = function () {
var token = this.tokens.current();
var variable = new Identifier(token.value, token.position);
return new AssignStatement(variable, this.parseExpression(), token.position);
};
Parser.prototype.parseExpression = function () {
var expr = this.parsePrimaryExpression();
if (this.tokens.current().isBinaryOperator()) {
Expand Down
5 changes: 4 additions & 1 deletion eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Evaluator} from "./dist/expression.mjs";


// const code = `!!+--a`;
const code = `d = 10; d + 2`;
const code = `d2.a++; d2.a + 2`;

const evaluator = new Evaluator();

Expand All @@ -15,6 +15,9 @@ const result2 = evaluator.evaluate(code, {
return {a: 10}
}
},
d2: {
a: 10
},
b: 2,
c: 6
});
Expand Down
15 changes: 14 additions & 1 deletion src/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export default class Lexer{
if (next === '=') {
type = TokenType.T_LE;
this.next();
} else if (next === '<') {
type = TokenType.T_SHL;
this.next();
}
break;
case '>':
Expand All @@ -90,10 +93,13 @@ export default class Lexer{
if (next === '=') {
type = TokenType.T_GE;
this.next();
} else if (next === '>') {
type = TokenType.T_SHR;
this.next();
}
break;
case '&':
type = TokenType.T_LEA;
type = TokenType.T_AMP;
next = this.look();
if (next === '&') {
type = TokenType.T_AND;
Expand Down Expand Up @@ -127,6 +133,13 @@ export default class Lexer{
type = TokenType.T_MOD;
break;

case '|':
type = TokenType.T_PIPE;
break;
case '^':
type = TokenType.T_XOR;
break;

case '(':
type = TokenType.T_LPAREN;
break;
Expand Down
15 changes: 9 additions & 6 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export default class Parser{
return stmt;
}

parseAssignStatement(): ast.AssignStatement{
const token = this.tokens.current();
const variable = new ast.Identifier(token.value, token.position);
this.tokens.next();
this.tokens.expect(TokenType.T_ASSIGN);
return new ast.AssignStatement(variable, this.parseExpression(), token.position);
}

parseBlockStatement(): ast.BlockStatement{
this.tokens.expect(TokenType.T_LBRACE, 'A block must begin with an opening braces');
const token = this.tokens.current();
Expand All @@ -46,12 +54,6 @@ export default class Parser{
return new ast.BlockStatement(stmts, token.position);
}

parseAssignStatement(): ast.AssignStatement{
const token = this.tokens.current();
const variable = new ast.Identifier(token.value, token.position);
return new ast.AssignStatement(variable, this.parseExpression(), token.position);
}

parseExpression(): ast.Expr{
let expr = this.parsePrimaryExpression();
if (this.tokens.current().isBinaryOperator()) {
Expand Down Expand Up @@ -93,6 +95,7 @@ export default class Parser{
expr = this.parseUpdateExpression(true);
break;
case TokenType.T_NOT:
case TokenType.T_KW_NOT:
case TokenType.T_ADD:
case TokenType.T_SUB:
expr = this.parseUnaryExpression();
Expand Down
29 changes: 24 additions & 5 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ export const enum TokenType {
T_STR, // abc
T_NUM, // 123
T_ID, // foo
// punctuation
T_ADD, // +
T_SUB, // -
T_MUL, // *
T_DIV, // /
T_MOD, // %

T_AMP, // &
T_PIPE, // |
T_XOR, // ^
T_SHL, // <<
T_SHR, // >>

T_INC, // ++
T_DEC, // --
T_NOT, // !
T_NEQ, // !=
T_LEA, // &
T_AND, // &&
T_OR, // ||
T_ASSIGN, // =
Expand All @@ -37,8 +44,10 @@ export const enum TokenType {
T_DOT, // .
T_QUESTION, // ?
T_KW_BEGIN,
T_KW_NOT, // not
T_KW_OR, // or
T_KW_AND, // and
T_KW_IN, // in
T_KW_END
}

Expand All @@ -54,11 +63,17 @@ Tokens[TokenType.T_SUB] = '-';
Tokens[TokenType.T_MUL] = '*';
Tokens[TokenType.T_DIV] = '/';
Tokens[TokenType.T_MOD] = '%';

Tokens[TokenType.T_AMP] = '&';
Tokens[TokenType.T_PIPE] = '|';
Tokens[TokenType.T_XOR] = '^';
Tokens[TokenType.T_SHL] = '<<';
Tokens[TokenType.T_SHR] = '>>';

Tokens[TokenType.T_INC] = '++';
Tokens[TokenType.T_DEC] = '--';
Tokens[TokenType.T_NOT] = '!';
Tokens[TokenType.T_NEQ] = '!=';
Tokens[TokenType.T_LEA] = '&';
Tokens[TokenType.T_AND] = '&&';
Tokens[TokenType.T_OR] = '||';
Tokens[TokenType.T_ASSIGN] = '=';
Expand All @@ -79,13 +94,17 @@ Tokens[TokenType.T_SEMICOLON] = ';';
Tokens[TokenType.T_DOT] = '.';
Tokens[TokenType.T_QUESTION] = '?'
// keywords
Tokens[TokenType.T_KW_NOT] = 'not'
Tokens[TokenType.T_KW_OR] = 'or'
Tokens[TokenType.T_KW_AND] = 'and'
Tokens[TokenType.T_KW_IN] = 'in'

// list all keywords.
const keywords: {[key: string]: TokenType} = {
'not': TokenType.T_KW_NOT,
'or': TokenType.T_KW_OR,
'and': TokenType.T_KW_AND
'and': TokenType.T_KW_AND,
'in': TokenType.T_KW_IN,
};

// binary & unary
Expand Down Expand Up @@ -119,14 +138,14 @@ const binaryOperators: {[key: string]: OperatorPrecedence} = {
'<=': {'precedence': 20, 'associativity': OperatorAssociativity.Left},
'not in': {'precedence': 20, 'associativity': OperatorAssociativity.Left},
'in': {'precedence': 20, 'associativity': OperatorAssociativity.Left},
'..': {'precedence': 25, 'associativity': OperatorAssociativity.Left},
'<<': {'precedence': 25, 'associativity': OperatorAssociativity.Left},
'>>': {'precedence': 25, 'associativity': OperatorAssociativity.Left},
'+': {'precedence': 30, 'associativity': OperatorAssociativity.Left},
'-': {'precedence': 30, 'associativity': OperatorAssociativity.Left},
'~': {'precedence': 40, 'associativity': OperatorAssociativity.Left},
'*': {'precedence': 60, 'associativity': OperatorAssociativity.Left},
'/': {'precedence': 60, 'associativity': OperatorAssociativity.Left},
'%': {'precedence': 60, 'associativity': OperatorAssociativity.Left},
'**': {'precedence': 200, 'associativity': OperatorAssociativity.Right},
};

const unaryOperators: {[key: string]: OperatorPrecedence} = {
Expand Down

0 comments on commit 9a58d5d

Please sign in to comment.