Esempio n. 1
0
File: msl.py Progetto: lun-4/msli
def eval_ast(ast, env):
    if not hasattr(ast, 'type'):
        merror.error("eval_ast: Error evaluating AST(%s) %s" % (type(ast), repr(ast)))

    if ast.type == 'symbol':
        return env.get(ast.symval)
    elif ast.type == 'list':
        res = []
        for e in ast.values:
            evaled = msl_eval(e, env)
            res.append(evaled)
        return mtypes.MslList(res)
    elif ast.type == 'vec':
        res = []
        for e in ast.values:
            evaled = msl_eval(e, env)
            res.append(evaled)
        return mtypes.MslVector(res)
    elif ast.type == 'hashmap':
        newhm = mtypes.MslHashmap([])
        for k in ast.hm:
            newhm.hm[k] = msl_eval(ast.hm[k], env)
        return newhm
    else:
        return ast
Esempio n. 2
0
def py_to_msl(obj):
    if isinstance(obj, bool):
        return MslBool(obj)
    elif isinstance(obj, int) or isinstance(obj, float):
        return MslNumber(obj)
    elif isinstance(obj, str):
        return MslStr(obj)
    elif isinstance(obj, list) or isinstance(obj, list):
        return MslList(obj)
    elif isinstance(obj, dict):
        feeder = []
        for k in obj:
            feeder.append(k)
            feeder.append(obj[k])
        return MslHashmap(feeder)
    elif callable(obj):
        params = []
        sig = inspect.signature(obj)
        for param in sig.parameters:
            param_obj = sig.parameters[param]
            params.append(param_obj.name)

        return MslFunction(obj, menv.Enviroment, None, None, params)
    else:
        merror.error("pytomsl: no instance found of %s" % type(obj))
        return None
Esempio n. 3
0
 def __init__(self, lst):
     MslObject.__init__(self, 'list')
     if isinstance(lst, list):
         self.values = lst
         self.hash = tuple(self.values)
     elif isinstance(lst, tuple):
         self.values = list(lst)
         self.hash = lst
     elif isinstance(lst, MslPList):
         self.values = lst.values
     else:
         merror.error("Error creating MslList with %s" % type(lst))
Esempio n. 4
0
def read_atom(reader):
    int_re = re.compile(r"-?[0-9]+$")
    float_re = re.compile(r"-?[0-9][0-9.]*$")
    token = reader.next()

    if re.match(int_re, token):
        return mtypes.MslNumber(token)
    elif re.match(float_re, token):
        return mtypes.MslNumber(token)
    elif token[0] == '"':
        if token[-1] == '"':
            return mtypes.MslStr(_unescape(token[1:-1]))
        else:
            merror.error('Expected ", got EOF')
    elif token[0] == ':':
        return mtypes.MslKeyword(token[1:])
    elif token == 'nil':
        return mtypes.MslNil()
    elif token == 'true':
        return mtypes.MslBool(True)
    elif token == 'false':
        return mtypes.MslBool(False)
    else:
        return mtypes.MslSymbol(token)
Esempio n. 5
0
File: msl.py Progetto: lun-4/msli
def msl_rep(string):
    try:
        return msl_print(msl_eval(msl_read(string), repl_env))
    except RuntimeError as e:
        merror.error("Runtime Error: %s" % str(e))