-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.swift
More file actions
89 lines (73 loc) · 1.97 KB
/
main.swift
File metadata and controls
89 lines (73 loc) · 1.97 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
//
// main.swift
//
// SwiSwi - a tiny Swift-like language
//
// Created for the Budapest Swift Meetup
// by Árpád Goretity (H2CO3)
// on 28/10/2015
//
// There's no warranty whatsoever.
//
import Foundation
func removeExtension(var path: String) -> String {
if let ext = NSURL(string: path)?.pathExtension {
let range = path.rangeOfString(
"." + ext, // path extension does _not_ include the dot
options: .BackwardsSearch
)
path.removeRange(range!)
}
return path
}
func main() {
let fname = String.fromCString(Process.unsafeArgv[1])!
print("compiling \(fname)...")
let source = try! NSString(contentsOfFile: fname, encoding: NSUTF8StringEncoding)
print("lex...")
let lexer = Lexer(source as String)
guard let tokens = lexer.lex() else {
print(lexer.error)
print(lexer.location)
return
}
print("parse...")
let parser = Parser(tokens)
guard var ast = parser.parse() else {
print(parser.error)
print(parser.location)
return
}
print("typecheck...")
var ctx = DeclCtx()
guard let _ = ast.inferType(&ctx) else {
print(ctx.errmsg!)
print("at \(ctx.errnode!.loc)")
return
}
print("constant propagation...")
ast = performConstProp(ast) as! ProgramAST
print("dead code elimination...")
ast = performDCE(ast) as! ProgramAST
// For debugging purposes...
// print(ast.toString(0))
// Generate LLVM bitcode
print("codegen...")
let codeGen = CodeGen()
let module = codeGen.codegenProgram(ast, fname)
if module != nil { // "if let" doesn't work with C pointers...
let execName = removeExtension(fname)
let bitcodeName = execName + ".bc"
// write binary bitcode to file
LLVMWriteBitcodeToFile(module, bitcodeName)
// make it human-readable (for debugging)
system("llvm-dis \(bitcodeName)")
// Piggy-back on clang to invoke linker
print("linking \(bitcodeName) -> \(execName)...")
system("clang -std=c99 -Wall -O -o \(execName) \(bitcodeName) runtime.c")
} else {
print(codeGen.errmsg!)
print("at \(codeGen.errnode!.loc)")
}
}
main()