]> git.mdlowis.com Git - proto/cerise-c.git/commitdiff
added checks to make sure we don't assign to string indexes
authorMike Lowis <mike.lowis@gentex.com>
Thu, 24 Oct 2024 20:53:22 +0000 (16:53 -0400)
committerMike Lowis <mike.lowis@gentex.com>
Thu, 24 Oct 2024 20:53:22 +0000 (16:53 -0400)
cerise-c.m
lib/codegen.rb
lib/lexer.rb
lib/type_checker.rb

index d96fb653211b69c3e2e1dc23c05dcede275b10df..1c1afd4983d662811c1bbe084fedd87f5d461aba 100644 (file)
@@ -79,8 +79,6 @@ TestStringOps()
     assert ("foo" + "bar") == "foobar"
     assert("foo"[0] == 'f')
 
-
-
 #    assert ("foo" + 123) == "foo123"
 #    assert ("foo" + 123.0) == "foo123.000000"
 #    assert ("foo" + true) == "footrue"
index ebf9be5d2bb6a11e1cce10f296ebfc2d4ca59a2e..c5e67ee7e5078de665fa5039a161b2e3ecf83c9b 100644 (file)
@@ -245,7 +245,7 @@ class Codegen
       object = emit(v.name.left)
       key = emit(v.name.right)
       value = emit(v.value)
-      putln "Object_Set(#{object}, #{key}, #{value});"
+      putln "Array_Set(#{object}, #{key}, #{value});"
     else
       temp = emit(v.value)
       error v,  "invalid local definition: #{v.name.name}" if not @locals.index(v.name.name)
index 98c76a33602a1a5b9d3e1b62615d8582a51486d4..550528076f57522d6567ab5267d4370f638d838b 100644 (file)
@@ -21,7 +21,7 @@ class Lexer
 
   def linenum(pos = nil)
     pos = [pos || @data.pos].flatten.last
-    @text[0..pos].count("\n") + 1
+    (@text[0..pos].count("\n") + 1)
   end
 
   SPACE = /([ \t\v\n\r]+|#.*\n)/
@@ -71,7 +71,7 @@ class Lexer
     else
       tok = Tok.new("EOF", @file, pos, :eof)
     end
-#    pp tok
+#    pp "#{@file}:#{linenum(pos)}: #{tok.type} - #{tok.text}"
     tok
   end
 end
index 9dcd64bd8c7b1d1d40e8b0d0918e4ccf79b8091a..6a550317c1100da6303f732b475f9009708ea452 100644 (file)
@@ -252,6 +252,11 @@ class TypeChecker
 
   def infer_set(env, expr)
     type = infer(env, expr.name)
+    # TODO: check that LHS is not a string access
+    dest = expr.name
+    if (dest.is_a? IR::Op and dest.op == "[") and dest.left.type == :string
+      error(expr.loc, "cannot assign to immutable value")
+    end
     check(env, expr.value, type)
     expr.type = :void
   end