2024-08-08 16:08:30 +02:00
|
|
|
import operator
|
|
|
|
|
import time
|
2014-04-02 22:23:37 -05:00
|
|
|
from itertools import chain
|
|
|
|
|
|
|
|
|
|
import mal_types as types
|
Test uncaught throw, catchless try* . Fix 46 impls.
Fixes made to: ada, c, chuck, clojure, coffee, common-lisp, cpp,
crystal, d, dart, elm, erlang, es6, factor, fsharp, gnu-smalltalk,
groovy, guile, haxe, hy, js, livescript, matlab, miniMAL, nasm, nim,
objc, objpascal, ocaml, perl, perl6, php, plsql, ps, python, r,
rpython, ruby, scheme, swift3, tcl, ts, vb, vimscript, wasm, yorick.
Catchless try* test is an optional test. Not all implementations
support catchless try* but a number were fixed so they at least don't
crash on catchless try*.
2018-12-03 13:20:44 -06:00
|
|
|
from mal_types import MalException, List, Vector
|
2014-04-16 23:57:50 -05:00
|
|
|
import mal_readline
|
|
|
|
|
import reader
|
2014-04-02 22:23:37 -05:00
|
|
|
import printer
|
|
|
|
|
|
|
|
|
|
# Errors/Exceptions
|
Test uncaught throw, catchless try* . Fix 46 impls.
Fixes made to: ada, c, chuck, clojure, coffee, common-lisp, cpp,
crystal, d, dart, elm, erlang, es6, factor, fsharp, gnu-smalltalk,
groovy, guile, haxe, hy, js, livescript, matlab, miniMAL, nasm, nim,
objc, objpascal, ocaml, perl, perl6, php, plsql, ps, python, r,
rpython, ruby, scheme, swift3, tcl, ts, vb, vimscript, wasm, yorick.
Catchless try* test is an optional test. Not all implementations
support catchless try* but a number were fixed so they at least don't
crash on catchless try*.
2018-12-03 13:20:44 -06:00
|
|
|
def throw(obj): raise MalException(obj)
|
2014-04-02 22:23:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# String functions
|
|
|
|
|
def pr_str(*args):
|
2024-08-08 16:08:30 +02:00
|
|
|
return printer.pr_list(args, " ", True)
|
2014-04-02 22:23:37 -05:00
|
|
|
|
|
|
|
|
def do_str(*args):
|
2024-08-08 16:08:30 +02:00
|
|
|
return printer.pr_list(args, "", False)
|
2014-04-02 22:23:37 -05:00
|
|
|
|
|
|
|
|
def prn(*args):
|
2024-08-08 16:08:30 +02:00
|
|
|
print(printer.pr_list(args, " ", True))
|
2014-04-02 22:23:37 -05:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def println(*args):
|
2024-08-08 16:08:30 +02:00
|
|
|
print(printer.pr_list(args, " ", False))
|
2014-04-02 22:23:37 -05:00
|
|
|
return None
|
|
|
|
|
|
2024-08-08 16:08:30 +02:00
|
|
|
def core_readline(prompt):
|
|
|
|
|
try:
|
|
|
|
|
return mal_readline.readline(prompt)
|
|
|
|
|
except EOFError:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def slurp(path):
|
|
|
|
|
with open(path) as f:
|
|
|
|
|
return f.read()
|
2014-04-02 22:23:37 -05:00
|
|
|
|
|
|
|
|
# Hash map functions
|
|
|
|
|
def assoc(src_hm, *key_vals):
|
2024-08-08 16:08:30 +02:00
|
|
|
hm = types.Hash_Map(src_hm)
|
|
|
|
|
hm.update(types.asPairs(key_vals))
|
2014-04-02 22:23:37 -05:00
|
|
|
return hm
|
|
|
|
|
|
|
|
|
|
def dissoc(src_hm, *keys):
|
2024-08-08 16:08:30 +02:00
|
|
|
hm = types.Hash_Map(src_hm)
|
2014-04-06 16:24:27 -05:00
|
|
|
for key in keys:
|
2017-09-13 12:00:34 +10:00
|
|
|
hm.pop(key, None)
|
2014-04-02 22:23:37 -05:00
|
|
|
return hm
|
|
|
|
|
|
|
|
|
|
def get(hm, key):
|
2017-09-13 12:05:04 +10:00
|
|
|
if hm is not None:
|
|
|
|
|
return hm.get(key)
|
2014-04-02 22:23:37 -05:00
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
2024-08-08 16:08:30 +02:00
|
|
|
contains_Q = types.Hash_Map.__contains__
|
2014-04-02 22:23:37 -05:00
|
|
|
|
2024-08-08 16:08:30 +02:00
|
|
|
keys = List
|
2014-04-02 22:23:37 -05:00
|
|
|
|
2024-08-08 16:08:30 +02:00
|
|
|
def vals(hm): return List(hm.values())
|
2014-04-02 22:23:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Sequence functions
|
2024-08-08 16:08:30 +02:00
|
|
|
def cons(x, seq): return concat((x,), seq)
|
2014-04-02 22:23:37 -05:00
|
|
|
|
|
|
|
|
def concat(*lsts): return List(chain(*lsts))
|
|
|
|
|
|
2024-08-08 16:08:30 +02:00
|
|
|
nth = tuple.__getitem__
|
2014-04-02 22:23:37 -05:00
|
|
|
|
2016-01-28 21:36:44 -05:00
|
|
|
def first(lst):
|
2024-08-08 16:08:30 +02:00
|
|
|
if lst:
|
|
|
|
|
return lst[0]
|
|
|
|
|
else: # lst is nil or empty
|
|
|
|
|
return None
|
2014-04-02 22:23:37 -05:00
|
|
|
|
2016-01-28 21:36:44 -05:00
|
|
|
def rest(lst):
|
2024-08-08 16:08:30 +02:00
|
|
|
if lst:
|
|
|
|
|
it = iter(lst)
|
|
|
|
|
next(it)
|
|
|
|
|
return List(it)
|
|
|
|
|
else: # lst is nil or empty
|
|
|
|
|
return List()
|
2014-04-02 22:23:37 -05:00
|
|
|
|
2024-08-08 16:08:30 +02:00
|
|
|
empty_Q = operator.not_
|
2014-04-02 22:23:37 -05:00
|
|
|
|
2014-12-18 20:33:49 -06:00
|
|
|
def count(lst):
|
|
|
|
|
if types._nil_Q(lst): return 0
|
|
|
|
|
else: return len(lst)
|
2014-04-02 22:23:37 -05:00
|
|
|
|
2024-08-08 16:08:30 +02:00
|
|
|
def apply(f, *args): return f(*chain(args[:-1], args[-1]))
|
2016-02-11 09:19:53 -06:00
|
|
|
|
|
|
|
|
def mapf(f, lst): return List(map(f, lst))
|
|
|
|
|
|
2014-04-02 22:23:37 -05:00
|
|
|
def conj(lst, *args):
|
2024-08-08 16:08:30 +02:00
|
|
|
if types._list_Q(lst):
|
|
|
|
|
return concat(reversed(args), lst)
|
2014-04-02 22:23:37 -05:00
|
|
|
else:
|
2024-08-08 16:08:30 +02:00
|
|
|
return Vector(chain(lst, args))
|
2014-04-02 22:23:37 -05:00
|
|
|
|
2016-02-11 09:19:53 -06:00
|
|
|
def seq(obj):
|
2024-08-08 16:08:30 +02:00
|
|
|
if not obj:
|
|
|
|
|
return None # obj is nil, (), [] or ""
|
2016-02-11 09:19:53 -06:00
|
|
|
if types._list_Q(obj):
|
2024-08-08 16:08:30 +02:00
|
|
|
return obj
|
|
|
|
|
elif types._vector_Q(obj) or types._string_Q(obj):
|
|
|
|
|
return List(obj)
|
2016-02-11 09:19:53 -06:00
|
|
|
else: throw ("seq: called on non-sequence")
|
2014-04-02 22:23:37 -05:00
|
|
|
|
|
|
|
|
# Metadata functions
|
|
|
|
|
def with_meta(obj, meta):
|
2014-04-16 22:42:17 -05:00
|
|
|
new_obj = types._clone(obj)
|
2014-04-02 22:23:37 -05:00
|
|
|
new_obj.__meta__ = meta
|
|
|
|
|
return new_obj
|
|
|
|
|
|
|
|
|
|
def meta(obj):
|
2017-09-13 12:03:07 +10:00
|
|
|
return getattr(obj, "__meta__", None)
|
2014-04-02 22:23:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Atoms functions
|
|
|
|
|
def deref(atm): return atm.val
|
|
|
|
|
def reset_BANG(atm,val):
|
|
|
|
|
atm.val = val
|
|
|
|
|
return atm.val
|
|
|
|
|
def swap_BANG(atm,f,*args):
|
|
|
|
|
atm.val = f(atm.val,*args)
|
|
|
|
|
return atm.val
|
|
|
|
|
|
|
|
|
|
|
2024-08-08 16:08:30 +02:00
|
|
|
ns = {
|
2014-04-02 22:23:37 -05:00
|
|
|
'=': types._equal_Q,
|
|
|
|
|
'throw': throw,
|
|
|
|
|
'nil?': types._nil_Q,
|
|
|
|
|
'true?': types._true_Q,
|
|
|
|
|
'false?': types._false_Q,
|
2017-10-11 10:28:04 +02:00
|
|
|
'number?': types._number_Q,
|
2016-02-11 09:19:53 -06:00
|
|
|
'string?': types._string_Q,
|
2014-04-02 22:23:37 -05:00
|
|
|
'symbol': types._symbol,
|
|
|
|
|
'symbol?': types._symbol_Q,
|
2014-12-18 20:33:49 -06:00
|
|
|
'keyword': types._keyword,
|
|
|
|
|
'keyword?': types._keyword_Q,
|
2017-10-11 10:28:04 +02:00
|
|
|
'fn?': lambda x: (types._function_Q(x) and not hasattr(x, '_ismacro_')),
|
|
|
|
|
'macro?': lambda x: (types._function_Q(x) and
|
2024-08-08 16:08:30 +02:00
|
|
|
hasattr(x, '_ismacro_')),
|
2014-04-16 23:57:50 -05:00
|
|
|
|
2014-04-02 22:23:37 -05:00
|
|
|
'pr-str': pr_str,
|
|
|
|
|
'str': do_str,
|
|
|
|
|
'prn': prn,
|
|
|
|
|
'println': println,
|
2024-08-08 16:08:30 +02:00
|
|
|
'readline': core_readline,
|
2014-04-16 23:57:50 -05:00
|
|
|
'read-string': reader.read_str,
|
2024-08-08 16:08:30 +02:00
|
|
|
'slurp': slurp,
|
|
|
|
|
'<': operator.lt,
|
|
|
|
|
'<=': operator.le,
|
|
|
|
|
'>': operator.gt,
|
|
|
|
|
'>=': operator.ge,
|
|
|
|
|
'+': operator.add,
|
|
|
|
|
'-': operator.sub,
|
|
|
|
|
'*': operator.mul,
|
|
|
|
|
'/': operator.floordiv,
|
2014-04-17 21:49:07 -05:00
|
|
|
'time-ms': lambda : int(time.time() * 1000),
|
2014-04-02 22:23:37 -05:00
|
|
|
|
|
|
|
|
'list': types._list,
|
|
|
|
|
'list?': types._list_Q,
|
|
|
|
|
'vector': types._vector,
|
|
|
|
|
'vector?': types._vector_Q,
|
|
|
|
|
'hash-map': types._hash_map,
|
|
|
|
|
'map?': types._hash_map_Q,
|
|
|
|
|
'assoc': assoc,
|
|
|
|
|
'dissoc': dissoc,
|
|
|
|
|
'get': get,
|
|
|
|
|
'contains?': contains_Q,
|
|
|
|
|
'keys': keys,
|
|
|
|
|
'vals': vals,
|
|
|
|
|
|
|
|
|
|
'sequential?': types._sequential_Q,
|
|
|
|
|
'cons': cons,
|
|
|
|
|
'concat': concat,
|
2020-07-21 18:01:48 +02:00
|
|
|
'vec': Vector,
|
2014-04-02 22:23:37 -05:00
|
|
|
'nth': nth,
|
|
|
|
|
'first': first,
|
|
|
|
|
'rest': rest,
|
|
|
|
|
'empty?': empty_Q,
|
|
|
|
|
'count': count,
|
|
|
|
|
'apply': apply,
|
|
|
|
|
'map': mapf,
|
|
|
|
|
|
2016-02-11 09:19:53 -06:00
|
|
|
'conj': conj,
|
|
|
|
|
'seq': seq,
|
|
|
|
|
|
2014-04-02 22:23:37 -05:00
|
|
|
'with-meta': with_meta,
|
|
|
|
|
'meta': meta,
|
|
|
|
|
'atom': types._atom,
|
|
|
|
|
'atom?': types._atom_Q,
|
|
|
|
|
'deref': deref,
|
|
|
|
|
'reset!': reset_BANG,
|
2024-08-08 16:08:30 +02:00
|
|
|
'swap!': swap_BANG,
|
|
|
|
|
}
|