aboutsummaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-03-14 13:47:40 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-03-14 13:47:40 -0400
commit80ffb8044ab60cedfe2ce1c3025419c282cb0965 (patch)
tree4487f38e4baa3cc4b92c0a57a4d8a41de20593d3 /compile.c
parentecf425fb9aa12a0f3dd08d23bcd74919fb11b362 (diff)
Better codegen for negation
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/compile.c b/compile.c
index bf0e7955..5f30d815 100644
--- a/compile.c
+++ b/compile.c
@@ -783,7 +783,20 @@ CORD compile(env_t *env, ast_t *ast)
}
break;
}
- case Not: return CORD_asprintf("not(%r)", compile(env, Match(ast, Not)->value));
+ case Not: {
+ type_t *t = get_type(env, ast);
+ ast_t *value = Match(ast, Not)->value;
+ if (t->tag == BoolType)
+ return CORD_all("!(", compile(env, value), ")");
+ else if (t->tag == IntType)
+ return CORD_all("~(", compile(env, value), ")");
+ else if (t->tag == ArrayType || t->tag == TableType)
+ return CORD_all("!(", compile(env, WrapAST(ast, Length, value)), ")");
+ else if (t->tag == TextType)
+ return CORD_all("!(", compile(env, value), ")");
+ else
+ code_err(ast, "I don't know how to negate values of type %T", t);
+ }
case Negative: return CORD_asprintf("-(%r)", compile(env, Match(ast, Negative)->value));
case HeapAllocate: return CORD_asprintf("$heap(%r)", compile(env, Match(ast, HeapAllocate)->value));
case StackReference: {