def t_identifier(p): obj = p[0].value try: return HyInteger(obj) except ValueError: pass if '/' in obj: try: lhs, rhs = obj.split('/') return HyExpression( [HySymbol('fraction'), HyInteger(lhs), HyInteger(rhs)]) except ValueError: pass try: return HyFloat(obj) except ValueError: pass if obj != 'j': try: return HyComplex(obj) except ValueError: pass if obj.startswith(":"): return HyKeyword(obj) obj = ".".join([hy_symbol_mangle(part) for part in obj.split(".")]) return HySymbol(obj)
def t_identifier(p): obj = p[0].value try: return HyInteger(obj) except ValueError: pass if '/' in obj: try: lhs, rhs = obj.split('/') return HyExpression( [HySymbol('fraction'), HyInteger(lhs), HyInteger(rhs)]) except ValueError: pass try: return HyFloat(obj) except ValueError: pass if obj != 'j': try: return HyComplex(obj) except ValueError: pass table = { "true": "True", "false": "False", "nil": "None", "null": "None", } if obj in table: return HySymbol(table[obj]) if obj.startswith(":"): return HyKeyword(obj) def mangle(p): if p.startswith("*") and p.endswith("*") and p not in ("*", "**"): p = p[1:-1].upper() if "-" in p and p != "-": p = p.replace("-", "_") if p.endswith("?") and p != "?": p = "is_%s" % (p[:-1]) return p obj = ".".join([mangle(part) for part in obj.split(".")]) return HySymbol(obj)
def compile_import_expression(self, expr): def _compile_import(expr, module, names=None, importer=ast.Import): return [ importer(lineno=expr.start_line, col_offset=expr.start_column, module=ast_str(module), names=names or [ast.alias(name=ast_str(module), asname=None)], level=0) ] expr.pop(0) # index rimports = [] while len(expr) > 0: iexpr = expr.pop(0) if isinstance(iexpr, HySymbol): rimports += _compile_import(expr, iexpr) continue if isinstance(iexpr, HyList) and len(iexpr) == 1: rimports += _compile_import(expr, iexpr.pop(0)) continue if isinstance(iexpr, HyList) and iexpr: module = iexpr.pop(0) entry = iexpr[0] if isinstance(entry, HyKeyword) and entry == HyKeyword(":as"): assert len(iexpr) == 2, "garbage after aliased import" iexpr.pop(0) # :as alias = iexpr.pop(0) rimports += _compile_import(expr, ast_str(module), [ ast.alias(name=ast_str(module), asname=ast_str(alias)) ]) continue if isinstance(entry, HyList): names = [] while entry: sym = entry.pop(0) if entry and isinstance(entry[0], HyKeyword): entry.pop(0) alias = ast_str(entry.pop(0)) else: alias = None names += [ast.alias(name=ast_str(sym), asname=alias)] rimports += _compile_import(expr, module, names, ast.ImportFrom) continue raise TypeError("Unknown entry (`%s`) in the HyList" % (entry)) if len(rimports) == 1: return rimports[0] else: return rimports
def _resolve_atom(obj): """ Resolve a bare atom into one of the following (in order): - Integer - LambdaListKeyword - Float - Complex - Symbol """ try: return HyInteger(obj) except ValueError: pass if obj.startswith("&"): return HyLambdaListKeyword(obj) try: return HyFloat(obj) except ValueError: pass if obj != "j": try: return HyComplex(obj) except ValueError: pass table = { "true": "True", "false": "False", "null": "None", } if obj in table: return HySymbol(table[obj]) if obj.startswith(":"): return HyKeyword(obj) if obj.startswith("*") and obj.endswith("*") and obj not in ("*", "**"): obj = obj[1:-1].upper() if "-" in obj and obj != "-": obj = obj.replace("-", "_") return HySymbol(obj)
def t_identifier(p): obj = p[0].value try: return HyInteger(obj) except ValueError: pass try: return HyFloat(obj) except ValueError: pass if obj != 'j': try: return HyComplex(obj) except ValueError: pass table = { "true": "True", "false": "False", "null": "None", } if obj in table: return HySymbol(table[obj]) if obj.startswith(":"): return HyKeyword(obj) if obj.startswith("&"): return HyLambdaListKeyword(obj) if obj.startswith("*") and obj.endswith("*") and obj not in ("*", "**"): obj = obj[1:-1].upper() if "-" in obj and obj != "-": obj = obj.replace("-", "_") return HySymbol(obj)