Skip to content

Commit

Permalink
Emit warning when HERA-C boilerplate is detected
Browse files Browse the repository at this point in the history
Resolves #19
  • Loading branch information
iafisher committed Dec 14, 2018
1 parent 7449122 commit 283fd62
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 7 additions & 1 deletion hera/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
class TreeToOplist(Transformer):
"""Transform Lark's parse tree into a list of HERA ops."""

def cpp_program(self, matches):
emit_warning("void HERA_main() { ... } is not necessary")
return matches

def op(self, matches):
return Op(matches[0], matches[1:])

Expand Down Expand Up @@ -115,8 +119,10 @@ def parse(text):

if isinstance(tree, Tree):
return tree.children
else:
elif isinstance(tree, Op):
return [tree]
else:
return tree


def replace_escapes(s):
Expand Down
2 changes: 1 addition & 1 deletion test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_program_to_string():


def test_dump_state(capsys):
dump_state(VirtualMachine())
dump_state(VirtualMachine(), verbose=True)

captured = capsys.readouterr()
assert "R1 = 0x0000 = 0" in captured.err
Expand Down
10 changes: 10 additions & 0 deletions test/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from unittest.mock import patch

from hera.parser import Op, parse, replace_escapes
from hera.utils import HERAError
Expand Down Expand Up @@ -97,6 +98,15 @@ def test_parse_hera_boilerplate_no_includes():
assert parse("void HERA_main() {SETLO(R1, 42)}") == [Op("SETLO", ["R1", 42])]


def test_parse_hera_boilerplate_gives_warning():
with patch("hera.utils._emit_msg") as mock_emit_warning:
parse("void HERA_main() {SETLO(R1, 42)}")
assert mock_emit_warning.call_count == 1
assert "Warning" in mock_emit_warning.call_args[0][0]
assert "HERA_main" in mock_emit_warning.call_args[0][0]
assert "not necessary" in mock_emit_warning.call_args[0][0]


def test_parse_another_single_line_comments():
program = """\
// Single-line comment
Expand Down

0 comments on commit 283fd62

Please sign in to comment.