summaryrefslogtreecommitdiff
path: root/tnslc/compile/compile.tnsl
blob: 0aade7b76f016d56e72033509513404492f2d446 (plain)
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
/##
	Copyright 2021 Kyle Gunger

	This file is licensed under the CDDL 1.0 (the License)
	and may only be used in accordance with the License.
	You should have received a copy of the License with this
	software/source code. If you did not, a copy can be found
	at the following URL:

	https://opensource.org/licenses/CDDL-1.0

	THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO
	WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE
	EXPRESS OR IMPLIED
#/

/; module compile
	# :include "compile/arch/arch.tnsl"
	:include "compile/isa_x86.tnsl"
	# :include "compile/comp.tnsl"
;/


;{}{}charp COMMON_ASM = {
	"\tret",
	"push %",
	"pop %"
}

;struct VType {
	uint _size,
	{}charp name
}

;struct VTrack {
	{}{}charp
		sym_names,
	
	{}VType
		sym_types
}

# Null type
;VType NT = {0, "null"}

/; find_var ({}charp name, ~VTrack tab) [{}charp]
	/; loop (int i = 0; i < 8) [i++]
		/; if (string_equate(tab`.sym_names{i}, name))
			;{}charp out = "r"
			;{}charp n = string_from_int(i + 8)
			;add_strings(~out, ~n)
			/; if (tab`.sym_sizes{i}._size == 4)
				;out.append('d')
			;; else if (tab`.sym_sizes{i}._size == 2)
				;out.append('w')
			;; else if (tab`.sym_sizes{i}._size == 1)
				;out.append('b')
			;/
			;return out
		;/
	;/

	;tnsl.io.print("Failed to find vairable ")
	;tnsl.io.println(name)
;/

/; token_is(~int cur, ~{}Token data, {}charp str) [bool]
	;return string_equate(data`{cur`}.data`, str)
;/

/; setup_vtrack (~int cur, ~{}Token data, ~VTrack tab)
	;cur`++

	/; loop (cur` < len data`) [cur`++]
		/; if (token_is(cur, data, ")"))
			;break
		;; 
		;/
	;/
;/

/; header_guard (~VTrack tab, ~{}charp csec)

;/

/; ret_guard (~VTrack tab, {}charp to_ret, ~{}charp csec)
	;tail_guard(csec)

;/

/; tail_guard (~{}charp csec)
;/

/; compile_statement (~int cur, ~{}Token data, ~{}charp hsec, csec, dsec)
	;cur`++
	/; if (cur` < len data`)
		/; if (token_is(cur, data, "asm"))
			;cur`++
			;{}charp raw_asm = unquote_string(data`{cur`}.data`)
			;raw_asm.append('\n')
			;csec`.append('\t')
			;add_strings(csec, ~raw_asm)
		;/
	;/
;/

/; compile_block (~int cur, ~{}Token data, ~{}charp hsec, csec, dsec)
	;VTrack tab = {
		{"", "", "", "", "", "", "", ""},
		{NT, NT, NT, NT, NT, NT, NT, NT}
	}

	;{}charp name = {}
	;bool r = false
	/; loop (cur`++; cur` < len data`) [cur`++]
		/; if (data`{cur`}.token_type == TOKEN_TYPE.DEFWORD && len name == 0)
			;name = data`{cur`}.data`
			;add_strings(csec, ~name)
			;csec`.append(':')
			;csec`.append('\n')
		;; else if (token_is(cur, data, "("))
			;setup_vtrack(cur, data, ~tab)
		;; else if (token_is(cur, data, "["))
			/; loop (cur`++; cur` < len data`) [cur`++]
				/; if (token_is(cur, data, "]"))
					;break
				;/
			;/
		;; else if (token_is(cur, data, "raw"))
			;r = true
		;; else
			;break
		;/
	;/

	/; if (!r)
		;header_guard(tab, csec)
	;/

	/; loop (cur` < len data`) [cur`++]
		/; if (string_equate(data`{cur`}.data`, ";/"))
			;add_strings(csec, ~(tnslc.COMMON_ASM{0}))
			;break
		;; else if (string_equate(data`{cur`}.data`, "/;"))
			;bool ch = true
			/; loop (ch)
				;compile_block(cur, data, hsec, csec, dsec)
				/; if (cur` !< len data`)
					;break
				;/
				;ch = string_equate(data`{cur`}.data`, ";;")
			;/			
		;; else if (string_equate(data`{cur`}.data`, ";"))
			;compile_statement(cur, data, hsec, csec, dsec)
		;; else
			;tnsl.io.print("Failed to compile token [compile_block]: ")
			;data`{cur`}.print()
			;tnsl.io.println("")
			;break
		;/
	;/

	/; if (!r)
		;tail_guard(csec)
	;/

	;csec`.append('\n')
;/

/; do_compile ({}charp file, ~{}Token data)
	;{}charp hsec = ".global main\n"
	;{}charp csec = ".text\n"
	;{}charp dsec = ".data\n"

	;int j = len data`

	/; loop (int i = 0; i < j) [i++]
		/; if (string_equate(data`{i}.data`, "/;"))
			;compile_block(~i, data, ~hsec, ~csec, ~dsec)
		;; else if (string_equate(data`{i}.data`, ";"))
			;compile_statement(~i, data, ~hsec, ~csec, ~dsec)
		;; else
			;break
		;/
	;/

	;tnsl.io.File out = tnsl.io.writeFile(file)
	
	/; loop (int i = 0; i < len hsec) [i++]
		;out.write(hsec{i})
	;/

	;out.write('\n')

	/; loop (int i = 0; i < len csec) [i++]
		;out.write(csec{i})
	;/

	;out.write('\n')

	/; loop (int i = 0; i < len dsec) [i++]
		;out.write(dsec{i})
	;/

	;out.write('\n')
	;out.close()
;/