cleanup
This commit is contained in:
@ -1,37 +1,36 @@
|
||||
registry = {}
|
||||
registry = {} # names are keys
|
||||
all_primitives = []
|
||||
primitive_number_of_arguments = []
|
||||
|
||||
|
||||
def primitive(name, unwrap_spec, wrap_spec): # decorator arguments
|
||||
def primitive(name, unwrap_spec, wrap_spec): # decorator args
|
||||
assert '$' + name not in registry, '${name} already defined'.format(name=name)
|
||||
primitive_number_of_arguments.append(len(unwrap_spec) - 1) # first argument is the receiver
|
||||
|
||||
def expose(func): # decorator
|
||||
def unwrapper(w_receiver, args_w, space):
|
||||
args = [w_receiver] + args_w
|
||||
if len(args) != len(unwrap_spec): # check that call args match primitive args
|
||||
raise TypeError(
|
||||
"Expected {ex} arguments, received {re}.".format(ex=len(unwrap_spec), re=len(args)))
|
||||
if len(args) != len(unwrap_spec): # call args match primitive args
|
||||
raise TypeError("Expected {ex} arguments, received {re}.".format(ex=len(unwrap_spec), re=len(args)))
|
||||
|
||||
unwrapped_args = ()
|
||||
for t, arg in zip(unwrap_spec, args): # unpack values from simple-objects
|
||||
if t in [int, str, float]: # Project: Double, String
|
||||
for t, arg in zip(unwrap_spec, args): # unpack
|
||||
if t in [int, str, float]:
|
||||
unwrapped_args += (arg.value,)
|
||||
elif t is bool: # Project: Boolean
|
||||
unwrapped_args += (bool(arg.value),) # isn't really necessary because "1 or 0" is also valid
|
||||
elif t is bool:
|
||||
unwrapped_args += (bool(arg.value),)
|
||||
else:
|
||||
unwrapped_args += (arg,)
|
||||
|
||||
result = func(*unwrapped_args) # actual call
|
||||
|
||||
if wrap_spec is int: # wrap the result
|
||||
if wrap_spec is int: # wrap
|
||||
return space.newint(result)
|
||||
elif wrap_spec is bool: # Project: Boolean
|
||||
elif wrap_spec is bool:
|
||||
return space.newbool(result)
|
||||
elif wrap_spec is str: # Project: String
|
||||
elif wrap_spec is str:
|
||||
return space.newstring(result)
|
||||
elif wrap_spec is float: # Project: Double
|
||||
elif wrap_spec is float:
|
||||
return space.newdouble(result)
|
||||
return result
|
||||
|
||||
@ -51,12 +50,37 @@ def get_number_of_arguments_of_primitive(idx):
|
||||
return primitive_number_of_arguments[idx]
|
||||
|
||||
|
||||
# int
|
||||
@primitive("int_eq", [int, int], bool)
|
||||
def simple_int_eq(a, b):
|
||||
return a == b
|
||||
|
||||
|
||||
@primitive("int_leq", [int, int], bool)
|
||||
def simple_int_leq(a, b):
|
||||
return a <= b
|
||||
|
||||
|
||||
@primitive("int_geq", [int, int], bool)
|
||||
def simple_int_geq(a, b):
|
||||
return a >= b
|
||||
|
||||
|
||||
@primitive("int_greater", [int, int], bool)
|
||||
def simple_int_greater(a, b):
|
||||
return a > b
|
||||
|
||||
|
||||
@primitive("int_less", [int, int], bool)
|
||||
def simple_int_less(a, b):
|
||||
return a < b
|
||||
|
||||
|
||||
@primitive('int_add', [int, int], int)
|
||||
def simple_int_add(a, b):
|
||||
return a + b
|
||||
|
||||
|
||||
# Project: Sugar
|
||||
@primitive("int_sub", [int, int], int)
|
||||
def simple_int_subtract(a, b):
|
||||
return a - b
|
||||
@ -82,23 +106,27 @@ def simple_int_increment(a):
|
||||
return a + 1
|
||||
|
||||
|
||||
# Project: Boolean, String, Double
|
||||
@primitive("int_tobool", [int], bool)
|
||||
def simple_int_tobool(a):
|
||||
return a
|
||||
|
||||
|
||||
@primitive("int_tostr", [int], str)
|
||||
def simple_int_tobstr(a):
|
||||
def simple_int_tostr(a):
|
||||
return a
|
||||
|
||||
|
||||
@primitive("int_todouble", [int], float)
|
||||
def simple_int_tobdouble(a):
|
||||
def simple_int_todouble(a):
|
||||
return a
|
||||
|
||||
|
||||
# Project: Boolean
|
||||
# bool
|
||||
@primitive("bool_eq", [bool, bool], bool)
|
||||
def simple_bool_eq(a, b):
|
||||
return a is b
|
||||
|
||||
|
||||
@primitive("bool_and", [bool, bool], bool)
|
||||
def simple_bool_and(a, b):
|
||||
return a and b
|
||||
@ -129,33 +157,7 @@ def simple_bool_todouble(a):
|
||||
return a
|
||||
|
||||
|
||||
# bool stuff for int
|
||||
@primitive("int_eq", [int, int], bool)
|
||||
def simple_int_eq(a, b):
|
||||
return a == b
|
||||
|
||||
|
||||
@primitive("int_leq", [int, int], bool)
|
||||
def simple_int_leq(a, b):
|
||||
return a <= b
|
||||
|
||||
|
||||
@primitive("int_geq", [int, int], bool)
|
||||
def simple_int_geq(a, b):
|
||||
return a >= b
|
||||
|
||||
|
||||
@primitive("int_greater", [int, int], bool)
|
||||
def simple_int_greater(a, b):
|
||||
return a > b
|
||||
|
||||
|
||||
@primitive("int_less", [int, int], bool)
|
||||
def simple_int_less(a, b):
|
||||
return a < b
|
||||
|
||||
|
||||
# Project: String
|
||||
# string
|
||||
@primitive("str_eq", [str, str], bool)
|
||||
def simple_str_eq(a, b):
|
||||
return a == b
|
||||
@ -191,7 +193,7 @@ def simple_str_todouble(a):
|
||||
return a
|
||||
|
||||
|
||||
# Project: Double
|
||||
# double
|
||||
@primitive("double_eq", [float, float], bool)
|
||||
def simple_double_eq(a, b):
|
||||
return a == b
|
||||
|
Reference in New Issue
Block a user