File tree 2 files changed +22
-0
lines changed 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ const (
10
10
IntegerLiteral = 1
11
11
ArithmeticOperator = 2
12
12
Identifier = 3
13
+ LeftParenthesis = 4
14
+ RightParenthesis = 4
13
15
)
14
16
15
17
// TokenMatch There is a match.
@@ -29,6 +31,16 @@ func isArithmeticOperator(char string) bool {
29
31
return char == "+" || char == "-" || char == "*" || char == "/"
30
32
}
31
33
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
+
32
44
// lex Lexing a string, and matching tokens.
33
45
func lex (line string ) []TokenMatch {
34
46
var results []TokenMatch
@@ -55,6 +67,10 @@ func lex(line string) []TokenMatch {
55
67
results = append (results , TokenMatch {tokenType : IntegerLiteral , raw : raw })
56
68
} else if isArithmeticOperator (char ) {
57
69
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 })
58
74
} else {
59
75
60
76
// Identifier
Original file line number Diff line number Diff line change @@ -66,6 +66,12 @@ func factor() (Node, error) {
66
66
expect (Identifier )
67
67
68
68
return VariableAccessNode {name : currentToken .raw }, nil
69
+ case LeftParenthesis :
70
+ expect (LeftParenthesis )
71
+ value := expression ()
72
+ expect (RightParenthesis )
73
+
74
+ return value , nil
69
75
}
70
76
71
77
return nil , errors .New ("didn't find this token" )
You can’t perform that action at this time.
0 commit comments