diff options
Diffstat (limited to 'examples/expRecAut.lua')
-rw-r--r-- | examples/expRecAut.lua | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/examples/expRecAut.lua b/examples/expRecAut.lua new file mode 100644 index 0000000..f870d73 --- /dev/null +++ b/examples/expRecAut.lua | |||
@@ -0,0 +1,122 @@ | |||
1 | local m = require"lpeglabelrec" | ||
2 | local re = require"relabelrec" | ||
3 | |||
4 | local num = m.R("09")^1 / tonumber | ||
5 | local op = m.S("+-") | ||
6 | |||
7 | local labels = {} | ||
8 | local nlabels = 0 | ||
9 | |||
10 | local function newError(lab, msg, psync, pcap) | ||
11 | nlabels = nlabels + 1 | ||
12 | psync = psync or m.P(-1) | ||
13 | pcap = pcap or m.P"" | ||
14 | labels[lab] = { id = nlabels, msg = msg, psync = psync, pcap = pcap } | ||
15 | end | ||
16 | |||
17 | newError("ExpTermFirst", "expected an expression", op + ")", m.Cc(1000)) | ||
18 | newError("ExpTermOp", "expected a term after the operator", op + ")", m.Cc(1000)) | ||
19 | newError("MisClose", "missing a closing ')' after the expression", m.P")") | ||
20 | newError("Extra", "extra characters found after the expression") | ||
21 | |||
22 | local errors, subject | ||
23 | |||
24 | local function expect(patt, labname) | ||
25 | local i = labels[labname].id | ||
26 | return patt + m.T(i) | ||
27 | end | ||
28 | |||
29 | local function compute(tokens) | ||
30 | local result = tokens[1] | ||
31 | for i = 2, #tokens, 2 do | ||
32 | if tokens[i] == '+' then | ||
33 | result = result + tokens[i+1] | ||
34 | elseif tokens[i] == '-' then | ||
35 | result = result - tokens[i+1] | ||
36 | else | ||
37 | error('unknown operation: ' .. tokens[i]) | ||
38 | end | ||
39 | end | ||
40 | return result | ||
41 | end | ||
42 | |||
43 | local g = m.P { | ||
44 | "Exp", | ||
45 | Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute, | ||
46 | OperandFirst = expect(m.V"Term", "ExpTermFirst"), | ||
47 | Operand = expect(m.V"Term", "ExpTermOp"), | ||
48 | Term = num + m.V"Group", | ||
49 | Group = "(" * m.V"Exp" * expect(")", "MisClose"), | ||
50 | } | ||
51 | |||
52 | function recorderror(pos, lab) | ||
53 | local line, col = re.calcline(subject, pos) | ||
54 | table.insert(errors, { line = line, col = col, msg = labels[lab].msg }) | ||
55 | end | ||
56 | |||
57 | function record (labname) | ||
58 | return (m.Cp() * m.Cc(labname)) / recorderror | ||
59 | end | ||
60 | |||
61 | function sync (p) | ||
62 | return (-p * m.P(1))^0 | ||
63 | end | ||
64 | |||
65 | function defaultValue (p) | ||
66 | return p or m.Cc(1000) | ||
67 | end | ||
68 | |||
69 | local grec = g * expect(m.P(-1), "Extra") | ||
70 | for k, v in pairs(labels) do | ||
71 | grec = m.Rec(grec, record(k) * sync(v.psync) * v.pcap, v.id) | ||
72 | end | ||
73 | |||
74 | local function eval(input) | ||
75 | errors = {} | ||
76 | io.write("Input: ", input, "\n") | ||
77 | subject = input | ||
78 | local result, label, suffix = grec:match(input) | ||
79 | io.write("Syntactic errors found: " .. #errors, "\n") | ||
80 | if #errors > 0 then | ||
81 | local out = {} | ||
82 | for i, err in ipairs(errors) do | ||
83 | local pos = err.col | ||
84 | local msg = err.msg | ||
85 | table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") | ||
86 | end | ||
87 | print(table.concat(out, "\n")) | ||
88 | end | ||
89 | io.write("Result = ") | ||
90 | return result | ||
91 | end | ||
92 | |||
93 | print(eval "90-70-(5)+3") | ||
94 | --> 18 | ||
95 | |||
96 | print(eval "15+") | ||
97 | --> 2 + 0 | ||
98 | |||
99 | print(eval "-2") | ||
100 | --> 0 - 2 | ||
101 | |||
102 | print(eval "1+3+-9") | ||
103 | --> 1 + 3 + [0] - 9 | ||
104 | |||
105 | print(eval "1+()3+") | ||
106 | --> 1 + ([0]) [+] 3 + [0] | ||
107 | |||
108 | print(eval "8-(2+)-5") | ||
109 | --> 8 - (2 + [0]) - 5 | ||
110 | |||
111 | print(eval "()") | ||
112 | |||
113 | print(eval "") | ||
114 | |||
115 | print(eval "1+()+") | ||
116 | |||
117 | print(eval "1+(") | ||
118 | |||
119 | print(eval "3)") | ||
120 | |||
121 | print(eval "11+()3") | ||
122 | --> 1 + ([0]) [+] 3 + [0] | ||