Skip to content

Commit

Permalink
✨ feat(binary-expresison): complex logic
Browse files Browse the repository at this point in the history
  • Loading branch information
h-vien committed May 21, 2024
1 parent e5d1a50 commit 239c66c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 28 deletions.
4 changes: 2 additions & 2 deletions package/src/__test__/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ describe('Test every thing', () => {
in ra (a)
a++
}`

const result = parserNode.parse(code, Program)
const test = ` test = 2 % i `
const result = parserNode.parse(test, Program)
console.log(JSON.stringify(result, null, 2))
})
})
48 changes: 33 additions & 15 deletions package/src/nodes/expressions/__test__/assignment.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parserNode } from '@parser/test'
import { AssignmentExpression } from '../assignment'
import toPlainObject from '@parser/utils/toPlainObject'
import { AssignmentExpression } from '../assignment'

describe('expression-assignment.test', () => {
it('should parse the syntax normally', () => {
Expand All @@ -13,14 +13,18 @@ describe('expression-assignment.test', () => {
},
operator: '=',
right: {
type: 'NumericLiteral',
value: 12,
extra: {
rawValue: 12,
raw: '12'
type: 'BinaryExpression',
left: {
type: 'NumericLiteral',
start: 4,
end: 6,
value: 12,
extra: {
rawValue: 12,
raw: '12'
}
},
start: 4,
end: 6
right: {}
}
})
})
Expand All @@ -34,14 +38,28 @@ describe('expression-assignment.test', () => {
},
operator: '=',
right: {
type: 'NumericLiteral',
value: 12,
extra: {
rawValue: 12,
raw: '12'
type: 'BinaryExpression',
left: {
type: 'NumericLiteral',
start: 4,
end: 6,
value: 12,
extra: {
rawValue: 12,
raw: '12'
}
},
start: 4,
end: 6
operator: '+',
right: {
type: 'NumericLiteral',
start: 9,
end: 10,
value: 2,
extra: {
rawValue: 2,
raw: '2'
}
}
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion package/src/nodes/expressions/__test__/binary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ describe('expression-binary.test', () => {
} as BinaryExpression)
})
it('should parse the syntax normally', () => {
const res = parserNode.parse('a % i === 0', Expression)
const res = parserNode.parse('a = 1 + 2 ', Expression)
})
})
13 changes: 8 additions & 5 deletions package/src/nodes/expressions/assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ import { Parser } from '@parser/parser'
import { Keyword } from '@parser/constants/keyword'
import { Identifier } from '../identifier/index'
import { Expression } from './index'
import { BinaryExpression } from './binary'

export class AssignmentExpression {
type = 'AssignmentExpression'

left: Identifier | Expression

operator: string
operator?: string

right: Identifier | Expression
right?: Identifier | Expression

constructor(parser: Parser, identifier?: Identifier | Expression) {
this.left =
identifier ?? (parser.nextToken?.type === Keyword.IDENTIFIER ? new Identifier(parser) : new Expression(parser))

this.operator = String(parser.validate('=').value)

this.right = new Expression(parser)
while (parser.nextToken?.type === '=') {
this.operator = String(parser.validate('=').value)
this.right = new BinaryExpression(parser)
return
}
}
}
2 changes: 0 additions & 2 deletions package/src/nodes/expressions/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export class BinaryExpression {
right: Identifier | Expression

constructor(parser: Parser, identifier?: Identifier) {
const hasMoreOperator = parser.tokenizer.hasMoreOperator()

this.left =
identifier ?? (parser.nextToken?.type === Keyword.IDENTIFIER ? new Identifier(parser) : new Expression(parser))
switch (parser.nextToken?.type) {
Expand Down
4 changes: 1 addition & 3 deletions package/src/nodes/expressions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ export class Expression {
case 'delete':
case 'void':
case 'typeof':
case '+':
case '-':
case '~':
case '!': {
Object.assign(this, new UnaryExpression(parser))
Expand Down Expand Up @@ -92,7 +90,7 @@ export class Expression {

switch (parser.nextToken?.type) {
case '=': {
Object.assign(this, new AssignmentExpression(parser, memberExpression))
Object.assign(this, AssignmentExpression(parser, memberExpression))
break
}
case '(': {
Expand Down

0 comments on commit 239c66c

Please sign in to comment.