-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (58 loc) · 1.27 KB
/
main.go
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
package main
import (
"bufio"
"expression-parsing/descent"
"expression-parsing/eval"
"expression-parsing/lexer"
"expression-parsing/tdop"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
if len(os.Args) > 2 {
fmt.Println("Incorrect usage. Sample usage : go run main.go [descent/tdop].")
return
}
if len(os.Args) == 1 || (len(os.Args) == 2 && os.Args[1] == "descent") {
for {
fmt.Print(">>> ")
text, _ := reader.ReadString('\n')
if text == "exit\n" {
return
}
l := lexer.Lexer{Source: text}
p := descent.NewParser(l)
if expr, parseErr := p.Parse(); parseErr != nil {
fmt.Println(parseErr)
} else {
if value, err := eval.Evaluate(expr); err != nil {
fmt.Println(err)
} else {
fmt.Println(value)
}
}
}
} else if os.Args[1] == "tdop" {
for {
fmt.Print(">>> ")
text, _ := reader.ReadString('\n')
if text == "exit\n" {
return
}
l := lexer.Lexer{Source: text}
p := tdop.NewParser(l)
if expr, parseErr := p.Parse(); parseErr != nil {
fmt.Println(parseErr)
} else {
if value, err := eval.Evaluate(expr); err != nil {
fmt.Println(err)
} else {
fmt.Println(value)
}
}
}
} else {
fmt.Println("Parser not found. Available choices : descent, tdop.")
}
}