diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-01-03 02:45:54 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-01-03 02:45:54 -0500 |
commit | 72b20e0c81144adb60f5f342af4ec1cec8a86e9c (patch) | |
tree | 717ac16648ec1d95eba1cda8fb1f4e89f82f0bd0 | |
parent | 5d567155b94b71cb247f13d381fcfedb7a30042c (diff) |
small things for more standard compliance
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | cgen.c | 22 | ||||
-rw-r--r-- | eval.c | 18 | ||||
-rw-r--r-- | main.c | 10 | ||||
-rw-r--r-- | parse.c | 9 | ||||
-rw-r--r-- | test.toc | 7 | ||||
-rwxr-xr-x | tests/test.sh | 2 | ||||
-rw-r--r-- | toc.c | 25 | ||||
-rw-r--r-- | tokenizer.c | 2 | ||||
-rw-r--r-- | types.c | 2 | ||||
-rw-r--r-- | types.h | 9 |
11 files changed, 75 insertions, 35 deletions
@@ -4,4 +4,6 @@ out.c out.top tests/**/*.c tests/**/*.bin -vgcore*
\ No newline at end of file +vgcore* +TAGS +tags @@ -415,7 +415,7 @@ static inline void cgen_fn_name(CGenerator *g, FnExpr *f) { } static inline void cgen_fn_instance_number(CGenerator *g, U64 instance) { - cgen_write(g, "%"PRIu64"_", instance); + cgen_write(g, U64_FMT"_", instance); } /* does this type have a type type in it? (e.g. [5]Type, &&Type) */ @@ -1144,7 +1144,7 @@ static bool cgen_expr(CGenerator *g, Expression *e) { cgen_write(g, "%.16Lf", (long double)e->floatl); /* TODO(eventually): better precision? */ break; case EXPR_LITERAL_INT: - cgen_write(g, "%"PRIu64, e->intl); + cgen_write(g, U64_FMT, e->intl); break; case EXPR_LITERAL_STR: { size_t c; @@ -1404,7 +1404,7 @@ static bool cgen_expr(CGenerator *g, Expression *e) { Value val; if (!eval_expr(g->evalr, e, &val)) return false; - cgen_write(g, "%"PRId64, val.i64); + cgen_write(g, I64_FMT, val.i64); } break; case EXPR_CAST: { Type *from = &e->cast.expr->type; @@ -1697,14 +1697,14 @@ static bool cgen_val_ptr(CGenerator *g, void *v, Type *t, Location where) { return false; case TYPE_BUILTIN: switch (t->builtin) { - case BUILTIN_I8: cgen_write(g, "%"PRId8, *(I8 *)v); break; - case BUILTIN_U8: cgen_write(g, "%"PRIu8, *(U8 *)v); break; - case BUILTIN_I16: cgen_write(g, "%"PRId16, *(I16 *)v); break; - case BUILTIN_U16: cgen_write(g, "%"PRIu16, *(U16 *)v); break; - case BUILTIN_I32: cgen_write(g, "%"PRId32, *(I32 *)v); break; - case BUILTIN_U32: cgen_write(g, "%"PRIu32, *(U32 *)v); break; - case BUILTIN_I64: cgen_write(g, "%"PRId64, *(I64 *)v); break; - case BUILTIN_U64: cgen_write(g, "%"PRIu64, *(U64 *)v); break; + case BUILTIN_I8: cgen_write(g, I8_FMT, *(I8 *)v); break; + case BUILTIN_U8: cgen_write(g, U8_FMT, *(U8 *)v); break; + case BUILTIN_I16: cgen_write(g, I16_FMT, *(I16 *)v); break; + case BUILTIN_U16: cgen_write(g, U16_FMT, *(U16 *)v); break; + case BUILTIN_I32: cgen_write(g, I32_FMT, *(I32 *)v); break; + case BUILTIN_U32: cgen_write(g, U32_FMT, *(U32 *)v); break; + case BUILTIN_I64: cgen_write(g, I64_FMT, *(I64 *)v); break; + case BUILTIN_U64: cgen_write(g, U64_FMT, *(U64 *)v); break; case BUILTIN_F32: cgen_write(g, F32_FMT"f", *(F32 *)v); break; case BUILTIN_F64: cgen_write(g, F64_FMT, *(F64 *)v); break; case BUILTIN_CHAR: cgen_write(g, "'\\x%02x'", *(char *)v); break; @@ -274,17 +274,17 @@ static void fprint_val_ptr(FILE *f, void *p, Type *t) { break; case TYPE_BUILTIN: switch (t->builtin) { - case BUILTIN_I8: fprintf(f, "%"PRId8, *(I8 *)p); break; - case BUILTIN_U8: fprintf(f, "%"PRIu8, *(U8 *)p); break; - case BUILTIN_I16: fprintf(f, "%"PRId16, *(I16 *)p); break; - case BUILTIN_U16: fprintf(f, "%"PRIu16, *(U16 *)p); break; - case BUILTIN_I32: fprintf(f, "%"PRId32, *(I32 *)p); break; - case BUILTIN_U32: fprintf(f, "%"PRIu32, *(U32 *)p); break; - case BUILTIN_I64: fprintf(f, "%"PRId64, *(I64 *)p); break; - case BUILTIN_U64: fprintf(f, "%"PRIu64, *(U64 *)p); break; + case BUILTIN_I8: fprintf(f, I8_FMT, *(I8 *)p); break; + case BUILTIN_U8: fprintf(f, U8_FMT, *(U8 *)p); break; + case BUILTIN_I16: fprintf(f, I16_FMT, *(I16 *)p); break; + case BUILTIN_U16: fprintf(f, U16_FMT, *(U16 *)p); break; + case BUILTIN_I32: fprintf(f, I32_FMT, *(I32 *)p); break; + case BUILTIN_U32: fprintf(f, U32_FMT, *(U32 *)p); break; + case BUILTIN_I64: fprintf(f, I64_FMT, *(I64 *)p); break; + case BUILTIN_U64: fprintf(f, U64_FMT, *(U64 *)p); break; case BUILTIN_F32: fprintf(f, F32_FMT, *(F32 *)p); break; case BUILTIN_F64: fprintf(f, F64_FMT, *(F64 *)p); break; - case BUILTIN_CHAR: fprintf(f, "'%c'", *(char *)p); break; + case BUILTIN_CHAR: fprint_char_literal(f, *(char *)p); break; case BUILTIN_BOOL: fprintf(f, "%s", *(bool *)p ? "true" : "false"); break; } break; @@ -29,12 +29,6 @@ make sure futurely/currently-declared types are only used by pointer/slice allow omission of trailing ; in foo ::= fn() {}? */ -#ifdef __cplusplus -#define new new_ -#define this this_ -#elif __STDC_VERSION__ < 199901 -#define inline -#endif #include "toc.c" @@ -141,8 +135,9 @@ int main(int argc, char **argv) { err_fprint(TEXT_IMPORTANT("Errors occured while determining types.\n")); return EXIT_FAILURE; } +#ifdef TOC_DEBUG fprint_parsed_file(stdout, &f); - +#endif FILE *out = fopen(out_filename, "w"); if (!out) { err_fprint(TEXT_IMPORTANT("Could not open output file (out.c).\n")); @@ -167,5 +162,6 @@ int main(int argc, char **argv) { fclose(out_pkg); #endif idents_free(&file_idents); + return 0; } @@ -234,7 +234,7 @@ static size_t type_to_str_(Type *t, char *buffer, size_t bufsize) { case TYPE_ARR: { size_t written = str_copy(buffer, bufsize, "["); if (t->flags & TYPE_IS_RESOLVED) { - snprintf(buffer + written, bufsize - written, "%"PRIu64, t->arr.n); + snprintf(buffer + written, bufsize - written, U64_FMT, t->arr.n); written += strlen(buffer + written); } else { written += str_copy(buffer + written, bufsize - written, "N"); @@ -2061,7 +2061,7 @@ static void fprint_expr(FILE *out, Expression *e) { fprintf(out, "%s", e->booll ? "true" : "false"); break; case EXPR_LITERAL_CHAR: - fprintf(out, "'%c'", e->charl); + fprint_char_literal(out, e->charl); break; case EXPR_IDENT: fprint_ident(out, e->ident); @@ -2227,6 +2227,11 @@ static void fprint_decl(FILE *out, Declaration *d) { fprintf(out, "="); fprint_expr(out, &d->expr); } + if (d->flags & DECL_FOUND_VAL) { + fprintf(out, "("); + fprint_val(out, d->val, &d->type); + fprintf(out, ")"); + } } static void fprint_stmt(FILE *out, Statement *s) { @@ -1,4 +1,7 @@ -#export foo :: f64 = 0.07321; +// #export +foo :: f64 = 0.07321; // asdf, dsajkhf, sadjkfh ::= 5; // #export asdf ::= "asdfasdfasdf"; -#export zla, asdf := foo, 9;
\ No newline at end of file +// #export +zla, asdf := foo, 9; +asfdsfad ::= '\n';
\ No newline at end of file diff --git a/tests/test.sh b/tests/test.sh index 88318c6..d0fc3cb 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -13,7 +13,7 @@ compile() { } do_tests() { - valgrind -q --exit-on-first-error=yes --error-exitcode=1 $TOC "$DIR/$1/$1.toc" -o "$DIR/$1/$1.c" > /dev/null || exit 1 + valgrind -q --exit-on-first-error=yes --error-exitcode=1 $TOC "$DIR/$1/$1.toc" -o "$DIR/$1/$1.c" >/dev/null || exit 1 for CC in $COMPILERS; do for EXTRA_CFLAGS in "-O0 -g" "-O3 -s"; do @@ -18,11 +18,27 @@ #include <float.h> #include <inttypes.h> + +#ifdef __cplusplus +#define new new_ +#define this this_ +#elif __STDC_VERSION__ < 199901 +#define inline +#endif + #include "types.h" /* forward declarations for debugging */ static void print_val(Value v, Type *t); +static void fprint_char_literal(FILE *f, char c) { + if (isprint(c)) + fprintf(f, "'%c'", c); + else + fprintf(f, "'\\x%02x'", c); +} + + /* utilities */ #include "allocator.c" #include "arr.c" @@ -52,3 +68,12 @@ static bool typedefs_file(CGenerator *g, ParsedFile *f); #ifdef TOC_DEBUG #include "tests.c" #endif + + + +#ifdef __cplusplus +#undef new +#undef this +#elif __STDC_VERSION__ < 199901 +#undef inline +#endif diff --git a/tokenizer.c b/tokenizer.c index dfa221a..f841d90 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -94,7 +94,7 @@ static void fprint_token(FILE *out, Token *t) { fprintf(out, "number: "); switch (t->num.kind) { case NUM_LITERAL_INT: - fprintf(out, "%"PRIu64, t->num.intval); + fprintf(out, U64_FMT, t->num.intval); break; case NUM_LITERAL_FLOAT: fprintf(out, "%g", (double)t->num.floatval); @@ -473,7 +473,7 @@ static bool type_resolve(Typer *tr, Type *t, Location where) { if (type_builtin_is_signed(n_expr->type.builtin)) { I64 ssize = val_to_i64(&val, n_expr->type.builtin); if (ssize < 0) { - err_print(t->arr.n_expr->where, "Negative array length (%" PRId64 ")", ssize); + err_print(t->arr.n_expr->where, "Negative array length (" I64_FMT ")", ssize); return false; } size = (U64)ssize; @@ -30,9 +30,14 @@ typedef uint32_t U32; #define U32_MAX UINT32_MAX typedef uint64_t U64; #define U64_MAX UINT64_MAX +#define U8_FMT "%" PRIu8 +#define U16_FMT "%" PRIu16 +#define U32_FMT "%" PRIu32 +#define U64_FMT "%" PRIu64 #if __STDC_VERSION__ >= 199901 #include <stdbool.h> +#elif defined __cplusplus #else typedef U8 bool; #endif @@ -46,6 +51,10 @@ typedef int32_t I32; #define I32_MAX INT32_MAX typedef int64_t I64; #define I64_MAX INT64_MAX +#define I8_FMT "%" PRId8 +#define I16_FMT "%" PRId16 +#define I32_FMT "%" PRId32 +#define I64_FMT "%" PRId64 /* NOTE: if you change these, make sure you change hash_tables.c */ typedef float F32; |