-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (129 loc) · 2.52 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
package main
import (
"bytes"
"encoding/gob"
"fmt"
"gold/compiler"
"gold/lexer"
"gold/object"
"gold/parser"
"gold/repl"
"gold/vm"
"os"
"os/user"
)
func main() {
args := os.Args
switch len(args) {
case 1:
user, err := user.Current()
if err != nil {
panic(err)
}
fmt.Printf("Hello %s! This is the Gold programming language!\n",
user.Username)
fmt.Printf("Feel free to type in commands\n")
repl.Start(os.Stdin, os.Stdout)
return
case 3:
var err error
switch args[1] {
case "compile", "c":
err = compileFile(args[2]+".gold", args[2]+".cold")
case "vm", "v", "run", "r":
err = runBinaryFile(args[2] + ".cold")
default:
panic("unknown command")
}
if err != nil {
panic(err)
}
}
}
func compileFile(inputFileName, outputFileName string) error {
inputFile, err := os.ReadFile(inputFileName)
if err != nil {
panic(err)
}
l := lexer.New(string(inputFile))
p := parser.New(l)
program := p.ParseProgram()
comp := compiler.New()
_, err = comp.Compile(program)
if err != nil {
return err
}
bytesBuffer, err := writeBytecode(comp.Bytecode())
if err != nil {
return err
}
outputFile, err := os.OpenFile(outputFileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
_, err = outputFile.Write(bytesBuffer.Bytes())
if err != nil {
return err
}
return nil
}
func writeBytecode(bytecode *compiler.Bytecode) (*bytes.Buffer, error) {
register()
var bytesBuffer bytes.Buffer
enc := gob.NewEncoder(&bytesBuffer)
err := enc.Encode(bytecode)
if err != nil {
return nil, err
}
return &bytesBuffer, nil
}
func register() {
l := []object.Object{
&object.Integer{},
&object.Float{},
&object.Boolean{},
&object.Null{},
&object.ReturnValue{},
&object.Function{},
&object.String{},
&object.Builtin{},
&object.Array{},
&object.Hash{},
&object.CompiledFunction{},
&object.Closure{},
}
for _, t := range l {
gob.Register(t)
}
}
func runBinaryFile(fileName string) error {
bytecode, err := readBytecode(fileName)
if err != nil {
return err
}
vm := vm.New(bytecode)
err = vm.Run()
if err != nil {
return err
}
return nil
}
func readBytecode(filename string) (*compiler.Bytecode, error) {
var bytesBuffer bytes.Buffer
file, err := os.Open(filename)
if err != nil {
return nil, err
}
_, err = bytesBuffer.ReadFrom(file)
if err != nil {
return nil, err
}
register()
dec := gob.NewDecoder(&bytesBuffer)
var bytecode compiler.Bytecode
err = dec.Decode(&bytecode)
if err != nil {
return nil, err
}
return &bytecode, nil
}