Skip to content

Commit

Permalink
✨ feat(binary-expression): implement builder binary
Browse files Browse the repository at this point in the history
  • Loading branch information
h-vien committed May 22, 2024
1 parent 239c66c commit d38ab88
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package/src/__test__/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Test every thing', () => {
in ra (a)
a++
}`
const test = ` test = 2 % i `
const test = ` test = 2 + i -1`
const result = parserNode.parse(test, Program)
console.log(JSON.stringify(result, null, 2))
})
Expand Down
2 changes: 1 addition & 1 deletion package/src/nodes/expressions/assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Parser } from '@parser/parser'
import { Keyword } from '@parser/constants/keyword'
import { Identifier } from '../identifier/index'
import { Expression } from './index'
import { BinaryExpression } from './binary'
import { BinaryExpression } from './binary/index'

export class AssignmentExpression {
type = 'AssignmentExpression'
Expand Down
38 changes: 38 additions & 0 deletions package/src/nodes/expressions/binary/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Identifier } from '@parser/nodes/identifier/index'
import { Expression } from '../index'
import { Parser } from '@parser/parser'
import { Token } from '@parser/types/token'
import { Operator } from '@parser/types/operator'

export class BinaryExpressionBuilder {
parser: Parser
constructor(_parser: Parser) {
this.parser = _parser
}
BuilderExpression(builderName: string, operatorToken: Operator) {
let left = this[builderName]?.()

while (this.parser.nextToken?.type === operatorToken) {
const operator = this.parser.validate(operatorToken).value
const right = this[builderName]()
left = {
type: 'BinaryExpression',
operator,
left,
right
}
}

return left
}

AdditiveExpression() {
console.log('vao dau')
return {
type: 'BinaryExpression',
operator: '+',
left: new Identifier(this.parser),
right: new Expression(this.parser)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Keyword } from '@parser/constants/keyword'
import { Identifier } from '@parser/nodes/identifier/index'
import { Parser } from '@parser/parser'
import { Identifier } from '../identifier/index'
import { Expression } from './index'
import { Expression } from '../index'
import { BinaryExpressionBuilder } from './builder'
import { Operator } from '@parser/types/operator'
import { CLIENT_RENEG_LIMIT } from 'tls'

export class BinaryExpression {
type = 'BinaryExpression'
Expand All @@ -13,8 +16,10 @@ export class BinaryExpression {
right: Identifier | Expression

constructor(parser: Parser, identifier?: Identifier) {
const binaryBuilder = new BinaryExpressionBuilder(parser)
this.left =
identifier ?? (parser.nextToken?.type === Keyword.IDENTIFIER ? new Identifier(parser) : new Expression(parser))
identifier ??
(parser.nextToken?.type === Keyword.IDENTIFIER ? new Identifier(parser) : binaryBuilder.AdditiveExpression())
switch (parser.nextToken?.type) {
case '+':
case '-':
Expand Down
4 changes: 2 additions & 2 deletions package/src/nodes/expressions/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Keyword } from '@parser/constants/keyword'
import { Parser } from '@parser/parser'
import { AssignmentExpression } from './assignment'
import { BinaryExpression } from './binary'
import { CallExpression } from './call'
import { MemberExpression } from './member'
import { UnaryExpression } from './unary'
Expand All @@ -12,6 +11,7 @@ import { LabelledStatement } from '../statements/label'
import { ObjectExpression } from './object'
import { Literal } from '../literal/index'
import { Identifier } from '../identifier/index'
import { BinaryExpression } from './binary/index'

export class Expression {
[key: string]: any
Expand Down Expand Up @@ -90,7 +90,7 @@ export class Expression {

switch (parser.nextToken?.type) {
case '=': {
Object.assign(this, AssignmentExpression(parser, memberExpression))
Object.assign(this, new AssignmentExpression(parser, memberExpression))
break
}
case '(': {
Expand Down
1 change: 1 addition & 0 deletions package/src/nodes/statements/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Statement } from './index'

export class StatementItem {
constructor(parser: Parser) {
console.log(parser.nextToken?.type)
switch (parser.nextToken?.type) {
case Keyword.LET:
case Keyword.CONST:
Expand Down
7 changes: 0 additions & 7 deletions package/src/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,4 @@ export class Tokenizer {

throw new SyntaxError(`Unexpected token: "${string[0]}"`)
}

public hasMoreOperator() {
// const operators = ["<", ">", "=", "!=", "===", "!=="];
// const parts = expression.split(new RegExp(`([${operators.join("")}])`, "g"));
const parts = this.parser.syntax.split('{')[0]
return /[+\-*/%<>=!&|^]+/.test(parts)
}
}

0 comments on commit d38ab88

Please sign in to comment.