Mercurial > repos > tabletprog
annotate modules/parser.tp @ 216:2dac67e9d18b
Add -m flag to compiler driver to select macro expansion only
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 21 Dec 2013 12:07:00 -0800 |
parents | e00a8bc6361b |
children | b799192e404b |
rev | line source |
---|---|
208
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #{ |
209
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
2 _applyMatch <- :fun tomatch { |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
3 fun: tomatch |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
4 } |
212
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
5 _matchString <- :str tomatch { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
6 if: (tomatch isString?) { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
7 if: (tomatch length) < (str length) { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
8 #{ |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
9 matched? <- { false } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
10 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
11 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
12 if: (tomatch length) > (str length) { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
13 tomatch <- tomatch from: 0 withLength: (str length) |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
14 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
15 if: str = tomatch { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
16 #{ |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
17 matched? <- { true } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
18 matchlen <- { str length } |
213
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
19 basicYield? <- { true } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
20 yield <- { str } |
212
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
21 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
22 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
23 #{ |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
24 matched? <- { false } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
25 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
26 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
27 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
28 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
29 #{ |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
30 matched? <- { false } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
31 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
32 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
33 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
34 ifmatch:else <- :matchres :elseblock { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
35 if: (matchres matched?) { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
36 matchres |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
37 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
38 elseblock: |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
39 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
40 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
41 _makeMatchCall <- :matchexpr { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
42 if: (matchexpr nodeType) = "lambda" { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
43 #{ |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
44 valid? <- { true } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
45 matchcall <- quote: (_applyMatch: matchexpr tomatch) |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
46 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
47 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
48 if: (matchexpr nodeType) = "symbol" { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
49 #{ |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
50 valid? <- { true } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
51 matchcall <- quote: (matchexpr: tomatch) |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
52 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
53 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
54 if: (matchexpr nodeType) = "strlit" { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
55 #{ |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
56 valid? <- { true } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
57 matchcall <- quote: (_matchString: matchexpr tomatch) |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
58 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
59 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
60 if: (matchexpr nodeType) = "op" { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
61 if: (matchexpr opName) = "." { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
62 left <- (_makeMatchCall: (matchexpr left)) matchcall |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
63 right <- (_makeMatchCall: (matchexpr right)) matchcall |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
64 #{ |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
65 valid? <- { true } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
66 matchcall <- quote: (_applyMatch: :tomatch { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
67 lm <- left |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
68 if: (lm matched?) { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
69 tomatch <- tomatch from: (lm matchlen) |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
70 rm <- right |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
71 if: (rm matched?) { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
72 total <- (rm matchlen) + (lm matchlen) |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
73 #{ |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
74 matched? <- { true } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
75 matchlen <- { total } |
213
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
76 basicYield? <- { true } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
77 yield <- { tomatch from: 0 withLen: total } |
212
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
78 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
79 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
80 rm |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
81 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
82 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
83 lm |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
84 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
85 } tomatch) |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
86 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
87 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
88 #{ |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
89 valid? <- { false } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
90 message <- "Unsupported operator " . (matchexpr opName) |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
91 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
92 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
93 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
94 #{ |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
95 valid? <- { false } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
96 message <- "Unsupported AST node type " . (matchexpr nodeType) |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
97 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
98 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
99 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
100 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
101 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
102 } |
208
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
103 expandClass <- :chars { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
104 if: (chars length) > 0 { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
105 pos <- 0 |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
106 inverted <- false |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
107 if: (chars byte: 0) = ("^" byte: 0) { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
108 pos <- 1 |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
109 inverted <- true |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
110 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
111 state_begin <- 0 |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
112 state_normal <- 1 |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
113 state_rangeend <- 2 |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
114 state <- state_begin |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
115 out <- "" |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
116 while: { pos < (chars byte_length)} do: { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
117 if: state = state_begin { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
118 out <- out . (chars from: pos withLength: 1) |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
119 state <- state_normal |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
120 } else: { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
121 if: state = state_normal { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
122 if: (chars byte: pos) = ("-" byte: 0) { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
123 state <- state_rangeend |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
124 } else: { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
125 out <- out . (chars from: pos withLength: 1) |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
126 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
127 } else: { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
128 rangestart <- out byte: ((out byte_length) - 1) |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
129 rangeend <- chars byte: pos |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
130 if: rangeend < rangestart { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
131 tmp <- rangeend |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
132 rangeend <- rangestart |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
133 rangestart <- tmp |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
134 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
135 out <- out from: 0 withLength: ((out length) - 1) |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
136 while: { rangestart <= rangeend } do: { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
137 out <- out . (rangestart asStringChar) |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
138 rangestart <- rangestart + 1 |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
139 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
140 state <- state_begin |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
141 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
142 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
143 pos <- pos + 1 |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
144 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
145 if: inverted { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
146 old <- out |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
147 out <- "" |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
148 cur <- 0 |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
149 while: { cur < 256 } do: { |
212
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
150 notfound <- true |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
151 idx <- 0 |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
152 len <- (old length) |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
153 while: { notfound && idx < len } do: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
154 if: cur = (old byte: idx) { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
155 notfound <- false |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
156 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
157 idx <- idx + 1 |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
158 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
159 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
160 if: notfound { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
161 out <- out . (cur asStringChar) |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
162 } |
208
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
163 cur <- cur + 1 |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
164 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
165 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
166 out |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
167 } else: { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
168 "" |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
169 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
170 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
171 charClass <- macro: :rawchars { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
172 eval: rawchars :chars { |
212
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
173 orig <- chars |
208
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
174 chars <- expandClass: chars |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
175 //TODO: Use a more sophisticated approach for large classes |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
176 quote: :tomatch { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
177 if: (tomatch isString?) { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
178 check <- 0 |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
179 |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
180 nomatch <- true |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
181 while: { nomatch && check < (chars byte_length) } do: { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
182 if: (tomatch byte: 0) = (chars byte: check) { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
183 nomatch <- false |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
184 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
185 check <- check + 1 |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
186 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
187 if: nomatch { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
188 #{ |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
189 matched? <- { false } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
190 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
191 } else: { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
192 #{ |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
193 matched? <- { true } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
194 matchlen <- { 1 } |
213
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
195 basicYield? <- { true } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
196 yield <- { tomatch from: 0 withLength: 1 } |
208
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
197 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
198 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
199 } else: { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
200 #{ |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
201 matched? <- { false } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
202 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
203 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
204 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
205 } else: { |
212
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
206 print: "#error Argument to charClass macro must be a compile-time constant\n" |
208
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
207 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
208 } |
209
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
209 |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
210 zeroPlus <- macro: :matchexpr { |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
211 funexpr <- false |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
212 valid <- false |
212
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
213 mc <- _makeMatchCall: matchexpr |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
214 if: (mc valid?) { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
215 mcall <- mc matchcall |
209
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
216 quote: :tomatch { |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
217 cur <- 0 |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
218 n <- tomatch byte_length |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
219 orig <- tomatch |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
220 match <- true |
213
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
221 allBasic? <- true |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
222 yieldvals <- [] |
209
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
223 while: { match && cur < n } do: { |
212
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
224 res <- mcall |
209
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
225 match <- res matched? |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
226 if: match { |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
227 //TODO: Use some kind of lightweight substring wrapper here |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
228 tomatch <- tomatch from: (res matchlen) |
213
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
229 if: allBasic? { |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
230 ifnot: (res basicYield?) { |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
231 allBasic? <- false |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
232 yieldvals <- (orig from: 0 withLength: cur) | yieldvals |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
233 } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
234 } else: { |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
235 yieldvals <- (res yield) | yieldvals |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
236 } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
237 allBasic? <- allBasic? && (res basicYield?) |
209
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
238 cur <- cur + (res matchlen) |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
239 } |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
240 } |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
241 if: cur > 0 { |
213
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
242 if: allBasic? { |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
243 #{ |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
244 matched? <- { true } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
245 matchlen <- { cur } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
246 basicYield? <- { true } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
247 yield <- { orig from: 0 withLength: cur } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
248 } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
249 } else: { |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
250 yieldvals <- yieldvals reverse |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
251 #{ |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
252 matched? <- { true } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
253 matchlen <- { cur } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
254 basicYield? <- { false } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
255 yield <- { yieldvals } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
256 } |
209
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
257 } |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
258 } else: { |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
259 #{ |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
260 matched? <- { false } |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
261 } |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
262 } |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
263 } |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
264 } else: { |
212
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
265 print: "#error Invalid zeroPlus macro call: " . (mc message) . "\n" |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
266 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
267 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
268 |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
269 matchOne <- macro: :options { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
270 options <- (options value) map: :option { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
271 _makeMatchCall: option |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
272 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
273 body <- options foldr: (quote: #{ |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
274 matched? <- { false } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
275 }) with: :acc el { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
276 if: (el valid?) { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
277 mcall <- el matchcall |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
278 quote: (ifmatch: mcall else: { acc }) |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
279 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
280 print: "#error Invalid matchOne macro call: " . (el message) . "\n" |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
281 acc |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
282 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
283 } |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
284 quote: :tomatch { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
285 body |
209
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
286 } |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
287 } |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
288 |
213
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
289 match:yield <- macro: :matchexpr :ylambda { |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
290 mc <- _makeMatchCall: matchexpr |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
291 if: (mc valid?) { |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
292 mcall <- mc matchcall |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
293 quote: :tomatch { |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
294 res <- mcall |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
295 if: (res matched?) { |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
296 #{ |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
297 matched? <- { true } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
298 matchlen <- { res matchlen } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
299 basicYield? <- { false } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
300 yield <- ylambda |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
301 } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
302 } else: { |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
303 res |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
304 } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
305 } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
306 } else: { |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
307 print: "#error Invalid macth:yield macro call: " . (mc message) . "\n" |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
308 } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
309 } |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
310 |
209
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
311 |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
312 _alpha <- charClass: "a-zA-Z" |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
313 alpha <- zeroPlus: _alpha |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
314 alphaNum <- zeroPlus: (charClass: "a-zA-Z0-9") |
212
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
315 hws <- zeroPlus: (matchOne: [ |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
316 (charClass: " \t") |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
317 "/*" . (zeroPlus: (matchOne: [(charClass: "^*") "*" . (charClass: "^/")])) . "*/" |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
318 ]) |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
319 |
213
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
320 digit <- matchOne: [ |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
321 (match: "0" yield: {0}) |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
322 (match: "1" yield: {1}) |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
323 (match: "2" yield: {2}) |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
324 (match: "3" yield: {3}) |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
325 (match: "4" yield: {4}) |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
326 (match: "5" yield: {5}) |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
327 (match: "6" yield: {6}) |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
328 (match: "7" yield: {7}) |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
329 (match: "8" yield: {8}) |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
330 (match: "9" yield: {9}) |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
331 ] |
208
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
332 |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
333 main <- { |
209
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
334 cmatch <- alpha: "czx0123" |
208
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
335 zeromatch <- alpha: "01234" |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
336 if: (cmatch matched?) { |
209
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
337 print: "czx0123 matched with length " . (cmatch matchlen) . "\n" |
208
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
338 } else: { |
209
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
339 print: "czx0123 didn't match\n" |
208
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
340 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
341 if: (zeromatch matched?) { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
342 print: "0123 matched with length " . (zeromatch matchlen) . "\n" |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
343 } else: { |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
344 print: "0123 didn't match\n" |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
345 } |
209
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
346 zeromatchanum <- alphaNum: "01234" |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
347 if: (zeromatchanum matched?) { |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
348 print: "01234 matched with length " . (zeromatchanum matchlen) . "\n" |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
349 } else: { |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
350 print: "01234 didn't match\n" |
4b3b57f39f10
Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents:
208
diff
changeset
|
351 } |
212
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
352 stuff <- " \t/* blah blah blah * blah */ foo" |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
353 hwsmatch <- hws: stuff |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
354 if: (hwsmatch matched?) { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
355 print: "'" . (stuff from: (hwsmatch matchlen)) . "' found after hws\n" |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
356 } else: { |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
357 print: stuff . " did not match hws rule\n" |
32080f96c3a0
Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents:
209
diff
changeset
|
358 } |
213
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
359 tmatch <- digit: "3" |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
360 if: (tmatch matched?) { |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
361 print: "3 matched with yield " . (tmatch yield) . ", yield + 1 = " . ((tmatch yield) + 1) . "\n" |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
362 } else: { |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
363 print: "3 did not match\n" |
e00a8bc6361b
Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents:
212
diff
changeset
|
364 } |
208
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
365 } |
a1b4a2bc8d72
Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
366 } |