def tostring(x): if x is None: return 'nil' elif type(x) in (int, float): return str(x) elif type(x) is Atom: return x.name() elif type(x) is Keyword: return ":"+x.name elif x.__class__.__name__ in ['function', 'builtin_function_or_method']: return str(x) elif type(x) is List: inner = ' '.join([tostring(x) for x in x]) return '(%s)' % inner elif type(x) is Vector: inner = ' '.join([tostring(x) for x in x]) return '[%s]' % inner elif type(x) is Map: inner = ', '.join(['%s %s' % (k, v) for k,v in x.items()]) return '{%s}' % inner else: raise TypeError('%s is unknown!' % x)
def main(): global_scope = GlobalScope() scopechain = [global_scope] while True: try: txt = raw_input("pyclojure> ") if re.search('^\s*$', txt): continue else: print(tostring(evaluate( parser.parse(txt, lexer=lexer), scopechain))) except EOFError: break except KeyboardInterrupt: print # Give user a newline after Cntrl-C for readability break except Exception, e: print e
def test_to_string(): parse = PyClojureParse().build().parse assert tostring(parse("nil")) == "nil" assert tostring(parse("666")) == "666" assert tostring(parse("6.66")) == "6.66" assert tostring(parse("666e-2")) == "6.66" assert tostring(parse("-666")) == "-666" assert tostring(parse("-6.66")) == "-6.66" assert tostring(parse("-666e-2")) == "-6.66" assert tostring(parse("()")) == "()" assert tostring(parse("(a)")) == "(a)" assert tostring(parse("(a b)")) == "(a b)" assert tostring(parse("(a (b c))")) == "(a (b c))" assert tostring(parse("[]")) == "[]" assert tostring(parse(":a")) == ":a" assert tostring(parse("{}")) == "{}" assert tostring(parse("{1 2}")) == "{1 2}" assert tostring(parse("{1 2 3 4}")) == "{1 2, 3 4}"
def test_to_string(): parse = PyClojureParse().build().parse assert tostring(parse("nil")) =="nil" assert tostring(parse("666")) =="666" assert tostring(parse("6.66")) == "6.66" assert tostring(parse("666e-2")) == "6.66" assert tostring(parse("-666")) =="-666" assert tostring(parse("-6.66")) == "-6.66" assert tostring(parse("-666e-2")) == "-6.66" assert tostring(parse("()")) == "()" assert tostring(parse("(a)")) == "(a)" assert tostring(parse("(a b)")) == "(a b)" assert tostring(parse("(a (b c))")) == "(a (b c))" assert tostring(parse("[]")) == "[]" assert tostring(parse(":a")) == ":a" assert tostring(parse("{}")) == "{}" assert tostring(parse("{1 2}")) == "{1 2}" assert tostring(parse("{1 2 3 4}")) == "{1 2, 3 4}"
def test_to_string(): parse = lispparser() assert tostring(parse("nil")) =="nil" assert tostring(parse("666")) =="666" assert tostring(parse("()")) == "()" assert tostring(parse("(a)")) == "(a)" assert tostring(parse("(a b)")) == "(a b)" assert tostring(parse("(a (b c))")) == "(a (b c))" assert tostring(parse("[]")) == "[]" assert tostring(parse(":a")) == ":a" assert tostring(parse("{}")) == "{}" assert tostring(parse("{1 2}")) == "{1 2}" assert tostring(parse("{1 2 3 4}")) == "{1 2, 3 4}"
def test_to_string(): parse = PyClojureParse().build().parse assert tostring(parse("nil")) =="nil" assert tostring(parse("666")) =="666" assert tostring(parse("666L")) == "666L" assert tostring(parse(str(sys.maxint + 1))) == str(sys.maxint + 1) + 'L' assert tostring(parse("6.66")) == "6.66" assert tostring(parse("666e-2")) == "6.66" assert tostring(parse("-666")) =="-666" assert tostring(parse("-6.66")) == "-6.66" assert tostring(parse("-666e-2")) == "-6.66" assert tostring(parse('"foo"')) == '""foo""' assert tostring(parse("()")) == "()" assert tostring(parse("(a)")) == "(a)" assert tostring(parse("(a b)")) == "(a b)" assert tostring(parse("(a (b c))")) == "(a (b c))" assert tostring(parse("[]")) == "[]" assert tostring(parse(":a")) == ":a" assert tostring(parse("{}")) == "{}" assert tostring(parse("{1 2}")) == "{1 2}" assert tostring(parse("{1 2 3 4}")) == "{1 2, 3 4}"