Repository: hedyorg/hedy

Issue:
Parsing (or error reporting) issue with combined math expressions
Marleen reports that entering `5 * 5 * 5` is not supported and yields an error that is not user-friendly (see attached image).

Should we consider allowing expressions like this? Or should we focus on improving the error message first?

![image](https://user-images.githubusercontent.com/1160018/109497674-afe89000-7a92-11eb-9f20-b79eae8e4e0e.png)



Discussion:

Resolving patch:
diff --git a/hedy.py b/hedy.py
--- a/hedy.py
+++ b/hedy.py
@@ -753,6 +753,7 @@ def print(self, args):
     def equality_check(self, args):
         arg0 = process_variable(args[0], self.lookup)
         arg1 = process_variable(args[1], self.lookup)
+        #TODO if we start using fstrings here, this str can go
         if len(args) == 2:
             return f"str({arg0}) == str({arg1})" #no and statements
         else:
@@ -771,18 +772,32 @@ def assign(self, args):
             values = args[1:]
             return parameter + " = [" + ", ".join(values) + "]"
 
+    def process_token_or_tree(self, argument):
+        if type(argument) is Tree:
+            return f'{str(argument.children)}'
+        else:
+            return f'int({argument})'
+
+    def process_calculation(self, args, operator):
+        # arguments of a sum are either a token or a
+        # tree resulting from earlier processing
+        # for trees we need to grap the inner string
+        # for tokens we add int around them
+
+        args = [self.process_token_or_tree(a) for a in args]
+        return Tree('sum', f'{args[0]} {operator} {args[1]}')
 
     def addition(self, args):
-        return Tree('sum', f'int({str(args[0])}) + int({str(args[1])})')
+        return self.process_calculation(args, '+')
 
     def substraction(self, args):
-        return Tree('sum', f'int({str(args[0])}) - int({str(args[1])})')
+        return self.process_calculation(args, '-')
 
     def multiplication(self, args):
-        return Tree('sum', f'int({str(args[0])}) * int({str(args[1])})')
+        return self.process_calculation(args, '*')
 
     def division(self, args):
-        return Tree('sum', f'int({str(args[0])}) // int({str(args[1])})')
+        return self.process_calculation(args, '//')
 
 @hedy_transpiler(level=6)
 class ConvertToPython_6(ConvertToPython_5):


Test patch:
diff --git a/tests/tests_level_05.py b/tests/tests_level_05.py
--- a/tests/tests_level_05.py
+++ b/tests/tests_level_05.py
@@ -83,7 +83,6 @@ def test_simple_calculation_without_space(self):
     self.assertEqual(expected, result.code)
     self.assertEqual(False, result.has_turtle)
 
-
   def test_transpile_turtle_basic(self):
     code = textwrap.dedent("""\
     forward 50
@@ -100,6 +99,33 @@ def test_transpile_turtle_basic(self):
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
 
+
+  def test_basic_calc(self):
+    code = textwrap.dedent("""\
+    print '5 keer 5 is ' 5 * 5""")
+
+    expected = textwrap.dedent("""\
+    print('5 keer 5 is '+str(int(5) * int(5)))""")
+
+    result = hedy.transpile(code, self.level)
+
+    self.assertEqual(expected, result.code)
+    self.assertEqual(False, result.has_turtle)
+
+  def test_transpile_multiple_calcs(self):
+    code = textwrap.dedent("""\
+    print '5 keer 5 keer 5 is ' 5 * 5 * 5""")
+
+    expected = textwrap.dedent("""\
+    print('5 keer 5 keer 5 is '+str(int(5) * int(5) * int(5)))""")
+
+    result = hedy.transpile(code, self.level)
+    self.assertEqual(expected, result.code)
+
+    output = run_code(result)
+    self.assertEqual(output, '5 keer 5 keer 5 is 125')
+    self.assertEqual(False, result.has_turtle)
+
   def test_allow_space_after_else_line(self):
     #this code has a space at the end of line 2
     code = textwrap.dedent("""\