aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUndecidable Robot <undecidabot@gmail.com>2016-06-16 09:52:47 +0800
committerUndecidable Robot <undecidabot@gmail.com>2016-06-16 09:52:47 +0800
commit816675350aae1ae1e4f9e98bced0dd5ddd413d1c (patch)
tree9f7ff985a5ca1050e3801991cca38c57e59c6d3f
parente16a4b989cc2275f7c8601bb44b6c21ff22e12e5 (diff)
downloadlpeglabel-816675350aae1ae1e4f9e98bced0dd5ddd413d1c.tar.gz
lpeglabel-816675350aae1ae1e4f9e98bced0dd5ddd413d1c.tar.bz2
lpeglabel-816675350aae1ae1e4f9e98bced0dd5ddd413d1c.zip
Adding evaluation to the example
-rw-r--r--examples/expect.md45
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
7local lpeg = require"lpeglabel" 7local lpeg = require"lpeglabel"
8 8
9local R, S, P, V, T = lpeg.R, lpeg.S, lpeg.P, lpeg.V, lpeg.T 9local R, S, P, V, C, Ct, T = lpeg.R, lpeg.S, lpeg.P, lpeg.V, lpeg.C, lpeg.Ct, lpeg.T
10 10
11local labels = { 11local 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)
27end 27end
28 28
29local num = R("09")^1 29local num = R("09")^1 / tonumber
30local op = S("+-*/") 30local op = S("+-*/")
31 31
32local 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
48end
49
32local g = P { 50local 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
39g = expect(g, "NoExp") * expect(-P(1), "Extra") 57g = expect(g, "NoExp") * expect(-P(1), "Extra")
40 58
41local function check(input) 59local 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
50end 68end
51 69
52print(check "(1+1-1*2/2") 70print(eval "98-76*(54/32)")
71--> 37.125
72
73print(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
55print(check "(1+)-1*(2/2)") 76print(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
58print(check "(1+1)-1*(/2)") 79print(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
61print(check "1+(1-(1*2))/2x") 82print(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
64print(check "-1+(1-(1*2))/2") 85print(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```