Skip to content

Commit bf4e1f8

Browse files
committed
add comparisons
1 parent 911f65a commit bf4e1f8

5 files changed

Lines changed: 80 additions & 20 deletions

File tree

loda/lang/operation.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,29 @@ class Operation:
4444

4545
class Type(Enum):
4646
"""Operation type. Type names are written as three-letter, lower-case words."""
47-
NOP = 1
48-
MOV = 2
49-
ADD = 3
50-
SUB = 4
51-
TRN = 5
52-
MUL = 6
53-
DIV = 7
54-
DIF = 8
55-
MOD = 9
56-
POW = 10
57-
GCD = 11
58-
BIN = 12
59-
EQU = 13
60-
MIN = 14
61-
MAX = 15
62-
LPB = 16
63-
LPE = 17
64-
CLR = 18
65-
SEQ = 19
66-
DBG = 20
47+
NOP = 1 # no operation
48+
MOV = 2 # assignment
49+
ADD = 3 # addition
50+
SUB = 4 # subtraction
51+
TRN = 5 # truncated subtraction
52+
MUL = 6 # multiplication
53+
DIV = 7 # division
54+
DIF = 8 # conditional division
55+
MOD = 9 # modulo
56+
POW = 10 # power
57+
GCD = 11 # greatest common divisor
58+
BIN = 12 # binomial coefficient
59+
EQU = 13 # equality
60+
NEQ = 14 # inequality
61+
LEQ = 15 # less or equal
62+
GEQ = 16 # greater or equal
63+
MIN = 17 # minimum
64+
MAX = 18 # maximum
65+
LPB = 19 # loop begin
66+
LPE = 20 # loop end
67+
CLR = 21 # clear
68+
SEQ = 22 # sequence
69+
DBG = 23 # debug
6770

6871
type: Type
6972
"""Type of this operation."""

loda/runtime/operations.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,27 @@ def equ(a, b):
126126
return 1 if a == b else 0
127127

128128

129+
def neq(a, b):
130+
"""Inequality."""
131+
if a == None or b == None:
132+
return None
133+
return 1 if a != b else 0
134+
135+
136+
def leq(a, b):
137+
"""Less or equal."""
138+
if a == None or b == None:
139+
return None
140+
return 1 if a <= b else 0
141+
142+
143+
def geq(a, b):
144+
"""Greater or equal."""
145+
if a == None or b == None:
146+
return None
147+
return 1 if a >= b else 0
148+
149+
129150
def min(a, b):
130151
"""Minimum."""
131152
if a == None or b == None:
@@ -166,6 +187,12 @@ def exec_arithmetic(t: Operation.Type, a, b):
166187
return bin(a, b)
167188
elif t == Operation.Type.EQU:
168189
return equ(a, b)
190+
elif t == Operation.Type.NEQ:
191+
return neq(a, b)
192+
elif t == Operation.Type.LEQ:
193+
return leq(a, b)
194+
elif t == Operation.Type.GEQ:
195+
return geq(a, b)
169196
elif t == Operation.Type.MIN:
170197
return min(a, b)
171198
elif t == Operation.Type.MAX:

tests/operations/geq.csv

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Op1,Op2,Result
2+
0,0,1
3+
1,1,1
4+
2,2,1
5+
-1,-1,1
6+
-2,-2,1
7+
1,0,1
8+
0,1,0
9+
-1,0,0
10+
0,-1,1

tests/operations/leq.csv

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Op1,Op2,Result
2+
0,0,1
3+
1,1,1
4+
2,2,1
5+
-1,-1,1
6+
-2,-2,1
7+
1,0,0
8+
0,1,1
9+
-1,0,1
10+
0,-1,0

tests/operations/neq.csv

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Op1,Op2,Result
2+
0,0,0
3+
1,1,0
4+
2,2,0
5+
-1,-1,0
6+
-2,-2,0
7+
1,0,1
8+
0,1,1
9+
-1,0,1
10+
0,-1,1

0 commit comments

Comments
 (0)