-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from h-vien/binary-builder
✨ feature: implement binary builder
- Loading branch information
Showing
17 changed files
with
270 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,77 @@ | ||
import { parserNode } from '@parser/test' | ||
import toPlainObject from '@parser/utils/toPlainObject' | ||
import { BinaryExpression } from '../binary' | ||
import { Expression } from '../index' | ||
import { BinaryExpression } from '../binary/index' | ||
|
||
describe('expression-binary.test', () => { | ||
it('should parse the syntax normally', () => { | ||
const result = parserNode.parse('xin chào === hello', Expression) | ||
|
||
expect(toPlainObject(result)).toStrictEqual({ | ||
type: 'BinaryExpression', | ||
operator: '===', | ||
const res = parserNode.parse('a = 1 + 2 ', Expression) | ||
expect(toPlainObject(res)).toEqual({ | ||
type: 'AssignmentExpression', | ||
left: { | ||
type: 'Identifier', | ||
name: 'xin_ch_224o' | ||
name: 'a' | ||
}, | ||
operator: '=', | ||
right: { | ||
type: 'Identifier', | ||
name: 'hello' | ||
type: 'BinaryExpression', | ||
operator: '+', | ||
left: { | ||
type: 'NumericLiteral', | ||
start: 4, | ||
end: 5, | ||
value: 1, | ||
extra: { | ||
rawValue: 1, | ||
raw: '1' | ||
} | ||
}, | ||
right: { | ||
type: 'NumericLiteral', | ||
start: 8, | ||
end: 9, | ||
value: 2, | ||
extra: { | ||
rawValue: 2, | ||
raw: '2' | ||
} | ||
} | ||
} | ||
} as BinaryExpression) | ||
}) | ||
}) | ||
it('should parse the syntax normally', () => { | ||
const res = parserNode.parse('a = 1 + 2 ', Expression) | ||
const res = parserNode.parse('a - 1 * 2 ', BinaryExpression) | ||
expect(toPlainObject(res)).toEqual({ | ||
type: 'BinaryExpression', | ||
left: { | ||
type: 'Identifier', | ||
name: 'a' | ||
}, | ||
operator: '-', | ||
right: { | ||
type: 'BinaryExpression', | ||
operator: '*', | ||
left: { | ||
type: 'NumericLiteral', | ||
start: 4, | ||
end: 5, | ||
value: 1, | ||
extra: { | ||
rawValue: 1, | ||
raw: '1' | ||
} | ||
}, | ||
right: { | ||
type: 'NumericLiteral', | ||
start: 8, | ||
end: 9, | ||
value: 2, | ||
extra: { | ||
rawValue: 2, | ||
raw: '2' | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Keyword } from '@parser/constants/keyword' | ||
import { Parser } from '@parser/parser' | ||
import { Token } from '@parser/types/token' | ||
import { UnaryExpression } from '../unary' | ||
import { Literal } from '@parser/nodes/literal/index' | ||
import { Expression } from '../index' | ||
import { Identifier } from '@parser/nodes/identifier/index' | ||
|
||
export class BinaryExpressionBuilder { | ||
parser: Parser | ||
identifier?: Identifier | ||
constructor(_parser: Parser, _identifier?: Identifier) { | ||
this.parser = _parser | ||
this.identifier = _identifier | ||
} | ||
BuilderExpression(builderName: string, operatorToken: Token['type']) { | ||
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() { | ||
const res = this.BuilderExpression('MultiplicativeExpression', Keyword.ADDITIVE_OPERATOR) | ||
return res | ||
} | ||
MultiplicativeExpression() { | ||
const res = this.BuilderExpression('PrimaryExpression', Keyword.MULTIPLICATIVE_OPERATOR) | ||
return res | ||
} | ||
RelationalExpression() { | ||
const res = this.BuilderExpression('AdditiveExpression', Keyword.RELATIONAL_OPERATOR) | ||
return res | ||
} | ||
PrimaryExpression() { | ||
switch (this.parser.nextToken?.type) { | ||
case '(': | ||
return this.ParenthesizedExpression() | ||
case Keyword.IDENTIFIER: | ||
return new Identifier(this.parser) | ||
default: | ||
return new Literal(this.parser) | ||
} | ||
} | ||
|
||
ParenthesizedExpression() { | ||
this.parser.validate('(') | ||
const expression = new Expression(this.parser) | ||
this.parser.validate(')') | ||
return expression | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Keyword } from '@parser/constants/keyword' | ||
import { Identifier } from '@parser/nodes/identifier/index' | ||
import { Parser } from '@parser/parser' | ||
import { Expression } from '../index' | ||
import { BinaryExpressionBuilder } from './builder' | ||
|
||
export class BinaryExpression { | ||
type = 'BinaryExpression' | ||
|
||
left: Identifier | Expression | ||
|
||
operator?: string | ||
|
||
right?: Identifier | Expression | ||
|
||
constructor(parser: Parser, identifier?: Identifier) { | ||
this.left = | ||
identifier ?? (parser.nextToken?.type === Keyword.IDENTIFIER ? new Identifier(parser) : new Expression(parser)) | ||
|
||
while ( | ||
[Keyword.RELATIONAL_OPERATOR, Keyword.ADDITIVE_OPERATOR, Keyword.MULTIPLICATIVE_OPERATOR].includes( | ||
parser.nextToken?.type as Keyword | ||
) | ||
) { | ||
this.operator = String(parser.validate(parser.nextToken?.type as any).value) | ||
const binaryBuilder = new BinaryExpressionBuilder(parser, identifier) | ||
|
||
this.right = binaryBuilder.RelationalExpression() | ||
} | ||
} | ||
} |
Oops, something went wrong.