aboutsummaryrefslogtreecommitdiff
path: root/examples/tiny.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/tiny.lua')
-rw-r--r--examples/tiny.lua147
1 files changed, 59 insertions, 88 deletions
diff --git a/examples/tiny.lua b/examples/tiny.lua
index cc718e7..fe0bced 100644
--- a/examples/tiny.lua
+++ b/examples/tiny.lua
@@ -1,92 +1,63 @@
1local re = require 'relabel' 1local re = require 'relabel'
2 2
3local terror = {} 3local terror = {
4 4 cmdSeq = "Missing ';' in CmdSeq",
5local function newError(l, msg) 5 ifExp = "Error in expresion of 'if'",
6 table.insert(terror, { l = l, msg = msg} ) 6 ifThen = "Error matching 'then' keyword",
7end 7 ifThenCmdSeq = "Error matching CmdSeq of 'then' branch",
8 8 ifElseCmdSeq = "Error matching CmdSeq of 'else' branch",
9newError("errSemi", "Error: missing ';'") 9 ifEnd = "Error matching 'end' keyword of 'if'",
10newError("errExpIf", "Error: expected expression after 'if'") 10 repeatCmdSeq = "Error matching CmdSeq of 'repeat'",
11newError("errThen", "Error: expected 'then' keyword") 11 repeatUntil = "Error matching 'until' keyword",
12newError("errCmdSeq1", "Error: expected at least a command after 'then'") 12 repeatExp = "Error matching expression of 'until'",
13newError("errCmdSeq2", "Error: expected at least a command after 'else'") 13 assignOp = "Error matching ':='",
14newError("errEnd", "Error: expected 'end' keyword") 14 assignExp = "Error matching expression of assignment",
15newError("errCmdSeqRep", "Error: expected at least a command after 'repeat'") 15 readName = "Error matching 'NAME' after 'read'",
16newError("errUntil", "Error: expected 'until' keyword") 16 writeExp = "Error matching expression after 'write'",
17newError("errExpRep", "Error: expected expression after 'until'") 17 simpleExp = "Error matching 'SimpleExp'",
18newError("errAssignOp", "Error: expected ':=' in assigment") 18 term = "Error matching 'Term'",
19newError("errExpAssign", "Error: expected expression after ':='") 19 factor = "Error matching 'Factor'",
20newError("errReadName", "Error: expected an identifier after 'read'") 20 openParExp = "Error matching expression after '('",
21newError("errWriteExp", "Error: expected expression after 'write'") 21 closePar = "Error matching ')'",
22newError("errSimpExp", "Error: expected '(', ID, or number after '<' or '='") 22 undefined = "Undefined Error"
23newError("errTerm", "Error: expected '(', ID, or number after '+' or '-'") 23}
24newError("errFactor", "Error: expected '(', ID, or number after '*' or '/'") 24
25newError("errExpFac", "Error: expected expression after '('") 25g = re.compile([[
26newError("errClosePar", "Error: expected ')' after expression") 26 Tiny <- CmdSeq^undefined
27 27 CmdSeq <- (Cmd SEMICOLON^cmdSeq) (Cmd SEMICOLON^cmdSeq)*
28 28 Cmd <- IfCmd / RepeatCmd / ReadCmd / WriteCmd / AssignCmd
29local labelCode = {} 29 IfCmd <- IF Exp^ifExp THEN^ifThen CmdSeq^ifThenCmdSeq (ELSE CmdSeq^ifElseCmdSeq / '') END^ifEnd
30for k, v in ipairs(terror) do 30 RepeatCmd <- REPEAT CmdSeq^repeatCmdSeq UNTIL^repeatUntil Exp^repeatExp
31 labelCode[v.l] = k 31 AssignCmd <- NAME ASSIGNMENT^assignOp Exp^assignExp
32end 32 ReadCmd <- READ NAME^readName
33 33 WriteCmd <- WRITE Exp^writeExp
34re.setlabels(labelCode) 34 Exp <- SimpleExp ((LESS / EQUAL) SimpleExp^simpleExp / '')
35 35 SimpleExp <- Term ((ADD / SUB) Term^term)*
36local g = re.compile[[ 36 Term <- Factor ((MUL / DIV) Factor^factor)*
37 Tiny <- CmdSeq 37 Factor <- OPENPAR Exp^openParExp CLOSEPAR^closePar / NUMBER / NAME
38 CmdSeq <- (Cmd (SEMICOLON / ErrSemi)) (Cmd (SEMICOLON / ErrSemi))* 38 ADD <- Sp '+'
39 Cmd <- IfCmd / RepeatCmd / ReadCmd / WriteCmd / AssignCmd 39 ASSIGNMENT <- Sp ':='
40 IfCmd <- IF (Exp / ErrExpIf) (THEN / ErrThen) (CmdSeq / ErrCmdSeq1) (ELSE (CmdSeq / ErrCmdSeq2) / '') (END / ErrEnd) 40 CLOSEPAR <- Sp ')'
41 RepeatCmd <- REPEAT (CmdSeq / ErrCmdSeqRep) (UNTIL / ErrUntil) (Exp / ErrExpRep) 41 DIV <- Sp '/'
42 AssignCmd <- NAME (ASSIGNMENT / ErrAssignOp) (Exp / ErrExpAssign) 42 IF <- Sp 'if'
43 ReadCmd <- READ (NAME / ErrReadName) 43 ELSE <- Sp 'else'
44 WriteCmd <- WRITE (Exp / ErrWriteExp) 44 END <- Sp 'end'
45 Exp <- SimpleExp ((LESS / EQUAL) (SimpleExp / ErrSimpExp) / '') 45 EQUAL <- Sp '='
46 SimpleExp <- Term ((ADD / SUB) (Term / ErrTerm))* 46 LESS <- Sp '<'
47 Term <- Factor ((MUL / DIV) (Factor / ErrFactor))* 47 MUL <- Sp '*'
48 Factor <- OPENPAR (Exp / ErrExpFac) (CLOSEPAR / ErrClosePar) / NUMBER / NAME 48 NAME <- !RESERVED Sp [a-z]+
49 ErrSemi <- %{errSemi} 49 NUMBER <- Sp [0-9]+
50 ErrExpIf <- %{errExpIf} 50 OPENPAR <- Sp '('
51 ErrThen <- %{errThen} 51 READ <- Sp 'read'
52 ErrCmdSeq1 <- %{errCmdSeq1} 52 REPEAT <- Sp 'repeat'
53 ErrCmdSeq2 <- %{errCmdSeq2} 53 SEMICOLON <- Sp ';'
54 ErrEnd <- %{errEnd} 54 SUB <- Sp '-'
55 ErrCmdSeqRep <- %{errCmdSeqRep} 55 THEN <- Sp 'then'
56 ErrUntil <- %{errUntil} 56 UNTIL <- Sp 'until'
57 ErrExpRep <- %{errExpRep} 57 WRITE <- Sp 'write'
58 ErrAssignOp <- %{errAssignOp} 58 RESERVED <- (IF / ELSE / END / READ / REPEAT / THEN / UNTIL / WRITE) ![a-z]+
59 ErrExpAssign <- %{errExpAssign} 59 Sp <- (%s / %nl)*
60 ErrReadName <- %{errReadName} 60]], terror)
61 ErrWriteExp <- %{errWriteExp}
62 ErrSimpExp <- %{errSimpExp}
63 ErrTerm <- %{errTerm}
64 ErrFactor <- %{errFactor}
65 ErrExpFac <- %{errExpFac}
66 ErrClosePar <- %{errClosePar}
67 ADD <- Sp '+'
68 ASSIGNMENT <- Sp ':='
69 CLOSEPAR <- Sp ')'
70 DIV <- Sp '/'
71 IF <- Sp 'if'
72 ELSE <- Sp 'else'
73 END <- Sp 'end'
74 EQUAL <- Sp '='
75 LESS <- Sp '<'
76 MUL <- Sp '*'
77 NAME <- Sp !RESERVED [a-z]+
78 NUMBER <- Sp [0-9]+
79 OPENPAR <- Sp '('
80 READ <- Sp 'read'
81 REPEAT <- Sp 'repeat'
82 SEMICOLON <- Sp ';'
83 SUB <- Sp '-'
84 THEN <- Sp 'then'
85 UNTIL <- Sp 'until'
86 WRITE <- Sp 'write'
87 RESERVED <- (IF / ELSE / END / READ / REPEAT / THEN / UNTIL / WRITE) ![a-z]+
88 Sp <- %s*
89]]
90 61
91 62
92local function mymatch(g, s) 63local function mymatch(g, s)
@@ -94,7 +65,7 @@ local function mymatch(g, s)
94 if not r then 65 if not r then
95 local line, col = re.calcline(s, pos) 66 local line, col = re.calcline(s, pos)
96 local msg = "Error at line " .. line .. " (col " .. col .. "): " 67 local msg = "Error at line " .. line .. " (col " .. col .. "): "
97 return r, msg .. terror[e].msg 68 return r, msg .. terror[e]
98 end 69 end
99 return r 70 return r
100end 71end