From: Mike Lowis Date: Wed, 6 Nov 2024 18:23:15 +0000 (-0500) Subject: implemented fully qualified identifiers. type checker needs updates to properly handl... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=bb8575898cbf5f28c0df09825eda41e262a93b76;p=proto%2Fcerise-c.git implemented fully qualified identifiers. type checker needs updates to properly handle them still --- diff --git a/lib/parser.rb b/lib/parser.rb index 49f0532..af77572 100644 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -180,6 +180,7 @@ class Parser def toplevel_defintion ident = identifier() + ident.module = @module if matches("(") then val = function_definition(ident) elsif accept(:is) @@ -261,7 +262,7 @@ class Parser def qualified_identifier() tok = expect(:ident) varname = IR::Var.new(tok.pos, nil, tok.text.to_sym, nil) - if matches(".") + if accept(".") mod = syms[varname.name] if mod.nil? error("no such module: '#{varname.name}'") @@ -492,23 +493,27 @@ class Parser elsif matches("{") hash_literal() else - tok = consume() - if tok.type == :bool - IR::Const.new(tok.pos, :bool, tok.text == "true") - elsif tok.type == :string - IR::Const.new(tok.pos, :string, tok.text) - elsif tok.type == :int - IR::Const.new(tok.pos, :int, tok.text.to_i) - elsif tok.type == :char - IR::Const.new(tok.pos, :int, tok.text[1].ord) - elsif tok.type == :float - IR::Const.new(tok.pos, :float, tok.text.to_f) - elsif tok.type == :void - IR::Const.new(tok.pos, :void, :void) - elsif tok.type == :ident + if peek().type == :ident qualified_identifier() else - error("invalid constant #{tok}") + tok = consume() + if tok.type == :bool + IR::Const.new(tok.pos, :bool, tok.text == "true") + elsif tok.type == :string + IR::Const.new(tok.pos, :string, tok.text) + elsif tok.type == :int + IR::Const.new(tok.pos, :int, tok.text.to_i) + elsif tok.type == :char + IR::Const.new(tok.pos, :int, tok.text[1].ord) + elsif tok.type == :float + IR::Const.new(tok.pos, :float, tok.text.to_f) + elsif tok.type == :void + IR::Const.new(tok.pos, :void, :void) +# elsif tok.type == :ident +# qualified_identifier() + else + error("invalid constant #{tok}") + end end end end diff --git a/lib/type_checker.rb b/lib/type_checker.rb index a4eba9d..439bf50 100644 --- a/lib/type_checker.rb +++ b/lib/type_checker.rb @@ -385,6 +385,14 @@ class TypeChecker expr.type = optype[-1] end + def typename(var) + if var.module.nil? + var.name.to_sym + else + "#{var.module}_#{var.name}".to_sym + end + end + def check_binary(env, expr, vtype) if expr.op == "[" left_type = infer(env, expr.left) @@ -403,7 +411,8 @@ class TypeChecker error(expr.loc, "don't know how to index into type: #{left_type}") end else - optype = BinaryOps[expr.op][vtype] + tname = typename(vtype) + optype = BinaryOps[expr.op][tname] error(expr.loc, "unknown binary operation '#{expr.op}' for operand type #{vtype}") if optype.nil? check(env, expr.left, optype[0]) check(env, expr.right, optype[1])