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
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
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))
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)
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))