-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
126 lines (110 loc) · 2.8 KB
/
main.go
File metadata and controls
126 lines (110 loc) · 2.8 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
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
package main
import (
"flag"
"fmt"
"os"
"os/user"
"github.com/moritz-tiesler/monkey/compiler"
"github.com/moritz-tiesler/monkey/evaluator"
"github.com/moritz-tiesler/monkey/lexer"
"github.com/moritz-tiesler/monkey/object"
"github.com/moritz-tiesler/monkey/parser"
"github.com/moritz-tiesler/monkey/repl"
"github.com/moritz-tiesler/monkey/vm"
)
func main() {
// interpretPtr := flag.Bool("i", true, "runs monkey with interpreter")
compilePtr := flag.Bool("c", false, "runs monkey with compiler")
filePtr := flag.String("f", "", "runs the provided monkey file")
flag.Parse()
in := os.Stdin
out := os.Stdout
switch *filePtr {
case "":
default:
filePath := *filePtr
file, err := os.ReadFile(filePath)
if err != nil {
println(fmt.Sprintf("Could not read file %s", filePath))
}
switch *compilePtr {
case true:
err = compileProgram(string(file))
if err != nil {
fmt.Println(err)
}
case false:
err = interpretProgamm(string(file))
if err != nil {
fmt.Println(err)
}
}
return
}
replMessage()
switch *compilePtr {
case true:
fmt.Printf("Compiling\n")
repl.StartVM(in, out)
default:
fmt.Printf("Interpreting\n")
repl.StartInterpreter(in, out)
}
}
func interpretProgamm(code string) error {
env := object.NewEnvironment()
l := lexer.New(code)
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
for _, e := range p.Errors() {
fmt.Println(e)
}
return fmt.Errorf("exited with parser error")
}
evaluated := evaluator.Eval(program, env)
if evalError, ok := evaluated.(*object.Error); ok {
fmt.Printf("Woops! Evaluation failed:\n %s\n", evalError.Inspect())
return fmt.Errorf("exited with evaluation error")
}
return nil
}
func compileProgram(sourceCode string) error {
constants := []object.Object{}
globals := make([]object.Object, vm.GlobalsSize)
symbolTable := compiler.NewSymbolTable()
for i, v := range object.Builtins {
symbolTable.DefineBuiltin(i, v.Name)
}
l := lexer.New(sourceCode)
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
for _, e := range p.Errors() {
println(e.Error())
}
return fmt.Errorf("exited with parser errors")
}
comp := compiler.NewWithState(symbolTable, constants)
err := comp.Compile(program)
if err != nil {
fmt.Printf("Woops! Compilation failed:\n %s\n", err)
return fmt.Errorf("exited with compiler error")
}
code := comp.Bytecode()
machine := vm.NewWithGlobalStore(code, globals)
err = machine.Run()
if err != nil {
fmt.Printf("Woops! Executing bytecode failed:\n %s\n", err)
return fmt.Errorf("exited with vm error")
}
return nil
}
func replMessage() {
user, err := user.Current()
if err != nil {
panic(err)
}
fmt.Printf("Hello %s! This is the monkey programming language!\n", user.Username)
fmt.Printf("Feel free to type in commands\n")
}