comparison modules/ast.tp @ 246:8c81afd6d2d3

Refactor some of the AST node object constructors into a separate AST module
author Michael Pavone <pavone@retrodev.com>
date Mon, 06 Jan 2014 19:41:35 -0800
parents
children b76f683d076e
comparison
equal deleted inserted replaced
245:3590ecca6bc9 246:8c81afd6d2d3
1 {
2 _binary <- 0
3 _string <- 1
4 _int <- 2
5
6 #{
7 binary <- { _binary }
8 stringlit <- { _string }
9 intlit <- { _int }
10
11
12 binaryOp:withArgs <- :opname :_left _right {
13 #{
14 nodeType <- { _binary }
15 left <- _left
16 op <- opname
17 right <- _right
18
19 leftAssociative? <- {
20 op != "|"
21 }
22 stringIndent <- :indent {
23 (left stringIndent: indent) . " " . op . (right stringIndent: indent)
24 }
25 string <- {
26 stringIndent: ""
27 }
28 }
29 }
30
31 stringLit <- :_val {
32 #{
33 nodeType <- { _string }
34 val <- _val
35 stringIndent <- :indent {
36 "\"" . val . "\""
37 }
38 string <- {
39 stringIndent: ""
40 }
41 }
42 }
43
44 intLit:withBits:andBase:signed? <- :_val :_bits :_base :_signed? {
45 #{
46 nodeType <- { _int }
47 val <- _val
48 base <- _base
49 bits <- _bits
50 signed? <- _signed?
51 stringIndent <- :indent {
52 suffix <- ""
53 if: bits != 32 || (not: signed?) {
54 suffix <- (if: signed? {"i"} else: {"u"}) . bits
55 }
56 if: base = 16 {
57 "0x" . (hex: val) . suffix
58 } else: {
59 if: base = 2 {
60 str <- "0b"
61 i <- bits - 1
62 printzero <- false
63 while: { i >= 0 } do: {
64 str <- str . (if: (lshift: 1 by: i) and val > 0 {
65 printzero <- true
66 "1"
67 } else: {
68 if: printzero {"0"} else: {""}
69 })
70 i <- i - 1
71 }
72 str . suffix
73 } else: {
74 (string: val) . suffix
75 }
76 }
77 }
78 string <- {
79 stringIndent: ""
80 }
81 }
82 }
83 }
84 }