summaryrefslogtreecommitdiff
path: root/src/compiler/compiler.lm
blob: b19743d77683575267eccae17c08a91d034c2f68 (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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# Filespec compiler
# Takes in fspec source code and outputs bytecode for further processing

include 'expr.lm'

context fspec
   context primitive
      token TYPE_SIGN / [us] /
      token TYPE_BITS / [1-9][0-9]/
      int strtoull(a:strb:int) = c_strtoull

      def type
         signed:bool
         bits:int
         [TYPE_SIGN TYPE_BITS] {
            lhs.signed = ($r1 == 's')
            lhs.bits = strtoull($r2, 10)
         }
   end

   context container
      context enum
         lex
            ignore / '//' [^\n]'\n' | space+ /
            literal `= `, `{ `}
         end

         literal `enum

         int
         const_int_expr(expr:collapser::collapsed) {
            if (!expr || !expr.result.value || !expr.result.value.number) reject
            return expr.result.value.number.value
         }

         def item
            value:int
            [name:reference::variable::type `= expr::enum::type `, item] { lhs.value = const_int_expr(r3.collapsed) }
         |  [name:reference::variable::type `= expr::enum::type] { lhs.value = const_int_expr(r3.collapsed) }
         |  [name:reference::variable::type `, item] { lhs.value = 0 } # TODO: count
         |  [name:reference::variable::type] { lhs.value = 0 } # TODO: count

         def type
            name:str
            items:item+
            select:expr::paren::type
            [type:`enum WS+ reference::variable::type? `{ item+ `}] {
               for n:reference::variable::type in child(r3)
               lhs.name = $n lhs.items = r5
            }
      end

      context strukt # <- struct is taken :(
         lex
            ignore / '//' [^\n]'\n' | space+ /
            literal `{ `}
         end

         literal `struct

         def item
            [data:declaration::type]

         def type
            name:str
            items:item+
            select:expr::paren::type
            [type:`struct WS+ reference::variable::type? `{ item+ `}] {
               for n:reference::variable::type in child(r3) lhs.name = $n
               lhs.items = r5
            }
      end

      context select
         lex
            ignore / '//' [^\n]'\n' | space+ /
            literal `( `) `{ `} `*
         end

         literal `select

         def item
            [`* `) data:declaration::type]
         |  [expr:expr::paren::type `) data:declaration::type]

         def type
            name:str
            items:item+ # BUG: marking item+ with items: in the match below causes weird behaviour
            select:expr::paren::type
            [type:`select `( expr::paren::type `) `{ item+ `}] { lhs.select = r3 lhs.items = r6 }
      end

      def type
         [data:enum::type] | [data:strukt::type] | [data:select::type]
   end

   context declaration
      lex
         ignore / '//' [^\n]'\n' | space+ /
         literal `: `[ `] `| `;
         token VISUAL / 'nul' | 'dec' | 'hex' | 'str' /
         token ENDIANESS / 'le' | 'be' /
      end

      literal `enum `struct

      def endianess
         [name:ENDIANESS]

      def visual
         [name:VISUAL]

      def filter
         [`| function:reference::function::type]

      def subscript
         [`[ expr:expr::bracket::type `: slice:expr::bracket::type `]]
      |  [`[ expr:expr::bracket::type `]]

      def extra
         # if set, this field has trivial length, otherwise need to read subscripts
         length:collapser::collapsed
         [subscript:subscript* filter:filter* visual:visual? endianess:endianess?] {
            f:str = ''
            has_slice:bool
            for l:subscript in repeat(r1) {
               if (l.slice) {
                  f = ''
                  has_slice = true
                  break
               }

               if (f != '')
                  f = f + '*'

               if (l.expr.collapsed.result.value) {
                  f = f + '(' + $l.expr.collapsed.result.value + ')'
               } else {
                  f = f + '(' + $l.expr.collapsed + ')'
               }
            }

            if (f == '' && !has_slice)
               f = '1'

            if (f != '')
               lhs.length = collapser::collapsestr(f)
         }

      def type
         # enum name <primitive> name <extra>;
         [cref:`enum WS+ parent:reference::variable::type WS+ primitive:primitive::type WS+ name:reference::variable::type extra:extra `;]
         # struct name name <extra>;
      |  [cref:`struct WS+ parent:reference::variable::type WS+ name:reference::variable::type extra:extra `;]
         # <primitive> name <extra>;
      |  [primitive:primitive::type WS+ name:reference::variable::type extra:extra `;]
         # select ((thing)) { ... } <extra>; INVALID
      |  [container::select::type extra `;] commit { reject }
         # select ((thing)) { ... } <primitive> name <extra>; INVALID
      |  [container::select::type primitive::type WS+ reference::variable::type extra `;] commit { reject }
         # struct (optional) { ... } <primitive> name <extra>; INVALID
      |  [container::strukt::type primitive::type WS+ reference::variable::type extra `;] commit { reject }
         # enum (optional) { ... } <primitive> name <extra>;
      |  [container:container::type primitive:primitive::type WS+ name:reference::variable::type extra:extra `;]
         # select ((expr)) { ... } name <extra>;
         # struct (optional) { ... } name <extra>;
      |  [container:container::type name:reference::variable::type extra:extra `;]
         # (enum|struct) name { ... } <(le|be)>;
      |  [container:container::type endianess:endianess? `;]
   end

   def source
      [items:declaration::type*] commit
end

parse source:fspec::source[stdin]

if (!source) {
   print(error)
   exit(1)
}

struct scope
   endianess:str
   names:map<str, map<strany>>
end

global g_scopes:list<scope> = new list<scope>()

void
push_scope(endianess:str) {
   s:scope = new scope()
   s->names = new map<str, map<strany>>()
   s->endianess = endianess
   g_scopes->push_head(s)
}

void
pop_scope()
{
   g_scopes->pop_head()
}

any
lookup_no_error(type:strname:str) {
   for s:scope in g_scopes {
      cmap:map<strany> = s->names->find(type)
      if (cmap) {
         var:any = cmap->find(name)
         if (var)
            return var
      }
   }
   return nil
}

any
insert(type:strname:strvar:any)
{
   if (!name)
      return var # <anon>

   if (type != 'variable' && lookup_no_error(type, name)) {
      print('`', type, ' ', name, '` is already declared as a `', type, '` in current scope!\n')
      exit(1)
   }

   cmap:map<strany> = g_scopes->top->names->find(type)

   if (!cmap) {
      cmap = new map<strany>()
   } else if (cmap->find(name)) {
      print('`', type, ' ', name, '` is already declared as a `', type, '` in current scope!\n')
      exit(1)
   }

   cmap->insert(name, var)
   g_scopes->top->names->insert(type, cmap)
   return var
}

any
lookup(type:strname:str)
{
   r:any = lookup_no_error(type, name)
   if (!r) {
      print('`', type, ' ', name, '` is not declared in this or outer scope!\n')
      exit(1)
   }
   return r
}

str
container_name_str(s:str) { if (!s) return '<anon>' return s }

str
signed_str(s:bool) { if (s) return 'signed' return 'unsigned' }

str
endianess_str(s:str)
{
   if (s == 'be')
      return 'big-endian'
   else if (s == 'le')
      return 'little-endian'

   print('something went wrong!\n')
   exit(1)
}

str
endianess_from_decl(d:fspec::declaration::type)
{
   endianess:str = g_scopes->top->endianess
   if (d.endianess)
      for e:fspec::declaration::endianess in child(d.endianess) endianess = $e
   if (d.extra && d.extra.endianess)
      for e:fspec::declaration::endianess in child(d.extra.endianess) endianess = $e
   return endianess
}

void
print_declaration(d:fspec::declaration::type)
{
   insert('variable', $d.name, d)
   print('variable `', $d.name, '` is ')

   c:fspec::container::type
   if (d.cref) c = lookup($d.cref, $d.parent) else c = d.container

   if (c) {
      print('`', c.data.type, ' ', container_name_str(c.data.name), '` ')

      if (c.data.select)
         print('with expression `', $c.data.select, '` ')
   }

   if (d.primitive)
      print(d.primitive.bits, ' bits and ', signed_str(d.primitive.signed))

   print('\n')

   if (d.extra) {
      if (d.extra.length) {
         if (!d.extra.length.result.value || d.extra.length.result.value.reference) {
            print('   it has a variable length that needs to be computed with formula `', $d.extra.length, '`\n')
         } else {
            if (d.extra.length.result.value.number) {
               print('   it has a constant length of ', $d.extra.length.result.value, '\n')
            } else if (d.extra.length.result.value.string) {
               print('   its length will increase until pattern `', $d.extra.length.result.value.string.raw, '` has been read from stream\n')
            }
         }
      } else {
         print('   the subscripts contain slices, and thus needs some runtime loops to be computed\n')
      }

      for f:fspec::declaration::filter in repeat(d.extra.filter)
         print('   it needs to be filtered with `', $f.function, '`\n')

      for v:fspec::declaration::visual in child(d.extra.visual)
         print('   and it should be visualized as `', $v.name, '`\n')

      print('   the field is in ', endianess_str(endianess_from_decl(d)), ' byte order\n')
   }
}

void
walk(d:fspec::declaration::type)
{
   if (!d.container) {
      print('something went wrong!\n')
      exit(1)
   }

   s:fspec::container::type = d.container
   insert($s.data.type, s.data.name, s)

   if ($s.data.type == 'enum') {
      for i:fspec::container::enum::item in repeat(s.data.items) {
         print('constant `', $i.name, '` is ', $i.value, '\n')
         insert('variable', $i.name, i)
      }
   } else if ($s.data.type == 'struct') {
      push_scope(endianess_from_decl(d))
      for i:fspec::container::strukt::item in repeat(s.data.items) {
         if (i.data.container)
            walk(i.data)
         else
            print_declaration(i.data)
      }
      pop_scope()
   } else if ($s.data.type == 'select') {
      push_scope(endianess_from_decl(d))
      print('━━━━ start of (', $s.data.select, ') ━━━━\n')
      for i:fspec::container::select::item in repeat(s.data.items) {
         if (i.expr)
            print('━━━━ (', $i.expr, ')\n')
         else
            print('━━━━ DEFAULT\n')

         if (i.data.container)
            walk(i.data)
         else
            print_declaration(i.data)
      }
      print('━━━━ end of (', $s.data.select, ') ━━━━\n')
      pop_scope()
   }
}

push_scope('le')
for d:fspec::declaration::type in repeat(source.items)
   walk(d)
pop_scope()