-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (118 loc) · 2.7 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"github.com/aerochrome/piper/internal/interpreter"
"github.com/aerochrome/piper/internal/parser"
"os"
"os/signal"
"reflect"
"syscall"
"time"
)
func main() {
forkPtr := flag.Bool("repl", false, "Starts a repl")
flag.Parse()
if !*forkPtr {
handleFileRead()
} else {
handleRepl()
}
}
func handleRepl() {
// Create a channel to handle 'Ctrl+C' (SIGINT)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("\nExiting...")
os.Exit(0)
}()
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("--- The Piper Language REPL ---\nType an expression and instantly see the result\nPress q to quit")
interpreterStruct := interpreter.NewInterpreter()
for {
fmt.Print(">")
input := scanner.Scan()
if !input {
return
}
if scanner.Text() == "q" {
fmt.Println("Quitting...")
break
}
startTime := time.Now()
// Parse
res, err := parser.Parse("", scanner.Bytes())
if err != nil {
fmt.Println(err)
break
}
resExpr := res.([]any)[0].([]any) // The REPL always just has one expression anyway, so no need to loop it
resExpr = interpreter.TransformToReversePolishNotation(resExpr)
evalResult := interpreterStruct.Evaluate(resExpr, nil)
duration := time.Since(startTime)
fmt.Println(fmt.Sprintf("(%s)%v -- took: %s", evalResult.(parser.Expression).GetType(), evalResult, duration))
}
}
func handleFileRead() {
dat, err := os.ReadFile("./src.p")
if err != nil {
panic(err)
}
res, err := parser.Parse("", dat)
if err != nil {
fmt.Println(err)
return
}
// Transform
for idx, v := range res.([]any) {
res.([]any)[idx] = interpreter.TransformToReversePolishNotation(v.([]any))
}
result, err := json.MarshalIndent(res, "", " ")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("output:\n%+v\n", string(result))
printDebug(res.([]any))
interpreterStruct := interpreter.NewInterpreter()
for _, v := range res.([]any) {
evalResult := interpreterStruct.Evaluate(v.([]any), nil)
fmt.Println(evalResult)
}
}
func printDebug(res []any) {
fmt.Println("Debug:")
for idx, v := range res {
if idx > 0 {
fmt.Print(",\n")
}
printAnySliceRecursive(v.([]any))
}
fmt.Printf("\n")
}
func printAnySliceRecursive(slice []any) {
for idx, item := range slice {
if idx > 0 {
fmt.Print(", ")
}
// Is any slice?
itemSlice, ok := item.([]any)
if ok {
fmt.Print("\n[")
printAnySliceRecursive(itemSlice)
fmt.Print("]")
continue
}
// is struct?
if reflect.ValueOf(item).Kind().String() == "struct" {
fmt.Printf("%s%+v", reflect.TypeOf(item), item)
continue
}
// everything else
fmt.Printf("%v", item)
}
}