All: fix get. All pass stepA tests.
authorJoel Martin <github@martintribe.org>
Tue, 15 Apr 2014 06:24:43 +0000 (01:24 -0500)
committerJoel Martin <github@martintribe.org>
Tue, 15 Apr 2014 06:24:43 +0000 (01:24 -0500)
13 files changed:
bash/core.sh
c/core.c
cs/core.cs
java/src/main/java/mal/core.java
java/src/main/java/mal/step6_file.java
java/src/main/java/mal/step7_quote.java
js/core.js
js/step2_eval.js
js/step3_env.js
js/step4_if_fn_do.js
make/core.mk
python/core.py
ruby/core.rb

index 0da820c..d5750e6 100644 (file)
@@ -145,7 +145,9 @@ _get () {
         local obj="${ANON["${1}"]}"
         eval r="\${${obj}[\"${2}\"]}" ;;
     list|vector)
-        _nth "${1}" "${2}"
+        _nth "${1}" "${2}" ;;
+    nil)
+        r="${__nil}" ;;
     esac
 }
 get () {
index 2a447e5..33bb991 100644 (file)
--- a/c/core.c
+++ b/c/core.c
@@ -182,6 +182,8 @@ MalVal *get(MalVal *obj, MalVal *key) {
         } else {
             return &mal_nil;
         }
+    case MAL_NIL:
+        return &mal_nil;
     default:
         abort("get called on unsupported type %d", obj->type);
     }
index f8bd7ff..13f082e 100644 (file)
@@ -87,8 +87,12 @@ namespace Mal {
         static MalFunction get = new MalFunction(
             a => {
                 string key = ((MalString)a[1]).getValue();
-                var dict = ((MalHashMap)a[0]).getValue();
-                return dict.ContainsKey(key) ? dict[key] : Nil;
+                if (a[0] == Nil) {
+                    return Nil;
+                } else {
+                    var dict = ((MalHashMap)a[0]).getValue();
+                    return dict.ContainsKey(key) ? dict[key] : Nil;
+                }
             });
 
         static MalFunction keys = new MalFunction(
index e1e6705..6ac4f8e 100644 (file)
@@ -205,13 +205,17 @@ public class core {
 
     static MalFunction get = new MalFunction() {
         public MalVal apply(MalList a) throws MalThrowable {
-            String key = ((MalString)a.nth(1)).getValue();
-            MalHashMap mhm = (MalHashMap)a.nth(0);
-            HashMap<String,MalVal> hm = (HashMap<String,MalVal>)mhm.value;
-            if (hm.containsKey(key)) {
-                return hm.get(key);
-            } else {
+            if (a.nth(0) == Nil) {
                 return Nil;
+            } else {
+                String key = ((MalString)a.nth(1)).getValue();
+                MalHashMap mhm = (MalHashMap)a.nth(0);
+                HashMap<String,MalVal> hm = (HashMap<String,MalVal>)mhm.value;
+                if (hm.containsKey(key)) {
+                    return hm.get(key);
+                } else {
+                    return Nil;
+                }
             }
         }
     };
index 8d05b50..6a4c981 100644 (file)
@@ -161,7 +161,7 @@ public class step6_file {
                     return reader.read_str(((MalString)args.nth(0)).getValue());
                 } catch (MalContinue c) {
                     return types.Nil;
-                return reader.read_str(((MalString)args.nth(0)).getValue());
+                }
             }
         });
         _ref(repl_env, "eval", new MalFunction() {
index fbebb10..c46b4f0 100644 (file)
@@ -192,7 +192,7 @@ public class step7_quote {
                     return reader.read_str(((MalString)args.nth(0)).getValue());
                 } catch (MalContinue c) {
                     return types.Nil;
-                return reader.read_str(((MalString)args.nth(0)).getValue());
+                }
             }
         });
         _ref(repl_env, "eval", new MalFunction() {
index b8b0062..7addc49 100644 (file)
@@ -51,7 +51,7 @@ function dissoc(src_hm) {
 }
 
 function get(hm, key) {
-    if (key in hm) {
+    if (hm != null && key in hm) {
         return hm[key];
     } else {
         return null;
index f111a58..adcefe5 100644 (file)
@@ -32,6 +32,7 @@ function eval_ast(ast, env) {
 }
 
 function _EVAL(ast, env) {
+    //printer.println("EVAL:", types._pr_str(ast, true));
     if (!types._list_Q(ast)) {
         return eval_ast(ast, env);
     }
index 1000437..86981d2 100644 (file)
@@ -33,6 +33,7 @@ function eval_ast(ast, env) {
 }
 
 function _EVAL(ast, env) {
+    //printer.println("EVAL:", types._pr_str(ast, true));
     if (!types._list_Q(ast)) {
         return eval_ast(ast, env);
     }
index 0bf988d..a80190e 100644 (file)
@@ -34,6 +34,7 @@ function eval_ast(ast, env) {
 }
 
 function _EVAL(ast, env) {
+    //printer.println("EVAL:", types._pr_str(ast, true));
     if (!types._list_Q(ast)) {
         return eval_ast(ast, env);
     }
index aaba3fd..fbea5ea 100644 (file)
@@ -102,9 +102,11 @@ vals = $(foreach new_list,$(call _list),$(new_list)$(eval $(new_list)_value := $
 # retrieve the value of a string key object from the hash map, or
 # retrive a vector by number object index
 get = $(strip \
-        $(if $(call _hash_map?,$(word 1,$(1))),\
-          $(call _get,$(word 1,$(1)),$(call str_decode,$($(word 2,$(1))_value))),\
-          $(call _get,$(word 1,$(1)),$(call int_decode,$($(word 2,$(1))_value)))))
+        $(if $(call _nil?,$(word 1,$(1))),\
+          $(__nil),\
+          $(if $(call _hash_map?,$(word 1,$(1))),\
+            $(call _get,$(word 1,$(1)),$(call str_decode,$($(word 2,$(1))_value))),\
+            $(call _get,$(word 1,$(1)),$(call int_decode,$($(word 2,$(1))_value))))))
 
 contains? = $(if $(call _contains?,$(word 1,$(1)),$(call str_decode,$($(word 2,$(1))_value))),$(__true),$(__false))
 
index 5e2ef75..8b01b69 100644 (file)
@@ -39,7 +39,7 @@ def dissoc(src_hm, *keys):
     return hm
 
 def get(hm, key):
-    if key in hm:
+    if hm and key in hm:
         return hm[key]
     else:
         return None
index 212374a..8823112 100644 (file)
@@ -27,7 +27,7 @@ $core_ns = {
     :map? =>      lambda {|a| a.is_a? Hash},
     :assoc =>     lambda {|*a| a[0].merge(Hash[a.drop(1).each_slice(2).to_a])},
     :dissoc =>    lambda {|*a| h = a[0].clone; a.drop(1).each{|k| h.delete k}; h},
-    :get =>       lambda {|a,b| a[b]},
+    :get =>       lambda {|a,b| return nil if a == nil; a[b]},
     :contains? => lambda {|a,b| a.key? b},
     :keys =>      lambda {|a| List.new a.keys},
     :vals =>      lambda {|a| List.new a.values},