Skip to content

Commit

Permalink
#16 [asm] added asmik eval tests with and operator
Browse files Browse the repository at this point in the history
  • Loading branch information
vityaman committed Feb 6, 2024
1 parent 9d03cc5 commit 7e06050
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
7 changes: 7 additions & 0 deletions sleepy/asmik/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .instruction import (
Addi,
Addim,
Andb,
Brn,
Divi,
Instruction,
Expand Down Expand Up @@ -152,6 +153,12 @@ def emit_intrinsic(
lhsr = self.reg_var(lhs)
rhsr = self.reg_var(rhs)
self.emit_i(Slti(dst, lhsr, rhsr))
case tafka.And(lhs, rhs):
lhsr = self.reg_var(lhs)
rhsr = self.reg_var(rhs)
self.emit_i(Andb(dst, lhsr, rhsr))
case _:
raise NotImplementedError(str(source))

def emit_invokation(
self,
Expand Down
6 changes: 4 additions & 2 deletions sleepy/tafka/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
)
from sleepy.tafka.representation import Conditional as TafConditional
from sleepy.tafka.representation import Kind as TafKind
from sleepy.tafka.representation.rvalue import Invokation
from sleepy.tafka.representation.rvalue import And, Invokation

UniqueNameSequence = Generator[str, None, None]

Expand Down Expand Up @@ -135,8 +135,10 @@ def visit_application_intrinsic(
self.emit_intermidiate(Eq(args[0], args[1]))
case "lt":
self.emit_intermidiate(Lt(args[0], args[1]))
case "and":
self.emit_intermidiate(And(args[0], args[1]))
case _:
raise RuntimeError(str(intrinsic))
raise NotImplementedError(str(intrinsic))

def visit_application_variable(
self,
Expand Down
1 change: 1 addition & 0 deletions sleepy/tafka/representation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .kind import Int, Kind, Signature
from .node import Node
from .rvalue import (
And,
BinaryOperator,
Copy,
Div,
Expand Down
13 changes: 13 additions & 0 deletions sleepy/tafka/representation/rvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,16 @@ def name(self) -> str:
@property
def value(self) -> Kind:
return Bool()


@dataclass(repr=False)
class And(BinaryOperator):
@override
@property
def name(self) -> str:
return "and"

@override
@property
def value(self) -> Kind:
return Bool()
12 changes: 12 additions & 0 deletions test/asmik/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
("(if (eq 1 2) 6 9)", "9"),
("(if (eq (rem 2 2) 0) 1 0)", "1"),
("(if (eq (div 2 2) 0) 1 0)", "0"),
("(def a 7) a", "7"),
("(def a 70) (def b 8) (sum a b)", "78"),
(
(
"(def a 1) "
"(def b a) "
"(def a (sum 1 a)) "
"(if (and (eq a 2) (eq b 1)) 1 0)"
),
"1",
),
("(if (eq 1 0) (if (eq 1 1) 0 0) (if (eq 1 0) 0 1))", "1"),
],
)
def test_evaluate(src: str, res: str) -> None:
Expand Down

0 comments on commit 7e06050

Please sign in to comment.