Now uses internal printf
git-svn-id: http://picoc.googlecode.com/svn/trunk@5 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
parent
32c5617d83
commit
96674aad41
2
parse.c
2
parse.c
|
@ -25,7 +25,7 @@ void ParseScan(const Str *FileName, const Str *Source)
|
||||||
while ( (Token = LexGetToken(&Lexer)) != TokenEOF)
|
while ( (Token = LexGetToken(&Lexer)) != TokenEOF)
|
||||||
{
|
{
|
||||||
/* do parsey things here */
|
/* do parsey things here */
|
||||||
printf("token %d\n", (int)Token);
|
StrPrintf("token %d\n", (int)Token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
8
picoc.c
8
picoc.c
|
@ -14,7 +14,7 @@ void Fail(const char *Message, ...)
|
||||||
va_list Args;
|
va_list Args;
|
||||||
|
|
||||||
va_start(Args, Message);
|
va_start(Args, Message);
|
||||||
vprintf(Message, Args);
|
vStrPrintf(Message, Args);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,8 +24,8 @@ void ProgramError(const Str *FileName, int Line, const char *Message, ...)
|
||||||
|
|
||||||
StrPrintf("%S:%d: ", FileName, Line);
|
StrPrintf("%S:%d: ", FileName, Line);
|
||||||
va_start(Args, Message);
|
va_start(Args, Message);
|
||||||
vprintf(Message, Args);
|
vStrPrintf(Message, Args);
|
||||||
printf("\n");
|
StrPrintf("\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,8 +73,6 @@ int main(int argc, char **argv)
|
||||||
Str FileName;
|
Str FileName;
|
||||||
Str StartFunc;
|
Str StartFunc;
|
||||||
|
|
||||||
printf("picoc\n");
|
|
||||||
|
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
Fail("Format: picoc <program.c> <args>...\n");
|
Fail("Format: picoc <program.c> <args>...\n");
|
||||||
|
|
||||||
|
|
4
picoc.h
4
picoc.h
|
@ -1,9 +1,12 @@
|
||||||
#ifndef PICOC_H
|
#ifndef PICOC_H
|
||||||
#define PICOC_H
|
#define PICOC_H
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
/* configurable options */
|
/* configurable options */
|
||||||
#define USE_MALLOC
|
#define USE_MALLOC
|
||||||
#define GLOBAL_TABLE_SIZE 199
|
#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 */
|
/* handy definitions */
|
||||||
#ifndef TRUE
|
#ifndef TRUE
|
||||||
|
@ -121,6 +124,7 @@ void StrFromC(Str *Dest, const char *Source);
|
||||||
int StrEqual(const Str *Str1, const Str *Str2);
|
int StrEqual(const Str *Str1, const Str *Str2);
|
||||||
int StrEqualC(const Str *Str1, const char *Str2);
|
int StrEqualC(const Str *Str1, const char *Str2);
|
||||||
void StrPrintf(const char *Format, ...);
|
void StrPrintf(const char *Format, ...);
|
||||||
|
void vStrPrintf(const char *Format, va_list Args);
|
||||||
|
|
||||||
/* picoc.c */
|
/* picoc.c */
|
||||||
void Fail(const char *Message, ...);
|
void Fail(const char *Message, ...);
|
||||||
|
|
48
str.c
48
str.c
|
@ -13,14 +13,12 @@ void StrToC(char *Dest, int DestSize, const Str *Source)
|
||||||
Dest[CopyLen] = '\0';
|
Dest[CopyLen] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void StrFromC(Str *Dest, const char *Source)
|
void StrFromC(Str *Dest, const char *Source)
|
||||||
{
|
{
|
||||||
Dest->Str = Source;
|
Dest->Str = Source;
|
||||||
Dest->Len = strlen(Source);
|
Dest->Len = strlen(Source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int StrEqual(const Str *Str1, const Str *Str2)
|
int StrEqual(const Str *Str1, const Str *Str2)
|
||||||
{
|
{
|
||||||
if (Str1->Len != Str2->Len)
|
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;
|
return memcmp(Str1->Str, Str2->Str, Str1->Len) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int StrEqualC(const Str *Str1, const char *Str2)
|
int StrEqualC(const Str *Str1, const char *Str2)
|
||||||
{
|
{
|
||||||
return strncmp(Str1->Str, Str2, Str1->Len) == 0 && Str2[Str1->Len] == '\0';
|
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, ...)
|
void StrPrintf(const char *Format, ...)
|
||||||
{
|
{
|
||||||
const char *FPos;
|
|
||||||
va_list Args;
|
va_list Args;
|
||||||
Str *str;
|
|
||||||
|
|
||||||
va_start(Args, Format);
|
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++)
|
for (FPos = Format; *FPos != '\0'; FPos++)
|
||||||
{
|
{
|
||||||
if (*FPos == '%')
|
if (*FPos == '%')
|
||||||
|
@ -50,13 +79,9 @@ void StrPrintf(const char *Format, ...)
|
||||||
FPos++;
|
FPos++;
|
||||||
switch (*FPos)
|
switch (*FPos)
|
||||||
{
|
{
|
||||||
case 'S':
|
case 'S': str = va_arg(Args, Str *); fwrite(str->Str, 1, str->Len, stdout); break;
|
||||||
str = va_arg(Args, Str *);
|
|
||||||
fwrite(str->Str, 1, str->Len, stdout);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 's': fputs(va_arg(Args, char *), 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 'c': fputc(va_arg(Args, int), stdout); break;
|
||||||
case '%': fputc('%', stdout); break;
|
case '%': fputc('%', stdout); break;
|
||||||
case '\0': FPos--; break;
|
case '\0': FPos--; break;
|
||||||
|
@ -65,6 +90,5 @@ void StrPrintf(const char *Format, ...)
|
||||||
else
|
else
|
||||||
putchar(*FPos);
|
putchar(*FPos);
|
||||||
}
|
}
|
||||||
va_end(Args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue