Move types() function to tools module

This commit is contained in:
Bartłomiej Pluta
2019-07-10 13:01:03 +02:00
parent a8e4700591
commit 83ea3aaf0f
3 changed files with 48 additions and 43 deletions

View File

@@ -1,6 +1,6 @@
from smnp.error.function import FunctionNotFoundException, MethodNotFoundException, IllegalFunctionInvocationException
from smnp.error.runtime import RuntimeException
from smnp.function.model import types
from smnp.function.tools import argsTypesToString
from smnp.runtime.evaluators.function import BodyEvaluator
@@ -22,7 +22,7 @@ class Environment():
if customMethodResult[0]:
return customMethodResult[1]
raise MethodNotFoundException(types([object], False), name)
raise MethodNotFoundException(argsTypesToString([object], False), name)
def _invokeBuiltinMethod(self, object, name, args):
for method in self.methods:
@@ -46,7 +46,7 @@ class Environment():
self.scopes.pop(-1)
return (True, result)
raise IllegalFunctionInvocationException(f"{method.name}{method.signature.string}",
f"{name}{types(args)}")
f"{name}{argsTypesToString(args)}")
return (False, None)
def invokeFunction(self, name, args):
@@ -80,7 +80,7 @@ class Environment():
self.callStack.pop(-1)
self.scopes.pop(-1)
return (True, result)
raise IllegalFunctionInvocationException(f"{function.name}{function.signature.string}", f"{name}{types(args)}")
raise IllegalFunctionInvocationException(f"{function.name}{function.signature.string}", f"{name}{argsTypesToString(args)}")
return (False, None)
def addCustomFunction(self, name, signature, arguments, body):

View File

@@ -1,6 +1,7 @@
from enum import Enum, auto
from smnp.error.function import IllegalFunctionInvocationException
from smnp.function.tools import argsTypesToString
from smnp.type.model import Type
@@ -29,7 +30,7 @@ class Function:
if ret is None:
return Type.void()
return ret
raise IllegalFunctionInvocationException(self.stringSignature(), f"{self.name}{types(args)}") #TODO: argumenty do typów, nie wartości
raise IllegalFunctionInvocationException(self.stringSignature(), f"{self.name}{argsTypesToString(args)}") #TODO: argumenty do typów, nie wartości
class CombinedFunction(Function):
@@ -53,43 +54,6 @@ class CombinedFunction(Function):
if ret is None:
return Type.void()
return ret
raise IllegalFunctionInvocationException(self.stringSignature(), f"{self.name}{types(args)}")
raise IllegalFunctionInvocationException(self.stringSignature(), f"{self.name}{argsTypesToString(args)}")
def types(args, parentheses=True):
output = []
for arg in args:
if arg.type == Type.LIST:
output.append(listTypes(arg.value, []))
elif arg.type == Type.MAP:
output.append(mapTypes(arg.value, {}))
else:
output.append(arg.type.name.lower())
return f"({', '.join(output)})" if parentheses else ', '.join(output)
def listTypes(l, output=None):
if output is None:
output = []
for item in l:
if item.type == Type.LIST:
output.append(listTypes(item.value, []))
if item.type == Type.MAP:
output.append(mapTypes(item.value, {}))
else:
output.append(item.type.name.lower())
return f"{Type.LIST.name.lower()}<{', '.join(set(output))}>"
def mapTypes(map, output=None):
if output is None:
output = {}
for k, v in map.items():
if v.type == Type.LIST:
output[k] = (listTypes(v.value, []))
elif v.type == Type.MAP:
output[k] = mapTypes(v.value, {})
else:
output[k] = v.type.name.lower()
return f"{Type.MAP.name.lower()}<{', '.join(set([ k.type.name.lower() for k, v in output.items() ]))}><{', '.join(set([ str(v) for k, v in output.items() ]))}>"

41
smnp/function/tools.py Normal file
View File

@@ -0,0 +1,41 @@
from smnp.type.model import Type
def argsTypesToString(args, parentheses=True):
output = []
for arg in args:
if arg.type == Type.LIST:
output.append(listTypes(arg.value, []))
elif arg.type == Type.MAP:
output.append(mapTypes(arg.value, {}))
else:
output.append(arg.type.name.lower())
return f"({', '.join(output)})" if parentheses else ', '.join(output)
def listTypes(l, output=None):
if output is None:
output = []
for item in l:
if item.type == Type.LIST:
output.append(listTypes(item.value, []))
if item.type == Type.MAP:
output.append(mapTypes(item.value, {}))
else:
output.append(item.type.name.lower())
return f"{Type.LIST.name.lower()}<{', '.join(set(output))}>"
def mapTypes(map, output=None):
if output is None:
output = {}
for k, v in map.items():
if v.type == Type.LIST:
output[k] = (listTypes(v.value, []))
elif v.type == Type.MAP:
output[k] = mapTypes(v.value, {})
else:
output[k] = v.type.name.lower()
return f"{Type.MAP.name.lower()}<{', '.join(set([ k.type.name.lower() for k, v in output.items() ]))}><{', '.join(set([ str(v) for k, v in output.items() ]))}>"