A bit of a reorganisation to make adding new platforms and
C standard library modules neater. git-svn-id: http://picoc.googlecode.com/svn/trunk@427 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
parent
5fdce80407
commit
0fc32f5bc7
5
Makefile
5
Makefile
|
@ -4,8 +4,9 @@ LIBS=-lm
|
|||
|
||||
TARGET = picoc
|
||||
SRCS = picoc.c table.c lex.c parse.c expression.c heap.c type.c \
|
||||
variable.c clibrary.c library_unix.c platform.c platform_unix.c \
|
||||
math_library.c include.c library_stdio.c
|
||||
variable.c clibrary.c platform.c include.c \
|
||||
platform/platform_unix.c platform/library_unix.c \
|
||||
cstdlib/library_stdio.c
|
||||
OBJS := $(SRCS:%.c=%.o)
|
||||
|
||||
all: depend $(TARGET)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/* stdio.h library for large systems - small embedded systems use clibrary.c instead */
|
||||
#include <errno.h>
|
||||
#include "picoc.h"
|
||||
#include "../picoc.h"
|
||||
|
||||
#ifndef BUILTIN_MINI_STDLIB
|
||||
|
||||
|
@ -305,6 +305,188 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
|
|||
return SOStream.CharCount;
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* scanf-style reading of an int or other word-sized object */
|
||||
void StdioScanfWord(StdOutStream *Stream, const char *Format, unsigned int *Value)
|
||||
{
|
||||
if (Stream->FilePtr != NULL)
|
||||
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
|
||||
|
||||
else if (Stream->StrOutLen >= 0)
|
||||
{
|
||||
int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value);
|
||||
Stream->StrOutPtr += CCount;
|
||||
Stream->StrOutLen -= CCount;
|
||||
Stream->CharCount += CCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
int CCount = sprintf(Stream->StrOutPtr, Format, Value);
|
||||
Stream->CharCount += CCount;
|
||||
Stream->StrOutPtr += CCount;
|
||||
}
|
||||
}
|
||||
|
||||
/* internal do-anything v[s][n]scanf() formatting system with input from strings or FILE * */
|
||||
int StdioBaseScanf(struct ParseState *Parser, FILE *Stream, char *StrOut, int StrOutLen, char *Format, struct StdVararg *Args)
|
||||
{
|
||||
struct Value *ThisArg = Args->Param[0];
|
||||
int ArgCount = 0;
|
||||
char *FPos = Format;
|
||||
char OneFormatBuf[MAX_FORMAT+1];
|
||||
int OneFormatCount;
|
||||
struct ValueType *ShowType;
|
||||
StdOutStream SOStream;
|
||||
int BadArg;
|
||||
|
||||
SOStream.FilePtr = Stream;
|
||||
SOStream.StrPtr = Str;
|
||||
SOStream.StrLen = StrLen;
|
||||
SOStream.CharCount = 0;
|
||||
|
||||
while (*FPos != '\0')
|
||||
{
|
||||
if (*FPos == '%')
|
||||
{
|
||||
/* work out what type we're scanning */
|
||||
FPos++;
|
||||
ShowType = NULL;
|
||||
OneFormatBuf[0] = '%';
|
||||
OneFormatCount = 1;
|
||||
|
||||
do
|
||||
{
|
||||
switch (*FPos)
|
||||
{
|
||||
case 'd': case 'D': case 'i': ShowType = &IntType; break; /* integer decimal */
|
||||
case 'o': case 'u': case 'x': case 'X': ShowType = &IntType; break; /* integer base conversions */
|
||||
case 'e': case 'E': ShowType = &FPType; break; /* double, exponent form */
|
||||
case 'f': case 'F': ShowType = &FPType; break; /* double, fixed-point */
|
||||
case 'g': case 'G': case 'a': ShowType = &FPType; break; /* double, flexible format */
|
||||
case 'c': ShowType = &IntType; break; /* character */
|
||||
case 's': ShowType = CharPtrType; break; /* string */
|
||||
case 'p': ShowType = VoidPtrType; break; /* pointer */
|
||||
case ']': ShowType = &VoidType; break; /* match an expression */
|
||||
case 'n': ShowType = &VoidType; break; /* number of characters written */
|
||||
case '%': ShowType = &VoidType; break; /* just a '%' character */
|
||||
case '\0': ShowType = &VoidType; break; /* end of format string */
|
||||
}
|
||||
|
||||
/* copy one character of format across to the OneFormatBuf */
|
||||
OneFormatBuf[OneFormatCount] = *FPos;
|
||||
OneFormatCount++;
|
||||
|
||||
/* do special actions depending on the conversion type */
|
||||
if (ShowType == &VoidType)
|
||||
{
|
||||
switch (*FPos)
|
||||
{
|
||||
case '%':
|
||||
/* match a '%' character */
|
||||
if (StdioPeekChar(&SOStream) == '%')
|
||||
StdioReadChar(&SOStream);
|
||||
else
|
||||
return ArgCount;
|
||||
|
||||
break;
|
||||
|
||||
case '\0':
|
||||
break;
|
||||
|
||||
case ']':
|
||||
/* match a sortof-regex expression (third param is a dummy) */
|
||||
StdioScanfWord(&SOStream, OneFormatBuf, &OneFormatCount);
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
|
||||
if (ThisArg->Typ->Base == TypeArray && ThisArg->Typ->FromType->Base == TypeInt)
|
||||
*(int *)ThisArg->Val->Integer = SOStream.CharCount;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
FPos++;
|
||||
|
||||
} while (ShowType == NULL && OneFormatCount < MAX_FORMAT);
|
||||
|
||||
BadArg = FALSE;
|
||||
if (ShowType != &VoidType)
|
||||
{
|
||||
if (ArgCount < Args->NumArgs)
|
||||
{
|
||||
/* null-terminate the buffer */
|
||||
OneFormatBuf[OneFormatCount] = '\0';
|
||||
|
||||
/* print this argument */
|
||||
ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
|
||||
if (ShowType == &IntType && IS_INTEGER_NUMERIC(ThisArg))
|
||||
StdioScanfWord(&SOStream, OneFormatBuf, ExpressionCoerceUnsignedInteger(ThisArg));
|
||||
|
||||
else if (ShowType == &FPType && IS_FP(ThisArg))
|
||||
StdioScanfFP(&SOStream, OneFormatBuf, ExpressionCoerceFP(ThisArg));
|
||||
|
||||
else if (ShowType == CharPtrType)
|
||||
{
|
||||
if (ThisArg->Typ->Base == TypePointer)
|
||||
StdioScanfPointer(&SOStream, OneFormatBuf, ThisArg->Val->NativePointer);
|
||||
|
||||
else if (ThisArg->Typ->Base == TypeArray && ThisArg->Typ->FromType->Base == TypeChar)
|
||||
StdioScanfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]);
|
||||
|
||||
else
|
||||
BadArg = TRUE;
|
||||
}
|
||||
else if (ShowType == VoidPtrType)
|
||||
{
|
||||
if (ThisArg->Typ->Base == TypePointer)
|
||||
StdioScanfPointer(&SOStream, OneFormatBuf, ThisArg->Val->NativePointer);
|
||||
|
||||
else if (ThisArg->Typ->Base == TypeArray)
|
||||
StdioScanfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]);
|
||||
|
||||
else
|
||||
BadArg = TRUE;
|
||||
}
|
||||
else
|
||||
BadArg = TRUE;
|
||||
|
||||
if (BadArg)
|
||||
{
|
||||
/* XXX - should set errno = invalid argument here */
|
||||
return EOF;
|
||||
}
|
||||
|
||||
ArgCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* a plain character, not a format specifier */
|
||||
if (isspace(*FPos))
|
||||
{
|
||||
/* get whitespace */
|
||||
while (isspace(StdioPeekChar(&SOStream)))
|
||||
StdioReadChar(&SOStream);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* match a character */
|
||||
if (StdioPeekChar(&SOStream) == *FPos)
|
||||
StdioReadChar(&SOStream);
|
||||
else
|
||||
return ArgCount;
|
||||
}
|
||||
|
||||
FPos++;
|
||||
}
|
||||
}
|
||||
|
||||
return SOStream.CharCount;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* stdio calls */
|
||||
void StdioFopen(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||
{
|
||||
|
@ -507,6 +689,17 @@ void StdioSnprintf(struct ParseState *Parser, struct Value *ReturnValue, struct
|
|||
ReturnValue->Val->Integer = StdioBasePrintf(Parser, NULL, Param[0]->Val->NativePointer, Param[1]->Val->Integer, Param[2]->Val->NativePointer, &PrintfArgs);
|
||||
}
|
||||
|
||||
#if 0
|
||||
void StdioScanf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||
{
|
||||
struct StdVararg ScanfArgs;
|
||||
|
||||
PrintfArgs.Param = Param;
|
||||
PrintfArgs.NumArgs = NumArgs-1;
|
||||
ReturnValue->Val->Integer = StdioBaseScanf(Parser, stdin, NULL, 0, Param[0]->Val->NativePointer, &ScanfArgs);
|
||||
}
|
||||
#endif
|
||||
|
||||
void StdioVsprintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||
{
|
||||
ReturnValue->Val->Integer = StdioBasePrintf(Parser, NULL, Param[0]->Val->NativePointer, -1, Param[1]->Val->NativePointer, Param[2]->Val->NativePointer);
|
||||
|
@ -564,10 +757,20 @@ struct LibraryFunction StdioFunctions[] =
|
|||
{ StdioFprintf, "int fprintf(FILE *, char *, ...);" },
|
||||
{ StdioSprintf, "int sprintf(char *, char *, ...);" },
|
||||
{ StdioSnprintf,"int snprintf(char *, int, char *, ...);" },
|
||||
#if 0
|
||||
{ StdioScanf, "int scanf(char *, ...);" },
|
||||
{ StdioFscanf, "int fscanf(FILE *, char *, ...);" },
|
||||
{ StdioSscanf, "int sscanf(char *, char *, ...);" },
|
||||
#endif
|
||||
{ StdioVprintf, "int vprintf(char *, va_list);" },
|
||||
{ StdioVfprintf,"int vfprintf(FILE *, char *, va_list);" },
|
||||
{ StdioVsprintf,"int vsprintf(char *, char *, va_list);" },
|
||||
{ StdioVsnprintf,"int vsnprintf(char *, int, char *, va_list);" },
|
||||
#if 0
|
||||
{ StdioVscanf, "int vscanf(char *, va_list);" },
|
||||
{ StdioVfscanf, "int vfscanf(FILE *, char *, va_list);" },
|
||||
{ StdioVsscanf, "int vsscanf(char *, char *, va_list);" },
|
||||
#endif
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
3151
math_library.c
3151
math_library.c
File diff suppressed because it is too large
Load diff
|
@ -1,4 +1,4 @@
|
|||
#include "picoc.h"
|
||||
#include "../picoc.h"
|
||||
|
||||
void PlatformLibraryInit()
|
||||
{
|
|
@ -1,4 +1,4 @@
|
|||
#include "picoc.h"
|
||||
#include "../picoc.h"
|
||||
|
||||
static int Blobcnt, Blobx1, Blobx2, Bloby1, Bloby2, Iy1, Iy2, Iu1, Iu2, Iv1, Iv2;
|
||||
static int GPSlat, GPSlon, GPSalt, GPSfix, GPSsat, GPSutc, Elcount, Ercount;
|
|
@ -1,4 +1,4 @@
|
|||
#include "picoc.h"
|
||||
#include "../picoc.h"
|
||||
|
||||
void PlatformLibraryInit()
|
||||
{
|
|
@ -1,4 +1,4 @@
|
|||
#include "picoc.h"
|
||||
#include "../picoc.h"
|
||||
|
||||
/* deallocate any storage */
|
||||
void PlatformCleanup()
|
|
@ -1,4 +1,4 @@
|
|||
#include "picoc.h"
|
||||
#include "../picoc.h"
|
||||
|
||||
|
||||
/* deallocate any storage */
|
|
@ -1,4 +1,4 @@
|
|||
#include "picoc.h"
|
||||
#include "../picoc.h"
|
||||
|
||||
/* a source file we need to clean up */
|
||||
static char *CleanupText = NULL;
|
Loading…
Reference in a new issue