summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-02-14 11:39:33 -0500
committerpommicket <pommicket@gmail.com>2022-02-14 11:39:33 -0500
commitf7a8a193c2e523b2e670d385244cd2d830453938 (patch)
tree4ab6c5a6620a7d4c9a1d2d8b44d06135d2f095fa
parent1f9534d750d6edb2af39ff9da544ae129ba9a146 (diff)
snprintf now working! (fixed problem will calls inside calls)
-rw-r--r--05/codegen.b4
-rw-r--r--05/main.c8
-rw-r--r--05/parse.b9
-rw-r--r--05/stdc_common.h6
-rw-r--r--05/stdio.h25
5 files changed, 44 insertions, 8 deletions
diff --git a/05/codegen.b b/05/codegen.b
index cd70a22..0220450 100644
--- a/05/codegen.b
+++ b/05/codegen.b
@@ -2432,9 +2432,8 @@ function generate_push_expression
return expr
:generate_call
expr += 8
- global 4000 expr_arg_ptrs_dat
local expr_arg_ptrs
- expr_arg_ptrs = &expr_arg_ptrs_dat
+ expr_arg_ptrs = malloc(4000)
local arg_idx
local call_function
local return_val_size
@@ -2475,6 +2474,7 @@ function generate_push_expression
arg_idx -= 1
goto push_args_loop
:push_args_loop_end
+ free(expr_arg_ptrs)
; create space on stack for return value
emit_sub_rsp_imm32(return_val_size)
diff --git a/05/main.c b/05/main.c
index 7503a10..5a25d19 100644
--- a/05/main.c
+++ b/05/main.c
@@ -2,9 +2,9 @@
int main(int argc, char **argv) {
char buf[200] = {0};
- sprintf(buf, "Hello, %d %.2f %g %s %p\n", 187, 77.3, 349e12, "Wow!", "yea");
- // snprintf(buf, 200, "Hello\n"); //<- NOT WORKING
- write(1, buf, sizeof buf);
- return *buf;
+ snprintf(buf, sizeof buf, "Hello, %d %.2f %g %s %p\n", 187, 77.3, 349e12, "Wow!", "yea");
+/* write(1, buf, sizeof buf); */
+ printf("%s\n",buf);
+ return 0;
}
diff --git a/05/parse.b b/05/parse.b
index 93de88e..15851d9 100644
--- a/05/parse.b
+++ b/05/parse.b
@@ -522,7 +522,11 @@ function parse_statement
token += 16
out += 8
*8out = expressions_end
+ c = expressions_end + 4
expressions_end = parse_expression(token, p, expressions_end)
+ c = types + *4c
+ if *1c > TYPE_UNSIGNED_LONG goto bad_switch_type
+
token = p + 16
out += 8
@@ -542,6 +546,11 @@ function parse_statement
:str_switch_no_lparen
string No ( after switch.
byte 0
+ :bad_switch_type
+ token_error(token, .str_bad_switch_type)
+ :str_bad_switch_type
+ string The expression in a switch statement must have an integer type.
+ byte 0
:stmt_while
write_statement_header(out, STATEMENT_WHILE, token)
token += 16
diff --git a/05/stdc_common.h b/05/stdc_common.h
index ad56b4d..16ddcdb 100644
--- a/05/stdc_common.h
+++ b/05/stdc_common.h
@@ -63,4 +63,10 @@ long write(int fd, void *buf, size_t count) {
__syscall(1, fd, buf, count, 0, 0, 0);
}
+size_t strlen(char *s) {
+ char *t = s;
+ while (*t) ++t;
+ return t - s;
+}
+
#endif // _STDC_COMMON_H
diff --git a/05/stdio.h b/05/stdio.h
index bd1c8aa..91f1169 100644
--- a/05/stdio.h
+++ b/05/stdio.h
@@ -1,6 +1,11 @@
+#ifndef _STDIO_H
+#define _STDIO_H
+
#include <stdc_common.h>
#include <stdarg.h>
+int printf(const char *, ...);
+
/* --- snprintf, adapted from github.com/nothings/stb stb_sprintf.h */
#ifndef STB_SPRINTF_MIN
@@ -1113,6 +1118,7 @@ typedef struct stbsp__context {
static char *stbsp__clamp_callback(const char *buf, void *user, int len)
{
stbsp__context *c = (stbsp__context *)user;
+
c->length += len;
if (len > c->count)
@@ -1150,7 +1156,6 @@ char * stbsp__count_clamp_callback( const char * buf, void * user, int len )
int vsnprintf( char * buf, int count, char const * fmt, va_list va )
{
stbsp__context c;
-
if ( (count == 0) && !buf )
{
c.length = 0;
@@ -1165,7 +1170,7 @@ int vsnprintf( char * buf, int count, char const * fmt, va_list va )
c.count = count;
c.length = 0;
- STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__clamp_callback, &c, stbsp__clamp_callback(0,&c,0), fmt, va );
+ __vsprintfcb( stbsp__clamp_callback, &c, stbsp__clamp_callback(0,&c,0), fmt, va );
// zero-terminate
l = (int)( c.buf - buf );
@@ -1545,3 +1550,19 @@ static int32_t stbsp__real_to_str(char const **start, uint32_t *len, char *out,
#undef stbsp__ddmultlos
#undef STBSP__SPECIAL
#undef STBSP__COPYFP
+
+int __fd_puts(int fd, const char *s) {
+ return write(fd, s, strlen(s));
+}
+
+int printf(const char *fmt, ...) {
+ // @TODO: use a callback with __vsnprintfcb
+ va_list args;
+ char buf[2000];
+ va_start(args, fmt);
+ vsprintf(buf, fmt, args);
+ va_end(args);
+ return __fd_puts(1, buf);
+}
+
+#endif // _STDIO_H