formatting

This commit is contained in:
Joseph Poirier 2015-06-06 23:51:02 -05:00
parent a61bd97d9b
commit c5384cb514
9 changed files with 497 additions and 547 deletions

View file

@ -54,14 +54,11 @@ void BasicIOInit(Picoc *pc)
/* output a single character to either a FILE * or a string */ /* output a single character to either a FILE * or a string */
void StdioOutPutc(int OutCh, StdOutStream *Stream) void StdioOutPutc(int OutCh, StdOutStream *Stream)
{ {
if (Stream->FilePtr != NULL) if (Stream->FilePtr != NULL) {
{
/* output to stdio stream */ /* output to stdio stream */
putc(OutCh, Stream->FilePtr); putc(OutCh, Stream->FilePtr);
Stream->CharCount++; Stream->CharCount++;
} } else if (Stream->StrOutLen < 0 || Stream->StrOutLen > 1) {
else if (Stream->StrOutLen < 0 || Stream->StrOutLen > 1)
{
/* output to a string */ /* output to a string */
*Stream->StrOutPtr = OutCh; *Stream->StrOutPtr = OutCh;
Stream->StrOutPtr++; Stream->StrOutPtr++;
@ -76,18 +73,13 @@ void StdioOutPutc(int OutCh, StdOutStream *Stream)
/* output a string to either a FILE * or a string */ /* output a string to either a FILE * or a string */
void StdioOutPuts(const char *Str, StdOutStream *Stream) void StdioOutPuts(const char *Str, StdOutStream *Stream)
{ {
if (Stream->FilePtr != NULL) if (Stream->FilePtr != NULL) {
{
/* output to stdio stream */ /* output to stdio stream */
fputs(Str, Stream->FilePtr); fputs(Str, Stream->FilePtr);
} } else {
else
{
/* output to a string */ /* output to a string */
while (*Str != '\0') while (*Str != '\0') {
{ if (Stream->StrOutLen < 0 || Stream->StrOutLen > 1) {
if (Stream->StrOutLen < 0 || Stream->StrOutLen > 1)
{
/* output to a string */ /* output to a string */
*Stream->StrOutPtr = *Str; *Stream->StrOutPtr = *Str;
Str++; Str++;
@ -107,9 +99,7 @@ void StdioFprintfWord(StdOutStream *Stream, const char *Format, unsigned long Va
{ {
if (Stream->FilePtr != NULL) if (Stream->FilePtr != NULL)
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value); Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
else if (Stream->StrOutLen >= 0) {
else if (Stream->StrOutLen >= 0)
{
#ifndef WIN32 #ifndef WIN32
int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value); int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value);
#else #else
@ -118,9 +108,7 @@ void StdioFprintfWord(StdOutStream *Stream, const char *Format, unsigned long Va
Stream->StrOutPtr += CCount; Stream->StrOutPtr += CCount;
Stream->StrOutLen -= CCount; Stream->StrOutLen -= CCount;
Stream->CharCount += CCount; Stream->CharCount += CCount;
} } else {
else
{
int CCount = sprintf(Stream->StrOutPtr, Format, Value); int CCount = sprintf(Stream->StrOutPtr, Format, Value);
Stream->CharCount += CCount; Stream->CharCount += CCount;
Stream->StrOutPtr += CCount; Stream->StrOutPtr += CCount;
@ -132,9 +120,7 @@ void StdioFprintfFP(StdOutStream *Stream, const char *Format, double Value)
{ {
if (Stream->FilePtr != NULL) if (Stream->FilePtr != NULL)
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value); Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
else if (Stream->StrOutLen >= 0) {
else if (Stream->StrOutLen >= 0)
{
#ifndef WIN32 #ifndef WIN32
int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value); int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value);
#else #else
@ -143,9 +129,7 @@ void StdioFprintfFP(StdOutStream *Stream, const char *Format, double Value)
Stream->StrOutPtr += CCount; Stream->StrOutPtr += CCount;
Stream->StrOutLen -= CCount; Stream->StrOutLen -= CCount;
Stream->CharCount += CCount; Stream->CharCount += CCount;
} } else {
else
{
int CCount = sprintf(Stream->StrOutPtr, Format, Value); int CCount = sprintf(Stream->StrOutPtr, Format, Value);
Stream->CharCount += CCount; Stream->CharCount += CCount;
Stream->StrOutPtr += CCount; Stream->StrOutPtr += CCount;
@ -157,9 +141,7 @@ void StdioFprintfPointer(StdOutStream *Stream, const char *Format, void *Value)
{ {
if (Stream->FilePtr != NULL) if (Stream->FilePtr != NULL)
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value); Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
else if (Stream->StrOutLen >= 0) {
else if (Stream->StrOutLen >= 0)
{
#ifndef WIN32 #ifndef WIN32
int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value); int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value);
#else #else
@ -168,9 +150,7 @@ void StdioFprintfPointer(StdOutStream *Stream, const char *Format, void *Value)
Stream->StrOutPtr += CCount; Stream->StrOutPtr += CCount;
Stream->StrOutLen -= CCount; Stream->StrOutLen -= CCount;
Stream->CharCount += CCount; Stream->CharCount += CCount;
} } else {
else
{
int CCount = sprintf(Stream->StrOutPtr, Format, Value); int CCount = sprintf(Stream->StrOutPtr, Format, Value);
Stream->CharCount += CCount; Stream->CharCount += CCount;
Stream->StrOutPtr += CCount; Stream->StrOutPtr += CCount;
@ -198,35 +178,31 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
SOStream.StrOutLen = StrOutLen; SOStream.StrOutLen = StrOutLen;
SOStream.CharCount = 0; SOStream.CharCount = 0;
while (*FPos != '\0') while (*FPos != '\0') {
{ if (*FPos == '%') {
if (*FPos == '%')
{
/* work out what type we're printing */ /* work out what type we're printing */
FPos++; FPos++;
ShowType = NULL; ShowType = NULL;
OneFormatBuf[0] = '%'; OneFormatBuf[0] = '%';
OneFormatCount = 1; OneFormatCount = 1;
do do {
{ switch (*FPos) {
switch (*FPos) case 'd': case 'i': ShowType = &pc->IntType; break; /* integer decimal */
{ case 'o': case 'u': case 'x': case 'X': ShowType = &pc->IntType; break; /* integer base conversions */
case 'd': case 'i': ShowType = &pc->IntType; break; /* integer decimal */
case 'o': case 'u': case 'x': case 'X': ShowType = &pc->IntType; break; /* integer base conversions */
#ifndef NO_FP #ifndef NO_FP
case 'e': case 'E': ShowType = &pc->FPType; break; /* double, exponent form */ case 'e': case 'E': ShowType = &pc->FPType; break; /* double, exponent form */
case 'f': case 'F': ShowType = &pc->FPType; break; /* double, fixed-point */ case 'f': case 'F': ShowType = &pc->FPType; break; /* double, fixed-point */
case 'g': case 'G': ShowType = &pc->FPType; break; /* double, flexible format */ case 'g': case 'G': ShowType = &pc->FPType; break; /* double, flexible format */
#endif #endif
case 'a': case 'A': ShowType = &pc->IntType; break; /* hexadecimal, 0x- format */ case 'a': case 'A': ShowType = &pc->IntType; break; /* hexadecimal, 0x- format */
case 'c': ShowType = &pc->IntType; break; /* character */ case 'c': ShowType = &pc->IntType; break; /* character */
case 's': ShowType = pc->CharPtrType; break; /* string */ case 's': ShowType = pc->CharPtrType; break; /* string */
case 'p': ShowType = pc->VoidPtrType; break; /* pointer */ case 'p': ShowType = pc->VoidPtrType; break; /* pointer */
case 'n': ShowType = &pc->VoidType; break; /* number of characters written */ case 'n': ShowType = &pc->VoidType; break; /* number of characters written */
case 'm': ShowType = &pc->VoidType; break; /* strerror(errno) */ case 'm': ShowType = &pc->VoidType; break; /* strerror(errno) */
case '%': ShowType = &pc->VoidType; break; /* just a '%' character */ case '%': ShowType = &pc->VoidType; break; /* just a '%' character */
case '\0': ShowType = &pc->VoidType; break; /* end of format string */ case '\0': ShowType = &pc->VoidType; break; /* end of format string */
} }
/* copy one character of format across to the OneFormatBuf */ /* copy one character of format across to the OneFormatBuf */
@ -234,18 +210,16 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
OneFormatCount++; OneFormatCount++;
/* do special actions depending on the conversion type */ /* do special actions depending on the conversion type */
if (ShowType == &pc->VoidType) if (ShowType == &pc->VoidType) {
{ switch (*FPos) {
switch (*FPos) case 'm': StdioOutPuts(strerror(errno), &SOStream); break;
{ case '%': StdioOutPutc(*FPos, &SOStream); break;
case 'm': StdioOutPuts(strerror(errno), &SOStream); break; case '\0': OneFormatBuf[OneFormatCount] = '\0'; StdioOutPutc(*FPos, &SOStream); break;
case '%': StdioOutPutc(*FPos, &SOStream); break; case 'n':
case '\0': OneFormatBuf[OneFormatCount] = '\0'; StdioOutPutc(*FPos, &SOStream); break; ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
case 'n': if (ThisArg->Typ->Base == TypeArray && ThisArg->Typ->FromType->Base == TypeInt)
ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg))); *(int *)ThisArg->Val->Pointer = SOStream.CharCount;
if (ThisArg->Typ->Base == TypeArray && ThisArg->Typ->FromType->Base == TypeInt) break;
*(int *)ThisArg->Val->Pointer = SOStream.CharCount;
break;
} }
} }
@ -253,19 +227,16 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
} while (ShowType == NULL && OneFormatCount < MAX_FORMAT); } while (ShowType == NULL && OneFormatCount < MAX_FORMAT);
if (ShowType != &pc->VoidType) if (ShowType != &pc->VoidType) {
{
if (ArgCount >= Args->NumArgs) if (ArgCount >= Args->NumArgs)
StdioOutPuts("XXX", &SOStream); StdioOutPuts("XXX", &SOStream);
else else {
{
/* null-terminate the buffer */ /* null-terminate the buffer */
OneFormatBuf[OneFormatCount] = '\0'; OneFormatBuf[OneFormatCount] = '\0';
/* print this argument */ /* print this argument */
ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg))); ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
if (ShowType == &pc->IntType) if (ShowType == &pc->IntType) {
{
/* show a signed integer */ /* show a signed integer */
if (IS_NUMERIC_COERCIBLE(ThisArg)) if (IS_NUMERIC_COERCIBLE(ThisArg))
StdioFprintfWord(&SOStream, OneFormatBuf, ExpressionCoerceUnsignedInteger(ThisArg)); StdioFprintfWord(&SOStream, OneFormatBuf, ExpressionCoerceUnsignedInteger(ThisArg));
@ -273,8 +244,7 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
StdioOutPuts("XXX", &SOStream); StdioOutPuts("XXX", &SOStream);
} }
#ifndef NO_FP #ifndef NO_FP
else if (ShowType == &pc->FPType) else if (ShowType == &pc->FPType) {
{
/* show a floating point number */ /* show a floating point number */
if (IS_NUMERIC_COERCIBLE(ThisArg)) if (IS_NUMERIC_COERCIBLE(ThisArg))
StdioFprintfFP(&SOStream, OneFormatBuf, ExpressionCoerceFP(ThisArg)); StdioFprintfFP(&SOStream, OneFormatBuf, ExpressionCoerceFP(ThisArg));
@ -282,25 +252,18 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
StdioOutPuts("XXX", &SOStream); StdioOutPuts("XXX", &SOStream);
} }
#endif #endif
else if (ShowType == pc->CharPtrType) else if (ShowType == pc->CharPtrType) {
{
if (ThisArg->Typ->Base == TypePointer) if (ThisArg->Typ->Base == TypePointer)
StdioFprintfPointer(&SOStream, OneFormatBuf, ThisArg->Val->Pointer); StdioFprintfPointer(&SOStream, OneFormatBuf, ThisArg->Val->Pointer);
else if (ThisArg->Typ->Base == TypeArray && ThisArg->Typ->FromType->Base == TypeChar) else if (ThisArg->Typ->Base == TypeArray && ThisArg->Typ->FromType->Base == TypeChar)
StdioFprintfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]); StdioFprintfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]);
else else
StdioOutPuts("XXX", &SOStream); StdioOutPuts("XXX", &SOStream);
} } else if (ShowType == pc->VoidPtrType) {
else if (ShowType == pc->VoidPtrType)
{
if (ThisArg->Typ->Base == TypePointer) if (ThisArg->Typ->Base == TypePointer)
StdioFprintfPointer(&SOStream, OneFormatBuf, ThisArg->Val->Pointer); StdioFprintfPointer(&SOStream, OneFormatBuf, ThisArg->Val->Pointer);
else if (ThisArg->Typ->Base == TypeArray) else if (ThisArg->Typ->Base == TypeArray)
StdioFprintfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]); StdioFprintfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]);
else else
StdioOutPuts("XXX", &SOStream); StdioOutPuts("XXX", &SOStream);
} }
@ -308,9 +271,7 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
ArgCount++; ArgCount++;
} }
} }
} } else {
else
{
/* just output a normal character */ /* just output a normal character */
StdioOutPutc(*FPos, &SOStream); StdioOutPutc(*FPos, &SOStream);
FPos++; FPos++;
@ -334,16 +295,13 @@ int StdioBaseScanf(struct ParseState *Parser, FILE *Stream, char *StrIn, char *F
if (Args->NumArgs > MAX_SCANF_ARGS) if (Args->NumArgs > MAX_SCANF_ARGS)
ProgramFail(Parser, "too many arguments to scanf() - %d max", MAX_SCANF_ARGS); ProgramFail(Parser, "too many arguments to scanf() - %d max", MAX_SCANF_ARGS);
for (ArgCount = 0; ArgCount < Args->NumArgs; ArgCount++) for (ArgCount = 0; ArgCount < Args->NumArgs; ArgCount++) {
{
ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg))); ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
if (ThisArg->Typ->Base == TypePointer) if (ThisArg->Typ->Base == TypePointer)
ScanfArg[ArgCount] = ThisArg->Val->Pointer; ScanfArg[ArgCount] = ThisArg->Val->Pointer;
else if (ThisArg->Typ->Base == TypeArray) else if (ThisArg->Typ->Base == TypeArray)
ScanfArg[ArgCount] = &ThisArg->Val->ArrayMem[0]; ScanfArg[ArgCount] = &ThisArg->Val->ArrayMem[0];
else else
ProgramFail(Parser, "non-pointer argument to scanf() - argument %d after format", ArgCount+1); ProgramFail(Parser, "non-pointer argument to scanf() - argument %d after format", ArgCount+1);
} }
@ -507,8 +465,7 @@ void StdioPuts(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
void StdioGets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioGets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Pointer = fgets(Param[0]->Val->Pointer, GETS_MAXValue, stdin); ReturnValue->Val->Pointer = fgets(Param[0]->Val->Pointer, GETS_MAXValue, stdin);
if (ReturnValue->Val->Pointer != NULL) if (ReturnValue->Val->Pointer != NULL) {
{
char *EOLPos = strchr(Param[0]->Val->Pointer, '\n'); char *EOLPos = strchr(Param[0]->Val->Pointer, '\n');
if (EOLPos != NULL) if (EOLPos != NULL)
*EOLPos = '\0'; *EOLPos = '\0';

View file

@ -3,7 +3,7 @@
/* list of all library functions and their prototypes */ /* list of all library functions and their prototypes */
struct LibraryFunction PlatformLibrary[] = struct LibraryFunction PlatformLibrary[] =
{ {
{ NULL, NULL } {NULL, NULL}
}; };
void PlatformLibraryInit() void PlatformLibraryInit()

View file

@ -18,9 +18,9 @@ void CLineNo (struct ParseState *Parser, struct Value *ReturnValue, struct Value
/* list of all library functions and their prototypes */ /* list of all library functions and their prototypes */
struct LibraryFunction MsvcFunctions[] = struct LibraryFunction MsvcFunctions[] =
{ {
{ CTest, "void Test(int);" }, {CTest, "void Test(int);"},
{ CLineNo, "int LineNo();" }, {CLineNo, "int LineNo();"},
{ NULL, NULL } {NULL, NULL}
}; };
void PlatformLibraryInit(Picoc *pc) void PlatformLibraryInit(Picoc *pc)

View file

@ -97,21 +97,21 @@ void Cpeek(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
ptr = Param[0]->Val->Integer; ptr = Param[0]->Val->Integer;
size = Param[1]->Val->Integer; size = Param[1]->Val->Integer;
switch (size) { switch (size) {
case 1: // char * case 1: // char *
cp = (unsigned char *)ptr; cp = (unsigned char *)ptr;
ReturnValue->Val->Integer = (int)((unsigned int)*cp); ReturnValue->Val->Integer = (int)((unsigned int)*cp);
break; break;
case 2: // short * case 2: // short *
sp = (unsigned short *)(ptr & 0xFFFFFFFE); // align with even boundary sp = (unsigned short *)(ptr & 0xFFFFFFFE); // align with even boundary
ReturnValue->Val->Integer = (int)((unsigned short)*sp); ReturnValue->Val->Integer = (int)((unsigned short)*sp);
break; break;
case 4: // int * case 4: // int *
ip = (unsigned int *)(ptr & 0xFFFFFFFC); // aling with quad boundary ip = (unsigned int *)(ptr & 0xFFFFFFFC); // aling with quad boundary
ReturnValue->Val->Integer = (int)*ip; ReturnValue->Val->Integer = (int)*ip;
break; break;
default: default:
ReturnValue->Val->Integer = 0; ReturnValue->Val->Integer = 0;
break; break;
} }
} }
@ -128,20 +128,20 @@ void Cpoke(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
size = Param[1]->Val->Integer; size = Param[1]->Val->Integer;
val = Param[2]->Val->Integer; val = Param[2]->Val->Integer;
switch (size) { switch (size) {
case 1: // char * case 1: // char *
cp = (unsigned char *)ptr; cp = (unsigned char *)ptr;
*cp = (unsigned char)(val & 0x000000FF); *cp = (unsigned char)(val & 0x000000FF);
break; break;
case 2: // short * case 2: // short *
sp = (unsigned short *)(ptr & 0xFFFFFFFE); sp = (unsigned short *)(ptr & 0xFFFFFFFE);
*sp = (unsigned short)(val & 0x0000FFFF); *sp = (unsigned short)(val & 0x0000FFFF);
break; break;
case 4: // int * case 4: // int *
ip = (unsigned int *)(ptr & 0xFFFFFFFC); ip = (unsigned int *)(ptr & 0xFFFFFFFC);
*ip = val; *ip = val;
break; break;
default: // don't bother with bad value default: // don't bother with bad value
break; break;
} }
} }
@ -228,15 +228,15 @@ void Claser(struct ParseState *Parser, struct Value *ReturnValue, struct Value *
{ {
*pPORTHIO &= 0xFD7F; // turn off both lasers *pPORTHIO &= 0xFD7F; // turn off both lasers
switch (Param[0]->Val->Integer) { switch (Param[0]->Val->Integer) {
case 1: case 1:
*pPORTHIO |= 0x0080; // turn on left laser *pPORTHIO |= 0x0080; // turn on left laser
break; break;
case 2: case 2:
*pPORTHIO |= 0x0200; // turn on right laser *pPORTHIO |= 0x0200; // turn on right laser
break; break;
case 3: case 3:
*pPORTHIO |= 0x0280; // turn on both lasers *pPORTHIO |= 0x0280; // turn on both lasers
break; break;
} }
} }
@ -446,15 +446,15 @@ void Canalog(struct ParseState *Parser, struct Value *ReturnValue, struct Value
ProgramFail(NULL, "analog(): invalid channel"); ProgramFail(NULL, "analog(): invalid channel");
device_id = 0; device_id = 0;
switch (ix / 10) { switch (ix / 10) {
case 0: case 0:
device_id = 0x20; // channels 1-8 device_id = 0x20; // channels 1-8
break; break;
case 1: case 1:
device_id = 0x23; // channels 11-18 device_id = 0x23; // channels 11-18
break; break;
case 2: case 2:
device_id = 0x24; // channels 21-28 device_id = 0x24; // channels 21-28
break; break;
} }
channel = ix % 10; channel = ix % 10;
if ((channel<1) || (channel>8)) if ((channel<1) || (channel>8))
@ -743,64 +743,64 @@ struct LibraryFunction PlatformLibrary[] =
/* list of all library functions included with srv1.h */ /* list of all library functions included with srv1.h */
struct LibraryFunction SRV1Functions[] = struct LibraryFunction SRV1Functions[] =
{ {
{ Csignal, "int signal();" }, {Csignal, "int signal();"},
{ Cinput, "int input();" }, {Cinput, "int input();"},
{ Cdelay, "void delay(int);" }, {Cdelay, "void delay(int);"},
{ Crand, "int rand(int);" }, {Crand, "int rand(int);"},
{ Ctime, "int time();" }, {Ctime, "int time();"},
{ Ciodir, "void iodir(int);" }, {Ciodir, "void iodir(int);"},
{ Cioread, "int ioread();" }, {Cioread, "int ioread();"},
{ Ciowrite, "void iowrite(int);" }, {Ciowrite, "void iowrite(int);"},
{ Cpeek, "int peek(int, int);" }, {Cpeek, "int peek(int, int);"},
{ Cpoke, "void poke(int, int, int);" }, {Cpoke, "void poke(int, int, int);"},
{ Cmotors, "void motors(int, int);" }, {Cmotors, "void motors(int, int);"},
{ Cmotors2, "void motors2(int, int);" }, {Cmotors2, "void motors2(int, int);"},
{ Cservos, "void servos(int, int);" }, {Cservos, "void servos(int, int);"},
{ Cservos2, "void servos2(int, int);" }, {Cservos2, "void servos2(int, int);"},
{ Cencoders, "void encoders();" }, {Cencoders, "void encoders();"},
{ Claser, "void laser(int);" }, {Claser, "void laser(int);"},
{ Csonar, "int sonar(int);" }, {Csonar, "int sonar(int);"},
{ Crange, "int range();" }, {Crange, "int range();"},
{ Cbattery, "int battery();" }, {Cbattery, "int battery();"},
{ Cvcolor, "void vcolor(int, int, int, int, int, int, int);" }, {Cvcolor, "void vcolor(int, int, int, int, int, int, int);"},
{ Cvfind, "int vfind(int, int, int, int, int);" }, {Cvfind, "int vfind(int, int, int, int, int);"},
{ Cvcam, "void vcam(int);" }, {Cvcam, "void vcam(int);"},
{ Cvcap, "void vcap();" }, {Cvcap, "void vcap();"},
{ Cvrcap, "void vrcap();" }, {Cvrcap, "void vrcap();"},
{ Cvdiff, "void vdiff(int);" }, {Cvdiff, "void vdiff(int);"},
{ Cvpix, "void vpix(int, int);" }, {Cvpix, "void vpix(int, int);"},
{ Cvscan, "int vscan(int, int);" }, {Cvscan, "int vscan(int, int);"},
{ Cvmean, "void vmean();" }, {Cvmean, "void vmean();"},
{ Cvblob, "int vblob(int, int);" }, {Cvblob, "int vblob(int, int);"},
{ Cvjpeg, "int vjpeg(int);" }, {Cvjpeg, "int vjpeg(int);"},
{ Cvsend, "void vsend(int);" }, {Cvsend, "void vsend(int);"},
{ Ccompass, "int compass();" }, {Ccompass, "int compass();"},
{ Canalog, "int analog(int);" }, {Canalog, "int analog(int);"},
{ Ctilt, "int tilt(int);" }, {Ctilt, "int tilt(int);"},
{ Cgps, "void gps();" }, {Cgps, "void gps();"},
{ Creadi2c, "int readi2c(int, int);" }, {Creadi2c, "int readi2c(int, int);"},
{ Creadi2c2, "int readi2c2(int, int);" }, {Creadi2c2, "int readi2c2(int, int);"},
{ Cwritei2c, "void writei2c(int, int, int);" }, {Cwritei2c, "void writei2c(int, int, int);"},
{ Csin, "int sin(int);" }, {Csin, "int sin(int);"},
{ Ccos, "int cos(int);" }, {Ccos, "int cos(int);"},
{ Ctan, "int tan(int);" }, {Ctan, "int tan(int);"},
{ Casin, "int asin(int, int);" }, {Casin, "int asin(int, int);"},
{ Cacos, "int acos(int, int);" }, {Cacos, "int acos(int, int);"},
{ Catan, "int atan(int, int);" }, {Catan, "int atan(int, int);"},
{ Cgps_head, "int gps_head(int, int, int, int);" }, {Cgps_head, "int gps_head(int, int, int, int);"},
{ Cgps_dist, "int gps_dist(int, int, int, int);" }, {Cgps_dist, "int gps_dist(int, int, int, int);"},
{ Csqrt, "int sqrt(int);" }, {Csqrt, "int sqrt(int);"},
{ Cnnshow, "void nnshow(int);" }, {Cnnshow, "void nnshow(int);"},
{ Cnnset, "void nnset(int, int, int, int, int, int, int, int, int);" }, {Cnnset, "void nnset(int, int, int, int, int, int, int, int, int);"},
{ Cnninit, "void nninit();" }, {Cnninit, "void nninit();"},
{ Cnntrain, "void nntrain();" }, {Cnntrain, "void nntrain();"},
{ Cnntest, "int nntest(int, int, int, int, int, int, int, int);" }, {Cnntest, "int nntest(int, int, int, int, int, int, int, int);"},
{ Cnnmatchblob, "int nnmatchblob(int);" }, {Cnnmatchblob, "int nnmatchblob(int);"},
{ Cnnlearnblob, "void nnlearnblob(int);" }, {Cnnlearnblob, "void nnlearnblob(int);"},
{ Cautorun, "void autorun(int);" }, {Cautorun, "void autorun(int);"},
{ Clineno, "int lineno();" }, {Clineno, "int lineno();"},
{ Cerrormsg, "void errormsg(char *);" }, {Cerrormsg, "void errormsg(char *);"},
{ NULL, NULL } {NULL, NULL}
}; };
void PlatformLibraryInit() void PlatformLibraryInit()

View file

@ -177,21 +177,21 @@ void Cpeek(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
ptr = Param[0]->Val->Integer; ptr = Param[0]->Val->Integer;
size = Param[1]->Val->Integer; size = Param[1]->Val->Integer;
switch (size) { switch (size) {
case 1: // char * case 1: // char *
cp = (unsigned char *)ptr; cp = (unsigned char *)ptr;
ReturnValue->Val->Integer = (int)((unsigned int)*cp); ReturnValue->Val->Integer = (int)((unsigned int)*cp);
break; break;
case 2: // short * case 2: // short *
sp = (unsigned short *)(ptr & 0xFFFFFFFE); // align with even boundary sp = (unsigned short *)(ptr & 0xFFFFFFFE); // align with even boundary
ReturnValue->Val->Integer = (int)((unsigned short)*sp); ReturnValue->Val->Integer = (int)((unsigned short)*sp);
break; break;
case 4: // int * case 4: // int *
ip = (unsigned int *)(ptr & 0xFFFFFFFC); // aling with quad boundary ip = (unsigned int *)(ptr & 0xFFFFFFFC); // aling with quad boundary
ReturnValue->Val->Integer = (int)*ip; ReturnValue->Val->Integer = (int)*ip;
break; break;
default: default:
ReturnValue->Val->Integer = 0; ReturnValue->Val->Integer = 0;
break; break;
} }
} }
@ -208,20 +208,20 @@ void Cpoke(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
size = Param[1]->Val->Integer; size = Param[1]->Val->Integer;
val = Param[2]->Val->Integer; val = Param[2]->Val->Integer;
switch (size) { switch (size) {
case 1: // char * case 1: // char *
cp = (unsigned char *)ptr; cp = (unsigned char *)ptr;
*cp = (unsigned char)(val & 0x000000FF); *cp = (unsigned char)(val & 0x000000FF);
break; break;
case 2: // short * case 2: // short *
sp = (unsigned short *)(ptr & 0xFFFFFFFE); sp = (unsigned short *)(ptr & 0xFFFFFFFE);
*sp = (unsigned short)(val & 0x0000FFFF); *sp = (unsigned short)(val & 0x0000FFFF);
break; break;
case 4: // int * case 4: // int *
ip = (unsigned int *)(ptr & 0xFFFFFFFC); ip = (unsigned int *)(ptr & 0xFFFFFFFC);
*ip = val; *ip = val;
break; break;
default: // don't bother with bad value default: // don't bother with bad value
break; break;
} }
} }
@ -344,15 +344,15 @@ void Claser(struct ParseState *Parser, struct Value *ReturnValue, struct Value *
{ {
*pPORTHIO &= 0xFD7F; // turn off both lasers *pPORTHIO &= 0xFD7F; // turn off both lasers
switch (Param[0]->Val->Integer) { switch (Param[0]->Val->Integer) {
case 1: case 1:
*pPORTHIO |= 0x0080; // turn on left laser *pPORTHIO |= 0x0080; // turn on left laser
break; break;
case 2: case 2:
*pPORTHIO |= 0x0200; // turn on right laser *pPORTHIO |= 0x0200; // turn on right laser
break; break;
case 3: case 3:
*pPORTHIO |= 0x0280; // turn on both lasers *pPORTHIO |= 0x0280; // turn on both lasers
break; break;
} }
} }
@ -869,76 +869,76 @@ void Cerrormsg (struct ParseState *Parser, struct Value *ReturnValue, struct Val
/* list of all library functions and their prototypes */ /* list of all library functions and their prototypes */
struct LibraryFunction PlatformLibrary[] = struct LibraryFunction PlatformLibrary[] =
{ {
{ Csignal, "int signal();" }, {Csignal, "int signal();"},
{ Csignal1, "int signal1();" }, {Csignal1, "int signal1();"},
{ Cinput, "int input();" }, {Cinput, "int input();"},
{ Cinput1, "int input1();" }, {Cinput1, "int input1();"},
{ Cinit_uart1, "void init_uart1(int);" }, {Cinit_uart1, "void init_uart1(int);"},
{ Cread_int, "int read_int();" }, {Cread_int, "int read_int();"},
{ Cread_str, "int read_str(char *);" }, {Cread_str, "int read_str(char *);"},
{ Coutput, "void output(int);" }, {Coutput, "void output(int);"},
{ Coutput1, "void output1(int);" }, {Coutput1, "void output1(int);"},
{ Cdelay, "void delay(int);" }, {Cdelay, "void delay(int);"},
{ Crand, "int rand(int);" }, {Crand, "int rand(int);"},
{ Ctime, "int time();" }, {Ctime, "int time();"},
{ Ciodir, "void iodir(int);" }, {Ciodir, "void iodir(int);"},
{ Cioread, "int ioread();" }, {Cioread, "int ioread();"},
{ Ciowrite, "void iowrite(int);" }, {Ciowrite, "void iowrite(int);"},
{ Cpeek, "int peek(int, int);" }, {Cpeek, "int peek(int, int);"},
{ Cpoke, "void poke(int, int, int);" }, {Cpoke, "void poke(int, int, int);"},
{ Cmotors, "void motors(int, int);" }, {Cmotors, "void motors(int, int);"},
{ Cmotors2, "void motors2(int, int);" }, {Cmotors2, "void motors2(int, int);"},
{ Cmotorx, "void motorx(int, int);" }, {Cmotorx, "void motorx(int, int);"},
{ Cservos, "void servos(int, int);" }, {Cservos, "void servos(int, int);"},
{ Cservos2, "void servos2(int, int);" }, {Cservos2, "void servos2(int, int);"},
{ Cencoders, "void encoders();" }, {Cencoders, "void encoders();"},
{ Cencoderx, "int encoderx(int);" }, {Cencoderx, "int encoderx(int);"},
{ Claser, "void laser(int);" }, {Claser, "void laser(int);"},
{ Csonar, "int sonar(int);" }, {Csonar, "int sonar(int);"},
{ Crange, "int range();" }, {Crange, "int range();"},
{ Cbattery, "int battery();" }, {Cbattery, "int battery();"},
{ Cvcolor, "void vcolor(int, int, int, int, int, int, int);" }, {Cvcolor, "void vcolor(int, int, int, int, int, int, int);"},
{ Cvfind, "int vfind(int, int, int, int, int);" }, {Cvfind, "int vfind(int, int, int, int, int);"},
{ Cvcam, "void vcam(int);" }, {Cvcam, "void vcam(int);"},
{ Cvcap, "void vcap();" }, {Cvcap, "void vcap();"},
{ Cvrcap, "void vrcap();" }, {Cvrcap, "void vrcap();"},
{ Cvdiff, "void vdiff(int);" }, {Cvdiff, "void vdiff(int);"},
{ Cvpix, "void vpix(int, int);" }, {Cvpix, "void vpix(int, int);"},
{ Cvscan, "int vscan(int, int);" }, {Cvscan, "int vscan(int, int);"},
{ Cvmean, "void vmean();" }, {Cvmean, "void vmean();"},
{ Cvblob, "int vblob(int, int);" }, {Cvblob, "int vblob(int, int);"},
{ Cvjpeg, "int vjpeg(int);" }, {Cvjpeg, "int vjpeg(int);"},
{ Cvsend, "void vsend(int);" }, {Cvsend, "void vsend(int);"},
{ Ccompass, "int compass();" }, {Ccompass, "int compass();"},
{ Ccompassx, "int compassx();" }, {Ccompassx, "int compassx();"},
{ Ccompassxcal, "void compassxcal(int, int, int, int, int);" }, {Ccompassxcal, "void compassxcal(int, int, int, int, int);"},
{ Canalog, "int analog(int);" }, {Canalog, "int analog(int);"},
{ Canalogx, "int analogx(int);" }, {Canalogx, "int analogx(int);"},
{ Ctilt, "int tilt(int);" }, {Ctilt, "int tilt(int);"},
{ Cgps, "void gps();" }, {Cgps, "void gps();"},
{ Creadi2c, "int readi2c(int, int);" }, {Creadi2c, "int readi2c(int, int);"},
{ Creadi2c2, "int readi2c2(int, int);" }, {Creadi2c2, "int readi2c2(int, int);"},
{ Cwritei2c, "void writei2c(int, int, int);" }, {Cwritei2c, "void writei2c(int, int, int);"},
{ Cabs, "int abs(int);" }, {Cabs, "int abs(int);"},
{ Csin, "int sin(int);" }, {Csin, "int sin(int);"},
{ Ccos, "int cos(int);" }, {Ccos, "int cos(int);"},
{ Ctan, "int tan(int);" }, {Ctan, "int tan(int);"},
{ Casin, "int asin(int, int);" }, {Casin, "int asin(int, int);"},
{ Cacos, "int acos(int, int);" }, {Cacos, "int acos(int, int);"},
{ Catan, "int atan(int, int);" }, {Catan, "int atan(int, int);"},
{ Cgps_head, "int gps_head(int, int, int, int);" }, {Cgps_head, "int gps_head(int, int, int, int);"},
{ Cgps_dist, "int gps_dist(int, int, int, int);" }, {Cgps_dist, "int gps_dist(int, int, int, int);"},
{ Csqrt, "int sqrt(int);" }, {Csqrt, "int sqrt(int);"},
{ Cnnshow, "void nnshow(int);" }, {Cnnshow, "void nnshow(int);"},
{ Cnnset, "void nnset(int, int, int, int, int, int, int, int, int);" }, {Cnnset, "void nnset(int, int, int, int, int, int, int, int, int);"},
{ Cnninit, "void nninit();" }, {Cnninit, "void nninit();"},
{ Cnntrain, "void nntrain();" }, {Cnntrain, "void nntrain();"},
{ Cnntest, "int nntest(int, int, int, int, int, int, int, int);" }, {Cnntest, "int nntest(int, int, int, int, int, int, int, int);"},
{ Cnnmatchblob, "int nnmatchblob(int);" }, {Cnnmatchblob, "int nnmatchblob(int);"},
{ Cnnlearnblob, "void nnlearnblob(int);" }, {Cnnlearnblob, "void nnlearnblob(int);"},
{ Cautorun, "void autorun(int);" }, {Cautorun, "void autorun(int);"},
{ Clineno, "int lineno();" }, {Clineno, "int lineno();"},
{ Cerrormsg, "void errormsg(char *);" }, {Cerrormsg, "void errormsg(char *);"},
{ NULL, NULL } {NULL, NULL}
}; };

View file

@ -18,9 +18,9 @@ void Clineno (struct ParseState *Parser, struct Value *ReturnValue, struct Value
/* list of all library functions and their prototypes */ /* list of all library functions and their prototypes */
struct LibraryFunction UnixFunctions[] = struct LibraryFunction UnixFunctions[] =
{ {
{ Ctest, "void test(int);" }, {Ctest, "void test(int);"},
{ Clineno, "int lineno();" }, {Clineno, "int lineno();"},
{ NULL, NULL } {NULL, NULL}
}; };
void PlatformLibraryInit(Picoc *pc) void PlatformLibraryInit(Picoc *pc)

View file

@ -62,10 +62,8 @@ char *PlatformReadFile(Picoc *pc, const char *FileName)
ReadText[BytesRead] = '\0'; ReadText[BytesRead] = '\0';
fclose(InFile); fclose(InFile);
if ((ReadText[0] == '#') && (ReadText[1] == '!')) if ((ReadText[0] == '#') && (ReadText[1] == '!')) {
{ for (p = ReadText; (*p != '\r') && (*p != '\n'); ++p) {
for (p = ReadText; (*p != '\r') && (*p != '\n'); ++p)
{
*p = ' '; *p = ' ';
} }
} }

View file

@ -22,8 +22,7 @@ char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt)
// If the first character is \n or \r, eat it // If the first character is \n or \r, eat it
ch = getch(); ch = getch();
if (ch == '\n' || ch == '\r') if (ch == '\n' || ch == '\r') {
{
// And get the next character // And get the next character
ch = getch(); ch = getch();
} }

View file

@ -39,8 +39,7 @@ void PlatformCleanup(Picoc *pc)
char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt) char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt)
{ {
#ifdef USE_READLINE #ifdef USE_READLINE
if (Prompt != NULL) if (Prompt != NULL) {
{
/* use GNU readline to read the line */ /* use GNU readline to read the line */
char *InLine = readline(Prompt); char *InLine = readline(Prompt);
if (InLine == NULL) if (InLine == NULL)
@ -105,10 +104,8 @@ char *PlatformReadFile(Picoc *pc, const char *FileName)
ReadText[BytesRead] = '\0'; ReadText[BytesRead] = '\0';
fclose(InFile); fclose(InFile);
if ((ReadText[0] == '#') && (ReadText[1] == '!')) if ((ReadText[0] == '#') && (ReadText[1] == '!')) {
{ for (p = ReadText; (*p != '\r') && (*p != '\n'); ++p) {
for (p = ReadText; (*p != '\r') && (*p != '\n'); ++p)
{
*p = ' '; *p = ' ';
} }
} }
@ -122,8 +119,7 @@ void PicocPlatformScanFile(Picoc *pc, const char *FileName)
char *SourceStr = PlatformReadFile(pc, FileName); char *SourceStr = PlatformReadFile(pc, FileName);
/* ignore "#!/path/to/picoc" .. by replacing the "#!" with "//" */ /* ignore "#!/path/to/picoc" .. by replacing the "#!" with "//" */
if (SourceStr != NULL && SourceStr[0] == '#' && SourceStr[1] == '!') if (SourceStr != NULL && SourceStr[0] == '#' && SourceStr[1] == '!') {
{
SourceStr[0] = '/'; SourceStr[0] = '/';
SourceStr[1] = '/'; SourceStr[1] = '/';
} }