| 1 |
#include <tads.h> |
| 2 |
#include <gramprod.h> |
| 3 |
#include <tok.h> |
| 4 |
|
| 5 |
dictionary cmdDict; |
| 6 |
dictionary property fname; |
| 7 |
|
| 8 |
sinFunc: object |
| 9 |
fname = 'sin' 'sine' |
| 10 |
; |
| 11 |
cosFunc: object |
| 12 |
fname = 'cos' |
| 13 |
; |
| 14 |
tanFunc: object |
| 15 |
fname = 'tan' |
| 16 |
; |
| 17 |
|
| 18 |
class ExprNode: DynamicProd |
| 19 |
display() |
| 20 |
{ |
| 21 |
if (t1 != nil) |
| 22 |
"<<t1.display()>> "; |
| 23 |
if (t2 != nil) |
| 24 |
"<<t2.display()>> "; |
| 25 |
if (op != nil) |
| 26 |
"<<op>>"; |
| 27 |
} |
| 28 |
|
| 29 |
t1 = nil |
| 30 |
t2 = nil |
| 31 |
op = nil |
| 32 |
; |
| 33 |
|
| 34 |
class ExprNode2: ExprNode |
| 35 |
display() |
| 36 |
{ |
| 37 |
"[<<grammarTag>>:ExprNode2 <<inherited()>>]"; |
| 38 |
} |
| 39 |
; |
| 40 |
|
| 41 |
class AtomNode2: ExprNode |
| 42 |
display() |
| 43 |
{ |
| 44 |
"[<<grammarTag>>:AtomNode2 <<n>>]"; |
| 45 |
} |
| 46 |
; |
| 47 |
|
| 48 |
class FuncNode2: ExprNode |
| 49 |
display() |
| 50 |
{ |
| 51 |
"[<<grammarTag>>:FuncNode2 <<e.display()>> <<f>>]"; |
| 52 |
} |
| 53 |
e = nil |
| 54 |
f = nil |
| 55 |
; |
| 56 |
|
| 57 |
grammar expr(main): term->t1 : ExprNode; |
| 58 |
|
| 59 |
grammar term(factor): factor->t1 : ExprNode; |
| 60 |
|
| 61 |
grammar term(expr): term->t1 ('+'->op | '-'->op) factor->t2 : ExprNode; |
| 62 |
|
| 63 |
grammar factor(pow): pow0->t1 : ExprNode; |
| 64 |
|
| 65 |
grammar factor(expr): factor->t1 ('*'->op | '/'->op) pow0->t2 : ExprNode; |
| 66 |
|
| 67 |
grammar pow0(expr): 'test only' : ExprNode; |
| 68 |
#if 0 // we add 'pow' as an entirely dynamic node now! |
| 69 |
grammar pow(atom): atom->t1 : ExprNode; |
| 70 |
grammar pow(expr): pow->t1 '^'->op atom->t2 : ExprNode; |
| 71 |
#endif |
| 72 |
|
| 73 |
grammar atom(int): tokInt->n : object |
| 74 |
display() { "<<n>>"; } |
| 75 |
; |
| 76 |
|
| 77 |
grammar atom(parens): '(' expr->t1 ')' : ExprNode; |
| 78 |
|
| 79 |
tokenizer: Tokenizer |
| 80 |
; |
| 81 |
enum token tokOp; |
| 82 |
|
| 83 |
class testClass: ExprNode |
| 84 |
display() { "[<<id>>]"; } |
| 85 |
grammarTag = 'pi/e' |
| 86 |
id = nil |
| 87 |
; |
| 88 |
|
| 89 |
symtab: PreinitObject |
| 90 |
execute() |
| 91 |
{ |
| 92 |
tab = t3GetGlobalSymbols(); |
| 93 |
revtab = new LookupTable(256, 512); |
| 94 |
tab.forEachAssoc({key, val: revtab[val] = key}); |
| 95 |
} |
| 96 |
tab = nil |
| 97 |
revtab = nil |
| 98 |
; |
| 99 |
|
| 100 |
PreinitObject |
| 101 |
execute() |
| 102 |
{ |
| 103 |
local m; |
| 104 |
|
| 105 |
local pow = new GrammarProd(); |
| 106 |
symtab.tab['pow'] = pow; |
| 107 |
|
| 108 |
m = new ExprNode2; |
| 109 |
m.grammarTag = 'pow(atom)'; |
| 110 |
pow.addAlt('atom->t1', m, cmdDict, symtab.tab); |
| 111 |
|
| 112 |
m = new ExprNode2; |
| 113 |
m.grammarTag = 'pow(expr)'; |
| 114 |
pow.addAlt('pow->t1 "^"->op atom->t2', m, cmdDict, symtab.tab); |
| 115 |
|
| 116 |
pow0.clearAlts(cmdDict); |
| 117 |
m = new ExprNode2; |
| 118 |
m.grammarTag = 'pow0(pow)'; |
| 119 |
pow0.addAlt('pow->t1', m, cmdDict, symtab.tab); |
| 120 |
|
| 121 |
m = new ExprNode2(); |
| 122 |
m.grammarTag = 'new[atom]'; |
| 123 |
pow.addAlt('atom->t1', m, cmdDict, symtab.tab); |
| 124 |
|
| 125 |
m = new AtomNode2(); |
| 126 |
m.grammarTag = 'new[string]'; |
| 127 |
atom.addAlt('tokString->n', m, cmdDict, symtab.tab); |
| 128 |
|
| 129 |
m = new FuncNode2; |
| 130 |
m.grammarTag = 'new[fname]'; |
| 131 |
atom.addAlt('fname->f "(" expr->e ")"', m, cmdDict, symtab.tab); |
| 132 |
} |
| 133 |
; |
| 134 |
|
| 135 |
main(args) |
| 136 |
{ |
| 137 |
tokenizer.insertRule( |
| 138 |
['operator', new RexPattern('[-^+*/()]'), tokOp, nil, nil], |
| 139 |
'punctuation', nil); |
| 140 |
|
| 141 |
atom.addAlt('"pi"->id | "e"->id', testClass, cmdDict, symtab.tab); |
| 142 |
|
| 143 |
#ifdef INTERACTIVE |
| 144 |
/* interactive command line version */ |
| 145 |
for (;;) |
| 146 |
{ |
| 147 |
">"; |
| 148 |
local l = inputLine(); |
| 149 |
if (l == nil) |
| 150 |
break; |
| 151 |
|
| 152 |
process(l); |
| 153 |
} |
| 154 |
|
| 155 |
#else /* INTERACTIVE */ |
| 156 |
/* automated testing version */ |
| 157 |
"Dictionary contents:\n"; |
| 158 |
cmdDict.forEachWord( |
| 159 |
{obj, str, prop: "'<<str>>' (<< |
| 160 |
symtab.revtab[obj]>>.<<symtab.revtab[prop]>>)\n"}); |
| 161 |
"\b"; |
| 162 |
|
| 163 |
process('3*4*5+6*7*8'); |
| 164 |
process('sin(pi^e)'); |
| 165 |
|
| 166 |
#endif /* INTERACTIVE */ |
| 167 |
} |
| 168 |
|
| 169 |
process(l) |
| 170 |
{ |
| 171 |
local t = tokenizer.tokenize(l); |
| 172 |
local lst = expr.parseTokens(t, cmdDict); |
| 173 |
|
| 174 |
if (lst.length() == 0) |
| 175 |
"Parsing error.\n"; |
| 176 |
else |
| 177 |
{ |
| 178 |
lst[1].display(); |
| 179 |
"\n"; |
| 180 |
showGrammar(lst[1]); |
| 181 |
} |
| 182 |
"\b"; |
| 183 |
} |
| 184 |
|
| 185 |
showGrammar(match, indent = 0) |
| 186 |
{ |
| 187 |
if (match != nil) |
| 188 |
{ |
| 189 |
"<<makeString('\ ', indent)>>"; |
| 190 |
|
| 191 |
switch (dataType(match)) |
| 192 |
{ |
| 193 |
case TypeSString: |
| 194 |
"'<<match>>'\n"; |
| 195 |
break; |
| 196 |
|
| 197 |
case TypeObject: |
| 198 |
local info = match.grammarInfo(); |
| 199 |
"<<info[1]>> [<<showGrammarText(match)>>]\n"; |
| 200 |
for (local i = 2 ; i <= info.length() ; ++i) |
| 201 |
showGrammar(info[i], indent + 1); |
| 202 |
break; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
showGrammarText(match) |
| 208 |
{ |
| 209 |
for (local i = match.firstTokenIndex ; i <= match.lastTokenIndex ; ++i) |
| 210 |
{ |
| 211 |
if (i > match.firstTokenIndex) |
| 212 |
" "; |
| 213 |
"<<getTokOrig(match.tokenList[i])>>"; |
| 214 |
} |
| 215 |
} |