-
Notifications
You must be signed in to change notification settings - Fork 0
Operations
An operation combines two input values to produce an output. To use an operation use the op function:
op(val1, op, val2)
op (the argument, not the function) can be +, -, *, /, ==, !=, >, or <.
+ adds val1 and val2. Note val1 or val2 can be strings.
- subtracts val1 and val2.
* multiplies val1 and val2.
/ divides val1 and val2.
== evaluates to "True" if val1 and val2 are equal.
!= evaluates to "True" if val1 and val2 are not equal.
> evaluates to "True" if val1 is greater than val2.
< evaluates to "True" if val1 is less than val2.
Example usage: op(int_(1), "+", int_(1)) (1 + 1)
from pythonOne import *
program = Program("py")
program.set_current_program()
with start():
new_var("one_plus_one", int_type(), op(int_(1), "+", int_(1))) # 2
new_var("two_times_point_five", float_type(), op(float_(2), "*", float_(0.5))) # 1
new_var("hello_world", str_type(), op(str_("hello"), "+", str_("world"))) # helloworld
new_var("less_than", bool_type(), op(int_(-1), "<", int_(1))) # True
new_var("equals", bool_type(), op(int_(0), "==", int_(1))) # False
new_var("crazy", int_type(), op(int_(1), "+", op(op(int_(10), "*", int_(9)), "-", int_(10)))) # 1 + ((10 * 9) - 10)
new_var("better", int_type(), "1 + ((10 * 9) - 10))")
program.build()
First and foremost, we import pythonOne and create a program:
from pythonOne import *
program = Program("py")
program.set_current_program()
Then start it:
with start():
Next, we find out what 1 + 1 is, which, of course, is 2, and store it in "one_plus_one":
new_var("one_plus_one", int_type(), op(int_(1), "+", int_(1))) # 2
Here, we use multiplication with floats to find out what 2(0.5) is:
new_var("two_times_point_five", float_type(), op(float_(2), "*", float_(0.5))) # 1
We can also glue strings together using +:
new_var("hello_world", str_type(), op(str_("hello"), "+", str_("world"))) # helloworld
Let's not ignore boolean operations:
new_var("less_than", bool_type(), op(int_(-1), "<", int_(1))) # True
new_var("equals", bool_type(), op(int_(0), "==", int_(1))) # False
Last, but not the very last, we nest operations to find out 1 + ((10 * 9) - 10), which could get a little crazy*:
new_var("crazy", int_type(), op(int_(1), "+", op(op(int_(10), "*", int_(9)), "-", int_(10)))) # 1 + ((10 * 9) - 10)
If that's the case it might be better to do things manually:
new_var("better", int_type(), "1 + ((10 * 9) - 10))")
Be careful when doing this, because "0.5" in Python is 0.5, but in C# "0.5" is 0.5f. For example, you could be adding floats like this:
new_var("um", float_type(), "0.1 + 0.2")
In Python, this would work perfectly fine, but in C# this would be disastrous, because you need f at the end.
To fix it, you could use type specifiers:
new_var("okay", float_type(), float_(0.1) + " + " + float_(0.2))
The downside is that the +s can get pretty tangled.
Last, and the very last (besides the footnote), we build the program:
program.build()
* It is highly recommend to have something in editor or IDE to highlight your parentheses.