-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
54 lines (51 loc) · 1.01 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
package main
import (
"fmt"
"lisp-compiler/core"
"lisp-compiler/utils"
"os"
"strings"
)
func main() {
if len(os.Args) < 2 {
fmt.Println(`
Usage: lisp-compiler <mode> <input-path>
mode: interpret,compile, default: compile
`)
return
}
var input string
var err error
var mode string
if len(os.Args) == 3 {
mode = os.Args[1]
input, err = utils.LoadLispFileToString(strings.TrimSpace(os.Args[2]))
}
if len(os.Args) == 2 {
mode = "compile"
input, err = utils.LoadLispFileToString(strings.TrimSpace(os.Args[1]))
}
if err != nil {
panic(err)
}
parser := core.NewParser(input)
asm := ""
symbol := "%sym1"
parsed := parser.Parse()
if mode == "interpret" {
scope := core.NewInterpreterScope(nil)
var value int
for _, parsedExpr := range parsed {
value = parsedExpr.Eval(scope)
}
fmt.Println(value)
return
} else {
scope := core.NewCompilerScope(nil)
for _, parsedExpr := range parsed {
parsedExpr.Codegen(&asm, symbol, scope)
asm += "\n"
}
utils.WriteLLVMAssembly(asm)
}
}