annotate modules/parser.tp @ 218:b799192e404b

Implemented match:where:yield and fixed a bug in zeroPlus
author Michael Pavone <pavone@retrodev.com>
date Sat, 21 Dec 2013 12:08:06 -0800
parents e00a8bc6361b
children a1a80af71b05
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 <- ""
218
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
148 //skip control characters for now
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
149 cur <- 32
208
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
150 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
151 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
152 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
153 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
154 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
155 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
156 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
157 } else: {
32080f96c3a0 Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents: 209
diff changeset
158 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
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 }
32080f96c3a0 Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents: 209
diff changeset
161 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
162 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
163 }
208
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
164 cur <- cur + 1
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 }
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
167 out
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
168 } else: {
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 }
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
172 charClass <- macro: :rawchars {
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
173 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
174 orig <- chars
208
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
175 chars <- expandClass: chars
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
176 //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
177 quote: :tomatch {
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
178 if: (tomatch isString?) {
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
179 check <- 0
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
180
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
181 nomatch <- true
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
182 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
183 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
184 nomatch <- false
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
185 }
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
186 check <- check + 1
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
187 }
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
188 if: nomatch {
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
189 #{
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
190 matched? <- { false }
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
191 }
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
192 } else: {
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
193 #{
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
194 matched? <- { true }
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
195 matchlen <- { 1 }
213
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
196 basicYield? <- { true }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
197 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
198 }
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
199 }
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
200 } else: {
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
201 #{
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
202 matched? <- { false }
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 }
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
206 } 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
207 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
208 }
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
209 }
209
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
210
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
211 zeroPlus <- macro: :matchexpr {
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
212 funexpr <- false
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
213 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
214 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
215 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
216 mcall <- mc matchcall
209
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
217 quote: :tomatch {
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
218 cur <- 0
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
219 n <- tomatch byte_length
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
220 orig <- tomatch
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
221 match <- true
213
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
222 allBasic? <- true
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
223 yieldvals <- []
209
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
224 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
225 res <- mcall
209
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
226 match <- res matched?
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
227 if: match {
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
228 //TODO: Use some kind of lightweight substring wrapper here
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
229 tomatch <- tomatch from: (res matchlen)
213
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
230 if: allBasic? {
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
231 ifnot: (res basicYield?) {
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
232 allBasic? <- false
218
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
233 if: cur > 0 {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
234 yieldvals <- (orig from: 0 withLength: cur) | yieldvals
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
235 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
236 yieldvals <- (res yield) | yieldvals
213
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
237 }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
238 } else: {
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
239 yieldvals <- (res yield) | yieldvals
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
240 }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
241 allBasic? <- allBasic? && (res basicYield?)
209
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
242 cur <- cur + (res matchlen)
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
243 }
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
244 }
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
245 if: cur > 0 {
213
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
246 if: allBasic? {
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
247 #{
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
248 matched? <- { true }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
249 matchlen <- { cur }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
250 basicYield? <- { true }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
251 yield <- { orig from: 0 withLength: cur }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
252 }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
253 } else: {
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
254 yieldvals <- yieldvals reverse
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
255 #{
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
256 matched? <- { true }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
257 matchlen <- { cur }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
258 basicYield? <- { false }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
259 yield <- { yieldvals }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
260 }
209
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 } else: {
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 matched? <- { false }
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
265 }
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
266 }
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
267 }
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
268 } 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
269 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
270 }
32080f96c3a0 Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents: 209
diff changeset
271 }
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 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
274 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
275 _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
276 }
32080f96c3a0 Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents: 209
diff changeset
277 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
278 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
279 }) 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
280 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
281 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
282 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
283 } else: {
32080f96c3a0 Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents: 209
diff changeset
284 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
285 acc
32080f96c3a0 Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents: 209
diff changeset
286 }
32080f96c3a0 Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents: 209
diff changeset
287 }
32080f96c3a0 Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents: 209
diff changeset
288 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
289 body
209
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
290 }
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
291 }
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
292
213
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
293 match:yield <- macro: :matchexpr :ylambda {
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
294 mc <- _makeMatchCall: matchexpr
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
295 if: (mc valid?) {
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
296 mcall <- mc matchcall
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
297 quote: :tomatch {
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
298 res <- mcall
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
299 if: (res matched?) {
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
300 #{
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
301 matched? <- { true }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
302 matchlen <- { res matchlen }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
303 basicYield? <- { false }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
304 yield <- ylambda
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 res
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 } else: {
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
311 print: "#error Invalid macth:yield macro call: " . (mc message) . "\n"
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
312 }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
313 }
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
314
218
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
315 match:where:yield <- macro: :matchexpr :whereclause :ylambda {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
316 syms <- []
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
317 withwhere <- (whereclause expressions) fold: (quote: :tomatch {}) with: :acc el {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
318
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
319 if: (el nodeType) = "assignment" {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
320 valassign <- quote: (val <- false)
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
321 valsym <- (valassign) symbol
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
322 valsym <- valsym name!: (valsym name) . ((el symbol) name)
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
323 valassign <- valassign symbol!: valsym
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
324 acc addExpression: valassign
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
325
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
326 matchassign <- quote: (hasmatch <- false)
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
327 matchsym <- (matchassign) symbol
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
328 matchsym <- matchsym name!: (matchsym name) . ((el symbol) name)
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
329 matchassign <- matchassign symbol!: matchsym
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
330 acc addExpression: matchassign
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
331
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
332 mc <- _makeMatchCall: (el expression)
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
333
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
334 if: (mc valid?) {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
335 mcall <- mc matchcall
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
336 matchfun <- quote: :tomatch {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
337 if: matchsym {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
338 if: valsym = tomatch {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
339 #{
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
340 matched? <- { true }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
341 matchlen <- { valsym length }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
342 basicYield? <- { true } //TODO: Check if this is correct
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
343 yield <- { valsym }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
344 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
345 } else: {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
346 #{
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
347 matched? <- { false }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
348 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
349 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
350 } else: {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
351 mr <- mcall
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
352 if: (mr matched?) {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
353 matchsym <- true
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
354 valsym <- (mr yield)
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
355 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
356 mr
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
357 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
358 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
359 acc <- acc addExpression: (el expression!: matchfun)
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
360 syms <- list node: #{
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
361 orig <- el symbol
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
362 matchval <- valsym
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
363 } withTail: syms
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
364 acc
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
365 } else: {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
366 print: "#error " . ((el symbol) name) . " does not have a valid match expression: " . (mc message) . "\n"
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
367 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
368
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
369 } else: {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
370 print: "#error Nodes of type " . (el nodeType) . " are not allowed in match where clauses\n"
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
371 acc
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
372 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
373 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
374 mcMain <- _makeMatchCall: matchexpr
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
375 if: (mcMain valid?) {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
376 mcall <- mcMain matchcall
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
377 withwhere addExpression: (quote: (matchres <- mcall))
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
378 successLambda <- quote: {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
379 //Extra assignments will be added here
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
380 mlen <- matchres matchlen
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
381 #{
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
382 matched? <- { true }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
383 matchlen <- { mlen }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
384 basicYield? <- { false }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
385 yield <- ylambda
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
386 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
387 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
388 sucexp <- syms fold: (successLambda expressions) with: :acc el {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
389 lsym <- el orig
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
390 rsym <- el matchval
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
391 (quote: (lsym <- rsym)) | acc
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
392 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
393 successLambda <- successLambda expressions!: sucexp
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
394 withwhere addExpression: (quote: (if: (matchres matched?) successLambda else: {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
395 matchres
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
396 }))
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
397 withwhere
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
398 } else: {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
399 print: "#error Error in main match expression of match:where:yield: " . (mcMain message) . "\n"
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
400 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
401 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
402
209
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
403
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
404 _alpha <- charClass: "a-zA-Z"
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
405 alpha <- zeroPlus: _alpha
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
406 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
407 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
408 (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
409 "/*" . (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
410 ])
32080f96c3a0 Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents: 209
diff changeset
411
213
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
412 digit <- matchOne: [
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
413 (match: "0" yield: {0})
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
414 (match: "1" yield: {1})
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
415 (match: "2" yield: {2})
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
416 (match: "3" yield: {3})
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
417 (match: "4" yield: {4})
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
418 (match: "5" yield: {5})
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
419 (match: "6" yield: {6})
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
420 (match: "7" yield: {7})
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
421 (match: "8" yield: {8})
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
422 (match: "9" yield: {9})
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
423 ]
208
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
424
218
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
425 posint <- match: Digits where: {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
426 Digits <- zeroPlus: digit
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
427 } yield: {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
428 num <- Digits fold: 0 with: :acc el {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
429 print: "Element " . el . "\n"
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
430 acc * 10 + el
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
431 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
432 #{
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
433 litval <- num
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
434 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
435 }
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
436
208
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
437 main <- {
209
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
438 cmatch <- alpha: "czx0123"
208
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
439 zeromatch <- alpha: "01234"
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
440 if: (cmatch matched?) {
209
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
441 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
442 } else: {
209
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
443 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
444 }
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
445 if: (zeromatch matched?) {
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
446 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
447 } else: {
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
448 print: "0123 didn't match\n"
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
449 }
209
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
450 zeromatchanum <- alphaNum: "01234"
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
451 if: (zeromatchanum matched?) {
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
452 print: "01234 matched with length " . (zeromatchanum matchlen) . "\n"
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
453 } else: {
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
454 print: "01234 didn't match\n"
4b3b57f39f10 Implement zeroPlus macro
Michael Pavone <pavone@retrodev.com>
parents: 208
diff changeset
455 }
212
32080f96c3a0 Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents: 209
diff changeset
456 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
457 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
458 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
459 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
460 } else: {
32080f96c3a0 Implement matchOne matching macro. Support more AST node types in zeroPlus matching macro.
Mike Pavone <pavone@retrodev.com>
parents: 209
diff changeset
461 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
462 }
213
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
463 tmatch <- digit: "3"
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
464 if: (tmatch matched?) {
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
465 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
466 } else: {
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
467 print: "3 did not match\n"
e00a8bc6361b Implement match:yield macro
Mike Pavone <pavone@retrodev.com>
parents: 212
diff changeset
468 }
218
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
469
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
470 posintm <- posint: "345"
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
471 if: (posintm matched?) {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
472 print: "345 matched with intlit value " . ((posintm yield) litval) . "\n"
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
473 } else: {
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
474 print: "345 did not match\n"
b799192e404b Implemented match:where:yield and fixed a bug in zeroPlus
Michael Pavone <pavone@retrodev.com>
parents: 213
diff changeset
475 }
208
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
476 }
a1b4a2bc8d72 Initial work on pattern match macrosfor the new parser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
477 }