-
Notifications
You must be signed in to change notification settings - Fork 0
8 Module expr
This module declares some variants of the macro expr to do basic mathematical operations.
The format of the variants is C-like operators.
$/lia
All basic mathematical operations are implemented using +, -, *, and / tokens. All the operations are implemented to use registers and literal numbers. Like example:
[import "$/lia", "$/var", "io", "expr"]
mov rc, (2 * (4 + 1))
call putn # Prints 10
iout '\n'
The % is used to gets the modulus of the division of the operands, like % C operator. And ** is used to calculates the exponentiation of the operands.
[import "$/lia", "$/var", "io", "expr"]
mov rc, (2 ** 8)
call putn # 256
iout '\n'
mov rc, (17 % 8)
call putn # 1
iout '\n'
You can negate an operand using the - before a register or number. Like in: mov rc, (-rc).
Variants using ++ and -- can be used in the same way as the correspondent operators of the C language. See example:
[import "$/lia", "$/var", "io", "expr"]
set rj, 4
mov rc, (5 * (rj++))
call putn # 20
iout '\n'
mov rc, rj
call putn # 5
iout '\n'
Like in C language, the usage of the tokens ++ after is a pos-increment, and before the value is a pre-increment. The next example increments the register before sets it to lvalue.
[import "$/lia", "$/var", "io", "expr"]
set rj, 4
mov rc, (5 * (++rj))
call putn # Now it's prints 25
iout '\n'
mov rc, rj
call putn # 5
iout '\n'
The variants are implemented to use numbers or registers, but not characters. If you want to use a character you can put it alone inside the parenthesis and this variant will return the value of the character inside the rc register. Like example:
[import "$/lia", "expr"]
mov rc, (('A') + 1)
out rc
iout '\n'
To use a variable inside a procedure it's required import $/var first, otherwise, the expansion of the macro will cause an error.
To gets the value of a variable use $ and the variable number. Example:
mov rc, (($0) + 8) # var0 + 8
You can call a procedure passing 0 or 1 argument and gets it returns. Just put the procedure name and, if necessary, the argument like a literal number or register. Example:
[import "$/lia", "$/var", "io", "expr"]
proc double
add rc, rc
ret rc
endproc
mov rc, (double (double 5))
call putn # 20
iout '\n'
Different of conventional high-level languages, Lia's expressions are not a really interpreted expression but otherwise, it's a special macro with lvalue. It's mean the Lia don't have operators in the expressions but a matched sequence of tokens. For example, ++ is not really an operator but a sequence of two tokens +.
Then (rc++) is the same as (rc + +), because it's just two tokens. And if it is a matched sequence of tokens, you must use a pre-defined variant. So you can't do something like (1 + 2 + 3) because this module doesn't declare this variant.
But if you want to do that, you can declare a variant like:
[macro expr(n1: number '+' n2: number '+' n3: number) =
# Do something...
]
To see help on how to make a variant to expr macro, read this page.