aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-04-05 00:58:45 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-04-05 00:58:45 -0400
commit355ad9532163f9513c01aa59333aed5363386022 (patch)
tree4e7204809e489d9d014dc189065e62f7e1670a24 /src
parentf0b1a0f227cb7545af9ec27fbb4742b8c2c03bcd (diff)
Fix remaining metamethods
Diffstat (limited to 'src')
-rw-r--r--src/compile.c10
-rw-r--r--src/typecheck.c19
2 files changed, 27 insertions, 2 deletions
diff --git a/src/compile.c b/src/compile.c
index 33621bd5..05cc7bcd 100644
--- a/src/compile.c
+++ b/src/compile.c
@@ -606,6 +606,16 @@ static CORD compile_binary_op(env_t *env, ast_t *ast)
return CORD_all(b->code, "(", compile_arguments(env, ast, fn->args, args), ")");
}
}
+ } else if ((ast->tag == Divide || ast->tag == Mod || ast->tag == Mod1) && is_numeric_type(rhs_t)) {
+ b = get_namespace_binding(env, binop.lhs, binop_method_name(ast->tag));
+ if (b && b->type->tag == FunctionType) {
+ auto fn = Match(b->type, FunctionType);
+ if (type_eq(fn->ret, lhs_t)) {
+ arg_ast_t *args = new(arg_ast_t, .value=binop.lhs, .next=new(arg_ast_t, .value=binop.rhs));
+ if (is_valid_call(env, fn->args, args, true))
+ return CORD_all(b->code, "(", compile_arguments(env, ast, fn->args, args), ")");
+ }
+ }
}
if (ast->tag == Or && lhs_t->tag == OptionalType) {
diff --git a/src/typecheck.c b/src/typecheck.c
index 0291b748..e4176508 100644
--- a/src/typecheck.c
+++ b/src/typecheck.c
@@ -1060,6 +1060,11 @@ type_t *get_type(env_t *env, ast_t *ast)
return Type(BoolType);
}
+ if (type_eq(lhs_t, rhs_t)) {
+ binding_t *b = get_metamethod_binding(env, ast->tag, binop.lhs, binop.rhs, lhs_t);
+ if (b) return lhs_t;
+ }
+
if (lhs_t->tag == OptionalType) {
if (rhs_t->tag == OptionalType) {
type_t *result = most_complete_type(lhs_t, rhs_t);
@@ -1096,6 +1101,11 @@ type_t *get_type(env_t *env, ast_t *ast)
return Type(BoolType);
}
+ if (type_eq(lhs_t, rhs_t)) {
+ binding_t *b = get_metamethod_binding(env, ast->tag, binop.lhs, binop.rhs, lhs_t);
+ if (b) return lhs_t;
+ }
+
// Bitwise AND:
if ((is_numeric_type(lhs_t) || lhs_t->tag == BoolType)
&& (is_numeric_type(rhs_t) || rhs_t->tag == BoolType)
@@ -1120,6 +1130,11 @@ type_t *get_type(env_t *env, ast_t *ast)
return Type(BoolType);
}
+ if (type_eq(lhs_t, rhs_t)) {
+ binding_t *b = get_metamethod_binding(env, ast->tag, binop.lhs, binop.rhs, lhs_t);
+ if (b) return lhs_t;
+ }
+
// Bitwise XOR:
if ((is_numeric_type(lhs_t) || lhs_t->tag == BoolType)
&& (is_numeric_type(rhs_t) || rhs_t->tag == BoolType)
@@ -1203,8 +1218,8 @@ type_t *get_type(env_t *env, ast_t *ast)
return lhs_t;
}
}
- } else if (ast->tag == Divide && is_numeric_type(rhs_t)) {
- binding_t *b = get_namespace_binding(env, binop.lhs, "divided_by");
+ } else if ((ast->tag == Divide || ast->tag == Mod || ast->tag == Mod1) && is_numeric_type(rhs_t)) {
+ binding_t *b = get_namespace_binding(env, binop.lhs, binop_method_name(ast->tag));
if (b && b->type->tag == FunctionType) {
auto fn = Match(b->type, FunctionType);
if (type_eq(fn->ret, lhs_t)) {