aboutsummaryrefslogtreecommitdiffhomepage
path: root/main.c
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-05-04 19:52:40 +0900
committernsfisis <nsfisis@gmail.com>2025-05-04 19:52:40 +0900
commit51e188a2f2e8cc0a4a9ff24437a3292ab0d3c5bb (patch)
treecc5b326c4b88d41052fc8b1fdb005d3953a5c0b6 /main.c
parent6dcc7f07abb3882055c85ce60ab940fc935f0b28 (diff)
downloadP4Dcc-51e188a2f2e8cc0a4a9ff24437a3292ab0d3c5bb.tar.gz
P4Dcc-51e188a2f2e8cc0a4a9ff24437a3292ab0d3c5bb.tar.zst
P4Dcc-51e188a2f2e8cc0a4a9ff24437a3292ab0d3c5bb.zip
desugar: remove while
Diffstat (limited to 'main.c')
-rw-r--r--main.c64
1 files changed, 32 insertions, 32 deletions
diff --git a/main.c b/main.c
index 27b1137..02cd04d 100644
--- a/main.c
+++ b/main.c
@@ -30,7 +30,7 @@ void todo() {
int read_all(char* buf) {
int c;
int n = 0;
- while ((c = getchar()) != -1) {
+ for (0; (c = getchar()) != -1; 0) {
buf[n] = c;
n += 1;
}
@@ -95,7 +95,7 @@ struct Token* tokenize(char* src, int len) {
struct Define* defines = calloc(1024, sizeof(struct Define));
struct Define* def = defines;
int pos = 0;
- while (pos < len) {
+ for (0; pos < len; 0) {
char c = src[pos];
if (c == '(') {
pos += 1;
@@ -206,7 +206,7 @@ struct Token* tokenize(char* src, int len) {
} else if (c == '"') {
pos += 1;
int start = pos;
- while (1) {
+ for (0; 1; 0) {
int ch = src[pos];
if (ch == '\\') {
pos += 1;
@@ -222,7 +222,7 @@ struct Token* tokenize(char* src, int len) {
tok += 1;
} else if (isdigit(c)) {
int start = pos;
- while (isdigit(src[pos])) {
+ for (0; isdigit(src[pos]); 0) {
pos += 1;
}
tok->kind = TK_L_INT;
@@ -231,7 +231,7 @@ struct Token* tokenize(char* src, int len) {
tok += 1;
} else if (isalpha(c)) {
int start = pos;
- while (isalnum(src[pos]) || src[pos] == '_') {
+ for (0; isalnum(src[pos]) || src[pos] == '_'; 0) {
pos += 1;
}
int len = pos - start;
@@ -264,7 +264,7 @@ struct Token* tokenize(char* src, int len) {
tok->value = calloc(len + 1, sizeof(char));
memcpy(tok->value, src + start, len);
int i = 0;
- while (defines + i != def) {
+ for (0; defines + i != def; 0) {
if (strcmp(tok->value, defines[i].from) == 0) {
tok->kind = defines[i].to->kind;
tok->value = defines[i].to->value;
@@ -283,20 +283,20 @@ struct Token* tokenize(char* src, int len) {
// TODO: too ugly implementation!
pos += 1;
pos += 6;
- while (isspace(src[pos])) {
+ for (0; isspace(src[pos]); 0) {
pos += 1;
}
int start = pos;
- while (isalnum(src[pos]) || src[pos] == '_') {
+ for (0; isalnum(src[pos]) || src[pos] == '_'; 0) {
pos += 1;
}
def->from = calloc(pos - start + 1, sizeof(char));
memcpy(def->from, src + start, pos - start);
- while (isspace(src[pos])) {
+ for (0; isspace(src[pos]); 0) {
pos += 1;
}
int start2 = pos;
- while (isdigit(src[pos])) {
+ for (0; isdigit(src[pos]); 0) {
pos += 1;
}
def->to = calloc(1, sizeof(struct Token));
@@ -497,7 +497,7 @@ int type_sizeof_struct(struct Type* ty) {
int struct_align = 0;
struct AstNode* member = ty->members->next;
- while (member) {
+ for (0; member; 0) {
int size = type_sizeof(member->ty);
int align = type_alignof(member->ty);
@@ -523,7 +523,7 @@ int type_alignof_struct(struct Type* ty) {
int struct_align = 0;
struct AstNode* member = ty->members->next;
- while (member) {
+ for (0; member; 0) {
int align = type_alignof(member->ty);
if (struct_align < align) {
@@ -543,7 +543,7 @@ int type_offsetof(struct Type* ty, char* name) {
int next_offset = 0;
struct AstNode* member = ty->members->next;
- while (member) {
+ for (0; member; 0) {
int size = type_sizeof(member->ty);
int align = type_alignof(member->ty);
@@ -568,7 +568,7 @@ struct Type* type_member_typeof(struct Type* ty, char* name) {
}
struct AstNode* member = ty->members->next;
- while (member) {
+ for (0; member; 0) {
if (strcmp(member->name, name) == 0) {
return member->ty;
}
@@ -722,7 +722,7 @@ struct AstNode* parse_primary_expr(struct Parser* p) {
struct AstNode* parse_arg_list(struct Parser* p) {
struct AstNode* list = ast_new_list(AST_ARG_LIST);
- while (peek_token(p)->kind != TK_PAREN_R) {
+ for (0; peek_token(p)->kind != TK_PAREN_R; 0) {
struct AstNode* arg = parse_expr(p);
list->last->next = arg;
list->last = arg;
@@ -738,7 +738,7 @@ struct AstNode* parse_arg_list(struct Parser* p) {
struct AstNode* parse_postfix_expr(struct Parser* p) {
struct AstNode* ret = parse_primary_expr(p);
- while (1) {
+ for (0; 1; 0) {
int tk = peek_token(p)->kind;
if (tk == TK_PAREN_L) {
next_token(p);
@@ -825,7 +825,7 @@ struct Type* parse_type(struct Parser* p) {
} else {
fatal_error("unreachable");
}
- while (1) {
+ for (0; 1; 0) {
struct Token* t2 = peek_token(p);
if (t2->kind == TK_STAR) {
next_token(p);
@@ -877,7 +877,7 @@ struct AstNode* parse_prefix_expr(struct Parser* p) {
struct AstNode* parse_multiplicative_expr(struct Parser* p) {
struct AstNode* lhs = parse_prefix_expr(p);
- while (1) {
+ for (0; 1; 0) {
int op = peek_token(p)->kind;
if (op == TK_STAR || op == TK_SLASH || op == TK_PERCENT) {
next_token(p);
@@ -893,7 +893,7 @@ struct AstNode* parse_multiplicative_expr(struct Parser* p) {
struct AstNode* parse_additive_expr(struct Parser* p) {
struct AstNode* lhs = parse_multiplicative_expr(p);
- while (1) {
+ for (0; 1; 0) {
int op = peek_token(p)->kind;
if (op == TK_PLUS) {
next_token(p);
@@ -928,7 +928,7 @@ struct AstNode* parse_additive_expr(struct Parser* p) {
struct AstNode* parse_relational_expr(struct Parser* p) {
struct AstNode* lhs = parse_additive_expr(p);
- while (1) {
+ for (0; 1; 0) {
int op = peek_token(p)->kind;
if (op == TK_LT || op == TK_LE) {
next_token(p);
@@ -954,7 +954,7 @@ struct AstNode* parse_relational_expr(struct Parser* p) {
struct AstNode* parse_equality_expr(struct Parser* p) {
struct AstNode* lhs = parse_relational_expr(p);
- while (1) {
+ for (0; 1; 0) {
int op = peek_token(p)->kind;
if (op == TK_EQ || op == TK_NE) {
next_token(p);
@@ -970,7 +970,7 @@ struct AstNode* parse_equality_expr(struct Parser* p) {
struct AstNode* parse_assignment_expr(struct Parser *p) {
struct AstNode* lhs = parse_equality_expr(p);
- while (1) {
+ for (0; 1; 0) {
int op = peek_token(p)->kind;
if (op == TK_ASSIGN) {
next_token(p);
@@ -1083,7 +1083,7 @@ struct AstNode* parse_expr_stmt(struct Parser* p) {
struct AstNode* parse_block_stmt(struct Parser* p) {
struct AstNode* list = ast_new_list(AST_BLOCK);
expect(p, TK_BRACE_L);
- while (peek_token(p)->kind != TK_BRACE_R) {
+ for (0; peek_token(p)->kind != TK_BRACE_R; 0) {
struct AstNode* stmt = parse_stmt(p);
list->last->next = stmt;
list->last = stmt;
@@ -1125,7 +1125,7 @@ struct AstNode* parse_struct_member(struct Parser* p) {
struct AstNode* parse_struct_members(struct Parser* p) {
struct AstNode* list = ast_new_list(AST_STRUCT_MEMBER_LIST);
- while (peek_token(p)->kind != TK_BRACE_R) {
+ for (0; peek_token(p)->kind != TK_BRACE_R; 0) {
struct AstNode* member = parse_struct_member(p);
list->last->next = member;
list->last = member;
@@ -1173,7 +1173,7 @@ void enter_func(struct Parser* p) {
void register_params(struct Parser* p, struct AstNode* params) {
struct AstNode* param = params->next;
- while (param) {
+ for (0; param; 0) {
p->locals[p->n_locals].name = param->name;
p->locals[p->n_locals].ty = param->ty;
p->n_locals += 1;
@@ -1201,7 +1201,7 @@ struct AstNode* parse_param(struct Parser* p) {
struct AstNode* parse_param_list(struct Parser* p) {
struct AstNode* list = ast_new_list(AST_PARAM_LIST);
- while (peek_token(p)->kind != TK_PAREN_R) {
+ for (0; peek_token(p)->kind != TK_PAREN_R; 0) {
struct AstNode* param = parse_param(p);
list->last->next = param;
list->last = param;
@@ -1247,7 +1247,7 @@ struct AstNode* parse_toplevel(struct Parser* p) {
struct AstNode* parse(struct Parser* p) {
struct AstNode* list = ast_new_list(AST_PROGRAM);
- while (eof(p)) {
+ for (0; eof(p); 0) {
struct AstNode* n = parse_toplevel(p);
list->last->next = n;
list->last = n;
@@ -1295,7 +1295,7 @@ void gen_func_prologue(struct CodeGen* g, struct AstNode* ast) {
printf(" mov rbp, rsp\n");
int param_index = 0;
struct AstNode* param = ast->func_params->next;
- while (param) {
+ for (0; param; 0) {
if (param_index == 0) {
printf(" push rdi\n");
} else if (param_index == 1) {
@@ -1461,7 +1461,7 @@ void gen_func_call(struct CodeGen* g, struct AstNode* ast) {
struct AstNode* args = ast->expr1;
struct AstNode* arg = args->next;
int n_args = 0;
- while (arg) {
+ for (0; arg; 0) {
n_args += 1;
gen_expr(g, arg, GEN_RVAL);
arg = arg->next;
@@ -1608,7 +1608,7 @@ void gen_var_decl(struct CodeGen* g, struct AstNode* ast) {
void gen_block_stmt(struct CodeGen* g, struct AstNode* ast) {
assert_ast_kind(ast, AST_BLOCK);
struct AstNode* stmt = ast->next;
- while (stmt) {
+ for (0; stmt; 0) {
gen_stmt(g, stmt);
stmt = stmt->next;
}
@@ -1654,7 +1654,7 @@ void gen(struct CodeGen* g, struct AstNode* ast) {
printf(".intel_syntax noprefix\n\n");
char** str_lit = ast->str_literals;
- while (*str_lit) {
+ for (0; *str_lit; 0) {
printf(".Lstr__%d:\n", str_lit - ast->str_literals + 1);
printf(" .string \"%s\"\n\n", *str_lit);
str_lit += 1;
@@ -1663,7 +1663,7 @@ void gen(struct CodeGen* g, struct AstNode* ast) {
printf(".globl main\n\n");
struct AstNode* func = ast->next;
- while (func) {
+ for (0; func; 0) {
if (func->kind == AST_FUNC_DEF) {
gen_func(g, func);
}