forked from veged/ometa-js-old
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOMeta_Tutorial.txt
150 lines (85 loc) · 3.11 KB
/
OMeta_Tutorial.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// a simple recognizer, produces no useful value
ometa L {
number = digit+,
addExpr = addExpr '+' mulExpr
| addExpr '-' mulExpr
| mulExpr,
mulExpr = mulExpr '*' primExpr
| mulExpr '/' primExpr
| primExpr,
primExpr = '(' expr ')'
| number,
expr = addExpr
}
L.matchAll('6*(4+3)', 'expr')
// a recognizer that also interprets
ometa Calc {
digit = ^digit:d -> d.digitValue(),
number = number:n digit:d -> (n * 10 + d)
| digit,
addExpr = addExpr:x '+' mulExpr:y -> (x + y)
| addExpr:x '-' mulExpr:y -> (x - y)
| mulExpr,
mulExpr = mulExpr:x '*' primExpr:y -> (x * y)
| mulExpr:x '/' primExpr:y -> (x / y)
| primExpr,
primExpr = '(' expr:x ')' -> x
| number,
expr = addExpr
}
Calc.matchAll('6**(4+3)', 'expr')
// parser and simple interpreter combo
ometa CalcParser {
digit = ^digit:d -> d.digitValue(),
number = number:n digit:d -> (n * 10 + d)
| digit,
addExpr = addExpr:x '+' mulExpr:y -> ['add', x, y]
| addExpr:x '-' mulExpr:y -> ['sub', x, y]
| mulExpr,
mulExpr = mulExpr:x '*' primExpr:y -> ['mul', x, y]
| mulExpr:x '/' primExpr:y -> ['div', x, y]
| primExpr,
primExpr = '(' expr:x ')' -> x
| number:n -> ['num', n],
expr = addExpr
}
tree = CalcParser.matchAll('6*(4+3)', 'expr')
ometa CalcInterpreter {
interp = ['num' anything:x] -> x
| ['add' interp:x interp:y] -> (x + y)
| ['sub' interp:x interp:y] -> (x - y)
| ['mul' interp:x interp:y] -> (x * y)
| ['div' interp:x interp:y] -> (x / y)
}
CalcInterpreter.match(tree, 'interp')
// we can write a "compiler" instead
ometa CalcCompiler {
comp = ['num' anything:x] -> x.toString()
| ['add' comp:x comp:y] -> ('(' + x + '+' + y + ')')
| ['sub' comp:x comp:y] -> ('(' + x + '-' + y + ')')
| ['mul' comp:x comp:y] -> ('(' + x + '*' + y + ')')
| ['div' comp:x comp:y] -> ('(' + x + '/' + y + ')')
}
code = CalcCompiler.match(tree, 'comp')
eval(code)
// spice things up with ML-like syntax
ometa CalcCompiler {
comp ['num' anything:x] -> x.toString(),
comp ['add' comp:x comp:y] -> ('(' + x + '+' + y + ')'),
comp ['sub' comp:x comp:y] -> ('(' + x + '-' + y + ')'),
comp ['mul' comp:x comp:y] -> ('(' + x + '*' + y + ')'),
comp ['div' comp:x comp:y] -> ('(' + x + '/' + y + ')')
}
code = CalcCompiler.match(tree, 'comp')
eval(code)
// a neat trick: dispatch on node tags using higher-order rule "apply"
ometa CalcCompiler {
comp [anything:t apply(t):ans] -> ans,
num anything:x -> x.toString(),
add comp:x comp:y -> ('(' + x + '+' + y + ')'),
sub comp:x comp:y -> ('(' + x + '-' + y + ')'),
mul comp:x comp:y -> ('(' + x + '*' + y + ')'),
div comp:x comp:y -> ('(' + x + '/' + y + ')')
}
code = CalcCompiler.match(tree, 'comp')
eval(code)