An arithmetic interpreter that allows users to enter, change, combine, and evaluate integer arithmetic expressions interactively. The name arithmetica was suggested by former Duke Professor Michael Littman as a pun on the popular mathematics interpreter Mathematica.
An example session might look like the following (the user's input is in bold):
Welcome to Arithmetica 1-> 3 + 7 * 2 17 2-> (3 + 7) * 2 20 3-> + 10 30 4-> * 10 300 5-> 1 - ans -299 6-> a = 2 * 4 * 2 16 7-> b = 16 16 8-> c = a - b 0 9-> b = 2 * 16 32 10-> c -16 11-> a = -b -32 12-> c -64 13-> z 0 14-> 1 - ans 300 15-> .
expression |
|
semantics |
|
---|---|---|---|
Constant |
[0-9]+ |
an integer constant |
87
13 0 |
Variable |
[a-zA-z]+ |
an expression represented by a word |
a
bugs total_squished |
Assignment |
|
assigns an expression to a variable |
a = 87
bugs = 12 * a a = b |
Unary Operator |
|
prefixes an expression
- // negation |
-13
-(a + b * c) -bugs |
Binary Operator |
|
combines two expressions into a single expression (in precedence order)
% // remainder * // times / // divide + // plus - // minus |
a + b
a % 2 a + 10 * c |
Unary Functions |
|
a function that takes an expression as its single argument
abs // absolute value sqrt // square root |
abs(a * b)
sqrt(x) - y ^ 2 |
Multi-Argument Functions |
|
a function that takes one or more expressions as arguments (each separated
by a comma)
sum average mean |
sum(a, 1, b + 1)
|
Parentheses |
|
raises an expression's precedence |
(a + b) * 3
sqrt(3*(bugs+t)) |
Operators have the following precedence from left to right (listed from
highest to lowest):
() | parentheses |
- | unary operators |
*, /, % | arithmetic operators |
+, - | arithmetic operators |
= | assignment |
Some suggested extensions follow (listed roughly in order from easiest to hardest):
BigInt | allow expressions of integers with unbounded precision (implementation available here) | ||
Right Associative Binary Operator |
|
combines two expressions into a single expression (in precedence order)
^ // exponentiation |
a ^ b
a ^ b ^ c |
Postfix Unary Operator |
|
an expression
! // factorial |
5!
(a + b)! |