summaryrefslogtreecommitdiff
path: root/src/compiler/types.lm
blob: b33adf7ca76e08f57f8eaaa16a29bd36888133a1 (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
# Basic (mostly) langauge independent types
# These are the foundation for understanding the source code

token WS / space /

context number
   context unsigned
      lex
         literal `true `false
         token OCT / '0'[0-7]/
         token DEC / [0-9]/
         token HEX / '0x' xdigit+ /
      end

      int strtoull(a:strb:int) = c_strtoull

      def type
         value:int
         [`false] { lhs.value = 0 }
      |  [`true] { lhs.value = 1 }
      |  [OCT] { lhs.value = strtoull($r1, 8) }
      |  [DEC] { lhs.value = strtoull($r1, 10) }
      |  [HEX] { lhs.value = strtoull($r1, 16) }
   end

   lex
      ignore / space+ /
      literal `+ `-
   end

   def type
      value:int
      [unsigned::type] { lhs.value = r1.value }
   |  [`- type] { lhs.value = r2.value - (r2.value * 2) }
   |  [`+ type] { lhs.value = r2.value }
end

context string
   rl ESC / '\\' /

   lex
      literal `' `"
      token ESC_CHR / ESC [abfnrtv\\'"e] /
      token ESC_HEX / ESC 'x' xdigit{2/
      token ESC_OCT / ESC [0-7]{1,3/
      token CHAR / ^cntrl /
   end

   def raw
      [ESC_CHR] # TODO: how to output raw bytes?
   |  [ESC_HEX] # TODO: how to output raw bytes?
   |  [ESC_OCT] # TODO: how to output raw bytes?
   |  [CHAR]

   def type
      length:int
      [`' raw:raw* `'] { i:int = 0 for s:raw in r2 i = i + 1 lhs.length = i }
   |  [`" raw:raw* `"] { i:int = 0 for s:raw in r2 i = i + 1 lhs.length = i }
end