Now uses internal printf

git-svn-id: http://picoc.googlecode.com/svn/trunk@5 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2008-10-14 11:18:43 +00:00
parent 32c5617d83
commit 96674aad41
4 changed files with 44 additions and 18 deletions

View file

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

View file

@ -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 <program.c> <args>...\n");

View file

@ -1,9 +1,12 @@
#ifndef PICOC_H
#define PICOC_H
#include <stdarg.h>
/* 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, ...);

48
str.c
View file

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