Skip to content

Commit f9ab70f

Browse files
committed
Added Parenthesis
1 parent ec35ed0 commit f9ab70f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

source/lexer.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const (
1010
IntegerLiteral = 1
1111
ArithmeticOperator = 2
1212
Identifier = 3
13+
LeftParenthesis = 4
14+
RightParenthesis = 4
1315
)
1416

1517
// TokenMatch There is a match.
@@ -29,6 +31,16 @@ func isArithmeticOperator(char string) bool {
2931
return char == "+" || char == "-" || char == "*" || char == "/"
3032
}
3133

34+
// isParenthesis checks if a string is a "("
35+
func isLeftParenthesis(char string) bool {
36+
return char == "("
37+
}
38+
39+
// isParenthesis checks if a string is a ")"
40+
func isRightParenthesis(char string) bool {
41+
return char == ")"
42+
}
43+
3244
// lex Lexing a string, and matching tokens.
3345
func lex(line string) []TokenMatch {
3446
var results []TokenMatch
@@ -55,6 +67,10 @@ func lex(line string) []TokenMatch {
5567
results = append(results, TokenMatch{tokenType: IntegerLiteral, raw: raw})
5668
} else if isArithmeticOperator(char) {
5769
results = append(results, TokenMatch{tokenType: ArithmeticOperator, raw: char})
70+
} else if isLeftParenthesis(char) {
71+
results = append(results, TokenMatch{tokenType: LeftParenthesis, raw: char})
72+
} else if isRightParenthesis(char) {
73+
results = append(results, TokenMatch{tokenType: RightParenthesis, raw: char})
5874
} else {
5975

6076
// Identifier

source/parser.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func factor() (Node, error) {
6666
expect(Identifier)
6767

6868
return VariableAccessNode{name: currentToken.raw}, nil
69+
case LeftParenthesis:
70+
expect(LeftParenthesis)
71+
value := expression()
72+
expect(RightParenthesis)
73+
74+
return value, nil
6975
}
7076

7177
return nil, errors.New("didn't find this token")

0 commit comments

Comments
 (0)