Skip to content

Commit

Permalink
Add a test for the mod Tiger stdlib function
Browse files Browse the repository at this point in the history
  • Loading branch information
iafisher committed Feb 7, 2019
1 parent 4e374d0 commit 28f9778
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
18 changes: 16 additions & 2 deletions hera/stdlib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# flake8: noqa
"""The standard library for the Tiger programming language, implemented in HERA.
All of the pure HERA functions in this module have been copied from the original
Tiger standard library file for HERA-C, written by Dave Wonnacott.
Author: Ian Fisher (iafisher@protonmail.com)
Version: February 2019
"""


# The standard library with parameters-on-the-stack functions.
TIGER_STDLIB_STACK = """
LABEL(printint)
__eval("print(vm.load_memory(vm.registers[14]+3), end='')")
Expand All @@ -23,7 +31,12 @@
LABEL(div)
__eval("left = vm.load_memory(vm.registers[14]+3); right = vm.load_memory(vm.registers[14]+4); vm.store_memory(vm.registers[14]+3, left // right)")
__eval("left = vm.load_memory(vm.registers[14]+3); right = vm.load_memory(vm.registers[14]+4); vm.store_memory(vm.registers[14]+3, left // right if right != 0 else 0)")
RETURN(FP_alt, PC_ret)
LABEL(mod)
__eval("left = vm.load_memory(vm.registers[14]+3); right = vm.load_memory(vm.registers[14]+4); vm.store_memory(vm.registers[14]+3, left % right if right != 0 else 0)")
RETURN(FP_alt, PC_ret)
Expand Down Expand Up @@ -313,6 +326,7 @@
"""


# The data segment for the parameters-on-the-stack functions.
TIGER_STDLIB_STACK_DATA = """
CONSTANT(first_space_for_fsheap, 0x4000)
CONSTANT(last_space_for_fsheap, 0xbfff)
Expand Down
35 changes: 35 additions & 0 deletions test/test_from_cs350.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
"""Test programs generated by a compiler for the Tiger language implemented for CS350.
"""
from .utils import execute_program_helper

from hera.main import main


def helper_stdlib(f, *args):
preamble = """\
#include <Tiger-stdlib-stack-data.hera>
CBON()
MOVE(R12, SP)
INC(SP, {})
"""
preamble = preamble.format(len(args) + 3)

load_args = ""
for i, arg in enumerate(args):
load_args += "SET(R1, {})\nSTORE(R1, {}, R12)\n".format(arg, i + 3)

epilogue = """\
CALL(R12, mod)
LOAD(R1, 3, R12)
DEC(SP, 5)
HALT()
#include <Tiger-stdlib-stack.hera>
"""
epilogue = epilogue.format(f)

return preamble + load_args + epilogue


def test_div_and_print(capsys):
main(["test/assets/cs350/div_and_print.hera"])

Expand All @@ -22,3 +51,9 @@ def test_lexical_scope_deep(capsys):

captured = capsys.readouterr()
assert captured.out == "42"


def test_stdlib_mod(capsys):
vm = execute_program_helper(helper_stdlib("mod", 10, 3))

assert vm.registers[1] == 1

0 comments on commit 28f9778

Please sign in to comment.