Skip to content

Commit

Permalink
follow up of renameing
Browse files Browse the repository at this point in the history
  • Loading branch information
hamidb80 committed Oct 14, 2022
1 parent 2da779e commit c4db311
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
4 changes: 2 additions & 2 deletions examples/cli.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
## hpi = sin(PI/2)
##
## you can also define a function by:
## <ident>(...[params]) = <expression>
## <ident>(...<ident>) = <expression>
## like:
## sum(x, y) = x + y
##
Expand All @@ -30,7 +30,7 @@ while true:
try:
let ast = parse input

if ast.kind == emnkInfix and ast.operator == emokAssign:
if ast.kind == emnkInfix and ast.operator == emoAssign:
case ast.left.kind
of emnkVar:
vars[ast.left.ident] = ast.right.eval(vars, fns)
Expand Down
42 changes: 21 additions & 21 deletions src/emath.nim
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,17 @@ func eval*(mn: MathNode,
of emnkPostfix:
let v = rec mn.inside
case mn.operator
of emokNotFact:
of emoNotFact:
if isInt v: float fac v.toInt
else: evalErr "factorial only works for integers, got float " & $v
else: evalErr "invalid postfix: " & $v

of emnkPrefix:
let v = rec mn.inside
case mn.operator
of emokPlus: v
of emokMinus: -v
of emokNotFact: float not v.toBinary
of emoPlus: v
of emoMinus: -v
of emoNotFact: float not v.toBinary
else: evalErr "invalid prefix: " & $mn.operator

of emnkInfix:
Expand All @@ -149,21 +149,21 @@ func eval*(mn: MathNode,
ri = rec mn.right

case mn.operator
of emokPow: pow(le, ri)
of emokMult: le * ri
of emokDiv: le / ri
of emokPlus: le + ri
of emokMinus: le - ri
of emokMod: floorMod(le, ri)
of emokLarger: float le > ri
of emokLargerEq: float le >= ri
of emokEq: float le == ri
of emokNotEq: float le != ri
of emokAlmostEq: float almostEqual(le, ri)
of emokLessEq: float le <= ri
of emokLess: float le < ri
of emokAnd: float le.toBinary and ri.toBinary
of emokOr: float le.toBinary or ri.toBinary
of emoPow: pow(le, ri)
of emoMult: le * ri
of emoDiv: le / ri
of emoPlus: le + ri
of emoMinus: le - ri
of emoMod: floorMod(le, ri)
of emoLarger: float le > ri
of emoLargerEq: float le >= ri
of emoEq: float le == ri
of emoNotEq: float le != ri
of emoAlmostEq: float almostEqual(le, ri)
of emoLessEq: float le <= ri
of emoLess: float le < ri
of emoAnd: float le.toBinary and ri.toBinary
of emoOr: float le.toBinary or ri.toBinary
else: evalErr "invalid infix operator " & $mn.operator

func eval*(mn: MathNode): float =
Expand Down Expand Up @@ -307,7 +307,7 @@ func parse*(input: string): MathNode =
of emtkOperator:
if isFinalValue stack.last:

if tk.operator == emokNotFact:
if tk.operator == emoNotFact:
let t = newPostfix(tk.operator, stack.pop)
stack.last.children[^1] = t
stack.add t
Expand All @@ -327,7 +327,7 @@ func parse*(input: string): MathNode =

else:
case tk.operator
of emokPlus, emokMinus, emokNotFact:
of emoPlus, emoMinus, emoNotFact:
let t = newPrefix tk.operator
stack.last.children.add t
stack.add t
Expand Down
50 changes: 25 additions & 25 deletions src/emath/model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import std/tables
type
EMathOperator* = enum
# -- operation
emokPow = "^"
emokMult = "*"
emokDiv = "/"
emokPlus = "+"
emokMinus = "-"
emokMod = "%"
emoPow = "^"
emoMult = "*"
emoDiv = "/"
emoPlus = "+"
emoMinus = "-"
emoMod = "%"
# -- logical
emokNotFact = "!"
emokAnd = "&"
emokOr = "|"
emoNotFact = "!"
emoAnd = "&"
emoOr = "|"
# -- comparison
emokLarger = ">"
emokLargerEq = ">="
emokEq = "=="
emokNotEq = "!="
emokAlmostEq = "~="
emokLessEq = "<="
emokLess = "<"
emoLarger = ">"
emoLargerEq = ">="
emoEq = "=="
emoNotEq = "!="
emoAlmostEq = "~="
emoLessEq = "<="
emoLess = "<"
# -- others
emokAssign = "="
emoAssign = "="

EMathTokenKind* = enum
emtkNumber
Expand Down Expand Up @@ -90,11 +90,11 @@ func inside*(mn: MathNode): MathNode =

func priority*(mo: EMathOperator): int =
case mo
of emokPow: 6
of emokMult, emokDiv: 5
of emokPlus, emokMinus: 4
of emokMod: 3
of emokNotFact: 2
of emokAnd, emokOr: 1
of emokLarger, emokLargerEq, emokAlmostEq, emokEq, emokNotEq, emokLessEq, emokLess: 0
of emokAssign: -1
of emoPow: 6
of emoMult, emoDiv: 5
of emoPlus, emoMinus: 4
of emoMod: 3
of emoNotFact: 2
of emoAnd, emoOr: 1
of emoLarger, emoLargerEq, emoAlmostEq, emoEq, emoNotEq, emoLessEq, emoLess: 0
of emoAssign: -1

0 comments on commit c4db311

Please sign in to comment.