Browse Source

The type system stores the real type of a Callable (user-defined or builtin function), including the hidden EvalContext parameter.

Joeri Exelmans 5 years ago
parent
commit
e9cd4faca9
2 changed files with 7 additions and 5 deletions
  1. 5 2
      src/sccd/syntax/expression.py
  2. 2 3
      src/sccd/syntax/scope.py

+ 5 - 2
src/sccd/syntax/expression.py

@@ -79,10 +79,13 @@ class FunctionCall(Expression):
         formal_types, return_type = get_args(function_type)
         self.type = return_type
 
+        # We always secretly pass an EvalContext object with every function call
+        # Not visible to the user.
+        assert formal_types[0] == EvalContext
+
         actual_types = [p.init_rvalue(scope) for p in self.parameters]
-        for i, (formal, actual) in enumerate(zip(formal_types, actual_types)):
+        for i, (formal, actual) in enumerate(zip(formal_types[1:], actual_types)):
             if formal != actual:
-                print(self.function)
                 raise Exception("Function call, argument %d: %s is not expected type %s, instead is %s" % (i, self.parameters[i].render(), str(formal), str(actual)))
         return self.type
 

+ 2 - 3
src/sccd/syntax/scope.py

@@ -198,8 +198,7 @@ class Scope:
   def add_python_function(self, name: str, function: Callable) -> Constant:
     sig = signature(function)
     return_type = sig.return_annotation
-    args = list(sig.parameters.values())[1:] # hide 'EvalContext' parameter to user
-    param_types = [a.annotation for a in args]
+    param_types = [a.annotation for a in sig.parameters.values()]
     function_type = Callable[param_types, return_type]
     
     c = Constant(name=name, type=function_type, value=function)
@@ -208,7 +207,7 @@ class Scope:
 
   def add_function(self, name: str, function: 'Function') -> Constant:
     return_type = function.return_type
-    param_types = [p.type for p in function.params]
+    param_types = [EvalContext] + [p.type for p in function.params]
     function_type = Callable[param_types, return_type]
 
     c = Constant(name=name, type=function_type, value=function)