-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
330 lines (281 loc) · 7.65 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"fmt"
"github.com/llir/llvm/ir"
"github.com/llir/llvm/ir/enum"
"github.com/llir/llvm/ir/types"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
var Target string
var BasePath string
func Assert(cond bool, msg string) {
if cond {
return
}
panic(msg)
}
func AssertErr(err error) {
if err != nil {
panic(err.Error())
}
}
func ExitPrintf(format string, args ...any) {
if len(args) > 0 {
fmt.Printf(format, args)
} else {
fmt.Print(format)
}
os.Exit(0)
}
func FatalPrintf(format string, args ...any) {
fmt.Printf(format, args)
os.Exit(1)
}
func Parse(path string) *Package {
dir, err2 := os.ReadDir(path)
if err2 != nil {
FatalPrintf("failed to read dir: %v", err2)
}
pack := &Package{
Name: "",
Files: make([]*File, 0),
FileMap: make(map[Stmt]string),
ImportMap: make(map[*UseStmt]string),
Path: path,
}
errors := make([]Error, 0)
for _, item := range dir {
if !item.IsDir() {
if filepath.Ext(item.Name()) == ".sea" {
join := filepath.Join(path, item.Name())
fileContent, err2 := os.ReadFile(join)
if err2 != nil {
FatalPrintf("failed to read file: %v", err2)
}
parser := NewParser(join, string(fileContent))
file, errors2 := parser.parse()
pack.Name = file.PackageStmt.Name.Name
errors = append(errors, errors2...)
pack.Files = append(pack.Files, file)
}
}
}
if len(errors) > 0 {
for _, err := range errors {
fmt.Print(err)
}
ExitPrintf("")
}
return pack
}
func Compile(pckg *Package) *ir.Module {
compiler := Compiler{}
compiler.module = ir.NewModule()
compiler.pkg = pckg
compiler.init()
compiler.compile()
return compiler.module
}
func CompilePackage(path string) *Module {
pack := Parse(path)
checker := Check(pack, make(map[string]*Module))
module := Compile(pack)
return &Module{
Module: module,
TypeDefs: checker.TypeDefs,
FuncDef: checker.FuncDefs,
Globals: checker.GlobalVarDefs,
Imports: checker.Imports,
Name: pack.Name,
ImportMap: checker.ImportMap,
}
}
func CompileWrite(p *string, module *Module) string {
outputPath := path.Join(os.TempDir(), module.Name+".ll")
if p != nil {
outputPath = *p
}
newFile, err := os.Create(outputPath)
if err != nil {
panic(err)
}
_, err = module.Module.WriteTo(newFile)
if err != nil {
panic(err)
}
return outputPath
}
func Check(pckg *Package, importMap map[string]*Module) *Checker {
var checker = &Checker{
Package: pckg,
Errors: make([]Error, 0),
ImportMap: importMap,
ImportAliasMap: make(map[string][]string),
PathAliasMap: make(map[string]string),
}
errors, err2 := checker.Check()
if err2 {
for _, err := range errors {
fmt.Print(err)
}
ExitPrintf("")
}
return checker
}
func parseCommandLineArgs() {
Target = "arm64-apple-darwin23.1.0" // TODO
argsLen := len(os.Args)
if argsLen < 2 || (argsLen == 2 && (os.Args[1] != "help" && os.Args[1] != "version")) {
if argsLen < 3 {
//binary arg, command, <file_name>
fmt.Printf("Invalid command: %s\nExample usage: sealang run <main_package_path>", strings.Join(os.Args, " "))
os.Exit(1)
}
}
command := os.Args[1]
if command == "version" {
ExitPrintf("sealang version 0.0.1\n")
} else if command == "help" {
fmt.Fprintf(os.Stdout, `SEALANG v0.0.1
sea is a toy programming language in C family pronounced as C :)
DISCLAIMER: This is the early unstable version
commands:
help: help prints useful command tips
build: build compiles input and runtime files and extracts executable binary to current directory or given output parameter path
USAGE:
sealang build <main_package_path>
sealang build <main_package_path> -o <output_path>
run: run builds the input file and runs executable binary
USAGE:
sealang run <main_package_path>
check: check checks syntax and semantic errors for given file
USAGE:
sealang check <main_package_path>
`)
} else if command == "run" {
BasePath = os.Args[2]
_, file := path.Split(BasePath)
CompileIt("./runtime/runtime.c", path.Join(BasePath, file), true)
} else if command == "check" {
pckg := Parse(os.Args[2])
Check(pckg, make(map[string]*Module))
} else if command == "build" {
BasePath = os.Args[2]
_, file := path.Split(BasePath)
outputPath := path.Join(BasePath, file)
if argsLen > 3 {
argv3 := os.Args[3]
if argv3 != "-o" {
ExitPrintf("invalid parameter: %s\nExample usage: ... -o output_path ", argv3)
}
if argsLen != 5 {
ExitPrintf("invalid parameters: %s\n", "")
}
outputPath = os.Args[4]
}
CompileIt("./runtime/runtime.c", outputPath, false)
}
}
func devMode() {
Target = "arm64-apple-darwin23.1.0" // TODO
_ = os.Setenv("DEBUG", "")
BasePath = "./example"
CompileIt("./runtime/runtime.c", BasePath, true)
}
func CompileIt(runtimePath string, outputPath string, runBinary bool) {
runtimeOut := compileRuntime(runtimePath)
pathList := make([]string, 0)
pathList = append(pathList, runtimeOut)
compilePackage := CompilePackage(BasePath)
join := path.Join(BasePath, ".out")
err := os.Mkdir(join, os.ModePerm)
if !os.IsExist(err) {
AssertErr(err)
}
o := path.Join(join, compilePackage.Name+".ll")
wrapInitFuncs(compilePackage)
output := CompileWrite(&o, compilePackage)
pathList = append(pathList, output)
for _, v := range compilePackage.ImportMap {
o := path.Join(join, v.Name+".ll")
write := CompileWrite(&o, v)
pathList = append(pathList, write)
}
compileAll(pathList, outputPath, true, runBinary)
}
func wrapInitFuncs(compilePackage *Module) {
initWrapperFn := compilePackage.Module.NewFunc("____INIT____", types.Void)
block := initWrapperFn.NewBlock("entry")
mainInit := getInitFunc("main", compilePackage.Module.Funcs)
if mainInit != nil {
block.NewCall(mainInit)
}
for _, module := range compilePackage.ImportMap {
depInit := getInitFunc(module.Name, module.Module.Funcs)
if depInit != nil {
extern := compilePackage.Module.NewFunc(depInit.Name(), depInit.Sig.RetType)
extern.Linkage = enum.LinkageExternal
block.NewCall(depInit)
}
}
block.NewRet(nil)
}
func getInitFunc(pack string, funcs []*ir.Func) *ir.Func {
for _, fn := range funcs {
if fn.Name() == fmt.Sprintf("__%s____init__", pack) {
return fn
}
}
return nil
}
func compileRuntime(runtimePath string) string {
runtimeOutputPath := runtimePath[:strings.LastIndex(runtimePath, ".")] + ".ll"
runtimeCompile := exec.Command("clang", runtimePath, "-S", "-emit-llvm", "-o", runtimeOutputPath)
if err := runtimeCompile.Run(); err != nil {
log.Fatalf("failed to run command for clang: %v", err)
}
return runtimeOutputPath
}
func main() {
//devMode()
parseCommandLineArgs()
}
func compileAll(pathList []string, outputPath string, outputForward bool, runBinary bool) {
argList := make([]string, 0)
argList = append(argList, pathList...)
command := exec.Command("clang", argList...)
command.Args = append(command.Args, "-o")
command.Args = append(command.Args, outputPath)
if outputForward {
command.Stdout = os.Stdout
command.Stderr = os.Stderr
}
err := command.Run()
if err != nil {
log.Fatalf("failed to run command for clang: %v", err)
}
if runBinary {
runBinaryCmd := exec.Command(outputPath)
runBinaryCmd.Stdin = os.Stdin
runBinaryCmd.Stdout = os.Stdout
runBinaryCmd.Stderr = os.Stderr
if err := runBinaryCmd.Run(); err != nil {
os.Exit(runBinaryCmd.ProcessState.ExitCode())
}
}
}
// TODO change it
func compile(path string, abc string, runBinary bool, outputForwarding bool, outputPath string) {
if outputPath == "" {
outputPath = path[:strings.LastIndex(path, ".")] + ""
}
// clang ${path} -o ${path}.o
pathList := make([]string, 0)
pathList = append(pathList, path)
compileAll(pathList, outputPath, runBinary, outputForwarding)
}