add getln() function and calculator example

Runtime-allocated strings currently always leak memory, I will
definitely have to fix that.
This commit is contained in:
r4
2021-12-26 11:36:52 +01:00
parent d8b470f0eb
commit dfe1ac90e8
3 changed files with 87 additions and 2 deletions

View File

@@ -57,6 +57,16 @@ Value eval_binary(IRInstr instr, const Value *lhs, const Value *rhs) {
case IRLe: res = lhs->Float <= rhs->Float; break;
default: ASSERT_UNREACHED();
};
} else if (lhs->type.kind == TypeArr && lhs->Arr.type.kind == TypeChar && lhs->Arr.is_string &&
rhs->type.kind == TypeArr && rhs->Arr.type.kind == TypeChar && rhs->Arr.is_string) {
switch (instr) {
case IREq:
res = lhs->Arr.len == rhs->Arr.len ? strncmp(lhs->Arr.vals, rhs->Arr.vals, lhs->Arr.len) == 0 : false;
break;
default:
set_err("String operation '%s' not supported", irinstr_str[instr]);
break;
};
} else {
set_err("Unsupported types for operation '%s': %s and %s", irinstr_str[instr], type_str[lhs->type.kind], type_str[rhs->type.kind]);
return (Value){0};