aboutsummaryrefslogtreecommitdiff
path: root/examples/expRecAut.lua
diff options
context:
space:
mode:
authorsergio <sqmedeiros@gmail.com>2016-12-09 10:50:34 -0300
committersergio <sqmedeiros@gmail.com>2016-12-09 10:50:34 -0300
commit2f1e173c3d7670f802a087cd2c80afc5b0ed23c3 (patch)
treeb5239cb07b8e92af6b48a11959688e3185f459cd /examples/expRecAut.lua
parent8e3c0330defb4b5da81f88c9b45bc5fc9361eb34 (diff)
downloadlpeglabel-2f1e173c3d7670f802a087cd2c80afc5b0ed23c3.tar.gz
lpeglabel-2f1e173c3d7670f802a087cd2c80afc5b0ed23c3.tar.bz2
lpeglabel-2f1e173c3d7670f802a087cd2c80afc5b0ed23c3.zip
Updating documentantion and polishing examples
Diffstat (limited to 'examples/expRecAut.lua')
-rw-r--r--examples/expRecAut.lua52
1 files changed, 21 insertions, 31 deletions
diff --git a/examples/expRecAut.lua b/examples/expRecAut.lua
index e098078..f870d73 100644
--- a/examples/expRecAut.lua
+++ b/examples/expRecAut.lua
@@ -1,12 +1,8 @@
1local m = require"lpeglabelrec" 1local m = require"lpeglabelrec"
2local re = require"relabelrec" 2local re = require"relabelrec"
3 3
4local R, S, P, V = m.R, m.S, m.P, m.V 4local num = m.R("09")^1 / tonumber
5local C, Cc, Ct, Cmt = m.C, m.Cc, m.Ct, m.Cmt 5local op = m.S("+-")
6local T, Rec = m.T, m.Rec
7
8local num = R("09")^1 / tonumber
9local op = S("+-")
10 6
11local labels = {} 7local labels = {}
12local nlabels = 0 8local nlabels = 0
@@ -27,7 +23,7 @@ local errors, subject
27 23
28local function expect(patt, labname) 24local function expect(patt, labname)
29 local i = labels[labname].id 25 local i = labels[labname].id
30 return patt + T(i) 26 return patt + m.T(i)
31end 27end
32 28
33local function compute(tokens) 29local function compute(tokens)
@@ -44,13 +40,13 @@ local function compute(tokens)
44 return result 40 return result
45end 41end
46 42
47local g = P { 43local g = m.P {
48 "Exp", 44 "Exp",
49 Exp = Ct(V"OperandFirst" * (C(op) * V"Operand")^0) / compute, 45 Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute,
50 OperandFirst = expect(V"Term", "ExpTermFirst"), 46 OperandFirst = expect(m.V"Term", "ExpTermFirst"),
51 Operand = expect(V"Term", "ExpTermOp"), 47 Operand = expect(m.V"Term", "ExpTermOp"),
52 Term = num + V"Group", 48 Term = num + m.V"Group",
53 Group = "(" * V"Exp" * expect(")", "MisClose"), 49 Group = "(" * m.V"Exp" * expect(")", "MisClose"),
54} 50}
55 51
56function recorderror(pos, lab) 52function recorderror(pos, lab)
@@ -70,27 +66,18 @@ function defaultValue (p)
70 return p or m.Cc(1000) 66 return p or m.Cc(1000)
71end 67end
72 68
73local recg2 = g 69local grec = g * expect(m.P(-1), "Extra")
74for k, v in pairs(labels) do 70for k, v in pairs(labels) do
75 recg2 = Rec(recg2, record(k) * sync(v.psync) * v.pcap, v.id) 71 grec = m.Rec(grec, record(k) * sync(v.psync) * v.pcap, v.id)
76end 72end
77 73
78local recg = P {
79 "S",
80 S = Rec(V"A", V"ErrExpTermFirst", labels["ExpTermFirst"].id), -- default value is 0
81 A = Rec(V"Sg", V"ErrExpTermOp", labels["ExpTermOp"].id),
82 Sg = Rec(g, V"ErrMisClose", labels["MisClose"].id),
83 ErrExpTermFirst = record("ExpTermFirst") * sync(op + ")") * defaultValue(),
84 ErrExpTermOp = record("ExpTermOp") * sync(op + ")") * defaultValue(),
85 ErrMisClose = record("MisClose") * sync(P")") * defaultValue(m.P""),
86}
87
88
89local function eval(input) 74local function eval(input)
90 errors = {} 75 errors = {}
76 io.write("Input: ", input, "\n")
91 subject = input 77 subject = input
92 local result, label, suffix = recg2:match(input) 78 local result, label, suffix = grec:match(input)
93 if #errors > 0 then 79 io.write("Syntactic errors found: " .. #errors, "\n")
80 if #errors > 0 then
94 local out = {} 81 local out = {}
95 for i, err in ipairs(errors) do 82 for i, err in ipairs(errors) do
96 local pos = err.col 83 local pos = err.col
@@ -99,13 +86,14 @@ local function eval(input)
99 end 86 end
100 print(table.concat(out, "\n")) 87 print(table.concat(out, "\n"))
101 end 88 end
89 io.write("Result = ")
102 return result 90 return result
103end 91end
104 92
105print(eval "90-70*5") 93print(eval "90-70-(5)+3")
106--> 20 94--> 18
107 95
108print(eval "2+") 96print(eval "15+")
109--> 2 + 0 97--> 2 + 0
110 98
111print(eval "-2") 99print(eval "-2")
@@ -130,3 +118,5 @@ print(eval "1+(")
130 118
131print(eval "3)") 119print(eval "3)")
132 120
121print(eval "11+()3")
122--> 1 + ([0]) [+] 3 + [0]