Exemplo n.º 1
0
def println(*args):
    s = " ".join([printer.pr_str(arg, False) for arg in args])
    print s
    return None
Exemplo n.º 2
0
    # compare the first two parameters and return true if they are the same
    # type and contain the same value. In the case of equal length lists,
    # each element of the list should be compared for equality and if they are
    # the same return true, otherwise false.
    '=': _chain(MalBool, op.eq),
    # '=': lambda a, b: MalBool(type(a) == type(b) and op.eq(a, b)),

    # <, <=, >, and >=:
    # treat the first two parameters as numbers and do the corresponding numeric
    # comparison, returning either true or false.
    '<': _chain(MalBool, op.lt),
    '<=': _chain(MalBool, op.le),
    '>': _chain(MalBool, op.gt),
    '>=': _chain(MalBool, op.ge),

    # string functions
    'pr-str': lambda *args: MalString(
        " ".join(printer.pr_str(arg, print_readably=True)
                 for arg in args)),
    'str': lambda *args: MalString(
        "".join(printer.pr_str(arg, print_readably=False)
                for arg in args)),
    'prn': lambda *args: print(
        MalString(" ".join(printer.pr_str(arg, print_readably=True)
                           for arg in args))) or nil,
    'println': lambda *args: print(
        " ".join(printer.pr_str(arg, print_readably=False)
                 for arg in args)) or nil,
}
Exemplo n.º 3
0
    elif type(e) == list:
        return seq_equals(e, other)
    elif type(e) == maltypes.Vector:
        return seq_equals(e, other)
    else:
        return e == other

ns[maltypes.Symbol("=")] = equals

ns[maltypes.Symbol("<")] = lambda x, y: x < y
ns[maltypes.Symbol("<=")] = lambda x, y: x <= y
ns[maltypes.Symbol(">")] = lambda x, y: x > y
ns[maltypes.Symbol(">=")] = lambda x, y: x >= y

ns[maltypes.Symbol("pr-str")] = lambda *args: " ".join(
        [printer.pr_str(arg, True) for arg in args])

ns[maltypes.Symbol("str")] = lambda *args: "".join(
        [printer.pr_str(arg, False) for arg in args])

def prn(*args):
    s = " ".join([printer.pr_str(arg, True) for arg in args])
    print s
    return None

def println(*args):
    s = " ".join([printer.pr_str(arg, False) for arg in args])
    print s
    return None

ns[maltypes.Symbol("prn")] = prn
Exemplo n.º 4
0
def PRINT(exp):
    return printer.pr_str(exp, True)
Exemplo n.º 5
0
def _println(*args):
    print String(" ".join(map(lambda exp: pr_str(exp, False), args)))
    return None
Exemplo n.º 6
0
def prn(*x):
    print(" ".join([printer.pr_str(v,True) for v in x]))
    return "nil"
Exemplo n.º 7
0
def print_(print_arr):
    return printer.pr_str(print_arr)
Exemplo n.º 8
0
def do_str(*args):
    return "".join(map(lambda exp: printer.pr_str(exp, False), args))
Exemplo n.º 9
0
def PRINT(mt):
    return printer.pr_str(mt)
Exemplo n.º 10
0
def println(*args):
    print(" ".join(map(lambda exp: printer.pr_str(exp, False), args)))
Exemplo n.º 11
0
def pr_str(*args):
    return " ".join(map(lambda exp: printer.pr_str(exp, True), args))
Exemplo n.º 12
0
def prn(*args):
    print(" ".join(map(lambda exp: printer.pr_str(exp, True), args)))
Exemplo n.º 13
0
 def test12(self):
     res = printer.pr_str("\\", True)
     exp = "\\\\"
     assert(res == exp), \
         err_str.format(exp, res)
Exemplo n.º 14
0
 def test11(self):
     res = printer.pr_str("test\\string", True)
     exp = "test\\\\string"
     assert(res == exp), \
         err_str.format(exp, res)
Exemplo n.º 15
0
def PRINT(code):
    return printer.pr_str(code)
Exemplo n.º 16
0
Arquivo: step5_tco.py Projeto: p7g/mal
def PRINT(in_: t.MalType) -> str:
    return pr_str(in_, print_readably=True)
Exemplo n.º 17
0
def prn(*args):
    print(" ".join([pr_str(i, print_readably=True) for i in args]))
    return mal_types.MalNil()
Exemplo n.º 18
0
def PRINT(data):
    return printer.pr_str(data, print_readably=True)
Exemplo n.º 19
0
def PRINT(exp: MalType) -> str:
    return printer.pr_str(exp, print_readably=True)
Exemplo n.º 20
0
rep("(def! not (fn* (a) (if a false true)))", out_print=False)
rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))",
    out_print=False)
#macro
rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))",
    out_print=False)
rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))",
    out_print=False)

if __name__ == '__main__':
    _args = sys.argv[2:]
    repl_env.set('*ARGV*', _MalData('LIST', list(_args)))

    #rep("(def! not (fn* (a) (if a false true)))")
    if len(sys.argv) >= 2:
        path = sys.argv[1]
        rep('(load-file "' + path + '")')
        sys.exit(0)

    while True:
        try:
            x = input('user> ')
            rep(x, repl_env)

        except MalException as e:
            print("Error:", printer.pr_str(e.object))
        except Exception as e:
            print("".join(traceback.format_exception(*sys.exc_info())))
        except KeyboardInterrupt:
            break
Exemplo n.º 21
0
def PRINT(s):
    return printer.pr_str(s)
Exemplo n.º 22
0
def PRINT(ast):
    txt = printer.pr_str(ast)
    return txt
Exemplo n.º 23
0
def PRINT(mal_type):
    """
    Convert result of mal instructions into string representation.
    """
    return pr_str(mal_type)
Exemplo n.º 24
0
def println(*args):
    print(" ".join([printer.pr_str(e, False) for e in args]))
    return MalNil()
Exemplo n.º 25
0
def PRINT(ast):
    res = printer.pr_str(ast, print_readably=True)
    return res
Exemplo n.º 26
0
def mal_str(*args):
    return printer.pr_str(args)
Exemplo n.º 27
0
def PRINT(code):
    return printer.pr_str(code)
Exemplo n.º 28
0
def prn(*arg):
    print(printer.pr_str(arg[0], True))
    return Nil()
Exemplo n.º 29
0
def prn(*args):
    s = " ".join([printer.pr_str(arg, True) for arg in args])
    print s
    return None
Exemplo n.º 30
0
def _str(*arg):
    return "".join(list(map(lambda x: printer.pr_str(x, False), arg)))
Exemplo n.º 31
0
def _str(*args):
    ret = []
    for ast in args:
        ret.append(printer.pr_str(ast, False))
    return _MalData("STRING", "".join(ret))
Exemplo n.º 32
0
def PRINT(exp):
    return pr_str(exp)
Exemplo n.º 33
0
 is_empty,
 "count":
 lambda x: mal_types.MalNumber(len(x)),
 "=":
 equal,
 "<":
 lambda x, y: mal_types.MalBool(x.data < y.data),
 "<=":
 lambda x, y: mal_types.MalBool(x.data <= y.data),
 ">":
 lambda x, y: mal_types.MalBool(x.data > y.data),
 ">=":
 lambda x, y: mal_types.MalBool(x.data >= y.data),
 "pr-str":
 lambda *args: mal_types.MalString(" ".join(
     [pr_str(i, print_readably=True) for i in args])),
 "str":
 lambda *args: mal_types.MalString("".join(
     [pr_str(i, print_readably=False) for i in args])),
 "prn":
 prn,
 "println":
 println,
 "read-string":
 read_string,
 "slurp":
 slurp,
 "atom":
 atom,
 "atom?":
 is_atom,
Exemplo n.º 34
0
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--interactive', action='store_true')
parser.add_argument('filename', nargs='?', help='Filename to be executed')
parser.add_argument('prog_args', nargs='*', help='Arguments passed to program')
parser.add_argument('--prompt', nargs='?', help='Prompt to show to user')
parser.add_argument('--disable-header', action='store_true')
args = parser.parse_args()


if __name__ == '__main__':
    arg_to_str = lambda arg: f'"{arg}"'
    rep(f'(def! *ARGV* {"(list " +  " ".join(arg_to_str(arg) for arg in args.prog_args) + ")" })')
    if args.filename is not None:
        rep(f'(def! *FILENAME* "{args.filename}")')
        rep('(load-file *FILENAME*)')
        if not args.interactive:
            exit(0)
    else:
        if not args.disable_header:
            rep("""(println (str "Mal [" *host-language* "]"))""")
    while True:
        inp = input(args.prompt or 'user> ')
        try:
            res = rep(inp)
        except MalException as e:
            print("Error:", pr_str(e))
        except Exception as e:  # compatibility
            print("Error:", e)
        else:
            print(res)
Exemplo n.º 35
0
 ,"*": lambda x,y: x*y
 ,"/": lambda x,y: x/y
 ,"nil": "nil"
 ,"not": lambda x: not ((type(x)==bool and x) or (type(x)!=bool and x!="nil"))
 ,"list": lambda *l: [*l]
 ,"list?": lambda lst: isinstance(lst,list)
 ,"empty?": lambda lst: len(lst)==0
 ,"count": lambda lst: len(lst) if isinstance(lst,(list,tuple)) else 0
 ,"=": lambda x,y: x==y
 ,">": lambda x,y: x>y
 ,"<": lambda x,y: x<y
 ,">=": lambda x,y: x>=y
 ,"<=": lambda x,y: x<=y
 ,"read-string": lambda s: reader.read_str(s.value)
 ,"slurp": lambda fname:maltype.String(readfile(fname))
 ,"pr-str": lambda *x: maltype.String(" ".join([printer.pr_str(v) for v in x]))
 ,"str": lambda *x: maltype.String("".join([printer.pr_str(v,False) for v in x]))
 ,"prn": prn
 ,"println": println
 ,"atom": lambda v:maltype.Atom(v)
 ,"atom?": maltype.atomp
 ,"deref": lambda a:a.value
 ,"reset!": lambda a,v:a.reset(v)
 ,"swap!": lambda a,f,*arg:a.reset(f(a.value,*arg) if not isinstance(f,dict) else f["fn"](a.value,*arg))
 ,"cons": lambda a,lst: [a,*lst]
 ,"concat": concat
 ,"nth": nth
 ,"first": lambda lst:"nil" if len(lst)==0 or lst=="nil" else nth(lst,0)
 ,"rest": rest
 ,"throw": throw
 ,"apply": apply
Exemplo n.º 36
0
def PRINT(result):
    output = pr_str(result)
    print(output)
    return
Exemplo n.º 37
0
def println(*x):
    print(" ".join([printer.pr_str(v,False) for v in x]))
    return "nil"
Exemplo n.º 38
0
def PRINT(output):
    print(printer.pr_str(output))
Exemplo n.º 39
0
def PRINT(dt):
    return printer.pr_str(dt)
Exemplo n.º 40
0
def PRINT(expr):
    return printer.pr_str(expr)
Exemplo n.º 41
0
def PRINT(x):
    return printer.pr_str(x)
Exemplo n.º 42
0
def PRINT(form):
    return printer.pr_str(form)
Exemplo n.º 43
0
def PRINT(ast):
    return printer.pr_str(ast)
Exemplo n.º 44
0
        return []
    else:
        return reduce(lambda x, y: x + y, args)


ns = {
    '+': lambda a, b: a + b,
    '-': lambda a, b: a - b,
    '*': lambda a, b: a * b,
    '/': lambda a, b: int(a / b),
    '<': lambda a, b: a < b,
    '>': lambda a, b: a > b,
    '=': equals,
    '<=': lambda a, b: a <= b,
    '>=': lambda a, b: a >= b,
    'prn': lambda x: pr_str(x),
    'list': list2,
    'list?': lambda x: type(x) is list,
    'count': lambda x: len(x) if x else 0,
    'empty?': lambda x: len(x) == 0,
    'read-string': read_str,
    'pr-str': pr_str,
    'str': do_str,
    'println': println,
    'slurp': slurp,
    'atom': lambda x: Atom(x),
    'atom?': lambda x: isinstance(x, Atom),
    'deref': lambda x: x.val,
    'reset!': reset,
    'swap!': 1,
    'cons': lambda x, y: [x] + y,
Exemplo n.º 45
0
def PRINT(print_in):
    return printer.pr_str(print_in)
Exemplo n.º 46
0
def do_str(*args):
    return "".join(map(lambda exp: pr_str(exp, False), args))
Exemplo n.º 47
0
def _str(*args):
    return String("".join(map(lambda exp: pr_str(exp, False), args)))
Exemplo n.º 48
0
def prn(*args):
    print(" ".join(map(lambda exp: pr_str(exp, True), args)))
    return None
Exemplo n.º 49
0
def PRINT(mt):
    res = printer.pr_str(mt)
    return res
Exemplo n.º 50
0
def println(*args):
    print(" ".join(map(lambda exp: pr_str(exp, False), args)))
    return None
Exemplo n.º 51
0
def PRINT(line):
    res = printer.pr_str(line)
    log(str(res),"PRINT")
    return res
Exemplo n.º 52
0
def PRINT(exp):
    return printer.pr_str(exp)