diff options
| author | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-28 14:29:49 -0300 |
|---|---|---|
| committer | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-28 14:29:49 -0300 |
| commit | 59da25ff241a83d8139e41199ef7a23f6e17fa65 (patch) | |
| tree | a9ad13fa0f8ddb50855b716b349e4ded5d5fac8d /examples/tiny.lua | |
| parent | 772df00e061db3cd7d0af92c8ab65bc023d8121d (diff) | |
| download | lpeglabel-59da25ff241a83d8139e41199ef7a23f6e17fa65.tar.gz lpeglabel-59da25ff241a83d8139e41199ef7a23f6e17fa65.tar.bz2 lpeglabel-59da25ff241a83d8139e41199ef7a23f6e17fa65.zip | |
Updating examples to the new semantics
Diffstat (limited to 'examples/tiny.lua')
| -rw-r--r-- | examples/tiny.lua | 147 |
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 @@ | |||
| 1 | local re = require 'relabel' | 1 | local re = require 'relabel' |
| 2 | 2 | ||
| 3 | local terror = {} | 3 | local terror = { |
| 4 | 4 | cmdSeq = "Missing ';' in CmdSeq", | |
| 5 | local 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", |
| 7 | end | 7 | ifThenCmdSeq = "Error matching CmdSeq of 'then' branch", |
| 8 | 8 | ifElseCmdSeq = "Error matching CmdSeq of 'else' branch", | |
| 9 | newError("errSemi", "Error: missing ';'") | 9 | ifEnd = "Error matching 'end' keyword of 'if'", |
| 10 | newError("errExpIf", "Error: expected expression after 'if'") | 10 | repeatCmdSeq = "Error matching CmdSeq of 'repeat'", |
| 11 | newError("errThen", "Error: expected 'then' keyword") | 11 | repeatUntil = "Error matching 'until' keyword", |
| 12 | newError("errCmdSeq1", "Error: expected at least a command after 'then'") | 12 | repeatExp = "Error matching expression of 'until'", |
| 13 | newError("errCmdSeq2", "Error: expected at least a command after 'else'") | 13 | assignOp = "Error matching ':='", |
| 14 | newError("errEnd", "Error: expected 'end' keyword") | 14 | assignExp = "Error matching expression of assignment", |
| 15 | newError("errCmdSeqRep", "Error: expected at least a command after 'repeat'") | 15 | readName = "Error matching 'NAME' after 'read'", |
| 16 | newError("errUntil", "Error: expected 'until' keyword") | 16 | writeExp = "Error matching expression after 'write'", |
| 17 | newError("errExpRep", "Error: expected expression after 'until'") | 17 | simpleExp = "Error matching 'SimpleExp'", |
| 18 | newError("errAssignOp", "Error: expected ':=' in assigment") | 18 | term = "Error matching 'Term'", |
| 19 | newError("errExpAssign", "Error: expected expression after ':='") | 19 | factor = "Error matching 'Factor'", |
| 20 | newError("errReadName", "Error: expected an identifier after 'read'") | 20 | openParExp = "Error matching expression after '('", |
| 21 | newError("errWriteExp", "Error: expected expression after 'write'") | 21 | closePar = "Error matching ')'", |
| 22 | newError("errSimpExp", "Error: expected '(', ID, or number after '<' or '='") | 22 | undefined = "Undefined Error" |
| 23 | newError("errTerm", "Error: expected '(', ID, or number after '+' or '-'") | 23 | } |
| 24 | newError("errFactor", "Error: expected '(', ID, or number after '*' or '/'") | 24 | |
| 25 | newError("errExpFac", "Error: expected expression after '('") | 25 | g = re.compile([[ |
| 26 | newError("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 | |
| 29 | local labelCode = {} | 29 | IfCmd <- IF Exp^ifExp THEN^ifThen CmdSeq^ifThenCmdSeq (ELSE CmdSeq^ifElseCmdSeq / '') END^ifEnd |
| 30 | for 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 |
| 32 | end | 32 | ReadCmd <- READ NAME^readName |
| 33 | 33 | WriteCmd <- WRITE Exp^writeExp | |
| 34 | re.setlabels(labelCode) | 34 | Exp <- SimpleExp ((LESS / EQUAL) SimpleExp^simpleExp / '') |
| 35 | 35 | SimpleExp <- Term ((ADD / SUB) Term^term)* | |
| 36 | local 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 | ||
| 92 | local function mymatch(g, s) | 63 | local 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 |
| 100 | end | 71 | end |
