def _lambda(self, body, args=None): flags, argnames, defaults = funcArgs(args) return_ = Symbol(rootops.base_ns, "return") do = Symbol(rootops.base_ns, "do:") if not hasResult(body): body = Doc(do, [body, Doc(return_, [None])]) return ast.Lambda(argnames, defaults, flags, topy(body))
def compileDeflang(self, name, parentx, body): assert isinstance(name, Symbol) if parentx: assertResult(parentx, "derive language from") parent = topy(parentx) else: parent = logixglobal('langlang') funcname = "#lang:%s" % name body = len(body) > 0 and block(body, False).nodes or [] funcbody = ast.Stmt( body + [ast.CallFunc(ast.Getattr(ast.Getattr(ast.Name(str(name)), '__impl__'), 'addDeflangLocals'), [ast.CallFunc(ast.Name('locals'), [])])]) res = ast.Stmt([ ast.Assign([compilePlace(Symbol(name))], ast.CallFunc(logixglobal('defLanguage'), [ast.Const(str(name)), parent, ast.CallFunc(GlobalName("globals"), [])])), astFunction(funcname, tuple(), tuple(), 0, None, funcbody), ast.CallFunc(ast.Name(funcname), []), ast.AssName(funcname, 'OP_DELETE')]) lineno = getmeta(self, 'lineno') for n in res.nodes: n.lineno = lineno res.lineno = lineno return res
def dollar_macro(namex, rulex): if namex == data.none: n = None elif isinstance(namex, Symbol): n = str(namex) else: n = namex optOp = Symbol(syntaxlang_ns, "[") if isDoc(rulex, optOp) and rulex.contentLen() == 1: # The rule being named is optional ([...]) with no alternative. # Set the alternative to 'omit' (because it's named) return makeRule(NamedRule, n, op(optOp, rulex[0], '-')) else: return makeRule(NamedRule, n, rulex)
def localModuleQuote(): return rootops.quote(rootops.localmodule(rootops.escape(Symbol("__name__"), extra=[])))
def localModuleQuote(): return Doc(rootops.quote, [Doc(rootops.getlmoduleOp, [Doc(rootops.escape, [Symbol("", "__name__")], {"extra":[]})])])
def compilePlace(place, opname='OP_ASSIGN'): # {{{ def compileSubscriptPlace(objx, keyxs): def compileSubscriptPlace(objx, keyxs): assertResult(objx, "subscript") for key in keyxs: assertResult(key, "subscript with") return ast.Subscript(topy(objx), opname, map(topy, keyxs)) # }}} # {{{ def compileSlicePlace(objx, fromx, tox, stepx): def compileSlicePlace(objx, fromx, tox, stepx): assertResult(objx, "slice") assertResult(fromx, "slice from ") assertResult(tox, "slice to") assertResult(stepx, "slice with step") return ast.Subscript(topy(objx), opname, [ast.Sliceobj(map(topy, (fromx, tox, stepx)))]) # }}} if isinstance(place, Symbol): if place.namespace != "": raise CompileError, "cannot assign to symbol with namespace: %s" % place return ast.AssName(place.name, opname) elif isDoc(place, rootops.sliceOp): return compileSlicePlace(place[0], place[1], place[2], place[3]) elif isDoc(place, rootops.subscriptOp): return compileSubscriptPlace(place[0], place[1:]) elif isPyOp(place): token = place.tag.name if token == ".": # {{{ assign to field expr = topy(place[0]) field = place[1] if isinstance(field, Symbol) and field.namespace == "": return ast.AssAttr(expr, field.name, opname) else: raise CompileError("Cannot assign to %s" % place) # }}} elif token == "": # continuation op # {{{ assign to slice or subscript last = place[-1] # expr = the continuationOp not including the last part if len(place) == 2: expr = place[0] else: expr = Doc(Symbol(rootops.base_ns, ""), place[:-1]) kind = last[-1] if kind == 'subscript': return compileSubscriptPlace(expr, last[:-1]) elif kind == "slice": start,end,step = last[:-1] return compileSlicePlace(expr, start, end, step) else: raise CompileError("Cannot asign to %s " % place) # }}} elif token == ",": return ast.AssTuple([compilePlace(p, opname) for p in list(place)]) elif token == "[" and place[1] == 'list': return ast.AssList([compilePlace(p, opname) for p in place]) elif token == "(": return compilePlace(place[1], opname) else: raise CompileError("Cannot asign to %s " % place) else: raise CompileError("Cannot assign to %s" % place)
import cPickle from parser import * from language import Language import language import data from data import Symbol, Doc # }}} # {{{ Base Operators base_ns = "base" std_ns = "std" callOp = Symbol("lx", "call") listOp = Symbol("lx", "list") tupleOp = Symbol("lx", "tuple") dictOp = Symbol("lx", "dict") getlmoduleOp = Symbol("lx", "getlmodule") subscriptOp = Symbol("lx", "subscript") sliceOp = Symbol("lx", "slice") langlang_ns = "langlang" switchlang = Symbol(langlang_ns, "(:") outerlang = Symbol(langlang_ns, "(^") defop = Symbol(langlang_ns, "defop") deflang = Symbol(langlang_ns, "deflang") setlang = Symbol(langlang_ns, "setlang") getops = Symbol(langlang_ns, "getops")