def topy(obj): """The main compiler function - convert a code-doc into an ast node""" # {{{ ln = line number if available (also set global \lineno) global lineno ln = getattr(obj, 'lineno', None) if ln != None: lineno = ln # }}} typ = type(obj).__name__ method = (getattr(objtopy, typ, None) or getattr(objtopy, "_" + typ, None)) compilefunc = method and method.im_func if compilefunc: node = compilefunc(obj) else: debug() raise CompileError("Cannot compile %s (of type %s)" % (obj, typ)) if ln: node.lineno = ln return node
def test(self, event, state): res, returnVal = self.expectation.test(event, state) if res == MATCH and debug: debug() return res, returnVal
def Symbol(symbol): if symbol == data.true: return ast.Const(True) elif symbol == data.false: return ast.Const(False) elif symbol == data.none: return ast.Const(None) elif symbol.namespace == "": return ast.Name(symbol.name) else: debug() raise CompileError, "can't compile symbol with namespace: %s" % symbol
def defLanguage(name, parent, globs=None): if globs is None: import sys debug() globs = sys._getframe(-1).f_globals existing = globs.get(name) if isinstance(existing, Language): if ((parent is None and existing.__impl__.parent is not None) or (parent is not None and existing.__impl__.parent.userlang is not parent)): raise LanguageBaseException("base language for %s differs from" " forward declaration" % name) existing.__impl__.clear() # Q: Is it ok if \existing is imported from another module? return existing else: return Language(name, parent, globs['__name__'])
def compileGetOp(symbol): lang = language.getLanguage(symbol.namespace) if lang is None: debug() raise CompileError, "not an operator symbol: %s" % symbol opmodname = lang.__module__ if opmodname == modulename: langexpr = GlobalName(lang.__impl__.name) else: langexpr = ast.Getattr(ast.Subscript(logixglobal('lmodules'), 'OP_APPLY', [ast.Const(opmodname)]), lang.__impl__.name) return ast.Subscript( ast.Getattr(ast.Getattr(langexpr, '__impl__'), 'operators'), 'OP_APPLY', [ast.Const(symbol.name)])
def cachedCodeCopy(code, linedelta=None): t = type(code) if t == flist: args = [cachedCodeCopy(x, linedelta) for x in code.elems] fields = dict([(name, cachedCodeCopy(val, linedelta)) for name, val in code.items()]) res = flist.new(args, fields) copymeta(code, res) return res elif issubclass(t, (tuple, list)): return t([cachedCodeCopy(x, linedelta) for x in code]) elif t == dict: return dict([(name, cachedCodeCopy(x, linedelta)) for name, x in code.items()]) elif t == Symbol and getmeta(code, 'lineno') is not None: sym = Symbol(code) setmeta(sym, lineno=getmeta(code, 'lineno')+linedelta) return sym elif isOperatorType(t): operands = cachedCodeCopy(code.__operands__, linedelta) op = instantiateOp(t, operands, None) copymeta(code, op) lineno = getmeta(code, 'lineno') if lineno is not None: setmeta(op, lineno=lineno+linedelta) return op else: assert t in (int, float, long, str, bool, Symbol, types.NoneType, OperatorType), debug() return code
def cachedCodeCopy(code, linedelta=None): t = type(code) if t == flist: args = [cachedCodeCopy(x, linedelta) for x in code.elems] fields = dict([(name, cachedCodeCopy(val, linedelta)) for name, val in code.items()]) res = flist.new(args, fields) copymeta(code, res) return res elif issubclass(t, (tuple, list)): return t([cachedCodeCopy(x, linedelta) for x in code]) elif t == dict: return dict([(name, cachedCodeCopy(x, linedelta)) for name, x in code.items()]) elif t == Symbol and getmeta(code, 'lineno') is not None: sym = Symbol(code) setmeta(sym, lineno=getmeta(code, 'lineno') + linedelta) return sym elif isOperatorType(t): operands = cachedCodeCopy(code.__operands__, linedelta) op = instantiateOp(t, operands, None) copymeta(code, op) lineno = getmeta(code, 'lineno') if lineno is not None: setmeta(op, lineno=lineno + linedelta) return op else: assert t in (int, float, long, str, bool, Symbol, types.NoneType, OperatorType), debug() return code