2014-12-19 08:21:39 +03:00
|
|
|
import scala.collection.mutable
|
|
|
|
import scala.io.Source
|
|
|
|
|
2014-12-19 08:52:59 +03:00
|
|
|
import types.{MalList, _list, _list_Q,
|
|
|
|
MalVector, _vector, _vector_Q,
|
|
|
|
MalHashMap, _hash_map_Q, _hash_map,
|
|
|
|
Func, MalFunction}
|
2014-12-19 08:21:39 +03:00
|
|
|
import printer._pr_list
|
|
|
|
|
|
|
|
object core {
|
|
|
|
def mal_throw(a: List[Any]) = {
|
|
|
|
throw new types.MalException(printer._pr_str(a(0))).init(a(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scalar functions
|
|
|
|
def keyword(a: List[Any]) = {
|
|
|
|
"\u029e" + a(0).asInstanceOf[String]
|
|
|
|
}
|
|
|
|
|
|
|
|
def keyword_Q(a: List[Any]) = {
|
|
|
|
a(0) match {
|
|
|
|
case s: String => s(0) == '\u029e'
|
|
|
|
case _ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 08:52:59 +03:00
|
|
|
|
|
|
|
// number functions
|
|
|
|
def _bool_op(a: List[Any], op: (Long, Long) => Boolean) = {
|
|
|
|
op(a(0).asInstanceOf[Long],a(1).asInstanceOf[Long])
|
|
|
|
}
|
|
|
|
|
|
|
|
def _num_op(a: List[Any], op: (Long, Long) => Long) = {
|
|
|
|
op(a(0).asInstanceOf[Long],a(1).asInstanceOf[Long])
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-19 08:21:39 +03:00
|
|
|
// string functions
|
|
|
|
def read_string(a: List[Any]) = {
|
|
|
|
reader.read_str(a(0).asInstanceOf[String])
|
|
|
|
}
|
|
|
|
|
|
|
|
def slurp(a: List[Any]) = {
|
2014-12-19 08:52:59 +03:00
|
|
|
Source.fromFile(a(0).asInstanceOf[String]).getLines.mkString("\n") + "\n"
|
2014-12-19 08:21:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hash Map functions
|
|
|
|
def assoc(a: List[Any]): Any = {
|
2014-12-19 08:52:59 +03:00
|
|
|
a(0).asInstanceOf[MalHashMap] ++ _hash_map(a.drop(1):_*)
|
2014-12-19 08:21:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
def dissoc(a: List[Any]): Any = {
|
2014-12-19 08:52:59 +03:00
|
|
|
var kSet = a.drop(1).toSet
|
|
|
|
a(0).asInstanceOf[MalHashMap]
|
2014-12-19 08:21:39 +03:00
|
|
|
.filterKeys{ !kSet.contains(_) }
|
|
|
|
}
|
|
|
|
|
|
|
|
def get(a: List[Any]): Any = {
|
2014-12-19 08:52:59 +03:00
|
|
|
val hm = a(0).asInstanceOf[MalHashMap]
|
2014-12-19 08:21:39 +03:00
|
|
|
val key = a(1).asInstanceOf[String]
|
2014-12-19 08:52:59 +03:00
|
|
|
if (hm != null && hm.value.contains(key)) hm(key) else null
|
2014-12-19 08:21:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
def contains_Q(a: List[Any]): Any = {
|
2014-12-19 08:52:59 +03:00
|
|
|
a(0).asInstanceOf[MalHashMap].value
|
2014-12-19 08:21:39 +03:00
|
|
|
.contains(a(1).asInstanceOf[String])
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// sequence functions
|
2014-12-19 08:52:59 +03:00
|
|
|
def concat(a: List[Any]): Any = {
|
|
|
|
_list((for (sq <- a) yield types._toIter(sq)).flatten:_*)
|
2014-12-19 08:21:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
def nth(a: List[Any]): Any = {
|
2014-12-19 08:52:59 +03:00
|
|
|
val lst = a(0).asInstanceOf[MalList].value
|
|
|
|
val idx = a(1).asInstanceOf[Long]
|
2014-12-19 08:21:39 +03:00
|
|
|
if (idx < lst.length) {
|
2014-12-19 08:52:59 +03:00
|
|
|
lst(idx.toInt)
|
2014-12-19 08:21:39 +03:00
|
|
|
} else {
|
|
|
|
throw new Exception("nth: index out of range")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def first(a: List[Any]): Any = {
|
2014-12-19 08:52:59 +03:00
|
|
|
val lst = a(0).asInstanceOf[MalList].value
|
2014-12-19 08:21:39 +03:00
|
|
|
if (lst.length > 0) lst(0) else null
|
|
|
|
}
|
|
|
|
|
2014-12-19 08:52:59 +03:00
|
|
|
def rest(a: List[Any]): Any = {
|
|
|
|
a(0) match {
|
|
|
|
case null => true
|
|
|
|
case ml: MalList => _list(ml.drop(1).value:_*)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def empty_Q(a: List[Any]): Any = {
|
|
|
|
a(0) match {
|
|
|
|
case null => true
|
|
|
|
case ml: MalList => ml.value.isEmpty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def count(a: List[Any]): Any = {
|
|
|
|
a(0) match {
|
|
|
|
case null => 0
|
|
|
|
case ml: MalList => ml.value.length.asInstanceOf[Long]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def conj(a: List[Any]): Any = {
|
|
|
|
a(0) match {
|
|
|
|
case mv: MalVector => {
|
|
|
|
_vector(mv.value ++ a.slice(1,a.length):_*)
|
|
|
|
}
|
|
|
|
case ml: MalList => {
|
|
|
|
_list(a.slice(1,a.length).reverse ++ ml.value:_*)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-19 08:21:39 +03:00
|
|
|
|
|
|
|
def apply(a: List[Any]): Any = {
|
|
|
|
a match {
|
|
|
|
case f :: rest => {
|
|
|
|
var args1 = rest.slice(0,rest.length-1)
|
2014-12-19 08:52:59 +03:00
|
|
|
var args = args1 ++ rest(rest.length-1).asInstanceOf[MalList].value
|
2014-12-19 08:21:39 +03:00
|
|
|
types._apply(f, args)
|
|
|
|
}
|
|
|
|
case _ => throw new Exception("invalid apply call")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def do_map(a: List[Any]): Any = {
|
|
|
|
a match {
|
|
|
|
case f :: seq :: Nil => {
|
2015-03-15 01:14:32 +03:00
|
|
|
var res = seq.asInstanceOf[MalList].map(x => types._apply(f,List(x)));
|
|
|
|
_list(res.value:_*)
|
2014-12-19 08:21:39 +03:00
|
|
|
}
|
|
|
|
case _ => throw new Exception("invalid map call")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-19 08:52:59 +03:00
|
|
|
// meta functions
|
|
|
|
def with_meta(a: List[Any]): Any = {
|
|
|
|
val meta: Any = a(1)
|
|
|
|
a(0) match {
|
|
|
|
case ml: MalList => {
|
|
|
|
val new_ml = ml.clone()
|
|
|
|
new_ml.meta = meta
|
|
|
|
new_ml
|
|
|
|
}
|
|
|
|
case hm: MalHashMap => {
|
|
|
|
val new_hm = hm.clone()
|
|
|
|
new_hm.meta = meta
|
|
|
|
new_hm
|
|
|
|
}
|
|
|
|
case fn: Func => {
|
|
|
|
val new_fn = fn.clone()
|
|
|
|
new_fn.meta = meta
|
|
|
|
new_fn
|
|
|
|
}
|
|
|
|
case fn: MalFunction => {
|
|
|
|
val new_fn = fn.clone()
|
|
|
|
new_fn.meta = meta
|
|
|
|
new_fn
|
|
|
|
}
|
|
|
|
case _ => throw new Exception("no meta support for " + a(0).getClass)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def meta(a: List[Any]): Any = {
|
|
|
|
a(0) match {
|
|
|
|
case ml: MalList => ml.meta
|
|
|
|
case hm: MalHashMap => hm.meta
|
|
|
|
case fn: Func => fn.meta
|
|
|
|
case fn: MalFunction => fn.meta
|
|
|
|
case _ => throw new Exception("no meta support for " + a(0).getClass)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-19 08:21:39 +03:00
|
|
|
// atom functions
|
|
|
|
def reset_BANG(a: List[Any]): Any = {
|
|
|
|
a(0).asInstanceOf[types.Atom].value = a(1)
|
|
|
|
a(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
def swap_BANG(a: List[Any]): Any = {
|
|
|
|
a match {
|
|
|
|
case a0 :: f :: rest => {
|
|
|
|
val atm = a0.asInstanceOf[types.Atom]
|
|
|
|
val args = atm.value +: rest
|
|
|
|
atm.value = types._apply(f, args)
|
|
|
|
atm.value
|
|
|
|
}
|
|
|
|
case _ => throw new Exception("invalid swap! call")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-19 08:52:59 +03:00
|
|
|
val ns: Map[String, (List[Any]) => Any] = Map(
|
2014-12-19 08:21:39 +03:00
|
|
|
"=" -> ((a: List[Any]) => types._equal_Q(a(0), a(1))),
|
|
|
|
"throw" -> mal_throw _,
|
|
|
|
"nil?" -> ((a: List[Any]) => a(0) == null),
|
|
|
|
"true?" -> ((a: List[Any]) => a(0) == true),
|
|
|
|
"false?" -> ((a: List[Any]) => a(0) == false),
|
|
|
|
"symbol" -> ((a: List[Any]) => Symbol(a(0).asInstanceOf[String])),
|
|
|
|
"symbol?" -> ((a: List[Any]) => a(0).isInstanceOf[Symbol]),
|
|
|
|
"keyword" -> keyword _,
|
|
|
|
"keyword?" -> keyword_Q _,
|
|
|
|
|
|
|
|
"pr-str" -> ((a: List[Any]) => _pr_list(a, true, " ")),
|
|
|
|
"str" -> ((a: List[Any]) => _pr_list(a, false, "")),
|
|
|
|
"prn" -> ((a: List[Any]) => { println(_pr_list(a, true, " ")); null}),
|
|
|
|
"println" -> ((a: List[Any]) => { println(_pr_list(a, false, " ")); null}),
|
2014-12-19 08:52:59 +03:00
|
|
|
"readline" -> ((a: List[Any]) => readLine(a(0).asInstanceOf[String])),
|
2014-12-19 08:21:39 +03:00
|
|
|
"read-string" -> read_string _,
|
|
|
|
"slurp" -> slurp _,
|
2014-12-19 08:52:59 +03:00
|
|
|
|
|
|
|
"<" -> ((a: List[Any]) => _bool_op(a, _ < _)),
|
|
|
|
"<=" -> ((a: List[Any]) => _bool_op(a, _ <= _)),
|
|
|
|
">" -> ((a: List[Any]) => _bool_op(a, _ > _)),
|
|
|
|
">=" -> ((a: List[Any]) => _bool_op(a, _ >= _)),
|
|
|
|
"+" -> ((a: List[Any]) => _num_op(a, _ + _)),
|
|
|
|
"-" -> ((a: List[Any]) => _num_op(a, _ - _)),
|
|
|
|
"*" -> ((a: List[Any]) => _num_op(a, _ * _)),
|
|
|
|
"/" -> ((a: List[Any]) => _num_op(a, _ / _)),
|
|
|
|
"time-ms" -> ((a: List[Any]) => System.currentTimeMillis),
|
|
|
|
|
|
|
|
"list" -> ((a: List[Any]) => _list(a:_*)),
|
|
|
|
"list?" -> ((a: List[Any]) => _list_Q(a(0))),
|
|
|
|
"vector" -> ((a: List[Any]) => _vector(a:_*)),
|
|
|
|
"vector?" -> ((a: List[Any]) => _vector_Q(a(0))),
|
|
|
|
"hash-map" -> ((a: List[Any]) => _hash_map(a:_*)),
|
|
|
|
"map?" -> ((a: List[Any]) => _hash_map_Q(a(0))),
|
2014-12-19 08:21:39 +03:00
|
|
|
"assoc" -> assoc _,
|
|
|
|
"dissoc" -> dissoc _,
|
|
|
|
"get" -> get _,
|
|
|
|
"contains?" -> contains_Q _,
|
2014-12-19 08:52:59 +03:00
|
|
|
"keys" -> ((a: List[Any]) => a(0).asInstanceOf[MalHashMap].keys),
|
|
|
|
"vals" -> ((a: List[Any]) => a(0).asInstanceOf[MalHashMap].vals),
|
2014-12-19 08:21:39 +03:00
|
|
|
|
|
|
|
"sequential?" -> ((a: List[Any]) => types._sequential_Q(a(0))),
|
2014-12-19 08:52:59 +03:00
|
|
|
"cons" -> ((a: List[Any]) => a(0) +: a(1).asInstanceOf[MalList]),
|
2014-12-19 08:21:39 +03:00
|
|
|
"concat" -> concat _,
|
|
|
|
"nth" -> nth _,
|
|
|
|
"first" -> first _,
|
2014-12-19 08:52:59 +03:00
|
|
|
"rest" -> rest _,
|
|
|
|
"empty?" -> empty_Q _,
|
|
|
|
"count" -> count _,
|
|
|
|
"conj" -> conj _,
|
2014-12-19 08:21:39 +03:00
|
|
|
"apply" -> apply _,
|
|
|
|
"map" -> do_map _,
|
|
|
|
|
2014-12-19 08:52:59 +03:00
|
|
|
"with-meta" -> with_meta _,
|
|
|
|
"meta" -> meta _,
|
2014-12-19 08:21:39 +03:00
|
|
|
"atom" -> ((a: List[Any]) => new types.Atom(a(0))),
|
|
|
|
"atom?" -> ((a: List[Any]) => a(0).isInstanceOf[types.Atom]),
|
|
|
|
"deref" -> ((a: List[Any]) => a(0).asInstanceOf[types.Atom].value),
|
|
|
|
"reset!" -> reset_BANG _,
|
|
|
|
"swap!" -> swap_BANG _
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// vim:ts=2:sw=2
|