diff options
author | Undecidable Robot <undecidabot@gmail.com> | 2016-06-16 09:52:47 +0800 |
---|---|---|
committer | Undecidable Robot <undecidabot@gmail.com> | 2016-06-16 09:52:47 +0800 |
commit | 816675350aae1ae1e4f9e98bced0dd5ddd413d1c (patch) | |
tree | 9f7ff985a5ca1050e3801991cca38c57e59c6d3f | |
parent | e16a4b989cc2275f7c8601bb44b6c21ff22e12e5 (diff) | |
download | lpeglabel-816675350aae1ae1e4f9e98bced0dd5ddd413d1c.tar.gz lpeglabel-816675350aae1ae1e4f9e98bced0dd5ddd413d1c.tar.bz2 lpeglabel-816675350aae1ae1e4f9e98bced0dd5ddd413d1c.zip |
Adding evaluation to the example
-rw-r--r-- | examples/expect.md | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/examples/expect.md b/examples/expect.md index 544a55e..469f1d4 100644 --- a/examples/expect.md +++ b/examples/expect.md | |||
@@ -6,7 +6,7 @@ record all errors encountered once error recovery is implemented. | |||
6 | ```lua | 6 | ```lua |
7 | local lpeg = require"lpeglabel" | 7 | local lpeg = require"lpeglabel" |
8 | 8 | ||
9 | local R, S, P, V, T = lpeg.R, lpeg.S, lpeg.P, lpeg.V, lpeg.T | 9 | local R, S, P, V, C, Ct, T = lpeg.R, lpeg.S, lpeg.P, lpeg.V, lpeg.C, lpeg.Ct, lpeg.T |
10 | 10 | ||
11 | local labels = { | 11 | local labels = { |
12 | {"NoExp", "no expression found"}, | 12 | {"NoExp", "no expression found"}, |
@@ -26,41 +26,62 @@ local function expect(patt, labname) | |||
26 | error("could not find label: " .. labname) | 26 | error("could not find label: " .. labname) |
27 | end | 27 | end |
28 | 28 | ||
29 | local num = R("09")^1 | 29 | local num = R("09")^1 / tonumber |
30 | local op = S("+-*/") | 30 | local op = S("+-*/") |
31 | 31 | ||
32 | local function compute(tokens) | ||
33 | local result = tokens[1] | ||
34 | for i = 2, #tokens, 2 do | ||
35 | if tokens[i] == '+' then | ||
36 | result = result + tokens[i+1] | ||
37 | elseif tokens[i] == '-' then | ||
38 | result = result - tokens[i+1] | ||
39 | elseif tokens[i] == '*' then | ||
40 | result = result * tokens[i+1] | ||
41 | elseif tokens[i] == '/' then | ||
42 | result = result / tokens[i+1] | ||
43 | else | ||
44 | error('unknown operation: ' .. tokens[i]) | ||
45 | end | ||
46 | end | ||
47 | return result | ||
48 | end | ||
49 | |||
32 | local g = P { | 50 | local g = P { |
33 | "Exp", | 51 | "Exp", |
34 | Exp = V"Term" * (op * expect(V"Term", "ExpTerm"))^0; | 52 | Exp = Ct(V"Term" * (C(op) * expect(V"Term", "ExpTerm"))^0) / compute; |
35 | Term = num + V"Group"; | 53 | Term = num + V"Group"; |
36 | Group = "(" * expect(V"Exp", "ExpExp") * expect(")", "MisClose"); | 54 | Group = "(" * expect(V"Exp", "ExpExp") * expect(")", "MisClose"); |
37 | } | 55 | } |
38 | 56 | ||
39 | g = expect(g, "NoExp") * expect(-P(1), "Extra") | 57 | g = expect(g, "NoExp") * expect(-P(1), "Extra") |
40 | 58 | ||
41 | local function check(input) | 59 | local function eval(input) |
42 | result, label, suffix = g:match(input) | 60 | local result, label, suffix = g:match(input) |
43 | if result ~= nil then | 61 | if result ~= nil then |
44 | return "ok" | 62 | return result |
45 | else | 63 | else |
46 | local pos = input:len() - suffix:len() + 1 | 64 | local pos = input:len() - suffix:len() + 1 |
47 | local msg = labels[label][2] | 65 | local msg = labels[label][2] |
48 | return "syntax error: " .. msg .. " (at index " .. pos .. ")" | 66 | return nil, "syntax error: " .. msg .. " (at index " .. pos .. ")" |
49 | end | 67 | end |
50 | end | 68 | end |
51 | 69 | ||
52 | print(check "(1+1-1*2/2") | 70 | print(eval "98-76*(54/32)") |
71 | --> 37.125 | ||
72 | |||
73 | print(eval "(1+1-1*2/2") | ||
53 | --> syntax error: missing a closing ')' after the expression (at index 11) | 74 | --> syntax error: missing a closing ')' after the expression (at index 11) |
54 | 75 | ||
55 | print(check "(1+)-1*(2/2)") | 76 | print(eval "(1+)-1*(2/2)") |
56 | --> syntax error: expected a term after the operator (at index 4) | 77 | --> syntax error: expected a term after the operator (at index 4) |
57 | 78 | ||
58 | print(check "(1+1)-1*(/2)") | 79 | print(eval "(1+1)-1*(/2)") |
59 | --> syntax error: expected an expression after the parenthesis (at index 10) | 80 | --> syntax error: expected an expression after the parenthesis (at index 10) |
60 | 81 | ||
61 | print(check "1+(1-(1*2))/2x") | 82 | print(eval "1+(1-(1*2))/2x") |
62 | --> syntax error: extra chracters found after the expression (at index 14) | 83 | --> syntax error: extra chracters found after the expression (at index 14) |
63 | 84 | ||
64 | print(check "-1+(1-(1*2))/2") | 85 | print(eval "-1+(1-(1*2))/2") |
65 | --> syntax error: no expression found (at index 1) | 86 | --> syntax error: no expression found (at index 1) |
66 | ``` | 87 | ``` |