Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions examples/input.zn
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
foreign i32 read(i32, *u8, u64)
foreign u0 printf(*char)
foreign *u8 malloc(u64)
foreign *u8 memset(*u8, i32, u64)

i32 main() {
buf := malloc(1024)
bytesRead := 1

for bytesRead > 0 {
memset(buf, 0, 1024)
bytesRead = read(0, buf, 1024)
printf(buf)
}

printf("Exited\n")

return 0
}
28 changes: 26 additions & 2 deletions tests/pass/13_if_statement.zn
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
foreign u0 printf(*char)

i32 main(i32 argc) {
u64 val = 0

if argc == 1 {
printf("No command provided\n")
} else {
printf("Command provided\n")
}

if val == 0 {
}

if val != 0 {

}

if val > 0 {

}

if val < 0 {

}

if val >= 0 {

}

if val <= 0 {

}

val = val + 1

return 0
}
1 change: 1 addition & 0 deletions zmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ void undoVisit(ZState *state) {
}

static void printLineHighlight(ZToken *tok, const char *color) {
if (!tok || !tok->start) return;
char *lineStart = tok->sourceLinePtr;

while (*lineStart && *lineStart != '\n') {
Expand Down
56 changes: 31 additions & 25 deletions zsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -824,40 +824,46 @@ static ZType *resolveBinary(ZSemantic *semantic, ZNode *curr) {
ZType *left = resolveType(semantic, curr->binary.left);
ZType *right = resolveType(semantic, curr->binary.right);


/* Auto promotion rules should be handled by typesCompatible. */
ZType *promoted = typesCompatible(semantic->state, left, right);

if (!promoted) {
error(semantic->state,
curr->binary.op,
"Incompatible type '%s' with '%s'",
stype(left),
stype(right)
);
}

if (typesEqual(left, right)) {
/* Types are equal. it doesn't matter whether left or right is returned. */
return left;
} else if (op == TOK_EQ) {
/* Assignment yields the type of the left-hand side. */
if (!isLvalue(curr->binary.left)) {
error(semantic->state, left->tok, "is not a valid lvalue");
}
return left;
}

curr->binary.left = implicitCast(curr->binary.left, promoted);
curr->binary.right = implicitCast(curr->binary.right, promoted);

/* Comparison / logical operators always produce a bool. */
if (op == TOK_EQEQ || op == TOK_NOTEQ ||
if (
op == TOK_EQEQ || op == TOK_NOTEQ ||
op == TOK_LT || op == TOK_GT ||
op == TOK_LTE || op == TOK_GTE ||
op == TOK_AND || op == TOK_OR ||
op == TOK_SAND || op == TOK_SOR) {
ZType *boolType = maketype(Z_TYPE_PRIMITIVE);
boolType->primitive.token = maketoken(TOK_BOOL, NULL);
return boolType;
} else if (op == TOK_EQ) {
/* Assignment yields the type of the left-hand side. */
if (!isLvalue(curr->binary.left)) {
error(semantic->state, left->tok, "is not a valid lvalue");
}
return left;
} else if (typesEqual(left, right)) {
/* Types are equal. it doesn't matter whether left or right is returned. */
return left;
} else {
/* Auto promotion rules should be handled by typesCompatible. */
ZType *result = typesCompatible(semantic->state, left, right);
if (!result) {
error(semantic->state, curr->binary.op,
"%s type incompatible with %s type",
stype(left), stype(right)
);
}

curr->binary.left = implicitCast(curr->binary.left, result);
curr->binary.right = implicitCast(curr->binary.right, result);

return result;
}
return NULL;

return promoted;
}

static ZType *resolveArrayLiteral(ZSemantic *semantic, ZNode *curr) {
Expand Down
Loading