Browse Source

Add new tests for the constructor

Yentl Van Tendeloo 9 years ago
parent
commit
125c5a44bf
49 changed files with 312 additions and 2 deletions
  1. 1 0
      .gitignore
  2. 1 0
      fix_linux.sh
  3. 3 1
      interface/HUTN/grammars/actionlanguage.g
  4. 6 1
      interface/HUTN/hutn_compiler/semantics_visitor.py
  5. 0 0
      interface/HUTN/test/constructor_compilation_action_language/__init__.py
  6. 7 0
      interface/HUTN/test/constructor_compilation_action_language/code/action.al
  7. 3 0
      interface/HUTN/test/constructor_compilation_action_language/code/assign.al
  8. 15 0
      interface/HUTN/test/constructor_compilation_action_language/code/factorial.al
  9. 15 0
      interface/HUTN/test/constructor_compilation_action_language/code/fibonacci.al
  10. 25 0
      interface/HUTN/test/constructor_compilation_action_language/code/fibonacci_smart.al
  11. 3 0
      interface/HUTN/test/constructor_compilation_action_language/code/float.al
  12. 2 0
      interface/HUTN/test/constructor_compilation_action_language/code/funccall.al
  13. 5 0
      interface/HUTN/test/constructor_compilation_action_language/code/funccall_params.al
  14. 2 0
      interface/HUTN/test/constructor_compilation_action_language/code/funcdef_params.al
  15. 5 0
      interface/HUTN/test/constructor_compilation_action_language/code/global.al
  16. 6 0
      interface/HUTN/test/constructor_compilation_action_language/code/ifelse.al
  17. 6 0
      interface/HUTN/test/constructor_compilation_action_language/code/include.al
  18. 1 0
      interface/HUTN/test/constructor_compilation_action_language/code/include_1.al
  19. 3 0
      interface/HUTN/test/constructor_compilation_action_language/code/include_2.al
  20. 3 0
      interface/HUTN/test/constructor_compilation_action_language/code/integer.al
  21. 2 0
      interface/HUTN/test/constructor_compilation_action_language/code/list.al
  22. 4 0
      interface/HUTN/test/constructor_compilation_action_language/code/multi_include.al
  23. 8 0
      interface/HUTN/test/constructor_compilation_action_language/code/mutual_recursion.al
  24. 2 0
      interface/HUTN/test/constructor_compilation_action_language/code/selection.al
  25. 6 0
      interface/HUTN/test/constructor_compilation_action_language/code/types.al
  26. 1 0
      interface/HUTN/test/constructor_compilation_action_language/code/vardecl.al
  27. 4 0
      interface/HUTN/test/constructor_compilation_action_language/code/while.al
  28. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/action
  29. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/assign
  30. 8 0
      interface/HUTN/test/constructor_compilation_action_language/expected/declare
  31. 32 0
      interface/HUTN/test/constructor_compilation_action_language/expected/dictionary
  32. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/factorial
  33. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/fibonacci
  34. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/fibonacci_smart
  35. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/funccall
  36. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/funccall_params
  37. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/funcdef_params
  38. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/global
  39. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/ifelse
  40. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/include
  41. 32 0
      interface/HUTN/test/constructor_compilation_action_language/expected/list
  42. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/multi_include
  43. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/mutual_recursion
  44. 0 0
      interface/HUTN/test/constructor_compilation_action_language/expected/selection
  45. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/types
  46. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/vardecl
  47. 1 0
      interface/HUTN/test/constructor_compilation_action_language/expected/while
  48. 28 0
      interface/HUTN/test/constructor_compilation_action_language/test_real.py
  49. 57 0
      interface/HUTN/test/constructor_compilation_action_language/test_simple.py

+ 1 - 0
.gitignore

@@ -7,6 +7,7 @@
 __pycache__
 interface/HUTN/test/grammar_action_language/util.py
 interface/HUTN/test/graph_compilation_action_language/util.py
+interface/HUTN/test/constructor_compilation_action_language/util.py
 kernel/test/functions/utils.py
 kernel/test/instructions/utils.py
 kernel/test/primitives/utils.py

+ 1 - 0
fix_linux.sh

@@ -6,3 +6,4 @@ ln -s ../utils.py kernel/test/rules/utils.py
 
 ln -s ../util.py interface/HUTN/test/grammar_action_language/util.py
 ln -s ../util.py interface/HUTN/test/graph_compilation_action_language/util.py
+ln -s ../util.py interface/HUTN/test/constructor_compilation_action_language/util.py

+ 3 - 1
interface/HUTN/grammars/actionlanguage.g

@@ -85,7 +85,9 @@ grammar{
     integer: DEC_NUMBER;
     float: FLOAT_NUMBER;
 
-    rvalue: ID;
+    rvalue
+        : (rvalue LSQUARE expression RSQUARE)
+        | ID;
 
     lvalue: ID;
 

+ 6 - 1
interface/HUTN/hutn_compiler/semantics_visitor.py

@@ -492,7 +492,12 @@ class SemanticsVisitor(Visitor):
         self.set_symbol(tree, symbol)
 
     def visit_rvalue(self, tree):
-        self.visit_id(tree)
+        if len(tree.get_tail()) > 1:
+            # Complex
+            raise Exception("TODO")
+        else:
+            # Simple
+            self.visit_id(tree)
 
     def visit_lvalue(self, tree):
         self.visit_id(tree)

+ 0 - 0
interface/HUTN/test/constructor_compilation_action_language/__init__.py


+ 7 - 0
interface/HUTN/test/constructor_compilation_action_language/code/action.al

@@ -0,0 +1,7 @@
+Void function test():
+	Action a
+	a = !if
+	a = !while
+	a = !assign
+	a = !global
+	a = !call

+ 3 - 0
interface/HUTN/test/constructor_compilation_action_language/code/assign.al

@@ -0,0 +1,3 @@
+Void function test():
+	Integer a
+	a = 1

+ 15 - 0
interface/HUTN/test/constructor_compilation_action_language/code/factorial.al

@@ -0,0 +1,15 @@
+Element integer_multiplication = ?primitives/integer_multiplication
+Element integer_lte = ?primitives/integer_lte
+Element integer_subtraction = ?primitives/integer_subtraction
+
+include "io.alh"
+
+Integer function factorial(n : Integer):
+	if(integer_lte(n, 1)):
+		return 1
+	else:
+		return integer_multiplication(n, factorial(integer_subtraction(n, 1)))
+
+Void function main():
+	while(True):
+		output(factorial(input()))

+ 15 - 0
interface/HUTN/test/constructor_compilation_action_language/code/fibonacci.al

@@ -0,0 +1,15 @@
+Element integer_addition = ?primitives/integer_addition
+Element integer_lte = ?primitives/integer_lte
+Element integer_subtraction = ?primitives/integer_subtraction
+
+include "io.alh"
+
+Integer function fib(param : Integer):
+	if (integer_lte(param, 2)):
+		return 1
+	else:
+		return integer_addition(fib(integer_subtraction(param, 1)), fib(integer_subtraction(param, 2)))
+
+Void function main():
+	while(True):
+		output(fib(input()))

+ 25 - 0
interface/HUTN/test/constructor_compilation_action_language/code/fibonacci_smart.al

@@ -0,0 +1,25 @@
+Element integer_addition = ?primitives/integer_addition
+Element integer_gt = ?primitives/integer_gt
+Element list_len = ?primitives/list_len
+Element list_append = ?primitives/list_append
+Element integer_subtraction = ?primitives/integer_subtraction
+Element dict_read = ?primitives/dict_read
+Element create_node = ?primitives/create_node
+
+Element numbers = ?
+
+include "io.alh"
+
+Integer function fib(param : Integer):
+	Integer new
+	while (integer_gt(param, list_len(numbers))):
+		new = list_len(numbers)
+		list_append(numbers, integer_addition(dict_read(numbers, integer_subtraction(new, 2)), dict_read(numbers, integer_subtraction(new, 1))))
+	return dict_read(numbers, integer_subtraction(param, 1))
+
+Void function main():
+	numbers = create_node()
+	list_append(numbers, 1)
+	list_append(numbers, 1)
+	while(True):
+		output(fib(input()))

+ 3 - 0
interface/HUTN/test/constructor_compilation_action_language/code/float.al

@@ -0,0 +1,3 @@
+Element float_addition = ?primitives/float_addition
+
+Element float_subtraction = ?primitives/float_subtraction

+ 2 - 0
interface/HUTN/test/constructor_compilation_action_language/code/funccall.al

@@ -0,0 +1,2 @@
+Integer function test():
+	test()

+ 5 - 0
interface/HUTN/test/constructor_compilation_action_language/code/funccall_params.al

@@ -0,0 +1,5 @@
+Integer function abc(a : Integer):
+	a = 1
+
+Void function main():
+	abc(2)

+ 2 - 0
interface/HUTN/test/constructor_compilation_action_language/code/funcdef_params.al

@@ -0,0 +1,2 @@
+Integer function abc(a : Integer):
+	a = 1

+ 5 - 0
interface/HUTN/test/constructor_compilation_action_language/code/global.al

@@ -0,0 +1,5 @@
+Integer a = 1
+String b
+
+Integer function abc():
+	String c

+ 6 - 0
interface/HUTN/test/constructor_compilation_action_language/code/ifelse.al

@@ -0,0 +1,6 @@
+Void function main():
+	Integer a
+	if (True):
+		a = 2
+	else:
+		a = 3

+ 6 - 0
interface/HUTN/test/constructor_compilation_action_language/code/include.al

@@ -0,0 +1,6 @@
+include "test/graph_compilation_action_language/code/integer.al"
+include "test/graph_compilation_action_language/code/float.al"
+
+Void function main():
+	integer_addition(1, 2)
+	float_addition(3, 4)

+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/code/include_1.al

@@ -0,0 +1 @@
+Element a

+ 3 - 0
interface/HUTN/test/constructor_compilation_action_language/code/include_2.al

@@ -0,0 +1,3 @@
+include "include_1.al"
+
+Element b

+ 3 - 0
interface/HUTN/test/constructor_compilation_action_language/code/integer.al

@@ -0,0 +1,3 @@
+Element integer_addition = ?primitives/integer_addition
+
+Element integer_subtraction = ?primitives/integer_subtraction

+ 2 - 0
interface/HUTN/test/constructor_compilation_action_language/code/list.al

@@ -0,0 +1,2 @@
+Element a
+a = ["a", "b", 4, 6, "def", ["g", "h", 3], 3]

+ 4 - 0
interface/HUTN/test/constructor_compilation_action_language/code/multi_include.al

@@ -0,0 +1,4 @@
+include "include_1.al"
+include "include_2.al"
+
+Element c

+ 8 - 0
interface/HUTN/test/constructor_compilation_action_language/code/mutual_recursion.al

@@ -0,0 +1,8 @@
+Void function main():
+	a()
+
+Integer function a():
+	return b()
+
+Integer function b():
+	return a()

+ 2 - 0
interface/HUTN/test/constructor_compilation_action_language/code/selection.al

@@ -0,0 +1,2 @@
+Integer a
+output(a[1])

+ 6 - 0
interface/HUTN/test/constructor_compilation_action_language/code/types.al

@@ -0,0 +1,6 @@
+Void function main():
+	Type a
+	a = Integer
+	a = Type
+	a = Action
+	a = String

+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/code/vardecl.al

@@ -0,0 +1 @@
+Integer a

+ 4 - 0
interface/HUTN/test/constructor_compilation_action_language/code/while.al

@@ -0,0 +1,4 @@
+Void function main():
+	Integer a
+	while (True):
+		a = 2

+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/action

@@ -0,0 +1 @@
+["\"funcdef\"", "\"test\"", "0", "\"declare\"", 0, "true", "\"assign\"", "\"resolve\"", 0, "\"const\"", "{\"value\": \"if\"}", "true", "\"assign\"", "\"resolve\"", 0, "\"const\"", "{\"value\": \"while\"}", "true", "\"assign\"", "\"resolve\"", 0, "\"const\"", "{\"value\": \"assign\"}", "true", "\"assign\"", "\"resolve\"", 0, "\"const\"", "{\"value\": \"global\"}", "true", "\"assign\"", "\"resolve\"", 0, "\"const\"", "{\"value\": \"call\"}", "false", "false"]

+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/assign

@@ -0,0 +1 @@
+["\"funcdef\"", "\"test\"", "0", "\"declare\"", 0, "true", "\"assign\"", "\"resolve\"", 0, "\"const\"", "1", "false", "false"]

+ 8 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/declare

@@ -0,0 +1,8 @@
+V auto_0(resolve)
+D auto_0,"var",123456
+V auto_1(1)
+V auto_2(constant)
+D auto_2,"node",auto_1
+V auto_initial_IP(assign)
+D auto_initial_IP,"var",auto_0
+D auto_initial_IP,"value",auto_2

+ 32 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/dictionary

@@ -0,0 +1,32 @@
+V auto_0("a")
+V auto_initial_IP(global)
+D auto_initial_IP,"var",auto_0
+V auto_2(resolve)
+D auto_2,"var",auto_0
+N auto_3
+V auto_4(1)
+V auto_5(2)
+E auto_6(auto_3,auto_5)
+E auto_7(auto_6,auto_4)
+V auto_8("abc")
+V auto_9(5)
+E auto_10(auto_3,auto_9)
+E auto_11(auto_10,auto_8)
+V auto_12(1)
+N auto_13
+V auto_14(5)
+V auto_15("def")
+E auto_16(auto_13,auto_15)
+E auto_17(auto_16,auto_14)
+E auto_18(auto_3,auto_13)
+E auto_19(auto_18,auto_12)
+V auto_20(True)
+V auto_21(False)
+E auto_22(auto_3,auto_21)
+E auto_23(auto_22,auto_20)
+V auto_24(constant)
+D auto_24,"node",auto_3
+V auto_25(assign)
+D auto_25,"var",auto_2
+D auto_25,"value",auto_24
+D auto_initial_IP,"next",auto_25

File diff suppressed because it is too large
+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/factorial


File diff suppressed because it is too large
+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/fibonacci


File diff suppressed because it is too large
+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/fibonacci_smart


+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/funccall

@@ -0,0 +1 @@
+["\"funcdef\"", "\"test\"", "0", "\"call\"", "\"access\"", "\"resolve\"", "\"test\"", "0", "false", "false"]

+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/funccall_params

@@ -0,0 +1 @@
+["\"funcdef\"", "\"abc\"", "1", 0, "\"assign\"", "\"resolve\"", 0, "\"const\"", "1", "false", "true", "\"funcdef\"", "\"main\"", "0", "\"call\"", "\"access\"", "\"resolve\"", "\"abc\"", "1", "\"const\"", "2", "false", "false"]

+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/funcdef_params

@@ -0,0 +1 @@
+["\"funcdef\"", "\"abc\"", "1", 0, "\"assign\"", "\"resolve\"", 0, "\"const\"", "1", "false", "false"]

+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/global

@@ -0,0 +1 @@
+["\"global\"", "\"a\"", "\"const\"", "1", "true", "\"global\"", "\"b\"", "true", "\"funcdef\"", "\"abc\"", "0", "\"declare\"", 0, "false", "false"]

+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/ifelse

@@ -0,0 +1 @@
+["\"funcdef\"", "\"main\"", "0", "\"declare\"", 0, "true", "\"if\"", "\"const\"", "true", "\"assign\"", "\"resolve\"", 0, "\"const\"", "2", "false", "true", "\"assign\"", "\"resolve\"", 0, "\"const\"", "3", "false", "false", "false"]

File diff suppressed because it is too large
+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/include


+ 32 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/list

@@ -0,0 +1,32 @@
+V auto_0("a")
+V auto_initial_IP(global)
+D auto_initial_IP,"var",auto_0
+V auto_2(resolve)
+D auto_2,"var",auto_0
+N auto_3
+V auto_4("a")
+D auto_3,0,auto_4
+V auto_5("b")
+D auto_3,1,auto_5
+V auto_6(4)
+D auto_3,2,auto_6
+V auto_7(6)
+D auto_3,3,auto_7
+V auto_8("def")
+D auto_3,4,auto_8
+N auto_9
+V auto_10("g")
+D auto_9,0,auto_10
+V auto_11("h")
+D auto_9,1,auto_11
+V auto_12(3)
+D auto_9,2,auto_12
+D auto_3,5,auto_9
+V auto_13(3)
+D auto_3,6,auto_13
+V auto_14(constant)
+D auto_14,"node",auto_3
+V auto_15(assign)
+D auto_15,"var",auto_2
+D auto_15,"value",auto_14
+D auto_initial_IP,"next",auto_15

+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/multi_include

@@ -0,0 +1 @@
+["\"global\"", "\"a\"", "true", "\"global\"", "\"b\"", "true", "\"global\"", "\"c\"", "false"]

+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/mutual_recursion

@@ -0,0 +1 @@
+["\"funcdef\"", "\"main\"", "0", "\"call\"", "\"access\"", "\"resolve\"", "\"a\"", "0", "false", "true", "\"funcdef\"", "\"a\"", "0", "\"return\"", "true", "\"call\"", "\"access\"", "\"resolve\"", "\"b\"", "0", "false", "true", "\"funcdef\"", "\"b\"", "0", "\"return\"", "true", "\"call\"", "\"access\"", "\"resolve\"", "\"a\"", "0", "false", "false"]

+ 0 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/selection


+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/types

@@ -0,0 +1 @@
+["\"funcdef\"", "\"main\"", "0", "\"declare\"", 0, "true", "\"assign\"", "\"resolve\"", 0, "\"const\"", "{\"value\": \"Integer\"}", "true", "\"assign\"", "\"resolve\"", 0, "\"const\"", "{\"value\": \"Type\"}", "true", "\"assign\"", "\"resolve\"", 0, "\"const\"", "{\"value\": \"Action\"}", "true", "\"assign\"", "\"resolve\"", 0, "\"const\"", "{\"value\": \"String\"}", "false", "false"]

+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/vardecl

@@ -0,0 +1 @@
+["\"global\"", "\"a\"", "false"]

+ 1 - 0
interface/HUTN/test/constructor_compilation_action_language/expected/while

@@ -0,0 +1 @@
+["\"funcdef\"", "\"main\"", "0", "\"declare\"", 0, "true", "\"while\"", "\"const\"", "true", "\"assign\"", "\"resolve\"", 0, "\"const\"", "2", "false", "false", "false"]

+ 28 - 0
interface/HUTN/test/constructor_compilation_action_language/test_real.py

@@ -0,0 +1,28 @@
+import unittest
+import util
+from postproc import postproc
+
+from hutn_compiler.compiler import main
+import json
+
+def compile_file(obj, filename):
+    result = main(util.get_code_path(filename), "grammars/actionlanguage.g", "CS", [])
+    try:
+        expected = json.loads(open(util.get_expected_path(filename)).read())
+    except:
+        #f = open(util.get_expected_path(filename), 'w')
+        #f.write(json.dumps(result))
+        #f.close()
+        pass
+    assert result == expected
+
+class TestReal(unittest.TestCase):
+    def test_fibonacci(self):
+        compile_file(self, "fibonacci.al")
+
+    def test_fibonacci_smart(self):
+        compile_file(self, "fibonacci_smart.al")
+
+    def test_factorial(self):
+        compile_file(self, "factorial.al")
+

+ 57 - 0
interface/HUTN/test/constructor_compilation_action_language/test_simple.py

@@ -0,0 +1,57 @@
+import unittest
+import util
+from postproc import postproc
+
+from hutn_compiler.compiler import main
+import json
+
+def compile_file(obj, filename):
+    result = main(util.get_code_path(filename), "grammars/actionlanguage.g", "CS", [])
+    try:
+        expected = json.loads(open(util.get_expected_path(filename)).read())
+    except:
+        #f = open(util.get_expected_path(filename), 'w')
+        #f.write(json.dumps(result))
+        #f.close()
+        pass
+    assert result == expected
+
+class TestSimple(unittest.TestCase):
+    def test_assign(self):
+        compile_file(self, "assign.al")
+
+    def test_vardecl(self):
+        compile_file(self, "vardecl.al")
+
+    def test_funccall(self):
+        compile_file(self, "funccall.al")
+
+    def test_ifelse(self):
+        compile_file(self, "ifelse.al")
+
+    def test_while(self):
+        compile_file(self, "while.al")
+
+    def test_funcdef_params(self):
+        compile_file(self, "funcdef_params.al")
+
+    def test_funccall_params(self):
+        compile_file(self, "funccall_params.al")
+
+    def test_action(self):
+        compile_file(self, "action.al")
+
+    def test_types(self):
+        compile_file(self, "types.al")
+
+    def test_mutual_recursion(self):
+        compile_file(self, "mutual_recursion.al")
+
+    def test_global(self):
+        compile_file(self, "global.al")
+
+    def test_include(self):
+        compile_file(self, "include.al")
+
+    def test_multi_include(self):
+        compile_file(self, "multi_include.al")