Skip to content

Commit

Permalink
feat: translated and and or operators, use && and || for logical oper…
Browse files Browse the repository at this point in the history
…ators
  • Loading branch information
pgagnidze committed May 31, 2023
1 parent c332912 commit 46af6cc
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 16 deletions.
8 changes: 5 additions & 3 deletions ena/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ local toName = {
[lop.notEqual] = "notEqual",
[lop.not_] = "not",
[lop.and_] = "and",
[lop.or_] = "or"
[lop.or_] = "or",
[lop.and_geo] = "and",
[lop.or_geo] = "or"
}

local unaryToName = {
Expand Down Expand Up @@ -163,12 +165,12 @@ function Compiler:codeExpression(ast)
self:codeExpression(ast.size)
self:addCode("newArray")
elseif ast.tag == "binaryOp" then
if ast.op == lop.and_ then
if ast.op == lop.and_ or ast.op == lop.and_geo then
self:codeExpression(ast.firstChild)
local fixupSSAnd = self:addJump("jumpIfFalseJumpNoPop")
self:codeExpression(ast.secondChild)
self:fixupJump(fixupSSAnd)
elseif ast.op == lop.or_ then
elseif ast.op == lop.or_ or ast.op == lop.or_geo then
self:codeExpression(ast.firstChild)
local fixupSSOr = self:addJump("jumpIfTrueJumpNoPop")
self:codeExpression(ast.secondChild)
Expand Down
6 changes: 3 additions & 3 deletions ena/interpreter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ function Interpreter:run(code)
elseif code[pc] == "execExpression" then
self:traceTwoCodes(code, pc)
local command = self.stack[self.top]
local file = assert(io.popen(command, 'r'), "failed to execute command " .. command)
local output = file:read('*all')
output = string.gsub(output, '^%s*(.-)%s*$', '%1')
local file = assert(io.popen(command, "r"), "failed to execute command " .. command)
local output = file:read("*all")
output = string.gsub(output, "^%s*(.-)%s*$", "%1")
file:close()
self.stack[self.top] = output
elseif code[pc] == "execStatement" then
Expand Down
6 changes: 4 additions & 2 deletions ena/lang/literals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ module.op.lessOrEqual = "<="
module.op.greaterOrEqual = ">="
module.op.equal = "=="
module.op.notEqual = "!="
module.op.and_ = "&"
module.op.or_ = "|"
module.op.and_ = "&&"
module.op.or_ = "||"
module.op.and_geo = "და"
module.op.or_geo = "ან"

-- unary operators --
module.op.not_ = "!"
Expand Down
2 changes: 1 addition & 1 deletion ena/lang/translator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.alphabet = "აბგდევზთიკლმნოპჟრსტუ
module.kwords.longForm.keyIf = "თუ პირობა სრულდება"
module.kwords.shortForm.keyIf = "თუ"

module.kwords.longForm.keyElseIf = "სხვა შემთხვევაში შეამოწმე"
module.kwords.longForm.keyElseIf = "სხვა შემთხვევაში შეამოწმე თუ"
module.kwords.shortForm.keyElseIf = "თუარადა"

module.kwords.longForm.keyElse = "სხვა შემთხვევაში"
Expand Down
2 changes: 1 addition & 1 deletion ena/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ tokens.op.unarySign = T(C(P(l.op.positive) + l.op.negate))
tokens.op.not_ = T(C(l.op.not_))
tokens.op.print = T(l.op.print)
tokens.op.exec = T(l.op.exec)
tokens.op.logical = T(C(l.op.and_) + C(l.op.or_))
tokens.op.logical = T(C(l.op.and_) + C(l.op.or_) + C(l.op.and_geo) + C(l.op.or_geo))

local op = tokens.op
local sep = tokens.sep
Expand Down
6 changes: 3 additions & 3 deletions ena/spec/e2e.lua
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,9 @@ function module:testArrays()
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];
test = test && array[1][2];
test = test && array[2][1];
test = test && array[2][2];
return test
}
]]
Expand Down
17 changes: 15 additions & 2 deletions ena/transpiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ local handlers = {
return node.value
end,
["string"] = function(transpiler, node, indentLevel, indent)
-- TODO: replace \ with \\ and " with \"
return '"' .. node.value .. '"'
end,
["nil"] = function(transpiler, node, indentLevel, indent)
Expand All @@ -128,6 +129,14 @@ local handlers = {
["print"] = function(transpiler, node, indentLevel, indent)
return indent .. "print(" .. transpiler:transpile(node.toPrint, indentLevel) .. ")"
end,
["exec"] = function(transpiler, node, indentLevel, indent)
return "(function()\n" ..
indent .. ' local command = ' .. transpiler:transpile(node.command, indentLevel) .. '\n' ..
indent .. ' local file = assert(io.popen(command, "r"), "failed to execute command " .. command)\n' ..
indent .. ' local output = file:read("*all")\n' ..
indent .. ' return string.gsub(output, "^%s*(.-)%s*$", "%1")\n' ..
indent .. "end)()"
end,
["variable"] = function(transpiler, node, indentLevel, indent)
return node.value
end,
Expand All @@ -136,12 +145,16 @@ local handlers = {
end,
["binaryOp"] = function(transpiler, node, indentLevel, indent)
local op = node.op
if op == "&" then
if op == "&&" then
op = "and"
elseif op == "|" then
elseif op == "||" then
op = "or"
elseif op == "!=" then
op = "~="
elseif op == "+" then -- TODO: handle variables that can be strings or numbers
if node.firstChild.tag == "string" or node.secondChild.tag == "string" then
op = ".."
end
end
return transpiler:transpile(node.firstChild, indentLevel) ..
" " .. op .. " " .. transpiler:transpile(node.secondChild, indentLevel)
Expand Down
2 changes: 1 addition & 1 deletion example/euler1.ena
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ნ = 1000;
ი = 0;
სანამ ი < ნ {
თუ ი % 3 == 0 | ი % 5 == 0 {
თუ ი % 3 == 0 || ი % 5 == 0 {
სულ = სულ + ი
};
ი = ი + 1;
Expand Down

0 comments on commit 46af6cc

Please sign in to comment.