aboutsummaryrefslogtreecommitdiff
path: root/src/compile
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-09-21 22:55:03 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-09-21 22:55:03 -0400
commit0d36812c6af951a41caac77d5f312949f3bc521f (patch)
treeae0f545805c524ffa467b925687bf70e88e2bc58 /src/compile
parentfa7a0ddc0963deb387f0cae7bb22ca968ee9146f (diff)
Deprecate binary ops (other than ++) for tables. Instead use proper
methods: t.with(other), t.without(other), t.intersection(other), t.difference(other)
Diffstat (limited to 'src/compile')
-rw-r--r--src/compile/binops.c11
-rw-r--r--src/compile/tables.c20
2 files changed, 23 insertions, 8 deletions
diff --git a/src/compile/binops.c b/src/compile/binops.c
index 0bff98c0..acf1e031 100644
--- a/src/compile/binops.c
+++ b/src/compile/binops.c
@@ -160,8 +160,6 @@ Text_t compile_binary_op(env_t *env, ast_t *ast) {
return Texts("(", lhs, " + ", rhs, ")");
}
case Minus: {
- if (overall_t->tag == TableType)
- return Texts("Table$minus(", lhs, ", ", rhs, ", ", compile_type_info(overall_t), ")");
if (overall_t->tag != IntType && overall_t->tag != NumType && overall_t->tag != ByteType)
code_err(ast,
"Math operations are only supported for values of the same "
@@ -204,8 +202,6 @@ Text_t compile_binary_op(env_t *env, ast_t *ast) {
case And: {
if (overall_t->tag == BoolType) return Texts("(", lhs, " && ", rhs, ")");
else if (overall_t->tag == IntType || overall_t->tag == ByteType) return Texts("(", lhs, " & ", rhs, ")");
- else if (overall_t->tag == TableType)
- return Texts("Table$and(", lhs, ", ", rhs, ", ", compile_type_info(overall_t), ")");
else
code_err(ast, "The 'and' operator isn't supported between ", type_to_text(lhs_t), " and ",
type_to_text(rhs_t), " values");
@@ -218,8 +214,6 @@ Text_t compile_binary_op(env_t *env, ast_t *ast) {
return Texts("(", lhs, " || ", rhs, ")");
} else if (overall_t->tag == IntType || overall_t->tag == ByteType) {
return Texts("(", lhs, " | ", rhs, ")");
- } else if (overall_t->tag == TableType) {
- return Texts("Table$or(", lhs, ", ", rhs, ", ", compile_type_info(overall_t), ")");
} else {
code_err(ast, "The 'or' operator isn't supported between ", type_to_text(lhs_t), " and ",
type_to_text(rhs_t), " values");
@@ -229,8 +223,6 @@ Text_t compile_binary_op(env_t *env, ast_t *ast) {
// TODO: support optional values in `xor` expressions
if (overall_t->tag == BoolType || overall_t->tag == IntType || overall_t->tag == ByteType)
return Texts("(", lhs, " ^ ", rhs, ")");
- else if (overall_t->tag == TableType)
- return Texts("Table$xor(", lhs, ", ", rhs, ", ", compile_type_info(overall_t), ")");
else
code_err(ast, "The 'xor' operator isn't supported between ", type_to_text(lhs_t), " and ",
type_to_text(rhs_t), " values");
@@ -245,6 +237,9 @@ Text_t compile_binary_op(env_t *env, ast_t *ast) {
return Texts("List$concat(", lhs, ", ", rhs, ", sizeof(",
compile_type(Match(overall_t, ListType)->item_type), "))");
}
+ case TableType: {
+ return Texts("Table$with(", lhs, ", ", rhs, ", ", compile_type_info(overall_t), ")");
+ }
default:
code_err(ast, "Concatenation isn't supported between ", type_to_text(lhs_t), " and ", type_to_text(rhs_t),
" values");
diff --git a/src/compile/tables.c b/src/compile/tables.c
index b6efdecd..814d81f5 100644
--- a/src/compile/tables.c
+++ b/src/compile/tables.c
@@ -139,5 +139,25 @@ Text_t compile_table_method_call(env_t *env, ast_t *ast) {
self = compile_to_pointer_depth(env, call->self, 0, false);
arg_t *arg_spec = new (arg_t, .name = "fallback", .type = Type(OptionalType, self_value_t));
return Texts("Table$with_fallback(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ")");
+ } else if (streq(call->name, "intersection")) {
+ self = compile_to_pointer_depth(env, call->self, 0, false);
+ arg_t *arg_spec = new (arg_t, .name = "other", .type = self_value_t);
+ return Texts("Table$intersection(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ",
+ compile_type_info(self_value_t), ")");
+ } else if (streq(call->name, "with")) {
+ self = compile_to_pointer_depth(env, call->self, 0, false);
+ arg_t *arg_spec = new (arg_t, .name = "other", .type = self_value_t);
+ return Texts("Table$with(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ",
+ compile_type_info(self_value_t), ")");
+ } else if (streq(call->name, "without")) {
+ self = compile_to_pointer_depth(env, call->self, 0, false);
+ arg_t *arg_spec = new (arg_t, .name = "other", .type = self_value_t);
+ return Texts("Table$without(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ",
+ compile_type_info(self_value_t), ")");
+ } else if (streq(call->name, "difference")) {
+ self = compile_to_pointer_depth(env, call->self, 0, false);
+ arg_t *arg_spec = new (arg_t, .name = "other", .type = self_value_t);
+ return Texts("Table$difference(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ",
+ compile_type_info(self_value_t), ")");
} else code_err(ast, "There is no '", call->name, "' method for tables");
}