diff --git a/parse.c b/parse.c index dbd43b3..9832124 100644 --- a/parse.c +++ b/parse.c @@ -25,7 +25,7 @@ void ParseScan(const Str *FileName, const Str *Source) while ( (Token = LexGetToken(&Lexer)) != TokenEOF) { /* do parsey things here */ - printf("token %d\n", (int)Token); + StrPrintf("token %d\n", (int)Token); } } diff --git a/picoc.c b/picoc.c index 69ffbaa..0919bb7 100644 --- a/picoc.c +++ b/picoc.c @@ -14,7 +14,7 @@ void Fail(const char *Message, ...) va_list Args; va_start(Args, Message); - vprintf(Message, Args); + vStrPrintf(Message, Args); exit(1); } @@ -24,8 +24,8 @@ void ProgramError(const Str *FileName, int Line, const char *Message, ...) StrPrintf("%S:%d: ", FileName, Line); va_start(Args, Message); - vprintf(Message, Args); - printf("\n"); + vStrPrintf(Message, Args); + StrPrintf("\n"); exit(1); } @@ -73,8 +73,6 @@ int main(int argc, char **argv) Str FileName; Str StartFunc; - printf("picoc\n"); - if (argc < 2) Fail("Format: picoc ...\n"); diff --git a/picoc.h b/picoc.h index 8b91ae3..157ed79 100644 --- a/picoc.h +++ b/picoc.h @@ -1,9 +1,12 @@ #ifndef PICOC_H #define PICOC_H +#include + /* configurable options */ #define USE_MALLOC #define GLOBAL_TABLE_SIZE 199 +#define LARGE_INT_POWER_OF_TEN 1000000000 /* the largest power of ten which fits in an int on this architecture */ /* handy definitions */ #ifndef TRUE @@ -121,6 +124,7 @@ void StrFromC(Str *Dest, const char *Source); int StrEqual(const Str *Str1, const Str *Str2); int StrEqualC(const Str *Str1, const char *Str2); void StrPrintf(const char *Format, ...); +void vStrPrintf(const char *Format, va_list Args); /* picoc.c */ void Fail(const char *Message, ...); diff --git a/str.c b/str.c index c8b8bcb..9005341 100644 --- a/str.c +++ b/str.c @@ -13,14 +13,12 @@ void StrToC(char *Dest, int DestSize, const Str *Source) Dest[CopyLen] = '\0'; } - void StrFromC(Str *Dest, const char *Source) { Dest->Str = Source; Dest->Len = strlen(Source); } - int StrEqual(const Str *Str1, const Str *Str2) { if (Str1->Len != Str2->Len) @@ -29,20 +27,51 @@ int StrEqual(const Str *Str1, const Str *Str2) return memcmp(Str1->Str, Str2->Str, Str1->Len) == 0; } - int StrEqualC(const Str *Str1, const char *Str2) { return strncmp(Str1->Str, Str2, Str1->Len) == 0 && Str2[Str1->Len] == '\0'; } +void StrPrintInt(int Num, FILE *Stream) +{ + int Div; + int Remainder; + + if (Num < 0) + { + fputc('-', Stream); + Num = -Num; + } + + if (Num == 0) + fputc('0', Stream); + else + { + Div = LARGE_INT_POWER_OF_TEN; + while (Div > 0) + { + Remainder = Num / Div; + fputc('0' + Remainder, Stream); + Num -= Remainder * Div; + Div /= 10; + } + } +} void StrPrintf(const char *Format, ...) { - const char *FPos; va_list Args; - Str *str; va_start(Args, Format); + vStrPrintf(Format, Args); + va_end(Args); +} + +void vStrPrintf(const char *Format, va_list Args) +{ + Str *str; + const char *FPos; + for (FPos = Format; *FPos != '\0'; FPos++) { if (*FPos == '%') @@ -50,13 +79,9 @@ void StrPrintf(const char *Format, ...) FPos++; switch (*FPos) { - case 'S': - str = va_arg(Args, Str *); - fwrite(str->Str, 1, str->Len, stdout); - break; - + case 'S': str = va_arg(Args, Str *); fwrite(str->Str, 1, str->Len, stdout); break; case 's': fputs(va_arg(Args, char *), stdout); break; - case 'd': printf("%d", va_arg(Args, int)); break; + case 'd': StrPrintInt(va_arg(Args, int), stdout); break; case 'c': fputc(va_arg(Args, int), stdout); break; case '%': fputc('%', stdout); break; case '\0': FPos--; break; @@ -65,6 +90,5 @@ void StrPrintf(const char *Format, ...) else putchar(*FPos); } - va_end(Args); }