-
Notifications
You must be signed in to change notification settings - Fork 21
/
compiler.go
233 lines (201 loc) · 4.54 KB
/
compiler.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// lexer.go
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strings"
)
func assemble(asm ASM, source, executable string) (err error) {
var srcFile *os.File
var e error
if source == "" {
srcFile, e = ioutil.TempFile("", "")
if e != nil {
err = fmt.Errorf("Creating temporary srcFile failed - %w", e)
return
}
defer os.Remove(srcFile.Name())
} else {
srcFile, e = os.Create(source)
if e != nil {
err = fmt.Errorf("Creating srcFile failed - %w", e)
return
}
}
objectFile, e := ioutil.TempFile("", "")
if e != nil {
err = fmt.Errorf("Creating temporary objectFile failed - %w", e)
return
}
objectFile.Close()
// Write assembly into tmp source file
defer os.Remove(objectFile.Name())
for _, v := range asm.header {
fmt.Fprintf(srcFile, "%v\n", v)
}
for k, v := range asm.constants {
fmt.Fprintf(srcFile, "%-12v%-10v%-15v\n", v, "equ", k)
}
for k, v := range asm.sysConstants {
fmt.Fprintf(srcFile, "%-12v%-10v%-15v\n", v, "equ", k)
}
for _, v := range asm.variables {
fmt.Fprintf(srcFile, "%-12v%-10v%-15v\n", v[0], v[1], v[2])
}
for _, v := range asm.sectionText {
fmt.Fprintf(srcFile, "%v\n", v)
}
for k, f := range asm.functions {
if !f.inline && f.used {
fmt.Fprintf(srcFile, "global %v\n", k)
fmt.Fprintf(srcFile, "%v:\n", k)
for _, v := range f.code {
fmt.Fprintf(srcFile, "%v%-10v%-10v\n", v[0], v[1], v[2])
}
}
}
for _, v := range asm.program {
fmt.Fprintf(srcFile, "%v%-10v%-10v\n", v[0], v[1], v[2])
}
srcFile.Close()
// Find yasm
yasm, e := exec.LookPath("yasm")
if e != nil {
err = fmt.Errorf("'yasm' not found. Please install - %w", e)
return
}
// Assemble
yasmCmd := &exec.Cmd{
Path: yasm,
Args: []string{yasm, "-f", "elf64", srcFile.Name(), "-o", objectFile.Name()},
Stdout: os.Stdout,
Stderr: os.Stderr,
}
if e := yasmCmd.Run(); e != nil {
err = fmt.Errorf("Error while assembling the source code - %w", e)
return
}
// Find ld
ld, e := exec.LookPath("ld")
if e != nil {
err = fmt.Errorf("'ld' not found. Please install - %w", e)
return
}
// Link
ldCmd := &exec.Cmd{
Path: ld,
Args: []string{ld, "-o", executable, objectFile.Name()},
Stdout: os.Stdout,
Stderr: os.Stderr,
}
if e := ldCmd.Run(); e != nil {
err = fmt.Errorf("Error while linking object file - %w", e)
return
}
return
}
func getConvenienceFunctions() []byte {
// the print and println functions for strings can be written in code instead of assembly
return []byte(`
fun print(s string) {
for i, c : s {
print(c)
}
}
fun println(s string) {
print(s)
print(char(10))
}
fun print(b bool) {
if b {
print("true")
} else {
print("false")
}
}
fun println(b bool) {
print(b)
print(char(10))
}
`)
}
func preprocess(program []byte) (out []byte) {
out = append(getConvenienceFunctions(), program...)
out = bytes.ReplaceAll(out, []byte("string"), []byte("[]char"))
re := regexp.MustCompile(`(\".*\")`)
out = re.ReplaceAllFunc(out, func(s []byte) []byte {
tmpS := string(s)
if tmpS == "\"\"" {
return []byte("[](char, 0)")
}
return []byte("['" + strings.Join(strings.Split(tmpS[1:][:len(tmpS)-2], ""), "','") + "']")
})
return
}
func compile(program []byte, sourceFile, binFile string) bool {
tokenChan := make(chan Token, 1)
lexerErr := make(chan error, 1)
program = preprocess(program)
go tokenize(program, tokenChan, lexerErr)
ast, parseErr := parse(tokenChan)
// check error channel on incoming errors
// As we lex and parse simultaneously, there is most likely a parser error as well. But that should be ignored
// as long as we have token errors before!
select {
case e := <-lexerErr:
fmt.Println(e)
return false
default:
}
if parseErr != nil {
fmt.Println(parseErr)
return false
}
ast, semanticErr := semanticAnalysis(ast)
if semanticErr != nil {
fmt.Println(semanticErr)
return false
}
asm := ast.generateCode()
if asmErr := assemble(asm, sourceFile, binFile); asmErr != nil {
fmt.Println(asmErr)
return false
}
return true
}
func main() {
var program []byte = []byte(`
fun test () int {
for ;; {
break
return 0
}
return 1
}
fun test (x int) int {
switch x {
case 1:
return 1
case 3:
return 32
default:
return 3
}
}
`)
if len(os.Args) > 1 {
var err error
program, err = ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
if !compile(program, "source.asm", "executable") {
os.Exit(1)
}
}