]> git.mdlowis.com Git - proto/cerise-c.git/commitdiff
finished code generation for hash literals
authorMichael D. Lowis <mike@mdlowis.com>
Thu, 13 Jun 2024 02:58:59 +0000 (22:58 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Thu, 13 Jun 2024 02:58:59 +0000 (22:58 -0400)
cerise-c.m
cerise-c.rb

index d8d91c4dc061e9e4a0d82e2b8d4bbe2352eb5f0c..832155a5b3f58104cc5b568342596cde18b5cd36 100644 (file)
@@ -86,6 +86,20 @@ TestStringOps()
 #    assert length("foo") == 3
 }
 
+TestArrayOps()
+{
+    def array = [1,2,3]
+    set item = array[0]
+#    set array[0] = item
+}
+
+TestHashOps()
+{
+    def hash = {foo: "bar", "baz": "boo"}
+    set item = hash["foo"]
+#    set hash["foo"] = item
+}
+
 Main()
 {
     def foo = [1,2,3]
index 6f1b1c6b3128156094b5d6ad8ad7539720f3b927..c7245b5c9c70511f253c2b66ac95dbb2fc271eb1 100755 (executable)
@@ -532,19 +532,33 @@ class Parser
       expect(",") if !matches("]")
     end
     expect("]")
-    exprs
     IR::Const.new(loc, nil, exprs)
   end
 
   def hash_literal()
-    exprs = {}
+    dict = {}
     expect("{")
-#      while !matches("}")
-#        exprs << expression()
-#        expect(",") if !matches("}")
-#      end
+    loc = location()
+    while !matches("}")
+      key = string_or_ident()
+      expect(":")
+      val = expression()
+      dict[key] = val
+      expect(",") if !matches("}")
+    end
     expect("}")
-    exprs
+    IR::Const.new(loc, nil, dict)
+  end
+
+  def string_or_ident()
+    tok = consume()
+    if tok.type == :string
+      IR::Const.new(tok.pos, Value::String, tok.text)
+    elsif tok.type == :ident
+      IR::Const.new(tok.pos, Value::String, "\"#{tok.text}\"")
+    else
+      error("not a string or identifier: #{tok}")
+    end
   end
 end
 
@@ -1121,7 +1135,21 @@ module Codegen
 
       end
     elsif v.value.is_a? Hash
-      putln state,  "Value #{var} = MakeHash(#{v.value});"
+#      putln state,  "Value #{var} = MakeHash(#{v.value});"
+
+      putln state, "Value #{var} = MakeHashOfLength(#{v.value.length});"
+      v.value.each do |k, v|
+        val = emit(state, v)
+        putln state, "Hash_SetIndex(#{var}, MakeString(#{k.value}), #{val});"
+#
+#
+#        pp k
+#        val = emit(state, e)
+#        putln state, "Array_SetIndex(#{var}, #{i}, #{val});"
+#
+      end
+
+
     else
       raise "code emitting constants of this type not implemented"
     end