Skip to content

Commit

Permalink
Add unary '-' operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Apr 6, 2024
1 parent 20d3f93 commit 437d116
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
5 changes: 1 addition & 4 deletions elys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ var x = 10
var y = 20
print(x + y, 2 + 2 * 2 / 10 * .1)
x += 2
x += 2
x += 2
x *= 2
x *= -2
print x
print 7 / 3
Expand Down
13 changes: 13 additions & 0 deletions modules/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ proc floatAst*(val: float): FloatAST = FloatAST(val: val, kind: akFloat)
proc stringAst*(val: string): StringAST = StringAST(val: val, kind: akString)
proc boolAst*(val: bool): BoolAST = BoolAST(val: val, kind: akBool)

proc unaryOpAst*(expr: ASTRoot, op: string): UnaryOpAST =
UnaryOpAST(expr: expr, op: op)

proc varAst*(val: string): VarAST = VarAST(name: val, kind: akVar)

proc binOpAst*(l, r: ASTRoot, op: string): BinOpAST = BinOpAST(l: l, r: r, op: op, kind: akBinOp)
Expand Down Expand Up @@ -233,6 +236,16 @@ method eval*(self: VarAST, env: Environment): ASTRoot =
raise newException(RuntimeError, "Variable " & self.name & " was not assigned before")
env.vars[self.name].val

method eval*(self: UnaryOpAST, env: Environment): ASTRoot =
let val = self.expr.eval(env)
if val.kind == akInt:
val.IntAST.val = -val.IntAST.val
return val
elif val.kind == akFloat:
val.FloatAST.val = -val.FloatAST.val
return val
raise newException(RuntimeError, "Can not to apply unary operator '" & self.op & "' to " & $self.expr)

method eval*(self: StmtList, env: Environment): ASTRoot =
var environment = newEnv(env)
for s in self.statements:
Expand Down
17 changes: 15 additions & 2 deletions modules/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ let
@["+", "-", "%"]
]
relationalOperators = @["==", "!=", ">=", "<=", "<", ">"]
unaryOperators = @["--", "++", "-"]
incDecOperators = @["--", "++"]
unaryOperators = @["-"]
assignOperators = @["//=", "+=", "-=", "*=", "/=", "="]
bExprPrecedenceLevels = @[
@["and", "&&"],
Expand All @@ -65,6 +66,9 @@ proc processVar(res: Result): Option[Result] = astRes(varAst(res.val.get))
proc processGroup(res: Result): Option[Result] = res.valx.valy.some


proc unaryOperatorStmt(): Combinator


proc aExprValue(): Combinator =
(
(intNumber ^ processIntNumber) |
Expand All @@ -86,7 +90,8 @@ proc aExprGroup(): Combinator =
proc aExprTerm(): Combinator =
(
aExprValue() |
aExprGroup()
aExprGroup() |
unaryOperatorStmt()
)

proc processBinOp(res: Result): Option[Result] =
Expand Down Expand Up @@ -166,6 +171,14 @@ proc processEof(res: Result): Option[Result] =
astRes(eofStmt())


proc processUnaryOperatorStmt(res: Result): Option[Result] =
echo res
astRes(unaryOpAst(res.valy.ast, res.valx.val.get))
proc unaryOperatorStmt(): Combinator =
# x = -x
(anyOpInList(unaryOperators) + lazy(aExpr)) ^ processUnaryOperatorStmt


proc stmt(): Combinator =
(
printStmt() |
Expand Down

0 comments on commit 437d116

Please sign in to comment.