def application(self, ast): function_name = ast.get("name") args = ast.get("arguments") if function_name not in self.functions: raise SemanticError("Unkown function %s" % function_name) function = self.functions[function_name] spec = inspect.getfullargspec(function.__call__) if not len(spec.args) == len(args) + 1: raise SemanticError( "Invalid number of arguments for function %s (expected %s and got %s)" % (function_name, len(spec.args) - 1, len(args))) return function(*args)
def _get_constructor(self, typename): typename = str(typename) if typename in self.constructors: return self.constructors[typename] constructor = builtins for name in typename.split('.'): try: context = vars(constructor) except Exception as e: raise SemanticError( 'Could not find constructor for %s (%s): %s' % (typename, type(constructor).__name__, str(e)) ) if name in context: constructor = context[name] else: constructor = None break if constructor: return constructor # synthethize a new type constructor = type(typename, (self.baseType,), {}) self._register_constructor(constructor) return constructor
def application(self, ast): function_name = ast.get("name") args = ast.get("arguments") if function_name in self.functions: self.used_functions.add(function_name) return Application(function_name, args) else: raise SemanticError("Unkown function %s" % function_name)
def head(self, ast): """ Flatten lists in head and do not allow normal disjunctions. """ if ast['disjunction']: raise SemanticError('Normal disjunctions are not allowed.') new_ast = AST() for item in ast: if ast[item]: new_ast[item] = list(flatten(ast[item])) else: new_ast[item] = None return new_ast
def _find_existing_constructor(self, typename): constructor = builtins for name in typename.split('.'): try: context = vars(constructor) except Exception as e: raise SemanticError( 'Could not find constructor for %s (%s): %s' % (typename, type(constructor).__name__, str(e))) if name in context: constructor = context[name] else: constructor = None break return constructor
def _default(self, ast, *args, **kwargs): if not args: return ast name = args[0] constructor = self._get_constructor(name) try: if type(constructor) is type and issubclass(constructor, Node): return constructor(*args[1:], ast=ast, ctx=self.ctx, **kwargs) else: return constructor(ast, *args[1:], **kwargs) except Exception as e: raise SemanticError( 'Could not call constructor for %s: %s' % (name, str(e)) )
def _default(self, ast, *args, **kwargs): if not args: return ast typespec = args[0].split(BASE_CLASS_TOKEN) typename = typespec[0] bases = typespec[1:] base = self.base_type for base in bases: base = self._get_constructor(bases[0], base) constructor = self._get_constructor(typename, base) try: if type(constructor) is type and issubclass(constructor, Node): return constructor(*args[1:], ast=ast, ctx=self.ctx, **kwargs) else: return constructor(ast, *args[1:], **kwargs) except Exception as e: raise SemanticError('Could not call constructor for %s: %s' % (typename, str(e)))
def variable(self, name): if name not in self.mapping.keys(): raise SemanticError("Unkown variable %s" % name) return self.mapping.get(name).data.df
def variable(self, ast): if ast in self.variables: self.used_variables.add(ast) else: raise SemanticError("Unknown variable %s" % ast) return ast
def weight_at_level(self, ast): """ Do not allow Weight Constraints. """ raise SemanticError( 'Weight constraints or optimize statements are not allowed.')