summaryrefslogtreecommitdiff
path: root/src/compiler/types.lm
blob: 592ddab14f830f0e52330e8cd161833d95f7325f (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
# 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+ /
         literal `0b
      end

      token BIN / [0-1]/

      int strtoull(a:strb:int) = c_strtoull

      def type
         value:int
         [`false] { lhs.value = 0 }
      |  [`true] { lhs.value = 1 }
      |  [`0b BIN] { lhs.value = strtoull($r2, 2) }
      |  [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
   lex
      literal `' `"
      token ESC / '\\' /
      token CHR / [abfnrtv\\'"e] /
      token HEX / 'x' xdigit{2/
      token OCT / [0-7]{1,3/
   end

   token CHAR_SQ / ^cntrl - ['\\] /
   token CHAR_DQ / ^cntrl - ["\\] /

   int strtoull(a:strb:int) = c_strtoull
   str esc2chr(a:str) = c_esc2chr
   str num2chr(a:int) = c_num2chr

   def escape
      data:str
      [ESC CHR] { lhs.data = esc2chr($r2) }
   |  [ESC HEX] { lhs.data = num2chr(strtoull('0' + $r2, 16)) }
   |  [ESC OCT] { lhs.data = num2chr(strtoull($r2, 8)) }

   def double_quoted
      data:str
      [escape] { lhs.data = r1.data } | [CHAR_DQ] { lhs.data = $r1 }

   def single_quoted
      data:str
      [escape] { lhs.data = r1.data } | [CHAR_SQ] { lhs.data = $r1 }

   def type
      raw:str
      length:int
      escaped:str
      [`' single_quoted* `'] {
         lhs.raw = ''
         lhs.escaped = $r2
         for i:single_quoted in repeat(r2) lhs.raw = lhs.raw + i.data
         lhs.length = lhs.raw.data.length
      }
   |  [`" double_quoted* `"] {
         lhs.raw = ''
         lhs.escaped = $r2
         for i:double_quoted in repeat(r2) lhs.raw = lhs.raw + i.data
         lhs.length = lhs.raw.data.length
      }
end