aboutsummaryrefslogtreecommitdiff
path: root/examples/tiny.lua
diff options
context:
space:
mode:
authorSergio Medeiros <sqmedeiros@gmail.com>2015-03-23 15:09:08 -0300
committerSergio Medeiros <sqmedeiros@gmail.com>2015-03-23 15:09:08 -0300
commitf124b2d4449a13ac21af9581a44b4455d89eaddb (patch)
treee0486555ac811c5f712927f110bb7bcda8569adf /examples/tiny.lua
parent0e93d536ba2d312502737cce2ab0cc21393c4842 (diff)
downloadlpeglabel-f124b2d4449a13ac21af9581a44b4455d89eaddb.tar.gz
lpeglabel-f124b2d4449a13ac21af9581a44b4455d89eaddb.tar.bz2
lpeglabel-f124b2d4449a13ac21af9581a44b4455d89eaddb.zip
Renaming "re.lua" to "relabel.lua".
Adding examples, README and lpeglabel.html
Diffstat (limited to 'examples/tiny.lua')
-rw-r--r--examples/tiny.lua163
1 files changed, 163 insertions, 0 deletions
diff --git a/examples/tiny.lua b/examples/tiny.lua
new file mode 100644
index 0000000..aa9dbea
--- /dev/null
+++ b/examples/tiny.lua
@@ -0,0 +1,163 @@
1local re = require 're'
2
3local terror = {}
4
5local function newError(l, msg)
6 table.insert(terror, { l = l, msg = msg} )
7end
8
9newError("errSemi", "Error: missing ';'")
10newError("errExpIf", "Error: expected expression after 'if'")
11newError("errThen", "Error: expected 'then' keyword")
12newError("errCmdSeq1", "Error: expected at least a command after 'then'")
13newError("errCmdSeq2", "Error: expected at least a command after 'else'")
14newError("errEnd", "Error: expected 'end' keyword")
15newError("errCmdSeqRep", "Error: expected at least a command after 'repeat'")
16newError("errUntil", "Error: expected 'until' keyword")
17newError("errExpRep", "Error: expected expression after 'until'")
18newError("errAssignOp", "Error: expected ':=' in assigment")
19newError("errExpAssign", "Error: expected expression after ':='")
20newError("errReadName", "Error: expected an identifier after 'read'")
21newError("errWriteExp", "Error: expected expression after 'write'")
22newError("errSimpExp", "Error: expected '(', ID, or number after '<' or '='")
23newError("errTerm", "Error: expected '(', ID, or number after '+' or '-'")
24newError("errFactor", "Error: expected '(', ID, or number after '*' or '/'")
25newError("errExpFac", "Error: expected expression after '('")
26newError("errClosePar", "Error: expected ')' after expression")
27
28local line
29
30local function incLine()
31 line = line + 1
32 return true
33end
34
35local function countLine(s, i)
36 line = 1
37 local p = re.compile([[
38 S <- (%nl -> incLine / .)*
39 ]], { incLine = incLine})
40 p:match(s:sub(1, i))
41 return true
42end
43
44local labelCode = {}
45for k, v in ipairs(terror) do
46 labelCode[v.l] = k
47end
48
49re.setlabels(labelCode)
50
51local g = re.compile([[
52 Tiny <- CmdSeq
53 CmdSeq <- (Cmd (SEMICOLON / ErrSemi)) (Cmd (SEMICOLON / ErrSemi))*
54 Cmd <- IfCmd / RepeatCmd / ReadCmd / WriteCmd / AssignCmd
55 IfCmd <- IF (Exp / ErrExpIf) (THEN / ErrThen) (CmdSeq / ErrCmdSeq1) (ELSE (CmdSeq / ErrCmdSeq2) / '') (END / ErrEnd)
56 RepeatCmd <- REPEAT (CmdSeq / ErrCmdSeqRep) (UNTIL / ErrUntil) (Exp / ErrExpRep)
57 AssignCmd <- NAME (ASSIGNMENT / ErrAssignOp) (Exp / ErrExpAssign)
58 ReadCmd <- READ (NAME / ErrReadName)
59 WriteCmd <- WRITE (Exp / ErrWriteExp)
60 Exp <- SimpleExp ((LESS / EQUAL) (SimpleExp / ErrSimpExp) / '')
61 SimpleExp <- Term ((ADD / SUB) (Term / ErrTerm))*
62 Term <- Factor ((MUL / DIV) (Factor / ErrFactor))*
63 Factor <- OPENPAR (Exp / ErrExpFac) (CLOSEPAR / ErrClosePar) / NUMBER / NAME
64 ErrSemi <- ErrCount %{errSemi}
65 ErrExpIf <- ErrCount %{errExpIf}
66 ErrThen <- ErrCount %{errThen}
67 ErrCmdSeq1 <- ErrCount %{errCmdSeq1}
68 ErrCmdSeq2 <- ErrCount %{errCmdSeq2}
69 ErrEnd <- ErrCount %{errEnd}
70 ErrCmdSeqRep <- ErrCount %{errCmdSeqRep}
71 ErrUntil <- ErrCount %{errUntil}
72 ErrExpRep <- ErrCount %{errExpRep}
73 ErrAssignOp <- ErrCount %{errAssignOp}
74 ErrExpAssign <- ErrCount %{errExpAssign}
75 ErrReadName <- ErrCount %{errReadName}
76 ErrWriteExp <- ErrCount %{errWriteExp}
77 ErrSimpExp <- ErrCount %{errSimpExp}
78 ErrTerm <- ErrCount %{errTerm}
79 ErrFactor <- ErrCount %{errFactor}
80 ErrExpFac <- ErrCount %{errExpFac}
81 ErrClosePar <- ErrCount %{errClosePar}
82 ErrCount <- '' => countLine
83 ADD <- Sp '+'
84 ASSIGNMENT <- Sp ':='
85 CLOSEPAR <- Sp ')'
86 DIV <- Sp '/'
87 IF <- Sp 'if'
88 ELSE <- Sp 'else'
89 END <- Sp 'end'
90 EQUAL <- Sp '='
91 LESS <- Sp '<'
92 MUL <- Sp '*'
93 NAME <- Sp !RESERVED [a-z]+
94 NUMBER <- Sp [0-9]+
95 OPENPAR <- Sp '('
96 READ <- Sp 'read'
97 REPEAT <- Sp 'repeat'
98 SEMICOLON <- Sp ';'
99 SUB <- Sp '-'
100 THEN <- Sp 'then'
101 UNTIL <- Sp 'until'
102 WRITE <- Sp 'write'
103 RESERVED <- (IF / ELSE / END / READ / REPEAT / THEN / UNTIL / WRITE) ![a-z]+
104 Sp <- %s*
105]], { countLine = countLine })
106
107
108local function printError(n, e)
109 assert(n == nil)
110 print("Line " .. line .. ": " .. terror[e].msg)
111end
112
113local s = [[
114n := 5;
115f := 1;
116repeat
117 f := f + n;
118 n := n - 1
119until (n < 1);
120write f;]]
121printError(g:match(s))
122
123s = [[
124n := 5;
125f := 1;
126repeat
127 f := f + n;
128 n := n - 1;
129until (n < 1);
130read ;]]
131printError(g:match(s))
132
133s = [[
134if a < 1 then
135 b := 2;
136else
137 b := 3;]]
138printError(g:match(s))
139
140s = [[
141n := 5;
142f := 1;
143repeat
144 f := f + n;
145 n := n - 1;
146untill (n < 1);
147]]
148printError(g:match(s))
149
150s = [[
151n := 5;
152f := 1;
153repeat
154 f := f + n;
155 n := n - 1;
1563 (n < 1);
157]]
158printError(g:match(s))
159
160printError(g:match("a : 2"))
161printError(g:match("a := (2"))
162
163