diff options
author | sergio <sqmedeiros@gmail.com> | 2016-12-09 10:50:34 -0300 |
---|---|---|
committer | sergio <sqmedeiros@gmail.com> | 2016-12-09 10:50:34 -0300 |
commit | 2f1e173c3d7670f802a087cd2c80afc5b0ed23c3 (patch) | |
tree | b5239cb07b8e92af6b48a11959688e3185f459cd /examples/expRecAut.lua | |
parent | 8e3c0330defb4b5da81f88c9b45bc5fc9361eb34 (diff) | |
download | lpeglabel-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.lua | 52 |
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 @@ | |||
1 | local m = require"lpeglabelrec" | 1 | local m = require"lpeglabelrec" |
2 | local re = require"relabelrec" | 2 | local re = require"relabelrec" |
3 | 3 | ||
4 | local R, S, P, V = m.R, m.S, m.P, m.V | 4 | local num = m.R("09")^1 / tonumber |
5 | local C, Cc, Ct, Cmt = m.C, m.Cc, m.Ct, m.Cmt | 5 | local op = m.S("+-") |
6 | local T, Rec = m.T, m.Rec | ||
7 | |||
8 | local num = R("09")^1 / tonumber | ||
9 | local op = S("+-") | ||
10 | 6 | ||
11 | local labels = {} | 7 | local labels = {} |
12 | local nlabels = 0 | 8 | local nlabels = 0 |
@@ -27,7 +23,7 @@ local errors, subject | |||
27 | 23 | ||
28 | local function expect(patt, labname) | 24 | local 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) |
31 | end | 27 | end |
32 | 28 | ||
33 | local function compute(tokens) | 29 | local function compute(tokens) |
@@ -44,13 +40,13 @@ local function compute(tokens) | |||
44 | return result | 40 | return result |
45 | end | 41 | end |
46 | 42 | ||
47 | local g = P { | 43 | local 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 | ||
56 | function recorderror(pos, lab) | 52 | function recorderror(pos, lab) |
@@ -70,27 +66,18 @@ function defaultValue (p) | |||
70 | return p or m.Cc(1000) | 66 | return p or m.Cc(1000) |
71 | end | 67 | end |
72 | 68 | ||
73 | local recg2 = g | 69 | local grec = g * expect(m.P(-1), "Extra") |
74 | for k, v in pairs(labels) do | 70 | for 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) |
76 | end | 72 | end |
77 | 73 | ||
78 | local 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 | |||
89 | local function eval(input) | 74 | local 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 |
103 | end | 91 | end |
104 | 92 | ||
105 | print(eval "90-70*5") | 93 | print(eval "90-70-(5)+3") |
106 | --> 20 | 94 | --> 18 |
107 | 95 | ||
108 | print(eval "2+") | 96 | print(eval "15+") |
109 | --> 2 + 0 | 97 | --> 2 + 0 |
110 | 98 | ||
111 | print(eval "-2") | 99 | print(eval "-2") |
@@ -130,3 +118,5 @@ print(eval "1+(") | |||
130 | 118 | ||
131 | print(eval "3)") | 119 | print(eval "3)") |
132 | 120 | ||
121 | print(eval "11+()3") | ||
122 | --> 1 + ([0]) [+] 3 + [0] | ||