1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-11 13:55:55 +03:00

Fixed several self-hosting issues, added atom? tests

This commit is contained in:
Javier Fernandez-Ivern 2015-10-26 20:27:29 -05:00
parent 4d1f07f11c
commit 2eb2cac230
3 changed files with 14 additions and 7 deletions

View File

@ -162,9 +162,16 @@ val ns = hashMapOf(
})),
Pair(MalSymbol("sequential?"), MalFunction({ a: ISeq -> if (a.nth(0) is ISeq) TRUE else FALSE })),
Pair(MalSymbol("with-meta"), MalFunction({ a: ISeq ->
val obj = a.nth(0)
val metadata = a.nth(1)
obj.with_meta(metadata)
})),
Pair(MalSymbol("meta"), MalFunction({ a: ISeq -> a.first().metadata })),
Pair(MalSymbol("conj"), MalFunction({ a: ISeq -> (a.first() as ISeq).conj(a.rest()) })),
Pair(MalSymbol("atom"), MalFunction({ a: ISeq -> MalAtom(a.first()) })),
Pair(MalSymbol("atom?"), MalFunction({ a: ISeq -> if (a.first() is MalAtom) TRUE else FALSE })),
Pair(MalSymbol("deref"), MalFunction({ a: ISeq -> (a.first() as MalAtom).value })),
Pair(MalSymbol("reset!"), MalFunction({ a: ISeq ->
val atom = (a.nth(0) as MalAtom)

View File

@ -41,7 +41,7 @@ fun eval(_ast: MalType, _env: Env): MalType {
} else if (first is MalSymbol && first.value == "if") {
val check = eval(ast.nth(1), env)
if (check != NIL && check != FALSE) {
if (check !== NIL && check !== FALSE) {
ast = ast.nth(2)
} else if (ast.seq().asSequence().count() > 3) {
ast = ast.nth(3)
@ -68,12 +68,6 @@ fun eval(_ast: MalType, _env: Env): MalType {
catchEnv.set(symbol, thrown)
return eval(catchBody, catchEnv)
}
} else if (first is MalSymbol && first.value == "with-meta") {
val obj = eval(ast.nth(1), env)
val metadata = eval(ast.nth(2), env)
return obj.with_meta(metadata)
} else if (first is MalSymbol && first.value == "atom") {
return MalAtom(eval(ast.nth(1), env))
} else {
val evaluated = eval_ast(ast, env) as ISeq
val firstEval = evaluated.first()

View File

@ -89,6 +89,12 @@
(def! a (atom 2))
;=>(atom 2)
(atom? a)
;=>true
(atom? 1)
;=>false
;;;(type a)
;;;;=>"atom"