-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (60 loc) · 1.18 KB
/
main.go
File metadata and controls
66 lines (60 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
)
// Notes here
// TODO read about types
// TODO read about interfaces
// TODO read about panic/recover
// TODO read about godoc
// Token types
const (
NONE = 0
INTEGER = 1
PLUS = 2
MINUS = 3
MUL = 4
DIV = 5
LPAREN = 6
RPAREN = 7
)
func main() {
reader := bufio.NewReader(os.Stdin)
for {
input, _ := reader.ReadString('\n')
text := strings.Split(strings.TrimRight(input, "\n"), "")
if len(text) == 0 {
continue
}
lxr := NewLexer(text)
p := parser{nil, &lxr, GetTokenTypeNames()}
result, parseError := p.Parse()
fmt.Println("result:", result, "parseError:", parseError)
if parseError != nil {
fmt.Println(parseError)
continue
}
i := interpreter{}
fmt.Println(i.Visit(result))
}
}
func GetTokenTypeNames() map[int]string {
ttNames := make(map[int]string)
ttNames[NONE] = "NONE"
ttNames[INTEGER] = "INTEGER"
ttNames[PLUS] = "PLUS"
ttNames[MINUS] = "MINUS"
ttNames[MUL] = "MUL"
ttNames[DIV] = "DIV"
ttNames[LPAREN] = "LPAREN"
ttNames[RPAREN] = "RPAREN"
return ttNames
}
func IsDigit(char string) bool {
isDigit, _ := regexp.MatchString("^[0-9]$", char)
return isDigit
}