snprintf now working! (fixed problem will calls inside calls)

This commit is contained in:
pommicket 2022-02-14 11:39:33 -05:00
parent 1f9534d750
commit f7a8a193c2
5 changed files with 44 additions and 8 deletions

View file

@ -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)

View file

@ -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;
}

View file

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

View file

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

View file

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