aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ast.c10
-rw-r--r--src/ast.h3
-rw-r--r--src/cli.c12
-rw-r--r--src/cli.h8
-rw-r--r--src/codegen.c6
-rw-r--r--src/common.c4
-rw-r--r--src/common.h2
-rw-r--r--src/io.c8
-rw-r--r--src/io.h4
-rw-r--r--src/json.c16
-rw-r--r--src/json.h2
-rw-r--r--src/parse.c112
-rw-r--r--src/parse.h2
-rw-r--r--src/preprocess.c70
-rw-r--r--src/std.h4
-rw-r--r--src/token.c2
-rw-r--r--src/token.h2
17 files changed, 137 insertions, 130 deletions
diff --git a/src/ast.c b/src/ast.c
index 1e444ba..84fe38e 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -35,6 +35,8 @@ const char* type_kind_stringify(TypeKind k) {
return "double";
else if (k == TypeKind_ldouble)
return "long double";
+ else if (k == TypeKind_bool)
+ return "bool";
else if (k == TypeKind_struct)
return "struct";
else if (k == TypeKind_union)
@@ -89,7 +91,7 @@ Type* type_new_func(Type* result, AstNode* params) {
return ty;
}
-BOOL type_is_unsized(Type* ty) {
+bool type_is_unsized(Type* ty) {
return ty->kind == TypeKind_void || ty->kind == TypeKind_func;
}
@@ -98,7 +100,8 @@ int type_sizeof(Type* ty) {
fatal_error("type_sizeof: type size cannot be determined");
}
- if (ty->kind == TypeKind_char || ty->kind == TypeKind_schar || ty->kind == TypeKind_uchar)
+ if (ty->kind == TypeKind_char || ty->kind == TypeKind_schar || ty->kind == TypeKind_uchar ||
+ ty->kind == TypeKind_bool)
return 1;
else if (ty->kind == TypeKind_short || ty->kind == TypeKind_ushort)
return 2;
@@ -126,7 +129,8 @@ int type_alignof(Type* ty) {
fatal_error("type_alignof: type size cannot be determined");
}
- if (ty->kind == TypeKind_char || ty->kind == TypeKind_schar || ty->kind == TypeKind_uchar)
+ if (ty->kind == TypeKind_char || ty->kind == TypeKind_schar || ty->kind == TypeKind_uchar ||
+ ty->kind == TypeKind_bool)
return 1;
else if (ty->kind == TypeKind_short || ty->kind == TypeKind_ushort)
return 2;
diff --git a/src/ast.h b/src/ast.h
index f5fc98f..e0d8410 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -32,6 +32,7 @@ typedef enum {
TypeKind_float,
TypeKind_double,
TypeKind_ldouble,
+ TypeKind_bool,
TypeKind_struct,
TypeKind_union,
TypeKind_enum,
@@ -67,7 +68,7 @@ Type* type_new_array(Type* elem, int size);
Type* type_new_static_string(int len);
Type* type_array_to_ptr(Type* ty);
Type* type_new_func(Type* result, AstNode* params);
-BOOL type_is_unsized(Type* ty);
+bool type_is_unsized(Type* ty);
int type_sizeof_struct(Type* ty);
int type_sizeof_union(Type* ty);
diff --git a/src/cli.c b/src/cli.c
index 4efd2e2..bd0d915 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -9,8 +9,8 @@ static void print_version() {
CliArgs* parse_cli_args(int argc, char** argv) {
const char* output_filename = NULL;
int positional_arguments_start = -1;
- BOOL only_compile = FALSE;
- BOOL generate_deps = FALSE;
+ bool only_compile = false;
+ bool generate_deps = false;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] != '-') {
@@ -33,9 +33,9 @@ CliArgs* parse_cli_args(int argc, char** argv) {
output_filename = argv[i + 1];
++i;
} else if (c == 'c') {
- only_compile = TRUE;
+ only_compile = true;
} else if (strcmp(argv[i], "-MMD") == 0) {
- generate_deps = TRUE;
+ generate_deps = true;
} else if (strcmp(argv[i], "--version") == 0) {
print_version();
exit(0);
@@ -52,12 +52,12 @@ CliArgs* parse_cli_args(int argc, char** argv) {
a->output_filename = output_filename;
a->output_assembly = !output_filename || str_ends_with(output_filename, ".s");
a->only_compile = only_compile;
- a->totally_deligate_to_gcc = FALSE;
+ a->totally_deligate_to_gcc = false;
a->gcc_command = NULL;
a->generate_deps = generate_deps;
if (!a->only_compile && str_ends_with(a->input_filename, ".o")) {
- a->totally_deligate_to_gcc = TRUE;
+ a->totally_deligate_to_gcc = true;
StrBuilder builder;
strbuilder_init(&builder);
strbuilder_append_string(&builder, "gcc ");
diff --git a/src/cli.h b/src/cli.h
index 0fb106a..ec1fa50 100644
--- a/src/cli.h
+++ b/src/cli.h
@@ -6,10 +6,10 @@
typedef struct {
const char* input_filename;
const char* output_filename;
- BOOL output_assembly;
- BOOL only_compile;
- BOOL generate_deps;
- BOOL totally_deligate_to_gcc;
+ bool output_assembly;
+ bool only_compile;
+ bool generate_deps;
+ bool totally_deligate_to_gcc;
const char* gcc_command;
} CliArgs;
diff --git a/src/codegen.c b/src/codegen.c
index 006ae20..a16ae73 100644
--- a/src/codegen.c
+++ b/src/codegen.c
@@ -584,11 +584,11 @@ void codegen(Program* prog, FILE* out) {
for (int i = 0; i < prog->vars->node_len; ++i) {
AstNode* var = prog->vars->node_items + i;
if (var->node_expr) {
- if (var->ty->kind == TypeKind_char)
+ if (type_sizeof(var->ty) == 1)
fprintf(g->out, " %s: .byte %d\n", var->name, var->node_expr->node_int_value);
- else if (var->ty->kind == TypeKind_short)
+ else if (type_sizeof(var->ty) == 2)
fprintf(g->out, " %s: .word %d\n", var->name, var->node_expr->node_int_value);
- else if (var->ty->kind == TypeKind_int)
+ else if (type_sizeof(var->ty) == 4)
fprintf(g->out, " %s: .int %d\n", var->name, var->node_expr->node_int_value);
else
unimplemented();
diff --git a/src/common.c b/src/common.c
index 595f2c7..d3b85a5 100644
--- a/src/common.c
+++ b/src/common.c
@@ -9,11 +9,11 @@ void fatal_error(const char* msg, ...) {
exit(1);
}
-BOOL str_ends_with(const char* s, const char* suffix) {
+bool str_ends_with(const char* s, const char* suffix) {
size_t l1 = strlen(s);
size_t l2 = strlen(suffix);
if (l1 < l2)
- return FALSE;
+ return false;
return strcmp(s + l1 - l2, suffix) == 0;
}
diff --git a/src/common.h b/src/common.h
index b31dfc4..4bd4c17 100644
--- a/src/common.h
+++ b/src/common.h
@@ -8,7 +8,7 @@ _Noreturn void fatal_error(const char* msg, ...);
#define unreachable() fatal_error("%s:%d: unreachable", __FILE__, __LINE__)
#define unimplemented() fatal_error("%s:%d: unimplemented", __FILE__, __LINE__)
-BOOL str_ends_with(const char* s, const char* suffix);
+bool str_ends_with(const char* s, const char* suffix);
typedef struct {
size_t len;
diff --git a/src/io.c b/src/io.c
index ecbb0c9..c2a53de 100644
--- a/src/io.c
+++ b/src/io.c
@@ -48,7 +48,7 @@ InFile* infile_open(const char* filename) {
return in_file;
}
-BOOL infile_eof(InFile* f) {
+bool infile_eof(InFile* f) {
return f->buf[f->pos] == '\0';
}
@@ -87,11 +87,11 @@ char infile_next_char(InFile* f) {
return c;
}
-BOOL infile_consume_if(InFile* f, char expected) {
+bool infile_consume_if(InFile* f, char expected) {
if (infile_peek_char(f) == expected) {
infile_next_char(f);
- return TRUE;
+ return true;
} else {
- return FALSE;
+ return false;
}
}
diff --git a/src/io.h b/src/io.h
index fff3558..604b8f6 100644
--- a/src/io.h
+++ b/src/io.h
@@ -18,9 +18,9 @@ typedef struct {
} InFile;
InFile* infile_open(const char* filename);
-BOOL infile_eof(InFile* f);
+bool infile_eof(InFile* f);
char infile_peek_char(InFile* f);
char infile_next_char(InFile* f);
-BOOL infile_consume_if(InFile* f, char expected);
+bool infile_consume_if(InFile* f, char expected);
#endif
diff --git a/src/json.c b/src/json.c
index c255221..8b6dc74 100644
--- a/src/json.c
+++ b/src/json.c
@@ -4,15 +4,15 @@
struct JsonBuilder {
StrBuilder buf;
// not supported by ducc for now
- // BOOL has_output_element[256];
- BOOL* has_output_element;
+ // bool has_output_element[256];
+ bool* has_output_element;
int depth;
};
JsonBuilder* jsonbuilder_new() {
JsonBuilder* b = calloc(1, sizeof(JsonBuilder));
strbuilder_init(&b->buf);
- b->has_output_element = calloc(256, sizeof(BOOL));
+ b->has_output_element = calloc(256, sizeof(bool));
return b;
}
@@ -28,7 +28,7 @@ static void emit_comma_if_needed(JsonBuilder* b) {
void jsonbuilder_object_start(JsonBuilder* b) {
strbuilder_append_char(&b->buf, '{');
- b->has_output_element[b->depth++] = FALSE;
+ b->has_output_element[b->depth++] = false;
}
void jsonbuilder_object_end(JsonBuilder* b) {
@@ -43,12 +43,12 @@ void jsonbuilder_object_member_start(JsonBuilder* b, const char* key) {
}
void jsonbuilder_object_member_end(JsonBuilder* b) {
- b->has_output_element[b->depth - 1] = TRUE;
+ b->has_output_element[b->depth - 1] = true;
}
void jsonbuilder_array_start(JsonBuilder* b) {
strbuilder_append_char(&b->buf, '[');
- b->has_output_element[b->depth++] = FALSE;
+ b->has_output_element[b->depth++] = false;
}
void jsonbuilder_array_end(JsonBuilder* b) {
@@ -61,14 +61,14 @@ void jsonbuilder_array_element_start(JsonBuilder* b) {
}
void jsonbuilder_array_element_end(JsonBuilder* b) {
- b->has_output_element[b->depth - 1] = TRUE;
+ b->has_output_element[b->depth - 1] = true;
}
void jsonbuilder_null(JsonBuilder* b) {
strbuilder_append_string(&b->buf, "null");
}
-void jsonbuilder_boolean(JsonBuilder* b, BOOL value) {
+void jsonbuilder_boolean(JsonBuilder* b, bool value) {
strbuilder_append_string(&b->buf, value ? "true" : "false");
}
diff --git a/src/json.h b/src/json.h
index b23dbc2..40d5cde 100644
--- a/src/json.h
+++ b/src/json.h
@@ -20,7 +20,7 @@ void jsonbuilder_array_element_start(JsonBuilder* b);
void jsonbuilder_array_element_end(JsonBuilder* b);
void jsonbuilder_null(JsonBuilder* b);
-void jsonbuilder_boolean(JsonBuilder* b, BOOL value);
+void jsonbuilder_boolean(JsonBuilder* b, bool value);
void jsonbuilder_integer(JsonBuilder* b, int value);
void jsonbuilder_string(JsonBuilder* b, const char* value);
diff --git a/src/parse.c b/src/parse.c
index 4abfebd..07259ad 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -190,7 +190,7 @@ static Token* consume_token_if(Parser* p, TokenKind expected) {
}
}
-static BOOL eof(Parser* p) {
+static bool eof(Parser* p) {
return peek_token(p)->kind != TokenKind_eof;
}
@@ -228,7 +228,7 @@ static int find_lvar(Parser* p, const char* name) {
return -1;
}
-static int calc_stack_offset(Parser* p, Type* ty, BOOL is_param) {
+static int calc_stack_offset(Parser* p, Type* ty, bool is_param) {
int align;
if (is_param) {
if (8 < type_sizeof(ty) || 8 < type_alignof(ty)) {
@@ -250,7 +250,7 @@ static int calc_stack_offset(Parser* p, Type* ty, BOOL is_param) {
return to_aligned(offset, align);
}
-static int add_lvar(Parser* p, const char* name, Type* ty, BOOL is_param) {
+static int add_lvar(Parser* p, const char* name, Type* ty, bool is_param) {
int stack_offset = calc_stack_offset(p, ty, is_param);
LocalVar* lvar = lvars_push_new(&p->lvars);
lvar->name = name;
@@ -263,7 +263,7 @@ static int add_lvar(Parser* p, const char* name, Type* ty, BOOL is_param) {
}
static AstNode* generate_temporary_lvar(Parser* p, Type* ty) {
- int stack_offset = add_lvar(p, NULL, ty, FALSE);
+ int stack_offset = add_lvar(p, NULL, ty, false);
AstNode* lvar = ast_new(AstNodeKind_lvar);
lvar->name = NULL;
lvar->node_stack_offset = stack_offset;
@@ -372,6 +372,10 @@ static AstNode* parse_primary_expr(Parser* p) {
Token* t = next_token(p);
if (t->kind == TokenKind_literal_int) {
return ast_new_int(t->value.integer);
+ } else if (t->kind == TokenKind_keyword_true) {
+ return ast_new_int(1);
+ } else if (t->kind == TokenKind_keyword_false) {
+ return ast_new_int(0);
} else if (t->kind == TokenKind_literal_str) {
AstNode* e = ast_new(AstNodeKind_str_expr);
e->node_idx = register_str_literal(p, t->value.string);
@@ -498,20 +502,21 @@ static AstNode* parse_postfix_expr(Parser* p) {
return ret;
}
-static BOOL is_typedef_name(Parser* p, Token* tok) {
+static bool is_typedef_name(Parser* p, Token* tok) {
return tok->kind == TokenKind_ident && find_typedef(p, tok->value.string) != -1;
}
-static BOOL is_type_token(Parser* p, Token* token) {
+static bool is_type_token(Parser* p, Token* token) {
if (token->kind == TokenKind_keyword_int || token->kind == TokenKind_keyword_short ||
token->kind == TokenKind_keyword_long || token->kind == TokenKind_keyword_char ||
- token->kind == TokenKind_keyword_void || token->kind == TokenKind_keyword_enum ||
- token->kind == TokenKind_keyword_struct || token->kind == TokenKind_keyword_union ||
- token->kind == TokenKind_keyword_const || token->kind == TokenKind_keyword_static) {
- return TRUE;
+ token->kind == TokenKind_keyword_void || token->kind == TokenKind_keyword_bool ||
+ token->kind == TokenKind_keyword_enum || token->kind == TokenKind_keyword_struct ||
+ token->kind == TokenKind_keyword_union || token->kind == TokenKind_keyword_const ||
+ token->kind == TokenKind_keyword_static) {
+ return true;
}
if (token->kind != TokenKind_ident) {
- return FALSE;
+ return false;
}
return find_typedef(p, token->value.string) != -1;
}
@@ -956,7 +961,7 @@ static AstNode* parse_parameter_list(Parser* p) {
static AstNode* parse_parameter_type_list(Parser* p) {
AstNode* params = parse_parameter_list(p);
- BOOL has_void = FALSE;
+ bool has_void = false;
for (int i = 0; i < params->node_len; ++i) {
if (params->node_items[i].name && strcmp(params->node_items[i].name, "...") == 0) {
if (i != params->node_len - 1) {
@@ -1096,7 +1101,7 @@ static AstNode* parse_var_decl(Parser* p) {
// TODO: use name's location.
fatal_error("%s:%d: '%s' redeclared", peek_token(p)->loc.filename, peek_token(p)->loc.line, decl->name);
}
- int stack_offset = add_lvar(p, decl->name, decl->ty, FALSE);
+ int stack_offset = add_lvar(p, decl->name, decl->ty, false);
if (decl->node_init) {
AstNode* lhs = ast_new(AstNodeKind_lvar);
@@ -1247,7 +1252,7 @@ static AstNode* parse_stmt(Parser* p) {
static void register_params(Parser* p, AstNode* params) {
for (int i = 0; i < params->node_len; ++i) {
AstNode* param = params->node_items + i;
- add_lvar(p, param->name, param->ty, TRUE);
+ add_lvar(p, param->name, param->ty, true);
}
}
@@ -1658,65 +1663,66 @@ typedef enum {
} TypeSpecifierMask;
static Type* distinguish_type_from_type_specifiers(int type_specifiers) {
- if (type_specifiers == TypeSpecifierMask_void) {
+ if (type_specifiers == TypeSpecifierMask_void)
return type_new(TypeKind_void);
- } else if (type_specifiers == TypeSpecifierMask_char) {
+ else if (type_specifiers == TypeSpecifierMask_char)
return type_new(TypeKind_char);
- } else if (type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_char)) {
+ else if (type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_char))
return type_new(TypeKind_schar);
- } else if (type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_char)) {
+ else if (type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_char))
return type_new(TypeKind_uchar);
- } else if (type_specifiers == TypeSpecifierMask_short ||
- type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_short) ||
- type_specifiers == (TypeSpecifierMask_short + TypeSpecifierMask_int) ||
- type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_short + TypeSpecifierMask_int)) {
+ else if (type_specifiers == TypeSpecifierMask_short ||
+ type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_short) ||
+ type_specifiers == (TypeSpecifierMask_short + TypeSpecifierMask_int) ||
+ type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_short + TypeSpecifierMask_int))
return type_new(TypeKind_short);
- } else if (type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_short) ||
- type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_short + TypeSpecifierMask_int)) {
+ else if (type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_short) ||
+ type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_short + TypeSpecifierMask_int))
return type_new(TypeKind_ushort);
- } else if (type_specifiers == TypeSpecifierMask_int || type_specifiers == TypeSpecifierMask_signed ||
- type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_int)) {
+ else if (type_specifiers == TypeSpecifierMask_int || type_specifiers == TypeSpecifierMask_signed ||
+ type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_int))
return type_new(TypeKind_int);
- } else if (type_specifiers == TypeSpecifierMask_unsigned ||
- type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_int)) {
+ else if (type_specifiers == TypeSpecifierMask_unsigned ||
+ type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_int))
return type_new(TypeKind_uint);
- } else if (type_specifiers == TypeSpecifierMask_long ||
- type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_long) ||
- type_specifiers == (TypeSpecifierMask_long + TypeSpecifierMask_int) ||
- type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_long + TypeSpecifierMask_int)) {
+ else if (type_specifiers == TypeSpecifierMask_long ||
+ type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_long) ||
+ type_specifiers == (TypeSpecifierMask_long + TypeSpecifierMask_int) ||
+ type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_long + TypeSpecifierMask_int))
return type_new(TypeKind_long);
- } else if (type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_long) ||
- type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_long + TypeSpecifierMask_int)) {
+ else if (type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_long) ||
+ type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_long + TypeSpecifierMask_int))
return type_new(TypeKind_ulong);
- } else if (type_specifiers == (TypeSpecifierMask_long + TypeSpecifierMask_long) ||
- type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_long + TypeSpecifierMask_long) ||
- type_specifiers == (TypeSpecifierMask_long + TypeSpecifierMask_long + TypeSpecifierMask_int) ||
- type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_long + TypeSpecifierMask_long +
- TypeSpecifierMask_int)) {
+ else if (type_specifiers == (TypeSpecifierMask_long + TypeSpecifierMask_long) ||
+ type_specifiers == (TypeSpecifierMask_signed + TypeSpecifierMask_long + TypeSpecifierMask_long) ||
+ type_specifiers == (TypeSpecifierMask_long + TypeSpecifierMask_long + TypeSpecifierMask_int) ||
+ type_specifiers ==
+ (TypeSpecifierMask_signed + TypeSpecifierMask_long + TypeSpecifierMask_long + TypeSpecifierMask_int))
return type_new(TypeKind_llong);
- } else if (type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_long + TypeSpecifierMask_long) ||
- type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_long + TypeSpecifierMask_long +
- TypeSpecifierMask_int)) {
+ else if (type_specifiers == (TypeSpecifierMask_unsigned + TypeSpecifierMask_long + TypeSpecifierMask_long) ||
+ type_specifiers ==
+ (TypeSpecifierMask_unsigned + TypeSpecifierMask_long + TypeSpecifierMask_long + TypeSpecifierMask_int))
return type_new(TypeKind_ullong);
- } else if (type_specifiers == TypeSpecifierMask_float) {
+ else if (type_specifiers == TypeSpecifierMask_float)
return type_new(TypeKind_float);
- } else if (type_specifiers == TypeSpecifierMask_double) {
+ else if (type_specifiers == TypeSpecifierMask_double)
return type_new(TypeKind_double);
- } else if (type_specifiers == TypeSpecifierMask_long + TypeSpecifierMask_double) {
+ else if (type_specifiers == TypeSpecifierMask_long + TypeSpecifierMask_double)
return type_new(TypeKind_ldouble);
- } else if (type_specifiers == TypeSpecifierMask_struct) {
+ else if (type_specifiers == TypeSpecifierMask_bool)
+ return type_new(TypeKind_bool);
+ else if (type_specifiers == TypeSpecifierMask_struct)
return NULL;
- } else if (type_specifiers == TypeSpecifierMask_union) {
+ else if (type_specifiers == TypeSpecifierMask_union)
return NULL;
- } else if (type_specifiers == TypeSpecifierMask_enum) {
+ else if (type_specifiers == TypeSpecifierMask_enum)
return NULL;
- } else if (type_specifiers == TypeSpecifierMask_typedef_name) {
+ else if (type_specifiers == TypeSpecifierMask_typedef_name)
return NULL;
- } else if (type_specifiers == 0) {
+ else if (type_specifiers == 0)
fatal_error("no type specifiers");
- } else {
+ else
unimplemented();
- }
}
// declaration-specifiers:
@@ -2280,7 +2286,7 @@ static int eval(AstNode* e) {
}
}
-BOOL pp_eval_constant_expression(TokenArray* pp_tokens) {
+bool pp_eval_constant_expression(TokenArray* pp_tokens) {
TokenArray* tokens = tokenize(pp_tokens);
Parser* p = parser_new(tokens);
AstNode* e = parse_constant_expression(p);
diff --git a/src/parse.h b/src/parse.h
index fe67b75..bda2999 100644
--- a/src/parse.h
+++ b/src/parse.h
@@ -5,6 +5,6 @@
#include "preprocess.h"
Program* parse(TokenArray* tokens);
-BOOL pp_eval_constant_expression(TokenArray* pp_tokens);
+bool pp_eval_constant_expression(TokenArray* pp_tokens);
#endif
diff --git a/src/preprocess.c b/src/preprocess.c
index 2fd334f..005a679 100644
--- a/src/preprocess.c
+++ b/src/preprocess.c
@@ -161,8 +161,8 @@ void macroargs_build_json(JsonBuilder* builder, MacroArgArray* macroargs) {
typedef struct {
InFile* src;
- BOOL at_bol;
- BOOL expect_header_name;
+ bool at_bol;
+ bool expect_header_name;
TokenArray* pp_tokens;
} PpLexer;
@@ -170,8 +170,8 @@ static PpLexer* pplexer_new(InFile* src) {
PpLexer* ppl = calloc(1, sizeof(PpLexer));
ppl->src = src;
- ppl->at_bol = TRUE;
- ppl->expect_header_name = FALSE;
+ ppl->at_bol = true;
+ ppl->expect_header_name = false;
ppl->pp_tokens = calloc(1, sizeof(TokenArray));
tokens_init(ppl->pp_tokens, 1024 * 16);
@@ -225,7 +225,7 @@ static void pplexer_tokenize_pp_directive(PpLexer* ppl, Token* tok) {
} else if (strcmp(pp_directive_name, "ifndef") == 0) {
tok->kind = TokenKind_pp_directive_ifndef;
} else if (strcmp(pp_directive_name, "include") == 0) {
- ppl->expect_header_name = TRUE;
+ ppl->expect_header_name = true;
tok->kind = TokenKind_pp_directive_include;
} else if (strcmp(pp_directive_name, "line") == 0) {
tok->kind = TokenKind_pp_directive_line;
@@ -267,7 +267,7 @@ static void pplexer_tokenize_all(PpLexer* ppl) {
infile_next_char(ppl->src);
tok->kind = TokenKind_header_name;
tok->value.string = builder.buf;
- ppl->expect_header_name = FALSE;
+ ppl->expect_header_name = false;
} else if (ppl->expect_header_name && c == '<') {
infile_next_char(ppl->src);
StrBuilder builder;
@@ -284,7 +284,7 @@ static void pplexer_tokenize_all(PpLexer* ppl) {
infile_next_char(ppl->src);
tok->kind = TokenKind_header_name;
tok->value.string = builder.buf;
- ppl->expect_header_name = FALSE;
+ ppl->expect_header_name = false;
} else if (c == '(') {
infile_next_char(ppl->src);
tok->kind = TokenKind_paren_l;
@@ -622,7 +622,7 @@ static Token* expect_pp_token(Preprocessor* pp, TokenKind expected) {
token_stringify(tok));
}
-static BOOL pp_eof(Preprocessor* pp) {
+static bool pp_eof(Preprocessor* pp) {
return peek_pp_token(pp)->kind == TokenKind_eof;
}
@@ -651,7 +651,7 @@ static void skip_whitespaces(Preprocessor* pp) {
;
}
-static void skip_whitespaces_or_newlines(Preprocessor* pp, BOOL skip_newline) {
+static void skip_whitespaces_or_newlines(Preprocessor* pp, bool skip_newline) {
while (!pp_eof(pp) && (consume_pp_token_if(pp, TokenKind_whitespace) ||
(skip_newline && consume_pp_token_if(pp, TokenKind_newline))))
;
@@ -777,7 +777,7 @@ static TokenArray* pp_parse_macro_parameters(Preprocessor* pp) {
// ws ::= many0(<whitespace>)
// macro-arguments ::= <ws> '(' <ws> opt(<any-token> <ws> many0(',' <ws> <any-token> <ws>)) ')'
-static MacroArgArray* pp_parse_macro_arguments(Preprocessor* pp, BOOL skip_newline) {
+static MacroArgArray* pp_parse_macro_arguments(Preprocessor* pp, bool skip_newline) {
MacroArgArray* args = macroargs_new();
skip_whitespaces_or_newlines(pp, skip_newline);
@@ -793,7 +793,7 @@ static MacroArgArray* pp_parse_macro_arguments(Preprocessor* pp, BOOL skip_newli
// Parse argument tokens, handling nested parentheses.
int nesting = 0;
- while (TRUE) {
+ while (true) {
tok = peek_pp_token(pp);
if (nesting == 0) {
@@ -872,12 +872,12 @@ static Token* concat_two_tokens(Token* left, Token* right) {
return result;
}
-static BOOL expand_macro(Preprocessor* pp, BOOL skip_newline) {
+static bool expand_macro(Preprocessor* pp, bool skip_newline) {
int macro_name_pos = pp->pos;
Token* macro_name = peek_pp_token(pp);
int macro_idx = find_macro(pp, macro_name->value.string);
if (macro_idx == -1) {
- return FALSE;
+ return false;
}
SourceLocation original_loc = macro_name->loc;
@@ -959,7 +959,7 @@ static BOOL expand_macro(Preprocessor* pp, BOOL skip_newline) {
} else {
unreachable();
}
- return TRUE;
+ return true;
}
typedef enum {
@@ -968,7 +968,7 @@ typedef enum {
GroupDelimiterKind_after_else_directive,
} GroupDelimiterKind;
-static BOOL is_delimiter_of_current_group(GroupDelimiterKind delimiter_kind, TokenKind token_kind) {
+static bool is_delimiter_of_current_group(GroupDelimiterKind delimiter_kind, TokenKind token_kind) {
if (delimiter_kind == GroupDelimiterKind_normal) {
return token_kind == TokenKind_eof;
} else if (delimiter_kind == GroupDelimiterKind_after_if_directive) {
@@ -983,9 +983,9 @@ static BOOL is_delimiter_of_current_group(GroupDelimiterKind delimiter_kind, Tok
}
static int replace_pp_tokens(Preprocessor*, int, int, TokenArray*);
-static void include_conditionally(Preprocessor* pp, GroupDelimiterKind delimiter_kind, BOOL do_include);
+static void include_conditionally(Preprocessor* pp, GroupDelimiterKind delimiter_kind, bool do_include);
-static BOOL preprocess_if_group_or_elif_group(Preprocessor* pp, BOOL did_include) {
+static bool preprocess_if_group_or_elif_group(Preprocessor* pp, bool did_include) {
Token* directive = next_pp_token(pp);
if (directive->kind == TokenKind_pp_directive_if || directive->kind == TokenKind_pp_directive_elif) {
@@ -1011,7 +1011,7 @@ static BOOL preprocess_if_group_or_elif_group(Preprocessor* pp, BOOL did_include
} else {
macro_name = expect_pp_token(pp, TokenKind_ident);
}
- BOOL is_defined = find_macro(pp, macro_name->value.string) != -1;
+ bool is_defined = find_macro(pp, macro_name->value.string) != -1;
TokenArray defined_results;
tokens_init(&defined_results, 1);
Token* defined_result = tokens_push_new(&defined_results);
@@ -1019,7 +1019,7 @@ static BOOL preprocess_if_group_or_elif_group(Preprocessor* pp, BOOL did_include
defined_result->value.integer = is_defined;
pp->pos = replace_pp_tokens(pp, defined_pos, pp->pos, &defined_results);
} else {
- BOOL expanded = expand_macro(pp, FALSE);
+ bool expanded = expand_macro(pp, false);
if (expanded) {
// A macro may expand to another macro. Re-scan the expanded tokens.
// TODO: if the macro is defined recursively, it causes infinite loop.
@@ -1038,7 +1038,7 @@ static BOOL preprocess_if_group_or_elif_group(Preprocessor* pp, BOOL did_include
for (int pos = condition_expression_start_pos; pos < pp->pos; ++pos) {
Token* tok = pp_token_at(pp, pos);
if (tok->kind == TokenKind_ident) {
- BOOL is_true = strcmp(tok->value.string, "true") == 0;
+ bool is_true = strcmp(tok->value.string, "true") == 0;
tok->kind = TokenKind_literal_int;
tok->value.integer = is_true;
}
@@ -1054,7 +1054,7 @@ static BOOL preprocess_if_group_or_elif_group(Preprocessor* pp, BOOL did_include
Token* eof_tok = tokens_push_new(&condition_expression_tokens);
eof_tok->kind = TokenKind_eof;
- BOOL do_include = pp_eval_constant_expression(&condition_expression_tokens) && !did_include;
+ bool do_include = pp_eval_constant_expression(&condition_expression_tokens) && !did_include;
include_conditionally(pp, GroupDelimiterKind_after_if_directive, do_include);
return do_include;
} else if (directive->kind == TokenKind_pp_directive_ifdef || directive->kind == TokenKind_pp_directive_elifdef) {
@@ -1064,7 +1064,7 @@ static BOOL preprocess_if_group_or_elif_group(Preprocessor* pp, BOOL did_include
fatal_error("");
}
- BOOL do_include = !did_include && find_macro(pp, macro_name->value.string) != -1;
+ bool do_include = !did_include && find_macro(pp, macro_name->value.string) != -1;
include_conditionally(pp, GroupDelimiterKind_after_if_directive, do_include);
return do_include;
} else if (directive->kind == TokenKind_pp_directive_ifndef || directive->kind == TokenKind_pp_directive_elifndef) {
@@ -1074,7 +1074,7 @@ static BOOL preprocess_if_group_or_elif_group(Preprocessor* pp, BOOL did_include
fatal_error("");
}
- BOOL do_include = !did_include && find_macro(pp, macro_name->value.string) == -1;
+ bool do_include = !did_include && find_macro(pp, macro_name->value.string) == -1;
include_conditionally(pp, GroupDelimiterKind_after_if_directive, do_include);
return do_include;
} else {
@@ -1082,25 +1082,25 @@ static BOOL preprocess_if_group_or_elif_group(Preprocessor* pp, BOOL did_include
}
}
-static BOOL preprocess_if_group(Preprocessor* pp) {
- return preprocess_if_group_or_elif_group(pp, FALSE);
+static bool preprocess_if_group(Preprocessor* pp) {
+ return preprocess_if_group_or_elif_group(pp, false);
}
-static BOOL preprocess_elif_group(Preprocessor* pp, BOOL did_include) {
+static bool preprocess_elif_group(Preprocessor* pp, bool did_include) {
return preprocess_if_group_or_elif_group(pp, did_include);
}
// elif-groups:
// { elif-group }+
-static BOOL preprocess_elif_groups_opt(Preprocessor* pp, BOOL did_include) {
+static bool preprocess_elif_groups_opt(Preprocessor* pp, bool did_include) {
while (!pp_eof(pp)) {
Token* tok = peek_pp_token(pp);
if (tok->kind == TokenKind_pp_directive_elif || tok->kind == TokenKind_pp_directive_elifdef ||
tok->kind == TokenKind_pp_directive_elifndef) {
// TODO: | and |= is not supported
// did_include |= preprocess_elif_group(pp, pp->pos, did_include);
- BOOL a = preprocess_elif_group(pp, did_include);
- did_include = did_include ? TRUE : a;
+ bool a = preprocess_elif_group(pp, did_include);
+ did_include = did_include ? true : a;
} else {
break;
}
@@ -1110,7 +1110,7 @@ static BOOL preprocess_elif_groups_opt(Preprocessor* pp, BOOL did_include) {
// else-group:
// '#' 'else' group?
-static void preprocess_else_group(Preprocessor* pp, BOOL did_include) {
+static void preprocess_else_group(Preprocessor* pp, bool did_include) {
skip_pp_token(pp, TokenKind_pp_directive_else);
skip_whitespaces(pp);
expect_pp_token(pp, TokenKind_newline);
@@ -1129,7 +1129,7 @@ static void preprocess_endif_directive(Preprocessor* pp) {
// if-section:
// if-group elif-groups? else-group? endif-line
static void preprocess_if_section(Preprocessor* pp) {
- BOOL did_include = preprocess_if_group(pp);
+ bool did_include = preprocess_if_group(pp);
did_include = preprocess_elif_groups_opt(pp, did_include);
if (peek_pp_token(pp)->kind == TokenKind_pp_directive_else) {
preprocess_else_group(pp, did_include);
@@ -1148,10 +1148,10 @@ static void preprocess_include_directive(Preprocessor* pp) {
}
if (include_name->value.string[0] == '"') {
- BOOL already_included = FALSE;
+ bool already_included = false;
for (size_t i = 0; i < pp->included_files->len; ++i) {
if (strcmp(pp->included_files->data[i], include_name_resolved) == 0) {
- already_included = TRUE;
+ already_included = true;
break;
}
}
@@ -1290,7 +1290,7 @@ static void preprocess_text_line(Preprocessor* pp) {
continue;
}
- BOOL expanded = expand_macro(pp, TRUE);
+ bool expanded = expand_macro(pp, true);
if (expanded) {
// A macro may expand to another macro. Re-scan the expanded tokens.
// TODO: if the macro is defined recursively, it causes infinite loop.
@@ -1391,7 +1391,7 @@ static void skip_group_opt(Preprocessor* pp, GroupDelimiterKind delimiter_kind)
expect_pp_token(pp, TokenKind_pp_directive_endif);
}
-static void include_conditionally(Preprocessor* pp, GroupDelimiterKind delimiter_kind, BOOL do_include) {
+static void include_conditionally(Preprocessor* pp, GroupDelimiterKind delimiter_kind, bool do_include) {
if (do_include) {
preprocess_group_opt(pp, delimiter_kind);
} else {
diff --git a/src/std.h b/src/std.h
index 0a40232..d584e1a 100644
--- a/src/std.h
+++ b/src/std.h
@@ -59,8 +59,4 @@ typedef long ssize_t;
ssize_t readlink(const char*, char*, size_t);
char* dirname(char*);
-#define BOOL int
-#define TRUE 1
-#define FALSE 0
-
#endif
diff --git a/src/token.c b/src/token.c
index 82e728f..7b3efbc 100644
--- a/src/token.c
+++ b/src/token.c
@@ -273,7 +273,7 @@ const char* token_kind_stringify(TokenKind k) {
unreachable();
}
-BOOL is_pp_directive(TokenKind k) {
+bool is_pp_directive(TokenKind k) {
return k == TokenKind_pp_directive_define || k == TokenKind_pp_directive_elif ||
k == TokenKind_pp_directive_elifdef || k == TokenKind_pp_directive_elifndef ||
k == TokenKind_pp_directive_else || k == TokenKind_pp_directive_embed || k == TokenKind_pp_directive_endif ||
diff --git a/src/token.h b/src/token.h
index 72db22b..0bf23f6 100644
--- a/src/token.h
+++ b/src/token.h
@@ -146,7 +146,7 @@ typedef enum {
} TokenKind;
const char* token_kind_stringify(TokenKind k);
-BOOL is_pp_directive(TokenKind k);
+bool is_pp_directive(TokenKind k);
// TokenValue is externally tagged by Token's kind.
typedef union {