From e7a8582a965b656400cda440205385648cbd0419 Mon Sep 17 00:00:00 2001 From: Mike Lowis Date: Thu, 24 Oct 2024 16:53:22 -0400 Subject: [PATCH] added checks to make sure we don't assign to string indexes --- cerise-c.m | 2 -- lib/codegen.rb | 2 +- lib/lexer.rb | 4 ++-- lib/type_checker.rb | 5 +++++ 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cerise-c.m b/cerise-c.m index d96fb65..1c1afd4 100644 --- a/cerise-c.m +++ b/cerise-c.m @@ -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" diff --git a/lib/codegen.rb b/lib/codegen.rb index ebf9be5..c5e67ee 100644 --- a/lib/codegen.rb +++ b/lib/codegen.rb @@ -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) diff --git a/lib/lexer.rb b/lib/lexer.rb index 98c76a3..5505280 100644 --- a/lib/lexer.rb +++ b/lib/lexer.rb @@ -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 diff --git a/lib/type_checker.rb b/lib/type_checker.rb index 9dcd64b..6a55031 100644 --- a/lib/type_checker.rb +++ b/lib/type_checker.rb @@ -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 -- 2.54.0