Skip to content

Commit

Permalink
feat: optional statement separator
Browse files Browse the repository at this point in the history
  • Loading branch information
pgagnidze committed May 31, 2023
1 parent 42a1db3 commit 4ba337b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ luac.out
*.hex

.DS_Store
example/localtest.*.ena
3 changes: 3 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ Execute the [input.ena](./example/input.ena) for the input program. `input.ena`

Check out the new [sysinfo.ena](./example/sysinfo.ena) program that shows various system informations for Linux and Darwin os.

**Summary of changes:**
- optional statement separator (;)

## New Features

The design choices remain consistent with the principles discussed in the lectures and exercises,
Expand Down
2 changes: 2 additions & 0 deletions ena/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ end
function Compiler:codeStatement(ast)
if ast.tag == "emptyStatement" then
return
elseif ast.tag == "emptyBlock" then
return
elseif ast.tag == "block" then
self:codeBlock(ast)
elseif ast.tag == "statementSequence" then
Expand Down
15 changes: 11 additions & 4 deletions ena/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ local nodeNil = node("nil")
local nodeWhile = node("while", "expression", "block")
local nodeFunction = node("function", "name", "params", "defaultArgument", "block")
local nodeFunctionCall = node("functionCall", "name", "args")
local nodeBlock = node("block", "body")
local nodeLocalVariable = node("local", "name", "init")
local nodeString = node("string", "value")

Expand All @@ -193,6 +192,14 @@ local function nodeStatementSequence(first, rest)
end
end

local function nodeBlock(body)
if body == "" then
return {tag = "emptyBlock"}
else
return {tag = "block", body = body}
end
end

local function addUnaryOp(operator, expression)
return {tag = "unaryOp", op = operator, child = expression}
end
Expand Down Expand Up @@ -269,11 +276,11 @@ local grammar = {
funcDec = KW_function * identifier * delim.openFunctionParameterList * funcParams *
((op.assign * expression) + Cc({})) *
delim.closeFunctionParameterList *
(blockStatement + sep.statement) /
(blockStatement + sep.statement ^ -1) /
nodeFunction,
funcParams = Ct((identifier * (delim.functionParameterSeparator * identifier) ^ 0) ^ -1),
statementList = statement ^ -1 * (sep.statement * statementList) ^ -1 / nodeStatementSequence,
blockStatement = delim.openBlock * statementList * sep.statement ^ -1 * delim.closeBlock / nodeBlock,
statementList = statement * (sep.statement ^ -1 * statementList) ^ -1 / nodeStatementSequence,
blockStatement = delim.openBlock * (statementList ^ -1 / nodeBlock) * sep.statement ^ -1 * delim.closeBlock,
elses = (KW_elseif * expression * blockStatement) * elses / nodeIf + (KW_else * blockStatement) ^ -1,
variable = identifier / nodeVariable,
functionCall = identifier * delim.openFunctionParameterList * funcArgs * delim.closeFunctionParameterList /
Expand Down
31 changes: 29 additions & 2 deletions ena/spec/e2e.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ end
function module:testIdentifiers()
local tests = {
{input = "_leading_underscore", expected = 1},
{input = "has spaces", expected = "Parsing failed"},
{input = "has spaces", expected = nil},
{input = "has-hyphens", expected = "Parsing failed"},
{input = "წერე_ქართულად", expected = 1},
{input = "0not valid", expected = "Parsing failed"}
Expand Down Expand Up @@ -197,7 +197,7 @@ function module:testAssignmentAndReturn()
end

function module:testEmptyStatements()
lu.assertEquals(self:endToEnd(";;;;", true), nil)
lu.assertEquals(self:endToEnd(";;;;", true), "Parsing failed")
end

function module:testEmptyInput()
Expand Down Expand Up @@ -487,4 +487,31 @@ function module:testShellExec()
)
end

function module:testOptionalStatementSeparator()
lu.assertEquals(
self:endToEnd(
[[
function main() {
a = 20
b = a
array = new [2][2] 1
array[1][1] = 0
test = 0
test = test && array[1][2]
test = test && array[2][1]
test = test && array[2][2]
if test {
} elseif b > a {
} else {
};
return b
}
]]
),
20
)
end



return module

0 comments on commit 4ba337b

Please sign in to comment.