-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
106 lines (89 loc) · 1.74 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
package main
import (
"bufio"
_ "embed"
"fmt"
"log"
"os"
"unicode"
)
//go:embed init.scm
var Init string
//go:embed srfi/case-lambda.scm
var CaseLambdaSRFI string
//go:embed srfi/lists.scm
var ListsSRFI string
func validate(code string) bool {
nonws := 0
count := 0
for _, r := range []rune(code) {
if !unicode.IsSpace(r) {
nonws++
}
if r == '(' {
count++
} else if r == ')' {
count--
}
}
return count == 0 && nonws != 0
}
func (ctx *Procedure) Run(code string, quiet bool) {
p := NewParser(code)
p.skipWs()
for len(p.data) > 0 {
v, err := p.GetValue()
p.skipWs()
if err != nil {
log.Fatalf("Error (parse): %v\n", err)
}
ctx.Ins = []Ins{}
if err := ctx.Gen(v); err != nil {
log.Fatalf("Error (gen): %v\n", err)
}
if err := ctx.Eval(); err != nil {
log.Fatalf("Error (eval): %v\n", err)
}
if !quiet {
fmt.Println()
if len(stack) > 0 {
WriteValue(stack.Top(), false)
}
fmt.Println()
}
}
}
func main() {
Top.Scope = TopScope // Put builtins into top-level scope
if int(SymLast) != len(SymbolNames) {
panic("Symbol table length mismatch")
}
Top.Run(Init, true)
Top.Run(CaseLambdaSRFI, true)
Top.Run(ListsSRFI, true)
for k, v := range TopScope.m { // Copy unmodified scope into basescope
BaseScope[k] = v
}
switch len(os.Args) {
case 1:
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
code, _ := reader.ReadString('\n')
for !validate(code) {
fmt.Print(">> ")
next, _ := reader.ReadString('\n')
code += next
}
Top.Run(code, false)
}
case 2:
b, err := os.ReadFile(os.Args[1])
if err != nil {
log.Fatalln("Error: Could not read file")
}
Top.Run(string(b), true)
default:
fmt.Printf("Usage: %s [filename]", os.Args[0])
}
}