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/expRec.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/expRec.lua')
-rw-r--r-- | examples/expRec.lua | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/examples/expRec.lua b/examples/expRec.lua index c5cbcca..5c5fd7d 100644 --- a/examples/expRec.lua +++ b/examples/expRec.lua | |||
@@ -1,10 +1,6 @@ | |||
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 | ||
5 | local C, Cc, Ct, Cmt = m.C, m.Cc, m.Ct, m.Cmt | ||
6 | local T, Rec = m.T, m.Rec | ||
7 | |||
8 | local labels = { | 4 | local labels = { |
9 | {"ExpTermFirst", "expected an expression"}, | 5 | {"ExpTermFirst", "expected an expression"}, |
10 | {"ExpTermOp", "expected a term after the operator"}, | 6 | {"ExpTermOp", "expected a term after the operator"}, |
@@ -22,14 +18,14 @@ end | |||
22 | 18 | ||
23 | local errors, subject | 19 | local errors, subject |
24 | 20 | ||
25 | local function expect(patt, labname, recpatt) | 21 | local function expect(patt, labname) |
26 | local i = labelindex(labname) | 22 | local i = labelindex(labname) |
27 | return patt + T(i) | 23 | return patt + m.T(i) |
28 | end | 24 | end |
29 | 25 | ||
30 | 26 | ||
31 | local num = R("09")^1 / tonumber | 27 | local num = m.R("09")^1 / tonumber |
32 | local op = S("+-") | 28 | local op = m.S("+-") |
33 | 29 | ||
34 | local function compute(tokens) | 30 | local function compute(tokens) |
35 | local result = tokens[1] | 31 | local result = tokens[1] |
@@ -45,13 +41,13 @@ local function compute(tokens) | |||
45 | return result | 41 | return result |
46 | end | 42 | end |
47 | 43 | ||
48 | local g = P { | 44 | local g = m.P { |
49 | "Exp", | 45 | "Exp", |
50 | Exp = Ct(V"OperandFirst" * (C(op) * V"Operand")^0) / compute, | 46 | Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute, |
51 | OperandFirst = expect(V"Term", "ExpTermFirst"), | 47 | OperandFirst = expect(m.V"Term", "ExpTermFirst"), |
52 | Operand = expect(V"Term", "ExpTermOp"), | 48 | Operand = expect(m.V"Term", "ExpTermOp"), |
53 | Term = num + V"Group", | 49 | Term = num + m.V"Group", |
54 | Group = "(" * V"Exp" * expect(")", "MisClose"), | 50 | Group = "(" * m.V"Exp" * expect(")", "MisClose"), |
55 | } | 51 | } |
56 | 52 | ||
57 | function recorderror(pos, lab) | 53 | function recorderror(pos, lab) |
@@ -71,22 +67,23 @@ function defaultValue (p) | |||
71 | return p or m.Cc(1000) | 67 | return p or m.Cc(1000) |
72 | end | 68 | end |
73 | 69 | ||
74 | local recg = P { | 70 | local grec = m.P { |
75 | "S", | 71 | "S", |
76 | S = Rec(V"A", V"ErrExpTermFirst", labelindex("ExpTermFirst")), -- default value is 0 | 72 | S = m.Rec(m.V"A", m.V"ErrExpTermFirst", labelindex("ExpTermFirst")), -- default value is 0 |
77 | A = Rec(V"Sg", V"ErrExpTermOp", labelindex("ExpTermOp")), | 73 | A = m.Rec(m.V"Sg", m.V"ErrExpTermOp", labelindex("ExpTermOp")), |
78 | Sg = Rec(g, V"ErrMisClose", labelindex("MisClose")), | 74 | Sg = m.Rec(g, m.V"ErrMisClose", labelindex("MisClose")), |
79 | ErrExpTermFirst = record("ExpTermFirst") * sync(op + ")") * defaultValue(), | 75 | ErrExpTermFirst = record("ExpTermFirst") * sync(op + ")") * defaultValue(), |
80 | ErrExpTermOp = record("ExpTermOp") * sync(op + ")") * defaultValue(), | 76 | ErrExpTermOp = record("ExpTermOp") * sync(op + ")") * defaultValue(), |
81 | ErrMisClose = record("MisClose") * sync(P")") * defaultValue(m.P""), | 77 | ErrMisClose = record("MisClose") * sync(m.P")") * defaultValue(m.P""), |
82 | } | 78 | } |
83 | 79 | ||
84 | |||
85 | local function eval(input) | 80 | local function eval(input) |
86 | errors = {} | 81 | errors = {} |
82 | io.write("Input: ", input, "\n") | ||
87 | subject = input | 83 | subject = input |
88 | local result, label, suffix = recg:match(input) | 84 | local result, label, suffix = grec:match(input) |
89 | if #errors > 0 then | 85 | io.write("Syntactic errors found: " .. #errors, "\n") |
86 | if #errors > 0 then | ||
90 | local out = {} | 87 | local out = {} |
91 | for i, err in ipairs(errors) do | 88 | for i, err in ipairs(errors) do |
92 | local pos = err.col | 89 | local pos = err.col |
@@ -95,13 +92,14 @@ local function eval(input) | |||
95 | end | 92 | end |
96 | print(table.concat(out, "\n")) | 93 | print(table.concat(out, "\n")) |
97 | end | 94 | end |
95 | io.write("Result = ") | ||
98 | return result | 96 | return result |
99 | end | 97 | end |
100 | 98 | ||
101 | print(eval "90-70*5") | 99 | print(eval "90-70-(5)+3") |
102 | --> 20 | 100 | --> 20 |
103 | 101 | ||
104 | print(eval "2+") | 102 | print(eval "15+") |
105 | --> 2 + 0 | 103 | --> 2 + 0 |
106 | 104 | ||
107 | print(eval "-2") | 105 | print(eval "-2") |
@@ -126,3 +124,5 @@ print(eval "1+(") | |||
126 | 124 | ||
127 | print(eval "3)") | 125 | print(eval "3)") |
128 | 126 | ||
127 | print(eval "11+())3") | ||
128 | |||