Skip to content

Commit 444e3bb

Browse files
committed
Add builtin type _Bool and tweak lexer aliasing
The size of type _Bool is same as char type, which takes 1 byte. Notice that currently lexer aliasing is unreliable due to the token type determination algorithm does not correctly recognize T_identifier and T_string at this moment.
1 parent 9860f84 commit 444e3bb

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/lexer.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ bool is_hex(char c)
130130

131131
bool is_numeric(char buffer[])
132132
{
133-
int hex = 0, size = strlen(buffer);
133+
bool hex = false;
134+
int size = strlen(buffer);
134135

135136
if (size > 2)
136137
hex = buffer[0] == '0' && buffer[1] == 'x';
@@ -518,7 +519,8 @@ token_t lex_token_internal(bool aliasing)
518519
if (aliasing) {
519520
alias = find_alias(token_str);
520521
if (alias) {
521-
token_t t = is_numeric(alias) ? T_numeric : T_string;
522+
/* TODO: Need more reliable way to identify the token's type */
523+
token_t t = is_numeric(alias) ? T_numeric : T_identifier;
522524
strcpy(token_str, alias);
523525
return t;
524526
}

src/parser.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,6 +2875,14 @@ void parse_internal()
28752875
type->base_type = TYPE_int;
28762876
type->size = 4;
28772877

2878+
/* builtin type _Bool was introduced in C99 specification, it is more well-known
2879+
* as macro type bool, which is defined in <std_bool.h> (in shecc, it is defined
2880+
* in 'lib/c.c').
2881+
*/
2882+
type = add_named_type("_Bool");
2883+
type->base_type = TYPE_char;
2884+
type->size = 1;
2885+
28782886
add_block(NULL, NULL, NULL); /* global block */
28792887
elf_add_symbol("", 0, 0); /* undef symbol */
28802888

tests/driver.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,4 +592,13 @@ int main()
592592
}
593593
EOF
594594

595+
# _Bool size should be equivalent to char, which is 1 byte
596+
try_output 0 "1" << EOF
597+
int main()
598+
{
599+
printf("%d", sizeof(bool));
600+
return 0;
601+
}
602+
EOF
603+
595604
echo OK

0 commit comments

Comments
 (0)