forked from meircif/lumi-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmrb.1.mr
More file actions
99 lines (80 loc) · 2.9 KB
/
mrb.1.mr
File metadata and controls
99 lines (80 loc) · 2.9 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
# Builds executables from MR files
#
# Gets input files as "<name>.<MR version>.mr",
# generates "<name>.c" files using <MR version> compiler,
# and compiles the last one to "<name>.exe" executable using "gcc".
# (MR0 and MR1 only supports one file)
#
# Supports MR0, MR1, MR2 & MR3.
native func print(user String text)
native func string-length(user String this): copy Int length
native func string-get(user String this, copy Int index): copy Char ch
native func string-copy(user String this, user String source)
native func string-append(user String this, copy Char ch)
native func string-concat(user String this, user String ext)
native func string-has(user String this, copy Char ch): copy Bool found
native func array-length(user Array{} arr): copy Int length
native func sys-system(user String command): copy Int status
main func(user Array{String} argv)
if argv.length < 2
print(user "usage: mrb [MR file name]...")
return
user String mr-file
[] String mr-file := argv[argv.length - 1]
if mr-file.actual-length < 6
print(user "MR file name too short")
return
var Bool has
if string-has(user mr-file, copy '"'): copy has
print(user "Illegal \" character in file name")
return
user String prefix(mr-file[0:mr-file.actual-length - 4])
var Char version
string-get(user mr-file, copy mr-file.actual-length - 4): copy version
if version < '0' or version > '3'
print(user "Unsupported MR version")
return
if version < '2' and argv.length > 2
print(user "Multiple files not supported before MR2")
return
# run MR compiler
var String{512} command
string-copy(user command, user "mr")
string-append(user command, copy version)
string-concat(user command, user "-compiler.exe")
var Int index(1)
do
while index < argv.length
[] String mr-file := argv[index]
string-concat(user command, copy " \"")
string-concat(user command, user mr-file)
string-concat(user command, copy "\"")
index := index + 1
if version < '2'
string-concat(user command, copy " \"")
string-concat(user command, user prefix)
string-concat(user command, copy "c\"")
var Int status(0)
print(user command)
sys-system(user command): copy status
if status != 0
print(user "MR compiler failed")
return
# run C compiler
string-copy(user command, user "gcc -g \"")
string-concat(user command, user prefix)
string-concat(user command, user "c\" ")
if version = '0'
string-concat(user command, user "mr0-file.c mr0-string.c")
else
string-concat(user command, user "mr.")
string-append(user command, copy version)
string-concat(user command, user ".c")
string-concat(user command, user " -o \"")
string-concat(user command, user prefix)
string-concat(user command, user "exe\"")
print(user command)
sys-system(user command): copy status
if status != 0
print(user "C compiler failed")
return