Skip to content

Commit 4244e2c

Browse files
committed
Add sqrt
Add int Add bitwise ops Fix treating everything as floats
1 parent c07fb55 commit 4244e2c

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

py/math_expression.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
ast.Pow: op.pow,
1313
ast.BitXor: op.xor,
1414
ast.USub: op.neg,
15-
ast.Mod: op.mod
15+
ast.Mod: op.mod,
16+
ast.BitAnd: op.and_,
17+
ast.BitOr: op.or_,
18+
ast.Invert: op.invert,
19+
ast.And: op.and_,
1620
}
1721

1822
# TODO: restructure args to provide more info, generate hint based on args to save duplication
@@ -52,6 +56,16 @@
5256
"call": lambda *args: random.choice(args),
5357
"hint": "...numbers"
5458
},
59+
"sqrt": {
60+
"args": (1, 1),
61+
"call": lambda a: math.sqrt(a),
62+
"hint": "number"
63+
},
64+
"int": {
65+
"args": (1, 1),
66+
"call": lambda a = None: int(a),
67+
"hint": "number"
68+
},
5569
}
5670

5771
autocompleteWords = list({
@@ -140,7 +154,11 @@ def eval_expr(node):
140154
if isinstance(node, ast.Num):
141155
return node.n
142156
elif isinstance(node, ast.BinOp):
143-
return operators[type(node.op)](float(eval_expr(node.left)), float(eval_expr(node.right)))
157+
l = eval_expr(node.left)
158+
r = eval_expr(node.right)
159+
l = l if isinstance(l, int) else float(l)
160+
r = r if isinstance(r, int) else float(r)
161+
return operators[type(node.op)](l, r)
144162
elif isinstance(node, ast.UnaryOp):
145163
return operators[type(node.op)](eval_expr(node.operand))
146164
elif isinstance(node, ast.Attribute):

0 commit comments

Comments
 (0)