def toplevel_defintion
ident = identifier()
+ ident.module = @module
if matches("(") then
val = function_definition(ident)
elsif accept(:is)
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}'")
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
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)
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])