Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Add floating constant

  • Loading branch information...
ShinyaKato committed Aug 17, 2018
1 parent 7d2a085 commit cdfdd69c3c64be710c3d2f3a00f3adcc68d04605
Showing with 20 additions and 17 deletions.
  1. +3 −2 cc.h
  2. +2 −1 gen.c
  3. +13 −13 lex.c
  4. +1 −1 parse.c
  5. +1 −0 tests/test.sh
5 cc.h
@@ -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);

@@ -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;
@@ -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;
3 gen.c
@@ -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");
}

26 lex.c
@@ -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;
@@ -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;
}

@@ -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."

0 comments on commit cdfdd69

Please sign in to comment.
You can’t perform that action at this time.