Skip to content
This repository has been archived by the owner on Aug 22, 2018. It is now read-only.

Commit

Permalink
Add floating constant
Browse files Browse the repository at this point in the history
  • Loading branch information
ShinyaKato committed Aug 17, 2018
1 parent 7d2a085 commit cdfdd69
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
5 changes: 3 additions & 2 deletions cc.h
Expand Up @@ -34,6 +34,7 @@ FILE *fopen(char *filename, char *modes);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
void exit(int status);
double strtod(char *s, char *endpoint);

int strcmp(char *s1, char *s2);

Expand Down Expand Up @@ -146,7 +147,7 @@ typedef enum token_type {
typedef struct token {
TokenType type;
int int_value;
char *float_pattern;
double double_value;
String *string_value;
char *identifier;
} Token;
Expand Down Expand Up @@ -278,7 +279,7 @@ typedef struct node {
enum node_type type;
Type *value_type;
int int_value;
char *float_pattern;
double double_value;
String *string_value;
int string_label, float_label;
char *identifier;
Expand Down
3 changes: 2 additions & 1 deletion gen.c
Expand Up @@ -100,7 +100,8 @@ void gen_int_const(Node *node) {
}

void gen_float_const(Node *node) {
printf(" movq $%s, %%rax\n", node->float_pattern);
int *d = (int *) &node->double_value;
printf(" movq $0x%08x%08x, %%rax\n", d[1], d[0]);
gen_push("rax");
}

Expand Down
26 changes: 13 additions & 13 deletions lex.c
Expand Up @@ -67,24 +67,24 @@ Token *lex() {
Token *token = token_new();

if (isdigit(peek_char())) {
int int_value = 0;
String *s = string_new();
while (isdigit(peek_char())) {
int_value = int_value * 10 + (get_char() - '0');
string_push(s, get_char());
}
if (!read_char('.')) {
token->type = tINT_CONST;
token->int_value = int_value;
} else {
int float_value = 0;
if (peek_char() == '.') {
string_push(s, get_char());
while (isdigit(peek_char())) {
float_value = float_value * 10 + (get_char() - '0');
string_push(s, get_char());
}
if (int_value == 123 && float_value == 456) {
token->type = tFLOAT_CONST;
token->float_pattern = "4638387859538109000";
} else {
error("unexpected foating constant.");
token->type = tFLOAT_CONST;
token->double_value = strtod(s->buffer, NULL);
} else {
int int_value = 0;
for (int i = 0; i < s->length; i++) {
int_value = int_value * 10 + (s->buffer[i] - '0');
}
token->type = tINT_CONST;
token->int_value = int_value;
}
} else if (read_char('\'')) {
int int_value;
Expand Down
2 changes: 1 addition & 1 deletion parse.c
Expand Up @@ -158,7 +158,7 @@ Node *primary_expression() {
if (token->type == tFLOAT_CONST) {
Node *node = node_new();
node->type = FLOAT_CONST;
node->float_pattern = token->float_pattern;
node->double_value = token->double_value;
return node;
}

Expand Down
1 change: 1 addition & 0 deletions tests/test.sh
Expand Up @@ -227,6 +227,7 @@ test_stdout "int main() { printf(\"%7.3f\n\", 123.456); }" "123.456"
test_stdout "int main() { double d = 123.456; printf(\"%7.3f\n\", d); }" "123.456"
test_stdout "int main() { double d[2] = { 123.456, 123.456 }; printf(\"%7.3f\n\", d[1]); }" "123.456"
test_stdout "double test_double(double d); int main() { printf(\"%7.3f\n\", test_double(123.456)); }" "246.912"
test_stdout "int main() { double d = 13211.5673; printf(\"%.4f\n\", d); }" "13211.5673"

test_error "int main() { 2 * (3 + 4; }" "tRPAREN is expected."
test_error "int main() { 5 + *; }" "unexpected primary expression."
Expand Down

0 comments on commit cdfdd69

Please sign in to comment.