Skip to content

Commit

Permalink
feat: translated command keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
pgagnidze committed May 31, 2023
1 parent a81eff2 commit c332912
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 48 deletions.
11 changes: 7 additions & 4 deletions ena/lang/translator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local module = {
module.alphabet = "აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰ"

-- custom language keywords --
module.kwords.longForm.keyIf = "თუ ეს პირობა სრულდება"
module.kwords.longForm.keyIf = "თუ პირობა სრულდება"
module.kwords.shortForm.keyIf = "თუ"

module.kwords.longForm.keyElseIf = "სხვა შემთხვევაში შეამოწმე"
Expand All @@ -20,15 +20,18 @@ module.kwords.shortForm.keyElseIf = "თუარადა"
module.kwords.longForm.keyElse = "სხვა შემთხვევაში"
module.kwords.shortForm.keyElse = "თუარა"

module.kwords.longForm.keyReturn = "დააბრუნე ეს მნიშვნელობა"
module.kwords.longForm.keyReturn = "დააბრუნე მნიშვნელობა"
module.kwords.shortForm.keyReturn = "დააბრუნე"

module.kwords.longForm.keyWhile = "სანამ ეს პირობა სრულდება გაიმეორე"
module.kwords.longForm.keyWhile = "სანამ პირობა სრულდება გაიმეორე"
module.kwords.shortForm.keyWhile = "სანამ"

module.kwords.longForm.keyPrint = "მაჩვენე ეს მნიშვნელობა ეკრანზე"
module.kwords.longForm.keyPrint = "მაჩვენე მნიშვნელობა ეკრანზე"
module.kwords.shortForm.keyPrint = "მაჩვენე"

module.kwords.longForm.keyExec = "გაუშვი ბრძანება"
module.kwords.shortForm.keyExec = "ბრძანება"

module.kwords.longForm.keyNew = "ახალი"
module.kwords.shortForm.keyNew = "ახალი"

Expand Down
66 changes: 27 additions & 39 deletions ena/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -247,67 +247,55 @@ local functionCall = lpeg.V "functionCall"
local funcParams = lpeg.V "funcParams"
local funcArgs = lpeg.V "funcArgs"

local KW_function =
(KW "function" + KW(translator.kwords.longForm.keyFunction) + KW(translator.kwords.shortForm.keyFunction))
local KW_elseif = (KW "elseif" + KW(translator.kwords.longForm.keyElseIf) + KW(translator.kwords.shortForm.keyElseIf))
local KW_else = (KW "else" + KW(translator.kwords.longForm.keyElse) + KW(translator.kwords.shortForm.keyElse))
local KW_local = (KW "local" + KW(translator.kwords.longForm.keyLocal) + KW(translator.kwords.shortForm.keyLocal))
local KW_if = (KW "if" + KW(translator.kwords.longForm.keyIf) + KW(translator.kwords.shortForm.keyIf))
local KW_return = (KW "return" + KW(translator.kwords.longForm.keyReturn) + KW(translator.kwords.shortForm.keyReturn))
local KW_while = (KW "while" + KW(translator.kwords.longForm.keyWhile) + KW(translator.kwords.shortForm.keyWhile))
local kW_print = (op.print + KW(translator.kwords.longForm.keyPrint) + KW(translator.kwords.shortForm.keyPrint))
local KW_exec = (op.exec + KW(translator.kwords.longForm.keyExec) + KW(translator.kwords.shortForm.keyExec))
local KW_true = (KW "true" + KW(translator.values.longForm.valTrue) + KW(translator.values.shortForm.valTrue))
local KW_false = (KW "false" + KW(translator.values.longForm.valFalse) + KW(translator.values.shortForm.valFalse))
local KW_new = (KW "new" + KW(translator.kwords.longForm.keyNew) + KW(translator.kwords.shortForm.keyNew))
local KW_nil = (KW "nil" + KW(translator.values.longForm.valNil) + KW(translator.values.shortForm.valNil))

local Ct = lpeg.Ct
local grammar = {
"program",
program = endToken * Ct(funcDec ^ 1) * -1,
funcDec = (KW "function" + KW(translator.kwords.longForm.keyFunction) + KW(translator.kwords.shortForm.keyFunction)) *
identifier *
delim.openFunctionParameterList *
funcParams *
funcDec = KW_function * identifier * delim.openFunctionParameterList * funcParams *
((op.assign * expression) + Cc({})) *
delim.closeFunctionParameterList *
(blockStatement + sep.statement) /
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,
elses = ((KW "elseif" + KW(translator.kwords.longForm.keyElseIf) + KW(translator.kwords.shortForm.keyElseIf)) *
expression *
blockStatement) *
elses /
nodeIf +
((KW "else" + KW(translator.kwords.longForm.keyElse) + KW(translator.kwords.shortForm.keyElse)) * blockStatement) ^
-1,
elses = (KW_elseif * expression * blockStatement) * elses / nodeIf + (KW_else * blockStatement) ^ -1,
variable = identifier / nodeVariable,
functionCall = identifier * delim.openFunctionParameterList * funcArgs * delim.closeFunctionParameterList /
nodeFunctionCall,
funcArgs = Ct((expression * (delim.functionParameterSeparator * expression) ^ 0) ^ -1),
writeTarget = Ct(variable * (delim.openArray * expression * delim.closeArray) ^ 0) / foldArrayElement,
statement = blockStatement + functionCall +
writeTarget * (op.assign * expression) ^ -1 * -delim.openBlock / nodeAssignment +
(KW "local" + KW(translator.kwords.longForm.keyLocal) + KW(translator.kwords.shortForm.keyLocal)) * identifier *
(op.assign * expression) ^ -1 /
nodeLocalVariable +
(KW "if" + KW(translator.kwords.longForm.keyIf) + KW(translator.kwords.shortForm.keyIf)) * expression *
blockStatement *
elses /
nodeIf +
(KW "return" + KW(translator.kwords.longForm.keyReturn) + KW(translator.kwords.shortForm.keyReturn)) *
expression /
nodeReturn +
(KW "while" + KW(translator.kwords.longForm.keyWhile) + KW(translator.kwords.shortForm.keyWhile)) * expression *
blockStatement /
nodeWhile +
(op.print + KW(translator.kwords.longForm.keyPrint) + KW(translator.kwords.shortForm.keyPrint)) * expression /
nodePrint +
(op.exec) * expression / nodeExec,
boolean = ((KW "true" + KW(translator.values.longForm.valTrue) + KW(translator.values.shortForm.valTrue)) * Cc(true) +
(KW "false" + KW(translator.values.longForm.valFalse) + KW(translator.values.shortForm.valFalse)) * Cc(false)) /
nodeBoolean,
primary = Ct(
(KW "new" + KW(translator.kwords.longForm.keyNew) + KW(translator.kwords.shortForm.keyNew)) *
(delim.openArray * expression * delim.closeArray) ^ 1
) *
primary /
foldNewArray +
functionCall +
KW_local * identifier * (op.assign * expression) ^ -1 / nodeLocalVariable +
KW_if * expression * blockStatement * elses / nodeIf +
KW_return * expression / nodeReturn +
KW_while * expression * blockStatement / nodeWhile +
kW_print * expression / nodePrint +
KW_exec * expression / nodeExec,
boolean = (KW_true * Cc(true) + KW_false * Cc(false)) / nodeBoolean,
primary = Ct(KW_new * (delim.openArray * expression * delim.closeArray) ^ 1) * primary / foldNewArray + functionCall +
writeTarget +
stringLiteral / nodeString +
numeral / nodeNumeral +
boolean +
(op.exec) * expression / nodeExec +
(KW "nil" + KW(translator.values.longForm.valNil) + KW(translator.values.shortForm.valNil)) / nodeNil +
KW_exec * expression / nodeExec +
KW_nil / nodeNil +
delim.openFactor * expression * delim.closeFactor,
exponentExpr = primary * (op.exponent * exponentExpr) ^ -1 / addExponentOp,
unaryExpr = op.unarySign * unaryExpr / addUnaryOp + exponentExpr,
Expand Down
2 changes: 1 addition & 1 deletion example/example.ena
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ფუნქცია ფაქტორიალი(ნ = 6) {
თუ ეს პირობა სრულდება ნ != 0 {
თუ ნ != 0 {
დააბრუნე ნ * ფაქტორიალი(ნ - 1)
} თუარა {
დააბრუნე 1
Expand Down
8 changes: 4 additions & 4 deletions example/example.long.ena
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
ფუნქცია სახელად ფაქტორიალი(ციფრი = 6) {
თუ ეს პირობა სრულდება ციფრი != 0 {
დააბრუნე ეს მნიშვნელობა ციფრი * ფაქტორიალი(ციფრი - 1)
თუ პირობა სრულდება ციფრი != 0 {
დააბრუნე მნიშვნელობა ციფრი * ფაქტორიალი(ციფრი - 1)
} სხვა შემთხვევაში {
დააბრუნე ეს მნიშვნელობა 1
დააბრუნე მნიშვნელობა 1
}
}

ფუნქცია სახელად მთავარი() {
დააბრუნე ეს მნიშვნელობა ფაქტორიალი()
დააბრუნე მნიშვნელობა ფაქტორიალი()
}

0 comments on commit c332912

Please sign in to comment.