2008-12-26 23:36:45 -05:00
|
|
|
#include "picoc.h"
|
|
|
|
|
2009-04-27 00:56:58 -04:00
|
|
|
struct OutputStream CStdOut;
|
|
|
|
|
2009-04-19 07:49:01 -04:00
|
|
|
static int TRUEValue = 1;
|
|
|
|
static int ZeroValue = 0;
|
|
|
|
|
2009-02-24 05:29:14 -05:00
|
|
|
/* initialise a library */
|
2009-02-24 06:16:37 -05:00
|
|
|
void LibraryInit(struct Table *GlobalTable, const char *LibraryName, struct LibraryFunction (*FuncList)[])
|
2009-02-24 05:29:14 -05:00
|
|
|
{
|
|
|
|
struct ParseState Parser;
|
|
|
|
int Count;
|
|
|
|
char *Identifier;
|
|
|
|
struct ValueType *ReturnType;
|
|
|
|
struct Value *NewValue;
|
|
|
|
void *Tokens;
|
|
|
|
const char *IntrinsicName = TableStrRegister("c library");
|
|
|
|
|
2009-04-27 00:56:58 -04:00
|
|
|
CStdOut.Putch = &PlatformPutc;
|
|
|
|
|
2009-02-24 06:16:37 -05:00
|
|
|
for (Count = 0; (*FuncList)[Count].Prototype != NULL; Count++)
|
2009-02-24 05:29:14 -05:00
|
|
|
{
|
2009-04-03 23:11:12 -04:00
|
|
|
Tokens = LexAnalyse(IntrinsicName, (*FuncList)[Count].Prototype, strlen((char *)(*FuncList)[Count].Prototype), NULL);
|
2009-02-24 05:29:14 -05:00
|
|
|
LexInitParser(&Parser, Tokens, IntrinsicName, Count+1, TRUE);
|
|
|
|
TypeParse(&Parser, &ReturnType, &Identifier);
|
|
|
|
NewValue = ParseFunctionDefinition(&Parser, ReturnType, Identifier, TRUE);
|
2009-02-24 06:16:37 -05:00
|
|
|
NewValue->Val->FuncDef.Intrinsic = (*FuncList)[Count].Func;
|
2009-02-24 05:29:14 -05:00
|
|
|
HeapFree(Tokens);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-19 07:49:01 -04:00
|
|
|
/* initialise the C library */
|
|
|
|
void CLibraryInit()
|
|
|
|
{
|
|
|
|
/* define some constants */
|
|
|
|
VariableDefinePlatformVar(NULL, "NULL", &IntType, (union AnyValue *)&ZeroValue, FALSE);
|
|
|
|
VariableDefinePlatformVar(NULL, "TRUE", &IntType, (union AnyValue *)&TRUEValue, FALSE);
|
|
|
|
VariableDefinePlatformVar(NULL, "FALSE", &IntType, (union AnyValue *)&ZeroValue, FALSE);
|
|
|
|
}
|
|
|
|
|
2009-04-27 00:56:58 -04:00
|
|
|
/* stream for writing into strings */
|
|
|
|
void SPutc(unsigned char Ch, union OutputStreamInfo *Stream)
|
|
|
|
{
|
|
|
|
struct StringOutputStream *Out = &Stream->Str;
|
|
|
|
*Out->WritePos++ = Ch;
|
|
|
|
if (Out->WritePos == Out->MaxPos)
|
|
|
|
Out->WritePos--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print a character to a stream without using printf/sprintf */
|
|
|
|
void PrintCh(char OutCh, struct OutputStream *Stream)
|
|
|
|
{
|
|
|
|
(*Stream->Putch)(OutCh, &Stream->i);
|
|
|
|
}
|
|
|
|
|
2009-02-24 06:16:37 -05:00
|
|
|
/* print a string to a stream without using printf/sprintf */
|
2009-04-27 00:56:58 -04:00
|
|
|
void PrintStr(const char *Str, struct OutputStream *Stream)
|
2009-02-24 06:16:37 -05:00
|
|
|
{
|
|
|
|
while (*Str != 0)
|
2009-04-27 00:56:58 -04:00
|
|
|
PrintCh(*Str++, Stream);
|
2009-02-24 06:16:37 -05:00
|
|
|
}
|
|
|
|
|
2009-02-20 21:35:52 -05:00
|
|
|
/* print an integer to a stream without using printf/sprintf */
|
2009-04-27 00:56:58 -04:00
|
|
|
void PrintInt(int Num, struct OutputStream *Stream)
|
2009-02-20 21:35:52 -05:00
|
|
|
{
|
|
|
|
int Div;
|
|
|
|
int Remainder = 0;
|
|
|
|
int Printing = FALSE;
|
|
|
|
|
|
|
|
if (Num < 0)
|
|
|
|
{
|
2009-04-27 00:56:58 -04:00
|
|
|
PrintCh('-', Stream);
|
2009-02-20 21:35:52 -05:00
|
|
|
Num = -Num;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Num == 0)
|
2009-04-27 00:56:58 -04:00
|
|
|
PrintCh('0', Stream);
|
2009-02-20 21:35:52 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Div = LARGE_INT_POWER_OF_TEN;
|
|
|
|
while (Div > 0)
|
|
|
|
{
|
|
|
|
Remainder = Num / Div;
|
|
|
|
if (Printing || Remainder > 0)
|
|
|
|
{
|
2009-04-27 00:56:58 -04:00
|
|
|
PrintCh('0' + Remainder, Stream);
|
2009-02-20 21:35:52 -05:00
|
|
|
Printing = TRUE;
|
|
|
|
}
|
|
|
|
Num -= Remainder * Div;
|
|
|
|
Div /= 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-23 23:52:35 -05:00
|
|
|
#ifndef NO_FP
|
2009-02-20 21:35:52 -05:00
|
|
|
/* print a double to a stream without using printf/sprintf */
|
2009-04-27 00:56:58 -04:00
|
|
|
void PrintFP(double Num, struct OutputStream *Stream)
|
2009-02-20 21:35:52 -05:00
|
|
|
{
|
|
|
|
int Exponent = 0;
|
|
|
|
|
|
|
|
if (abs(Num) >= 1e7)
|
|
|
|
Exponent = log(Num) / LOG10E;
|
|
|
|
else if (abs(Num) <= 1e-7)
|
|
|
|
Exponent = log(Num) / LOG10E - 0.999999999;
|
|
|
|
|
|
|
|
Num /= pow(10.0, Exponent);
|
2009-04-27 00:56:58 -04:00
|
|
|
PrintInt((int)Num, Stream);
|
|
|
|
PrintCh('.', Stream);
|
2009-02-20 21:35:52 -05:00
|
|
|
for (Num -= (int)Num; Num != 0.0; Num *= 10.0)
|
2009-04-27 00:56:58 -04:00
|
|
|
PrintCh('0' + (int)Num, Stream);
|
2009-02-20 21:35:52 -05:00
|
|
|
|
|
|
|
if (Exponent)
|
|
|
|
{
|
2009-04-27 00:56:58 -04:00
|
|
|
PrintCh('e', Stream);
|
|
|
|
PrintInt(Exponent, Stream);
|
2009-02-20 21:35:52 -05:00
|
|
|
}
|
|
|
|
}
|
2009-02-23 23:52:35 -05:00
|
|
|
#endif
|
2009-02-20 21:35:52 -05:00
|
|
|
|
2009-04-23 21:02:25 -04:00
|
|
|
/* print a type to a stream without using printf/sprintf */
|
2009-04-27 00:56:58 -04:00
|
|
|
void PrintType(struct ValueType *Typ, struct OutputStream *Stream)
|
2009-04-23 21:02:25 -04:00
|
|
|
{
|
|
|
|
switch (Typ->Base)
|
|
|
|
{
|
2009-04-27 00:56:58 -04:00
|
|
|
case TypeVoid: PrintStr("void", Stream); break;
|
|
|
|
case TypeInt: PrintStr("int", Stream); break;
|
2009-04-23 21:02:25 -04:00
|
|
|
#ifndef NO_FP
|
2009-04-27 00:56:58 -04:00
|
|
|
case TypeFP: PrintStr("double", Stream); break;
|
2009-04-23 21:02:25 -04:00
|
|
|
#endif
|
2009-04-27 00:56:58 -04:00
|
|
|
case TypeChar: PrintStr("char", Stream); break;
|
|
|
|
case TypeFunction: PrintStr("function", Stream); break;
|
|
|
|
case TypeMacro: PrintStr("macro", Stream); break;
|
|
|
|
case TypePointer: if (Typ->FromType) PrintType(Typ->FromType, Stream); PrintCh('*', Stream); break;
|
|
|
|
case TypeArray: PrintType(Typ->FromType, Stream); PrintCh('[', Stream); if (Typ->ArraySize != 0) PrintInt(Typ->ArraySize, Stream); PrintCh(']', Stream); break;
|
|
|
|
case TypeStruct: PrintStr("struct ", Stream); PrintStr(Typ->Identifier, Stream); break;
|
|
|
|
case TypeUnion: PrintStr("union ", Stream); PrintStr(Typ->Identifier, Stream); break;
|
|
|
|
case TypeEnum: PrintStr("enum ", Stream); PrintStr(Typ->Identifier, Stream); break;
|
2009-04-23 21:02:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-20 21:35:52 -05:00
|
|
|
/* intrinsic functions made available to the language */
|
2009-04-27 00:56:58 -04:00
|
|
|
void GenericPrintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs, struct OutputStream *Stream)
|
2008-12-26 23:36:45 -05:00
|
|
|
{
|
2009-02-20 21:35:52 -05:00
|
|
|
char *FPos;
|
|
|
|
struct Value *NextArg = Param[0];
|
|
|
|
struct ValueType *FormatType;
|
|
|
|
int ArgCount = 1;
|
2009-05-05 20:25:09 -04:00
|
|
|
#ifndef NATIVE_POINTERS
|
|
|
|
char *Format;
|
|
|
|
struct Value *CharArray = Param[0]->Val->Pointer.Segment;
|
|
|
|
|
2009-03-15 03:07:21 -04:00
|
|
|
if (Param[0]->Val->Pointer.Offset < 0 || Param[0]->Val->Pointer.Offset >= CharArray->Val->Array.Size)
|
2009-02-20 21:35:52 -05:00
|
|
|
Format = StrEmpty;
|
|
|
|
else
|
2009-03-15 03:07:21 -04:00
|
|
|
Format = CharArray->Val->Array.Data + Param[0]->Val->Pointer.Offset;
|
2009-05-05 20:25:09 -04:00
|
|
|
#else
|
|
|
|
char *Format = Param[0]->Val->NativePointer;
|
|
|
|
// XXX - dereference this properly
|
|
|
|
#endif
|
2009-02-20 21:35:52 -05:00
|
|
|
|
|
|
|
for (FPos = Format; *FPos != '\0'; FPos++)
|
|
|
|
{
|
|
|
|
if (*FPos == '%')
|
|
|
|
{
|
|
|
|
FPos++;
|
|
|
|
switch (*FPos)
|
|
|
|
{
|
|
|
|
case 's': FormatType = CharPtrType; break;
|
2009-02-23 19:21:17 -05:00
|
|
|
case 'd': case 'c': FormatType = &IntType; break;
|
|
|
|
#ifndef NO_FP
|
2009-02-20 21:35:52 -05:00
|
|
|
case 'f': FormatType = &FPType; break;
|
2009-02-23 19:21:17 -05:00
|
|
|
#endif
|
2009-04-27 00:56:58 -04:00
|
|
|
case '%': PrintCh('%', Stream); FormatType = NULL; break;
|
2009-02-20 21:35:52 -05:00
|
|
|
case '\0': FPos--; FormatType = NULL; break;
|
2009-04-27 00:56:58 -04:00
|
|
|
default: PrintCh(*FPos, Stream); FormatType = NULL; break;
|
2009-02-20 21:35:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FormatType != NULL)
|
|
|
|
{ /* we have to format something */
|
|
|
|
if (ArgCount >= NumArgs)
|
2009-04-27 00:56:58 -04:00
|
|
|
PrintStr("XXX", Stream); /* not enough parameters for format */
|
2009-02-20 21:35:52 -05:00
|
|
|
else
|
|
|
|
{
|
2009-03-02 17:13:47 -05:00
|
|
|
NextArg = (struct Value *)((void *)NextArg + sizeof(struct Value) + TypeStackSizeValue(NextArg));
|
2009-04-22 08:06:58 -04:00
|
|
|
if (NextArg->Typ != FormatType && !(FormatType == &IntType && IS_INTEGER_COERCIBLE(NextArg)))
|
2009-04-27 00:56:58 -04:00
|
|
|
PrintStr("XXX", Stream); /* bad type for format */
|
2009-02-20 21:35:52 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (*FPos)
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
{
|
2009-05-05 20:25:09 -04:00
|
|
|
#ifndef NATIVE_POINTERS
|
2009-02-20 21:35:52 -05:00
|
|
|
struct Value *CharArray = NextArg->Val->Pointer.Segment;
|
|
|
|
char *Str;
|
|
|
|
|
2009-03-15 03:07:21 -04:00
|
|
|
if (NextArg->Val->Pointer.Offset < 0 || NextArg->Val->Pointer.Offset >= CharArray->Val->Array.Size)
|
2009-02-20 21:35:52 -05:00
|
|
|
Str = StrEmpty;
|
|
|
|
else
|
2009-03-15 03:07:21 -04:00
|
|
|
Str = CharArray->Val->Array.Data + NextArg->Val->Pointer.Offset;
|
2009-05-05 20:25:09 -04:00
|
|
|
#else
|
|
|
|
char *Str = NextArg->Val->NativePointer;
|
|
|
|
// XXX - dereference this properly
|
|
|
|
#endif
|
2009-02-20 21:35:52 -05:00
|
|
|
|
2009-04-27 00:56:58 -04:00
|
|
|
PrintStr(Str, Stream);
|
2009-02-20 21:35:52 -05:00
|
|
|
break;
|
|
|
|
}
|
2009-04-27 00:56:58 -04:00
|
|
|
case 'd': PrintInt(COERCE_INTEGER(NextArg), Stream); break;
|
|
|
|
case 'c': PrintCh(COERCE_INTEGER(NextArg), Stream); break;
|
2009-02-23 19:21:17 -05:00
|
|
|
#ifndef NO_FP
|
2009-04-27 00:56:58 -04:00
|
|
|
case 'f': PrintFP(NextArg->Val->FP, Stream); break;
|
2009-02-23 19:21:17 -05:00
|
|
|
#endif
|
2009-02-20 21:35:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ArgCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-04-27 00:56:58 -04:00
|
|
|
PrintCh(*FPos, Stream);
|
2009-02-20 21:35:52 -05:00
|
|
|
}
|
2008-12-26 23:36:45 -05:00
|
|
|
}
|
|
|
|
|
2009-04-27 00:56:58 -04:00
|
|
|
/* printf(): print to console output */
|
|
|
|
void LibPrintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
|
|
|
{
|
|
|
|
struct OutputStream ConsoleStream;
|
|
|
|
|
|
|
|
ConsoleStream.Putch = &PlatformPutc;
|
|
|
|
GenericPrintf(Parser, ReturnValue, Param, NumArgs, &ConsoleStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sprintf(): print to a string */
|
|
|
|
void LibSPrintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
|
|
|
{
|
|
|
|
struct OutputStream StrStream;
|
|
|
|
struct Value *DerefVal;
|
|
|
|
int DerefOffset;
|
|
|
|
struct ValueType *DerefType;
|
|
|
|
StrStream.i.Str.WritePos = VariableDereferencePointer(StrStream.i.Str.Parser, Param[0], &DerefVal, &DerefOffset, &DerefType);
|
|
|
|
|
|
|
|
if (DerefVal->Typ->Base != TypeArray)
|
|
|
|
ProgramFail(Parser, "can only print to arrays of char");
|
|
|
|
|
|
|
|
StrStream.Putch = &SPutc;
|
|
|
|
StrStream.i.Str.Parser = Parser;
|
|
|
|
StrStream.i.Str.MaxPos = StrStream.i.Str.WritePos- DerefOffset + DerefVal->Val->Array.Size;
|
|
|
|
GenericPrintf(Parser, ReturnValue, Param+1, NumArgs-1, &StrStream);
|
|
|
|
PrintCh(0, &StrStream);
|
2009-05-05 20:25:09 -04:00
|
|
|
#ifndef NATIVE_POINTERS
|
2009-04-27 00:56:58 -04:00
|
|
|
ReturnValue->Val->Pointer.Segment = *Param;
|
|
|
|
ReturnValue->Val->Pointer.Offset = 0;
|
2009-05-05 20:25:09 -04:00
|
|
|
#else
|
|
|
|
ReturnValue->Val->NativePointer = *Param;
|
|
|
|
#endif
|
2009-04-27 00:56:58 -04:00
|
|
|
}
|
|
|
|
|
2009-03-15 05:57:19 -04:00
|
|
|
/* get a line of input. protected from buffer overrun */
|
2009-03-15 03:07:21 -04:00
|
|
|
void LibGets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
|
|
|
{
|
2009-05-05 20:25:09 -04:00
|
|
|
#ifndef NATIVE_POINTERS
|
2009-03-15 05:57:19 -04:00
|
|
|
struct Value *CharArray = Param[0]->Val->Pointer.Segment;
|
|
|
|
char *ReadBuffer = CharArray->Val->Array.Data + Param[0]->Val->Pointer.Offset;
|
|
|
|
int MaxLength = CharArray->Val->Array.Size - Param[0]->Val->Pointer.Offset;
|
|
|
|
char *Result;
|
|
|
|
|
2009-04-27 00:56:58 -04:00
|
|
|
ReturnValue->Val->Pointer.Segment = NULL;
|
2009-03-15 05:57:19 -04:00
|
|
|
ReturnValue->Val->Pointer.Offset = 0;
|
|
|
|
|
|
|
|
if (Param[0]->Val->Pointer.Offset < 0 || MaxLength < 0)
|
|
|
|
return; /* no room for data */
|
|
|
|
|
|
|
|
Result = PlatformGetLine(ReadBuffer, MaxLength);
|
|
|
|
if (Result == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ReturnValue->Val->Pointer = Param[0]->Val->Pointer;
|
2009-05-05 20:25:09 -04:00
|
|
|
#else
|
|
|
|
struct Value *CharArray = (struct Value *)(Param[0]->Val->NativePointer);
|
|
|
|
char *ReadBuffer = CharArray->Val->Array.Data;
|
|
|
|
int MaxLength = CharArray->Val->Array.Size;
|
|
|
|
char *Result;
|
|
|
|
|
|
|
|
ReturnValue->Val->NativePointer = NULL;
|
|
|
|
|
|
|
|
if (MaxLength < 0)
|
|
|
|
return; /* no room for data */
|
|
|
|
|
|
|
|
Result = PlatformGetLine(ReadBuffer, MaxLength);
|
|
|
|
if (Result == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ReturnValue->Val->NativePointer = Param[0]->Val->NativePointer;
|
|
|
|
#endif
|
2009-03-15 03:07:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void LibGetc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
|
|
|
{
|
2009-03-15 05:57:19 -04:00
|
|
|
ReturnValue->Val->Integer = PlatformGetCharacter();
|
2009-03-15 03:07:21 -04:00
|
|
|
}
|
|
|
|
|
2009-02-24 06:16:37 -05:00
|
|
|
/* list of all library functions and their prototypes */
|
|
|
|
struct LibraryFunction CLibrary[] =
|
2008-12-26 23:36:45 -05:00
|
|
|
{
|
2009-02-24 06:16:37 -05:00
|
|
|
{ LibPrintf, "void printf(char *, ...)" },
|
2009-04-27 00:56:58 -04:00
|
|
|
{ LibSPrintf, "char *sprintf(char *, char *, ...)" },
|
2009-03-15 03:07:21 -04:00
|
|
|
{ LibGets, "void gets(char *, int)" },
|
2009-03-15 05:57:19 -04:00
|
|
|
{ LibGetc, "int getchar()" },
|
2009-02-24 06:16:37 -05:00
|
|
|
{ NULL, NULL }
|
2008-12-26 23:36:45 -05:00
|
|
|
};
|