formatting

This commit is contained in:
Joseph Poirier 2015-06-07 01:28:10 -05:00
parent af9b423aaa
commit e0775801f9
9 changed files with 282 additions and 261 deletions

View file

@ -77,9 +77,7 @@ void PrintType(struct ValueType *Typ, IOFILE *Stream)
} }
} }
#ifdef BUILTIN_MINI_STDLIB #ifdef BUILTIN_MINI_STDLIB
/* /*
* This is a simplified standard library for small embedded systems. It doesn't require * This is a simplified standard library for small embedded systems. It doesn't require
* a system stdio library to operate. * a system stdio library to operate.
@ -100,9 +98,9 @@ void BasicIOInit(Picoc *pc)
void CLibraryInit(Picoc *pc) void CLibraryInit(Picoc *pc)
{ {
/* define some constants */ /* define some constants */
VariableDefinePlatformVar(pc, NULL, "NULL", &IntType, (union AnyValue *)&ZeroValue, FALSE); VariableDefinePlatformVar(pc, NULL, "NULL", &IntType, (union AnyValue*)&ZeroValue, FALSE);
VariableDefinePlatformVar(pc, NULL, "TRUE", &IntType, (union AnyValue *)&TRUEValue, FALSE); VariableDefinePlatformVar(pc, NULL, "TRUE", &IntType, (union AnyValue*)&TRUEValue, FALSE);
VariableDefinePlatformVar(pc, NULL, "FALSE", &IntType, (union AnyValue *)&ZeroValue, FALSE); VariableDefinePlatformVar(pc, NULL, "FALSE", &IntType, (union AnyValue*)&ZeroValue, FALSE);
} }
/* stream for writing into strings */ /* stream for writing into strings */
@ -276,12 +274,10 @@ void GenericPrintf(struct ParseState *Parser, struct Value *ReturnValue, struct
case 's': case 's':
{ {
char *Str; char *Str;
if (NextArg->Typ->Base == TypePointer) if (NextArg->Typ->Base == TypePointer)
Str = NextArg->Val->Pointer; Str = NextArg->Val->Pointer;
else else
Str = &NextArg->Val->ArrayMem[0]; Str = &NextArg->Val->ArrayMem[0];
if (Str == NULL) if (Str == NULL)
PrintStr("NULL", Stream); PrintStr("NULL", Stream);
else else
@ -603,53 +599,53 @@ void LibMemcmp(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
/* list of all library functions and their prototypes */ /* list of all library functions and their prototypes */
struct LibraryFunction CLibrary[] = struct LibraryFunction CLibrary[] =
{ {
{ LibPrintf, "void printf(char *, ...);" }, LibPrintf, "void printf(char *, ...);"},
{ LibSPrintf, "char *sprintf(char *, char *, ...);" }, LibSPrintf, "char *sprintf(char *, char *, ...);"},
{ LibGets, "char *gets(char *);" }, LibGets, "char *gets(char *);"},
{ LibGetc, "int getchar();" }, LibGetc, "int getchar();"},
{ LibExit, "void exit(int);" }, LibExit, "void exit(int);"},
#ifdef PICOC_LIBRARY #ifdef PICOC_LIBRARY
{ LibSin, "float sin(float);" }, {LibSin, "float sin(float);"},
{ LibCos, "float cos(float);" }, {LibCos, "float cos(float);"},
{ LibTan, "float tan(float);" }, {LibTan, "float tan(float);"},
{ LibAsin, "float asin(float);" }, {LibAsin, "float asin(float);"},
{ LibAcos, "float acos(float);" }, {LibAcos, "float acos(float);"},
{ LibAtan, "float atan(float);" }, {LibAtan, "float atan(float);"},
{ LibSinh, "float sinh(float);" }, {LibSinh, "float sinh(float);"},
{ LibCosh, "float cosh(float);" }, {LibCosh, "float cosh(float);"},
{ LibTanh, "float tanh(float);" }, {LibTanh, "float tanh(float);"},
{ LibExp, "float exp(float);" }, {LibExp, "float exp(float);"},
{ LibFabs, "float fabs(float);" }, {LibFabs, "float fabs(float);"},
{ LibLog, "float log(float);" }, {LibLog, "float log(float);"},
{ LibLog10, "float log10(float);" }, {LibLog10, "float log10(float);"},
{ LibPow, "float pow(float,float);" }, {LibPow, "float pow(float,float);"},
{ LibSqrt, "float sqrt(float);" }, {LibSqrt, "float sqrt(float);"},
{ LibRound, "float round(float);" }, {LibRound, "float round(float);"},
{ LibCeil, "float ceil(float);" }, {LibCeil, "float ceil(float);"},
{ LibFloor, "float floor(float);" }, {LibFloor, "float floor(float);"},
#endif #endif
{ LibMalloc, "void *malloc(int);" }, {LibMalloc, "void *malloc(int);"},
#ifndef NO_CALLOC #ifndef NO_CALLOC
{ LibCalloc, "void *calloc(int,int);" }, {LibCalloc, "void *calloc(int,int);"},
#endif #endif
#ifndef NO_REALLOC #ifndef NO_REALLOC
{ LibRealloc, "void *realloc(void *,int);" }, {LibRealloc, "void *realloc(void *,int);"},
#endif #endif
{ LibFree, "void free(void *);" }, {LibFree, "void free(void *);"},
#ifndef NO_STRING_FUNCTIONS #ifndef NO_STRING_FUNCTIONS
{ LibStrcpy, "void strcpy(char *,char *);" }, {LibStrcpy, "void strcpy(char *,char *);"},
{ LibStrncpy, "void strncpy(char *,char *,int);" }, {LibStrncpy, "void strncpy(char *,char *,int);"},
{ LibStrcmp, "int strcmp(char *,char *);" }, {LibStrcmp, "int strcmp(char *,char *);"},
{ LibStrncmp, "int strncmp(char *,char *,int);" }, {LibStrncmp, "int strncmp(char *,char *,int);"},
{ LibStrcat, "void strcat(char *,char *);" }, {LibStrcat, "void strcat(char *,char *);"},
{ LibIndex, "char *index(char *,int);" }, {LibIndex, "char *index(char *,int);"},
{ LibRindex, "char *rindex(char *,int);" }, {LibRindex, "char *rindex(char *,int);"},
{ LibStrlen, "int strlen(char *);" }, {LibStrlen, "int strlen(char *);"},
{ LibMemset, "void memset(void *,int,int);" }, {LibMemset, "void memset(void *,int,int);"},
{ LibMemcpy, "void memcpy(void *,void *,int);" }, {LibMemcpy, "void memcpy(void *,void *,int);"},
{ LibMemcmp, "int memcmp(void *,void *,int);" }, {LibMemcmp, "int memcmp(void *,void *,int);"},
#endif #endif
{ NULL, NULL } {NULL, NULL}
}; };
#endif /* BUILTIN_MINI_STDLIB */ #endif /* BUILTIN_MINI_STDLIB */

View file

@ -400,91 +400,91 @@ typedef int intptr_t;\
/* all unistd.h functions */ /* all unistd.h functions */
struct LibraryFunction UnistdFunctions[] = struct LibraryFunction UnistdFunctions[] =
{ {
{ UnistdAccess, "int access(char *, int);" }, {UnistdAccess, "int access(char *, int);"},
{ UnistdAlarm, "unsigned int alarm(unsigned int);" }, {UnistdAlarm, "unsigned int alarm(unsigned int);"},
/* { UnistdBrk, "int brk(void *);" }, */ /* {UnistdBrk, "int brk(void *);"}, */
{ UnistdChdir, "int chdir(char *);" }, {UnistdChdir, "int chdir(char *);"},
{ UnistdChroot, "int chroot(char *);" }, {UnistdChroot, "int chroot(char *);"},
{ UnistdChown, "int chown(char *, uid_t, gid_t);" }, {UnistdChown, "int chown(char *, uid_t, gid_t);"},
{ UnistdClose, "int close(int);" }, {UnistdClose, "int close(int);"},
{ UnistdConfstr, "size_t confstr(int, char *, size_t);" }, {UnistdConfstr, "size_t confstr(int, char *, size_t);"},
{ UnistdCtermid, "char *ctermid(char *);" }, {UnistdCtermid, "char *ctermid(char *);"},
/* { UnistdCuserid, "char *cuserid(char *);" }, */ /* {UnistdCuserid, "char *cuserid(char *);"}, */
{ UnistdDup, "int dup(int);" }, {UnistdDup, "int dup(int);"},
{ UnistdDup2, "int dup2(int, int);" }, {UnistdDup2, "int dup2(int, int);"},
/* { UnistdEncrypt, "void encrypt(char[64], int);" }, */ /* {UnistdEncrypt, "void encrypt(char[64], int);"}, */
/* { UnistdExecl, "int execl(char *, char *, ...);" }, */ /* {UnistdExecl, "int execl(char *, char *, ...);"}, */
/* { UnistdExecle, "int execle(char *, char *, ...);" }, */ /* {UnistdExecle, "int execle(char *, char *, ...);"}, */
/* { UnistdExeclp, "int execlp(char *, char *, ...);" }, */ /* {UnistdExeclp, "int execlp(char *, char *, ...);"}, */
/* { UnistdExecv, "int execv(char *, char *[]);" }, */ /* {UnistdExecv, "int execv(char *, char *[]);"}, */
/* { UnistdExecve, "int execve(char *, char *[], char *[]);" }, */ /* {UnistdExecve, "int execve(char *, char *[], char *[]);"}, */
/* { UnistdExecvp, "int execvp(char *, char *[]);" }, */ /* {UnistdExecvp, "int execvp(char *, char *[]);"}, */
{ Unistd_Exit, "void _exit(int);" }, {Unistd_Exit, "void _exit(int);"},
{ UnistdFchown, "int fchown(int, uid_t, gid_t);" }, {UnistdFchown, "int fchown(int, uid_t, gid_t);"},
{ UnistdFchdir, "int fchdir(int);" }, {UnistdFchdir, "int fchdir(int);"},
{ UnistdFdatasync, "int fdatasync(int);" }, {UnistdFdatasync, "int fdatasync(int);"},
{ UnistdFork, "pid_t fork(void);" }, {UnistdFork, "pid_t fork(void);"},
{ UnistdFpathconf, "long fpathconf(int, int);" }, {UnistdFpathconf, "long fpathconf(int, int);"},
{ UnistdFsync, "int fsync(int);" }, {UnistdFsync, "int fsync(int);"},
{ UnistdFtruncate, "int ftruncate(int, off_t);" }, {UnistdFtruncate, "int ftruncate(int, off_t);"},
{ UnistdGetcwd, "char *getcwd(char *, size_t);" }, {UnistdGetcwd, "char *getcwd(char *, size_t);"},
{ UnistdGetdtablesize, "int getdtablesize(void);" }, {UnistdGetdtablesize, "int getdtablesize(void);"},
{ UnistdGetegid, "gid_t getegid(void);" }, {UnistdGetegid, "gid_t getegid(void);"},
{ UnistdGeteuid, "uid_t geteuid(void);" }, {UnistdGeteuid, "uid_t geteuid(void);"},
{ UnistdGetgid, "gid_t getgid(void);" }, {UnistdGetgid, "gid_t getgid(void);"},
/* { UnistdGetgroups, "int getgroups(int, gid_t []);" }, */ /* {UnistdGetgroups, "int getgroups(int, gid_t []);"}, */
{ UnistdGethostid, "long gethostid(void);" }, {UnistdGethostid, "long gethostid(void);"},
{ UnistdGetlogin, "char *getlogin(void);" }, {UnistdGetlogin, "char *getlogin(void);"},
{ UnistdGetlogin_r, "int getlogin_r(char *, size_t);" }, {UnistdGetlogin_r, "int getlogin_r(char *, size_t);"},
/* { UnistdGetopt, "int getopt(int, char * [], char *);" }, */ /* {UnistdGetopt, "int getopt(int, char * [], char *);"}, */
{ UnistdGetpagesize, "int getpagesize(void);" }, {UnistdGetpagesize, "int getpagesize(void);"},
{ UnistdGetpass, "char *getpass(char *);" }, {UnistdGetpass, "char *getpass(char *);"},
/* { UnistdGetpgid, "pid_t getpgid(pid_t);" }, */ /* {UnistdGetpgid, "pid_t getpgid(pid_t);"}, */
{ UnistdGetpgrp, "pid_t getpgrp(void);" }, {UnistdGetpgrp, "pid_t getpgrp(void);"},
{ UnistdGetpid, "pid_t getpid(void);" }, {UnistdGetpid, "pid_t getpid(void);"},
{ UnistdGetppid, "pid_t getppid(void);" }, {UnistdGetppid, "pid_t getppid(void);"},
/* { UnistdGetsid, "pid_t getsid(pid_t);" }, */ /* {UnistdGetsid, "pid_t getsid(pid_t);"}, */
{ UnistdGetuid, "uid_t getuid(void);" }, {UnistdGetuid, "uid_t getuid(void);"},
{ UnistdGetwd, "char *getwd(char *);" }, {UnistdGetwd, "char *getwd(char *);"},
{ UnistdIsatty, "int isatty(int);" }, {UnistdIsatty, "int isatty(int);"},
{ UnistdLchown, "int lchown(char *, uid_t, gid_t);" }, {UnistdLchown, "int lchown(char *, uid_t, gid_t);"},
{ UnistdLink, "int link(char *, char *);" }, {UnistdLink, "int link(char *, char *);"},
{ UnistdLockf, "int lockf(int, int, off_t);" }, {UnistdLockf, "int lockf(int, int, off_t);"},
{ UnistdLseek, "off_t lseek(int, off_t, int);" }, {UnistdLseek, "off_t lseek(int, off_t, int);"},
{ UnistdNice, "int nice(int);" }, {UnistdNice, "int nice(int);"},
{ UnistdPathconf, "long pathconf(char *, int);" }, {UnistdPathconf, "long pathconf(char *, int);"},
{ UnistdPause, "int pause(void);" }, {UnistdPause, "int pause(void);"},
/* { UnistdPipe, "int pipe(int [2]);" }, */ /* {UnistdPipe, "int pipe(int [2]);"}, */
/* { UnistdPread, "ssize_t pread(int, void *, size_t, off_t);" }, */ /* {UnistdPread, "ssize_t pread(int, void *, size_t, off_t);"}, */
/* { UnistdPthread_atfork,"int pthread_atfork(void (*)(void), void (*)(void), void(*)(void));" }, */ /* {UnistdPthread_atfork,"int pthread_atfork(void (*)(void), void (*)(void), void(*)(void));"}, */
/* { UnistdPwrite, "ssize_t pwrite(int, void *, size_t, off_t);" }, */ /* {UnistdPwrite, "ssize_t pwrite(int, void *, size_t, off_t);"}, */
{ UnistdRead, "ssize_t read(int, void *, size_t);" }, {UnistdRead, "ssize_t read(int, void *, size_t);"},
{ UnistdReadlink, "int readlink(char *, char *, size_t);" }, {UnistdReadlink, "int readlink(char *, char *, size_t);"},
{ UnistdRmdir, "int rmdir(char *);" }, {UnistdRmdir, "int rmdir(char *);"},
{ UnistdSbrk, "void *sbrk(intptr_t);" }, {UnistdSbrk, "void *sbrk(intptr_t);"},
{ UnistdSetgid, "int setgid(gid_t);" }, {UnistdSetgid, "int setgid(gid_t);"},
{ UnistdSetpgid, "int setpgid(pid_t, pid_t);" }, {UnistdSetpgid, "int setpgid(pid_t, pid_t);"},
{ UnistdSetpgrp, "pid_t setpgrp(void);" }, {UnistdSetpgrp, "pid_t setpgrp(void);"},
{ UnistdSetregid, "int setregid(gid_t, gid_t);" }, {UnistdSetregid, "int setregid(gid_t, gid_t);"},
{ UnistdSetreuid, "int setreuid(uid_t, uid_t);" }, {UnistdSetreuid, "int setreuid(uid_t, uid_t);"},
{ UnistdSetsid, "pid_t setsid(void);" }, {UnistdSetsid, "pid_t setsid(void);"},
{ UnistdSetuid, "int setuid(uid_t);" }, {UnistdSetuid, "int setuid(uid_t);"},
{ UnistdSleep, "unsigned int sleep(unsigned int);" }, {UnistdSleep, "unsigned int sleep(unsigned int);"},
/* { UnistdSwab, "void swab(void *, void *, ssize_t);" }, */ /* {UnistdSwab, "void swab(void *, void *, ssize_t);"}, */
{ UnistdSymlink, "int symlink(char *, char *);" }, {UnistdSymlink, "int symlink(char *, char *);"},
{ UnistdSync, "void sync(void);" }, {UnistdSync, "void sync(void);"},
{ UnistdSysconf, "long sysconf(int);" }, {UnistdSysconf, "long sysconf(int);"},
{ UnistdTcgetpgrp, "pid_t tcgetpgrp(int);" }, {UnistdTcgetpgrp, "pid_t tcgetpgrp(int);"},
{ UnistdTcsetpgrp, "int tcsetpgrp(int, pid_t);" }, {UnistdTcsetpgrp, "int tcsetpgrp(int, pid_t);"},
{ UnistdTruncate, "int truncate(char *, off_t);" }, {UnistdTruncate, "int truncate(char *, off_t);"},
{ UnistdTtyname, "char *ttyname(int);" }, {UnistdTtyname, "char *ttyname(int);"},
{ UnistdTtyname_r, "int ttyname_r(int, char *, size_t);" }, {UnistdTtyname_r, "int ttyname_r(int, char *, size_t);"},
{ UnistdUalarm, "useconds_t ualarm(useconds_t, useconds_t);" }, {UnistdUalarm, "useconds_t ualarm(useconds_t, useconds_t);"},
{ UnistdUnlink, "int unlink(char *);" }, {UnistdUnlink, "int unlink(char *);"},
{ UnistdUsleep, "int usleep(useconds_t);" }, {UnistdUsleep, "int usleep(useconds_t);"},
{ UnistdVfork, "pid_t vfork(void);" }, {UnistdVfork, "pid_t vfork(void);"},
{ UnistdWrite, "ssize_t write(int, void *, size_t);" }, {UnistdWrite, "ssize_t write(int, void *, size_t);"},
{ NULL, NULL } {NULL, NULL}
}; };
/* creates various system-dependent definitions */ /* creates various system-dependent definitions */

View file

@ -53,26 +53,51 @@ struct OpPrecedence
/* NOTE: the order of this array must correspond exactly to the order of these tokens in enum LexToken */ /* NOTE: the order of this array must correspond exactly to the order of these tokens in enum LexToken */
static struct OpPrecedence OperatorPrecedence[] = static struct OpPrecedence OperatorPrecedence[] =
{ {
/* TokenNone, */ { 0, 0, 0, "none" }, /* TokenNone, */ {0, 0, 0, "none"},
/* TokenComma, */ { 0, 0, 0, "," }, /* TokenComma, */ {0, 0, 0, ","},
/* TokenAssign, */ { 0, 0, 2, "=" }, /* TokenAddAssign, */ { 0, 0, 2, "+=" }, /* TokenSubtractAssign, */ { 0, 0, 2, "-=" }, /* TokenAssign, */ {0, 0, 2, "="},
/* TokenMultiplyAssign, */ { 0, 0, 2, "*=" }, /* TokenDivideAssign, */ { 0, 0, 2, "/=" }, /* TokenModulusAssign, */ { 0, 0, 2, "%=" }, /* TokenAddAssign, */ {0, 0, 2, "+="},
/* TokenShiftLeftAssign, */ { 0, 0, 2, "<<=" }, /* TokenShiftRightAssign, */ { 0, 0, 2, ">>=" }, /* TokenArithmeticAndAssign, */ { 0, 0, 2, "&=" }, /* TokenSubtractAssign, */ {0, 0, 2, "-="},
/* TokenArithmeticOrAssign, */ { 0, 0, 2, "|=" }, /* TokenArithmeticExorAssign, */ { 0, 0, 2, "^=" }, /* TokenMultiplyAssign, */ {0, 0, 2, "*="},
/* TokenQuestionMark, */ { 0, 0, 3, "?" }, /* TokenColon, */ { 0, 0, 3, ":" }, /* TokenDivideAssign, */ { 0, 0, 2, "/=" },
/* TokenLogicalOr, */ { 0, 0, 4, "||" }, /* TokenModulusAssign, */ { 0, 0, 2, "%=" },
/* TokenLogicalAnd, */ { 0, 0, 5, "&&" }, /* TokenShiftLeftAssign, */ {0, 0, 2, "<<="},
/* TokenArithmeticOr, */ { 0, 0, 6, "|" }, /* TokenShiftRightAssign, */ { 0, 0, 2, ">>=" },
/* TokenArithmeticExor, */ { 0, 0, 7, "^" }, /* TokenArithmeticAndAssign, */ { 0, 0, 2, "&=" },
/* TokenAmpersand, */ { 14, 0, 8, "&" }, /* TokenArithmeticOrAssign, */ {0, 0, 2, "|="},
/* TokenEqual, */ { 0, 0, 9, "==" }, /* TokenNotEqual, */ { 0, 0, 9, "!=" }, /* TokenArithmeticExorAssign, */ { 0, 0, 2, "^=" },
/* TokenLessThan, */ { 0, 0, 10, "<" }, /* TokenGreaterThan, */ { 0, 0, 10, ">" }, /* TokenLessEqual, */ { 0, 0, 10, "<=" }, /* TokenGreaterEqual, */ { 0, 0, 10, ">=" }, /* TokenQuestionMark, */ {0, 0, 3, "?"},
/* TokenShiftLeft, */ { 0, 0, 11, "<<" }, /* TokenShiftRight, */ { 0, 0, 11, ">>" }, /* TokenColon, */ {0, 0, 3, ":" },
/* TokenPlus, */ { 14, 0, 12, "+" }, /* TokenMinus, */ { 14, 0, 12, "-" }, /* TokenLogicalOr, */ {0, 0, 4, "||"},
/* TokenAsterisk, */ { 14, 0, 13, "*" }, /* TokenSlash, */ { 0, 0, 13, "/" }, /* TokenModulus, */ { 0, 0, 13, "%" }, /* TokenLogicalAnd, */ {0, 0, 5, "&&"},
/* TokenIncrement, */ { 14, 15, 0, "++" }, /* TokenDecrement, */ { 14, 15, 0, "--" }, /* TokenUnaryNot, */ { 14, 0, 0, "!" }, /* TokenUnaryExor, */ { 14, 0, 0, "~" }, /* TokenSizeof, */ { 14, 0, 0, "sizeof" }, /* TokenCast, */ { 14, 0, 0, "cast" }, /* TokenArithmeticOr, */ {0, 0, 6, "|"},
/* TokenLeftSquareBracket, */ { 0, 0, 15, "[" }, /* TokenRightSquareBracket, */ { 0, 15, 0, "]" }, /* TokenDot, */ { 0, 0, 15, "." }, /* TokenArrow, */ { 0, 0, 15, "->" }, /* TokenArithmeticExor, */ {0, 0, 7, "^"},
/* TokenOpenBracket, */ { 15, 0, 0, "(" }, /* TokenCloseBracket, */ { 0, 15, 0, ")" } /* TokenAmpersand, */ {14, 0, 8, "&"},
/* TokenEqual, */ {0, 0, 9, "=="},
/* TokenNotEqual, */ {0, 0, 9, "!="},
/* TokenLessThan, */ {0, 0, 10, "<"},
/* TokenGreaterThan, */ {0, 0, 10, ">"},
/* TokenLessEqual, */ {0, 0, 10, "<="},
/* TokenGreaterEqual, */ {0, 0, 10, ">="},
/* TokenShiftLeft, */ {0, 0, 11, "<<"},
/* TokenShiftRight, */ {0, 0, 11, ">>"},
/* TokenPlus, */ {14, 0, 12, "+"},
/* TokenMinus, */ {14, 0, 12, "-"},
/* TokenAsterisk, */ {14, 0, 13, "*"},
/* TokenSlash, */ {0, 0, 13, "/"},
/* TokenModulus, */ {0, 0, 13, "%"},
/* TokenIncrement, */ {14, 15, 0, "++"},
/* TokenDecrement, */ {14, 15, 0, "--"},
/* TokenUnaryNot, */ {14, 0, 0, "!"},
/* TokenUnaryExor, */ {14, 0, 0, "~"},
/* TokenSizeof, */ {14, 0, 0, "sizeof"},
/* TokenCast, */ {14, 0, 0, "cast"},
/* TokenLeftSquareBracket, */ {0, 0, 15, "["},
/* TokenRightSquareBracket, */ {0, 15, 0, "]"},
/* TokenDot, */ {0, 0, 15, "."},
/* TokenArrow, */ {0, 0, 15, "->"},
/* TokenOpenBracket, */ {15, 0, 0, "("},
/* TokenCloseBracket, */ {0, 15, 0, ")"}
}; };
void ExpressionParseFunctionCall(struct ParseState *Parser, struct ExpressionStack **StackTop, const char *FuncName, int RunIt); void ExpressionParseFunctionCall(struct ParseState *Parser, struct ExpressionStack **StackTop, const char *FuncName, int RunIt);

44
heap.c
View file

@ -10,7 +10,8 @@ void ShowBigList(Picoc *pc)
{ {
struct AllocNode *LPos; struct AllocNode *LPos;
printf("Heap: bottom=0x%lx 0x%lx-0x%lx, big freelist=", (long)pc->HeapBottom, (long)&(pc->HeapMemory)[0], (long)&(pc->HeapMemory)[HEAP_SIZE]); printf("Heap: bottom=0x%lx 0x%lx-0x%lx, big freelist=", (long)pc->HeapBottom,
(long)&(pc->HeapMemory)[0], (long)&(pc->HeapMemory)[HEAP_SIZE]);
for (LPos = pc->FreeListBig; LPos != NULL; LPos = LPos->NextFree) for (LPos = pc->FreeListBig; LPos != NULL; LPos = LPos->NextFree)
printf("0x%lx:%d ", (long)LPos, LPos->Size); printf("0x%lx:%d ", (long)LPos, LPos->Size);
@ -26,20 +27,20 @@ void HeapInit(Picoc *pc, int StackOrHeapSize)
#ifdef USE_MALLOC_STACK #ifdef USE_MALLOC_STACK
pc->HeapMemory = malloc(StackOrHeapSize); pc->HeapMemory = malloc(StackOrHeapSize);
pc->HeapBottom = NULL; /* the bottom of the (downward-growing) heap */ pc->HeapBottom = NULL; /* the bottom of the (downward-growing) heap */
pc->StackFrame = NULL; /* the current stack frame */ pc->StackFrame = NULL; /* the current stack frame */
pc->HeapStackTop = NULL; /* the top of the stack */ pc->HeapStackTop = NULL; /* the top of the stack */
#else #else
# ifdef SURVEYOR_HOST # ifdef SURVEYOR_HOST
pc->HeapMemory = (unsigned char *)C_HEAPSTART; /* all memory - stack and heap */ pc->HeapMemory = (unsigned char *)C_HEAPSTART; /* all memory - stack and heap */
pc->HeapBottom = (void *)C_HEAPSTART + HEAP_SIZE; /* the bottom of the (downward-growing) heap */ pc->HeapBottom = (void *)C_HEAPSTART + HEAP_SIZE; /* the bottom of the (downward-growing) heap */
pc->StackFrame = (void *)C_HEAPSTART; /* the current stack frame */ pc->StackFrame = (void *)C_HEAPSTART; /* the current stack frame */
pc->HeapStackTop = (void *)C_HEAPSTART; /* the top of the stack */ pc->HeapStackTop = (void *)C_HEAPSTART; /* the top of the stack */
pc->HeapMemStart = (void *)C_HEAPSTART; pc->HeapMemStart = (void *)C_HEAPSTART;
# else # else
pc->HeapBottom = &HeapMemory[HEAP_SIZE]; /* the bottom of the (downward-growing) heap */ pc->HeapBottom = &HeapMemory[HEAP_SIZE]; /* the bottom of the (downward-growing) heap */
pc->StackFrame = &HeapMemory[0]; /* the current stack frame */ pc->StackFrame = &HeapMemory[0]; /* the current stack frame */
pc->HeapStackTop = &HeapMemory[0]; /* the top of the stack */ pc->HeapStackTop = &HeapMemory[0]; /* the top of the stack */
# endif # endif
#endif #endif
@ -96,7 +97,8 @@ int HeapPopStack(Picoc *pc, void *Addr, int Size)
return FALSE; return FALSE;
#ifdef DEBUG_HEAP #ifdef DEBUG_HEAP
printf("HeapPopStack(0x%lx, %ld) back to 0x%lx\n", (unsigned long)Addr, (unsigned long)MEM_ALIGN(Size), (unsigned long)pc->HeapStackTop - ToLose); printf("HeapPopStack(0x%lx, %ld) back to 0x%lx\n", (unsigned long)Addr,
(unsigned long)MEM_ALIGN(Size), (unsigned long)pc->HeapStackTop - ToLose);
#endif #endif
pc->HeapStackTop = (void *)((char *)pc->HeapStackTop - ToLose); pc->HeapStackTop = (void *)((char *)pc->HeapStackTop - ToLose);
assert(Addr == NULL || pc->HeapStackTop == Addr); assert(Addr == NULL || pc->HeapStackTop == Addr);
@ -157,7 +159,7 @@ void *HeapAllocMem(Picoc *pc, int Size)
printf("allocating %d(%d) from bucket", Size, AllocSize); printf("allocating %d(%d) from bucket", Size, AllocSize);
#endif #endif
NewMem = pc->FreeListBucket[Bucket]; NewMem = pc->FreeListBucket[Bucket];
assert((unsigned long)NewMem >= (unsigned long)&(pc->HeapMemory)[0] && (unsigned char *)NewMem - &(pc->HeapMemory)[0] < HEAP_SIZE); assert((unsigned long)NewMem >= (unsigned long)&(pc->HeapMemory)[0] && (unsigned char*)NewMem - &(pc->HeapMemory)[0] < HEAP_SIZE);
pc->FreeListBucket[Bucket] = *(struct AllocNode **)NewMem; pc->FreeListBucket[Bucket] = *(struct AllocNode **)NewMem;
assert(pc->FreeListBucket[Bucket] == NULL || ((unsigned long)pc->FreeListBucket[Bucket] >= (unsigned long)&(pc->HeapMemory)[0] && (unsigned char *)pc->FreeListBucket[Bucket] - &(pc->HeapMemory)[0] < HEAP_SIZE)); assert(pc->FreeListBucket[Bucket] == NULL || ((unsigned long)pc->FreeListBucket[Bucket] >= (unsigned long)&(pc->HeapMemory)[0] && (unsigned char *)pc->FreeListBucket[Bucket] - &(pc->HeapMemory)[0] < HEAP_SIZE));
NewMem->Size = AllocSize; NewMem->Size = AllocSize;
@ -167,7 +169,7 @@ void *HeapAllocMem(Picoc *pc, int Size)
} }
if (*FreeNode != NULL) { if (*FreeNode != NULL) {
assert((unsigned long)*FreeNode >= (unsigned long)&(pc->HeapMemory)[0] && (unsigned char *)*FreeNode - &(pc->HeapMemory)[0] < HEAP_SIZE); assert((unsigned long)*FreeNode >= (unsigned long)&(pc->HeapMemory)[0] && (unsigned char*)*FreeNode - &(pc->HeapMemory)[0] < HEAP_SIZE);
assert((*FreeNode)->Size < HEAP_SIZE && (*FreeNode)->Size > 0); assert((*FreeNode)->Size < HEAP_SIZE && (*FreeNode)->Size > 0);
if ((*FreeNode)->Size < AllocSize + SPLIT_MEM_THRESHOLD) { if ((*FreeNode)->Size < AllocSize + SPLIT_MEM_THRESHOLD) {
/* close in size - reduce fragmentation by not splitting */ /* close in size - reduce fragmentation by not splitting */
@ -175,7 +177,7 @@ void *HeapAllocMem(Picoc *pc, int Size)
printf("allocating %d(%d) from freelist, no split (%d)", Size, AllocSize, (*FreeNode)->Size); printf("allocating %d(%d) from freelist, no split (%d)", Size, AllocSize, (*FreeNode)->Size);
#endif #endif
NewMem = *FreeNode; NewMem = *FreeNode;
assert((unsigned long)NewMem >= (unsigned long)&(pc->HeapMemory)[0] && (unsigned char *)NewMem - &(pc->HeapMemory)[0] < HEAP_SIZE); assert((unsigned long)NewMem >= (unsigned long)&(pc->HeapMemory)[0] && (unsigned char*)NewMem - &(pc->HeapMemory)[0] < HEAP_SIZE);
*FreeNode = NewMem->NextFree; *FreeNode = NewMem->NextFree;
} else { } else {
/* split this big memory chunk */ /* split this big memory chunk */
@ -183,7 +185,7 @@ void *HeapAllocMem(Picoc *pc, int Size)
printf("allocating %d(%d) from freelist, split chunk (%d)", Size, AllocSize, (*FreeNode)->Size); printf("allocating %d(%d) from freelist, split chunk (%d)", Size, AllocSize, (*FreeNode)->Size);
#endif #endif
NewMem = (void *)((char *)*FreeNode + (*FreeNode)->Size - AllocSize); NewMem = (void *)((char *)*FreeNode + (*FreeNode)->Size - AllocSize);
assert((unsigned long)NewMem >= (unsigned long)&(pc->HeapMemory)[0] && (unsigned char *)NewMem - &(pc->HeapMemory)[0] < HEAP_SIZE); assert((unsigned long)NewMem >= (unsigned long)&(pc->HeapMemory)[0] && (unsigned char*)NewMem - &(pc->HeapMemory)[0] < HEAP_SIZE);
(*FreeNode)->Size -= AllocSize; (*FreeNode)->Size -= AllocSize;
NewMem->Size = AllocSize; NewMem->Size = AllocSize;
} }
@ -193,17 +195,17 @@ void *HeapAllocMem(Picoc *pc, int Size)
if (NewMem == NULL) { if (NewMem == NULL) {
/* couldn't allocate from a freelist - try to increase the size of the heap area */ /* couldn't allocate from a freelist - try to increase the size of the heap area */
#ifdef DEBUG_HEAP #ifdef DEBUG_HEAP
printf("allocating %d(%d) at bottom of heap (0x%lx-0x%lx)", Size, AllocSize, (long)((char *)pc->HeapBottom - AllocSize), (long)HeapBottom); printf("allocating %d(%d) at bottom of heap (0x%lx-0x%lx)", Size, AllocSize, (long)((char*)pc->HeapBottom - AllocSize), (long)HeapBottom);
#endif #endif
if ((char *)pc->HeapBottom - AllocSize < (char *)pc->HeapStackTop) if ((char*)pc->HeapBottom - AllocSize < (char*)pc->HeapStackTop)
return NULL; return NULL;
pc->HeapBottom = (void *)((char *)pc->HeapBottom - AllocSize); pc->HeapBottom = (void*)((char*)pc->HeapBottom - AllocSize);
NewMem = pc->HeapBottom; NewMem = pc->HeapBottom;
NewMem->Size = AllocSize; NewMem->Size = AllocSize;
} }
ReturnMem = (void *)((char *)NewMem + MEM_ALIGN(sizeof(NewMem->Size))); ReturnMem = (void*)((char*)NewMem + MEM_ALIGN(sizeof(NewMem->Size)));
memset(ReturnMem, '\0', AllocSize - MEM_ALIGN(sizeof(NewMem->Size))); memset(ReturnMem, '\0', AllocSize - MEM_ALIGN(sizeof(NewMem->Size)));
#ifdef DEBUG_HEAP #ifdef DEBUG_HEAP
printf(" = %lx\n", (unsigned long)ReturnMem); printf(" = %lx\n", (unsigned long)ReturnMem);
@ -218,13 +220,13 @@ void HeapFreeMem(Picoc *pc, void *Mem)
#ifdef USE_MALLOC_HEAP #ifdef USE_MALLOC_HEAP
free(Mem); free(Mem);
#else #else
struct AllocNode *MemNode = (struct AllocNode *)((char *)Mem - MEM_ALIGN(sizeof(MemNode->Size))); struct AllocNode *MemNode = (struct AllocNode*)((char*)Mem - MEM_ALIGN(sizeof(MemNode->Size)));
int Bucket = MemNode->Size >> 2; int Bucket = MemNode->Size >> 2;
#ifdef DEBUG_HEAP #ifdef DEBUG_HEAP
printf("HeapFreeMem(0x%lx)\n", (unsigned long)Mem); printf("HeapFreeMem(0x%lx)\n", (unsigned long)Mem);
#endif #endif
assert((unsigned long)Mem >= (unsigned long)&(pc->HeapMemory)[0] && (unsigned char *)Mem - &(pc->HeapMemory)[0] < HEAP_SIZE); assert((unsigned long)Mem >= (unsigned long)&(pc->HeapMemory)[0] && (unsigned char*)Mem - &(pc->HeapMemory)[0] < HEAP_SIZE);
assert(MemNode->Size < HEAP_SIZE && MemNode->Size > 0); assert(MemNode->Size < HEAP_SIZE && MemNode->Size > 0);
if (Mem == NULL) if (Mem == NULL)
return; return;

85
lex.c
View file

@ -23,9 +23,9 @@
#define LEXER_INC(l) ( (l)->Pos++, (l)->CharacterPos++ ) #define LEXER_INC(l) ( (l)->Pos++, (l)->CharacterPos++ )
#define LEXER_INCN(l, n) ( (l)->Pos+=(n), (l)->CharacterPos+=(n) ) #define LEXER_INCN(l, n) ( (l)->Pos+=(n), (l)->CharacterPos+=(n) )
#define TOKEN_DATA_OFFSET 2 #define TOKEN_DATA_OFFSET (2)
#define MAX_CHAR_VALUE 255 /* maximum value which can be represented by a "char" data type */ #define MAX_CHAR_VALUE (255) /* maximum value which can be represented by a "char" data type */
struct ReservedWord struct ReservedWord
@ -36,49 +36,49 @@ struct ReservedWord
static struct ReservedWord ReservedWords[] = static struct ReservedWord ReservedWords[] =
{ {
{ "#define", TokenHashDefine }, {"#define", TokenHashDefine},
{ "#else", TokenHashElse }, {"#else", TokenHashElse},
{ "#endif", TokenHashEndif }, {"#endif", TokenHashEndif},
{ "#if", TokenHashIf }, {"#if", TokenHashIf},
{ "#ifdef", TokenHashIfdef }, {"#ifdef", TokenHashIfdef},
{ "#ifndef", TokenHashIfndef }, {"#ifndef", TokenHashIfndef},
{ "#include", TokenHashInclude }, {"#include", TokenHashInclude},
{ "auto", TokenAutoType }, {"auto", TokenAutoType},
{ "break", TokenBreak }, {"break", TokenBreak},
{ "case", TokenCase }, {"case", TokenCase},
{ "char", TokenCharType }, {"char", TokenCharType},
{ "continue", TokenContinue }, {"continue", TokenContinue},
{ "default", TokenDefault }, {"default", TokenDefault},
{ "delete", TokenDelete }, {"delete", TokenDelete},
{ "do", TokenDo }, {"do", TokenDo},
#ifndef NO_FP #ifndef NO_FP
{ "double", TokenDoubleType }, {"double", TokenDoubleType},
#endif #endif
{ "else", TokenElse }, {"else", TokenElse},
{ "enum", TokenEnumType }, {"enum", TokenEnumType},
{ "extern", TokenExternType }, {"extern", TokenExternType},
#ifndef NO_FP #ifndef NO_FP
{ "float", TokenFloatType }, {"float", TokenFloatType},
#endif #endif
{ "for", TokenFor }, {"for", TokenFor},
{ "goto", TokenGoto }, {"goto", TokenGoto},
{ "if", TokenIf }, {"if", TokenIf},
{ "int", TokenIntType }, {"int", TokenIntType},
{ "long", TokenLongType }, {"long", TokenLongType},
{ "new", TokenNew }, {"new", TokenNew},
{ "register", TokenRegisterType }, {"register", TokenRegisterType},
{ "return", TokenReturn }, {"return", TokenReturn},
{ "short", TokenShortType }, {"short", TokenShortType},
{ "signed", TokenSignedType }, {"signed", TokenSignedType},
{ "sizeof", TokenSizeof }, {"sizeof", TokenSizeof},
{ "static", TokenStaticType }, {"static", TokenStaticType},
{ "struct", TokenStructType }, {"struct", TokenStructType},
{ "switch", TokenSwitch }, {"switch", TokenSwitch},
{ "typedef", TokenTypedef }, {"typedef", TokenTypedef},
{ "union", TokenUnionType }, {"union", TokenUnionType},
{ "unsigned", TokenUnsignedType }, {"unsigned", TokenUnsignedType},
{ "void", TokenVoidType }, {"void", TokenVoidType},
{ "while", TokenWhile } {"while", TokenWhile}
}; };
@ -383,8 +383,7 @@ void LexSkipComment(struct LexState *Lexer, char NextChar, enum LexToken *Return
{ {
if (NextChar == '*') { if (NextChar == '*') {
/* conventional C comment */ /* conventional C comment */
while (Lexer->Pos != Lexer->End && (*(Lexer->Pos-1) != '*' || *Lexer->Pos != '/')) while (Lexer->Pos != Lexer->End && (*(Lexer->Pos-1) != '*' || *Lexer->Pos != '/')) {
{
if (*Lexer->Pos == '\n') if (*Lexer->Pos == '\n')
Lexer->EmitExtraNewlines++; Lexer->EmitExtraNewlines++;

View file

@ -24,7 +24,7 @@ int main(int argc, char **argv)
printf(PICOC_VERSION " \n" printf(PICOC_VERSION " \n"
"Format: picoc <file1.c>... [- <arg1>...] : run a program (calls main() to start it)\n" "Format: picoc <file1.c>... [- <arg1>...] : run a program (calls main() to start it)\n"
" picoc -s <file1.c>... [- <arg1>...] : script mode - runs the program without calling main()\n" " picoc -s <file1.c>... [- <arg1>...] : script mode - runs the program without calling main()\n"
" picoc -i : interactive mode\n"); " picoc -i : interactive mode\n");
exit(1); exit(1);
} }
@ -56,7 +56,6 @@ int main(int argc, char **argv)
return pc.PicocExitValue; return pc.PicocExitValue;
} }
#elif defined(SURVEYOR_HOST) #elif defined(SURVEYOR_HOST)
#define HEAP_SIZE C_HEAPSIZE #define HEAP_SIZE C_HEAPSIZE
#include <setjmp.h> #include <setjmp.h>
#include "../srv.h" #include "../srv.h"

14
picoc.h
View file

@ -34,16 +34,16 @@ extern int PicocExitBuf[];
#endif #endif
/* parse.c */ /* parse.c */
void PicocParse(Picoc *pc, const char *FileName, const char *Source, int SourceLen, int RunIt, int CleanupNow, int CleanupSource, int EnableDebugger); extern void PicocParse(Picoc *pc, const char *FileName, const char *Source, int SourceLen, int RunIt, int CleanupNow, int CleanupSource, int EnableDebugger);
void PicocParseInteractive(Picoc *pc); extern void PicocParseInteractive(Picoc *pc);
/* platform.c */ /* platform.c */
void PicocCallMain(Picoc *pc, int argc, char **argv); extern void PicocCallMain(Picoc *pc, int argc, char **argv);
void PicocInitialise(Picoc *pc, int StackSize); extern void PicocInitialise(Picoc *pc, int StackSize);
void PicocCleanup(Picoc *pc); extern void PicocCleanup(Picoc *pc);
void PicocPlatformScanFile(Picoc *pc, const char *FileName); extern void PicocPlatformScanFile(Picoc *pc, const char *FileName);
/* include.c */ /* include.c */
void PicocIncludeAllSystemHeaders(Picoc *pc); extern void PicocIncludeAllSystemHeaders(Picoc *pc);
#endif /* PICOC_H */ #endif /* PICOC_H */

54
type.c
View file

@ -48,7 +48,7 @@ struct ValueType *TypeGetMatching(Picoc *pc, struct ParseState *Parser, struct V
case TypePointer: Sizeof = sizeof(void *); AlignBytes = PointerAlignBytes; break; case TypePointer: Sizeof = sizeof(void *); AlignBytes = PointerAlignBytes; break;
case TypeArray: Sizeof = ArraySize * ParentType->Sizeof; AlignBytes = ParentType->AlignBytes; break; case TypeArray: Sizeof = ArraySize * ParentType->Sizeof; AlignBytes = ParentType->AlignBytes; break;
case TypeEnum: Sizeof = sizeof(int); AlignBytes = IntAlignBytes; break; case TypeEnum: Sizeof = sizeof(int); AlignBytes = IntAlignBytes; break;
default: Sizeof = 0; AlignBytes = 0; break; /* structs and unions will get bigger when we add members to them */ default: Sizeof = 0; AlignBytes = 0; break; /* structs and unions will get bigger when we add members to them */
} }
return TypeAdd(pc, Parser, ParentType, Base, ArraySize, Identifier, Sizeof, AlignBytes); return TypeAdd(pc, Parser, ParentType, Base, ArraySize, Identifier, Sizeof, AlignBytes);
@ -67,7 +67,7 @@ int TypeStackSizeValue(struct Value *Val)
int TypeSizeValue(struct Value *Val, int Compact) int TypeSizeValue(struct Value *Val, int Compact)
{ {
if (IS_INTEGER_NUMERIC(Val) && !Compact) if (IS_INTEGER_NUMERIC(Val) && !Compact)
return sizeof(ALIGN_TYPE); /* allow some extra room for type extension */ return sizeof(ALIGN_TYPE); /* allow some extra room for type extension */
else if (Val->Typ->Base != TypeArray) else if (Val->Typ->Base != TypeArray)
return Val->Typ->Sizeof; return Val->Typ->Sizeof;
else else
@ -78,7 +78,7 @@ int TypeSizeValue(struct Value *Val, int Compact)
int TypeSize(struct ValueType *Typ, int ArraySize, int Compact) int TypeSize(struct ValueType *Typ, int ArraySize, int Compact)
{ {
if (IS_INTEGER_NUMERIC_TYPE(Typ) && !Compact) if (IS_INTEGER_NUMERIC_TYPE(Typ) && !Compact)
return sizeof(ALIGN_TYPE); /* allow some extra room for type extension */ return sizeof(ALIGN_TYPE); /* allow some extra room for type extension */
else if (Typ->Base != TypeArray) else if (Typ->Base != TypeArray)
return Typ->Sizeof; return Typ->Sizeof;
else else
@ -104,41 +104,41 @@ void TypeAddBaseType(Picoc *pc, struct ValueType *TypeNode, enum BaseType Base,
/* initialise the type system */ /* initialise the type system */
void TypeInit(Picoc *pc) void TypeInit(Picoc *pc)
{ {
struct IntAlign { char x; int y; } ia; struct IntAlign {char x; int y;} ia;
struct ShortAlign { char x; short y; } sa; struct ShortAlign {char x; short y;} sa;
struct CharAlign { char x; char y; } ca; struct CharAlign {char x; char y;} ca;
struct LongAlign { char x; long y; } la; struct LongAlign {char x; long y;} la;
#ifndef NO_FP #ifndef NO_FP
struct DoubleAlign { char x; double y; } da; struct DoubleAlign {char x; double y;} da;
#endif #endif
struct PointerAlign { char x; void *y; } pa; struct PointerAlign {char x; void *y;} pa;
IntAlignBytes = (char *)&ia.y - &ia.x; IntAlignBytes = (char*)&ia.y - &ia.x;
PointerAlignBytes = (char *)&pa.y - &pa.x; PointerAlignBytes = (char*)&pa.y - &pa.x;
pc->UberType.DerivedTypeList = NULL; pc->UberType.DerivedTypeList = NULL;
TypeAddBaseType(pc, &pc->IntType, TypeInt, sizeof(int), IntAlignBytes); TypeAddBaseType(pc, &pc->IntType, TypeInt, sizeof(int), IntAlignBytes);
TypeAddBaseType(pc, &pc->ShortType, TypeShort, sizeof(short), (char *)&sa.y - &sa.x); TypeAddBaseType(pc, &pc->ShortType, TypeShort, sizeof(short), (char*)&sa.y - &sa.x);
TypeAddBaseType(pc, &pc->CharType, TypeChar, sizeof(char), (char *)&ca.y - &ca.x); TypeAddBaseType(pc, &pc->CharType, TypeChar, sizeof(char), (char*)&ca.y - &ca.x);
TypeAddBaseType(pc, &pc->LongType, TypeLong, sizeof(long), (char *)&la.y - &la.x); TypeAddBaseType(pc, &pc->LongType, TypeLong, sizeof(long), (char*)&la.y - &la.x);
TypeAddBaseType(pc, &pc->UnsignedIntType, TypeUnsignedInt, sizeof(unsigned int), IntAlignBytes); TypeAddBaseType(pc, &pc->UnsignedIntType, TypeUnsignedInt, sizeof(unsigned int), IntAlignBytes);
TypeAddBaseType(pc, &pc->UnsignedShortType, TypeUnsignedShort, sizeof(unsigned short), (char *)&sa.y - &sa.x); TypeAddBaseType(pc, &pc->UnsignedShortType, TypeUnsignedShort, sizeof(unsigned short), (char*)&sa.y - &sa.x);
TypeAddBaseType(pc, &pc->UnsignedLongType, TypeUnsignedLong, sizeof(unsigned long), (char *)&la.y - &la.x); TypeAddBaseType(pc, &pc->UnsignedLongType, TypeUnsignedLong, sizeof(unsigned long), (char*)&la.y - &la.x);
TypeAddBaseType(pc, &pc->UnsignedCharType, TypeUnsignedChar, sizeof(unsigned char), (char *)&ca.y - &ca.x); TypeAddBaseType(pc, &pc->UnsignedCharType, TypeUnsignedChar, sizeof(unsigned char), (char*)&ca.y - &ca.x);
TypeAddBaseType(pc, &pc->VoidType, TypeVoid, 0, 1); TypeAddBaseType(pc, &pc->VoidType, TypeVoid, 0, 1);
TypeAddBaseType(pc, &pc->FunctionType, TypeFunction, sizeof(int), IntAlignBytes); TypeAddBaseType(pc, &pc->FunctionType, TypeFunction, sizeof(int), IntAlignBytes);
TypeAddBaseType(pc, &pc->MacroType, TypeMacro, sizeof(int), IntAlignBytes); TypeAddBaseType(pc, &pc->MacroType, TypeMacro, sizeof(int), IntAlignBytes);
TypeAddBaseType(pc, &pc->GotoLabelType, TypeGotoLabel, 0, 1); TypeAddBaseType(pc, &pc->GotoLabelType, TypeGotoLabel, 0, 1);
#ifndef NO_FP #ifndef NO_FP
TypeAddBaseType(pc, &pc->FPType, TypeFP, sizeof(double), (char *)&da.y - &da.x); TypeAddBaseType(pc, &pc->FPType, TypeFP, sizeof(double), (char*)&da.y - &da.x);
TypeAddBaseType(pc, &pc->TypeType, Type_Type, sizeof(double), (char *)&da.y - &da.x); /* must be large enough to cast to a double */ TypeAddBaseType(pc, &pc->TypeType, Type_Type, sizeof(double), (char*)&da.y - &da.x); /* must be large enough to cast to a double */
#else #else
TypeAddBaseType(pc, &pc->TypeType, Type_Type, sizeof(struct ValueType *), PointerAlignBytes); TypeAddBaseType(pc, &pc->TypeType, Type_Type, sizeof(struct ValueType *), PointerAlignBytes);
#endif #endif
pc->CharArrayType = TypeAdd(pc, NULL, &pc->CharType, TypeArray, 0, pc->StrEmpty, sizeof(char), (char *)&ca.y - &ca.x); pc->CharArrayType = TypeAdd(pc, NULL, &pc->CharType, TypeArray, 0, pc->StrEmpty, sizeof(char), (char*)&ca.y - &ca.x);
pc->CharPtrType = TypeAdd(pc, NULL, &pc->CharType, TypePointer, 0, pc->StrEmpty, sizeof(void *), PointerAlignBytes); pc->CharPtrType = TypeAdd(pc, NULL, &pc->CharType, TypePointer, 0, pc->StrEmpty, sizeof(void*), PointerAlignBytes);
pc->CharPtrPtrType = TypeAdd(pc, NULL, pc->CharPtrType, TypePointer, 0, pc->StrEmpty, sizeof(void *), PointerAlignBytes); pc->CharPtrPtrType = TypeAdd(pc, NULL, pc->CharPtrType, TypePointer, 0, pc->StrEmpty, sizeof(void*), PointerAlignBytes);
pc->VoidPtrType = TypeAdd(pc, NULL, &pc->VoidType, TypePointer, 0, pc->StrEmpty, sizeof(void *), PointerAlignBytes); pc->VoidPtrType = TypeAdd(pc, NULL, &pc->VoidType, TypePointer, 0, pc->StrEmpty, sizeof(void*), PointerAlignBytes);
} }
/* deallocate heap-allocated types */ /* deallocate heap-allocated types */
@ -210,8 +210,8 @@ void TypeParseStruct(struct ParseState *Parser, struct ValueType **Typ, int IsSt
LexGetToken(Parser, NULL, TRUE); LexGetToken(Parser, NULL, TRUE);
(*Typ)->Members = VariableAlloc(pc, Parser, sizeof(struct Table) + STRUCT_TABLE_SIZE * sizeof(struct TableEntry), TRUE); (*Typ)->Members = VariableAlloc(pc, Parser, sizeof(struct Table) + STRUCT_TABLE_SIZE * sizeof(struct TableEntry), TRUE);
(*Typ)->Members->HashTable = (struct TableEntry **)((char *)(*Typ)->Members + sizeof(struct Table)); (*Typ)->Members->HashTable = (struct TableEntry**)((char*)(*Typ)->Members + sizeof(struct Table));
TableInitTable((*Typ)->Members, (struct TableEntry **)((char *)(*Typ)->Members + sizeof(struct Table)), STRUCT_TABLE_SIZE, TRUE); TableInitTable((*Typ)->Members, (struct TableEntry**)((char*)(*Typ)->Members + sizeof(struct Table)), STRUCT_TABLE_SIZE, TRUE);
do { do {
TypeParse(Parser, &MemberType, &MemberIdentifier, NULL); TypeParse(Parser, &MemberType, &MemberIdentifier, NULL);
@ -263,8 +263,8 @@ struct ValueType *TypeCreateOpaqueStruct(Picoc *pc, struct ParseState *Parser, c
/* create the (empty) table */ /* create the (empty) table */
Typ->Members = VariableAlloc(pc, Parser, sizeof(struct Table) + STRUCT_TABLE_SIZE * sizeof(struct TableEntry), TRUE); Typ->Members = VariableAlloc(pc, Parser, sizeof(struct Table) + STRUCT_TABLE_SIZE * sizeof(struct TableEntry), TRUE);
Typ->Members->HashTable = (struct TableEntry **)((char *)Typ->Members + sizeof(struct Table)); Typ->Members->HashTable = (struct TableEntry**)((char*)Typ->Members + sizeof(struct Table));
TableInitTable(Typ->Members, (struct TableEntry **)((char *)Typ->Members + sizeof(struct Table)), STRUCT_TABLE_SIZE, TRUE); TableInitTable(Typ->Members, (struct TableEntry**)((char*)Typ->Members + sizeof(struct Table)), STRUCT_TABLE_SIZE, TRUE);
Typ->Sizeof = Size; Typ->Sizeof = Size;
return Typ; return Typ;

View file

@ -86,7 +86,7 @@ void *VariableAlloc(Picoc *pc, struct ParseState *Parser, int Size, int OnHeap)
struct Value *VariableAllocValueAndData(Picoc *pc, struct ParseState *Parser, int DataSize, int IsLValue, struct Value *LValueFrom, int OnHeap) struct Value *VariableAllocValueAndData(Picoc *pc, struct ParseState *Parser, int DataSize, int IsLValue, struct Value *LValueFrom, int OnHeap)
{ {
struct Value *NewValue = VariableAlloc(pc, Parser, MEM_ALIGN(sizeof(struct Value)) + DataSize, OnHeap); struct Value *NewValue = VariableAlloc(pc, Parser, MEM_ALIGN(sizeof(struct Value)) + DataSize, OnHeap);
NewValue->Val = (union AnyValue *)((char *)NewValue + MEM_ALIGN(sizeof(struct Value))); NewValue->Val = (union AnyValue*)((char*)NewValue + MEM_ALIGN(sizeof(struct Value)));
NewValue->ValOnHeap = OnHeap; NewValue->ValOnHeap = OnHeap;
NewValue->AnyValOnHeap = FALSE; NewValue->AnyValOnHeap = FALSE;
NewValue->ValOnStack = !OnHeap; NewValue->ValOnStack = !OnHeap;
@ -120,10 +120,10 @@ struct Value *VariableAllocValueAndCopy(Picoc *pc, struct ParseState *Parser, st
int CopySize = TypeSizeValue(FromValue, TRUE); int CopySize = TypeSizeValue(FromValue, TRUE);
assert(CopySize <= MAX_TMP_COPY_BUF); assert(CopySize <= MAX_TMP_COPY_BUF);
memcpy((void *)&TmpBuf[0], (void *)FromValue->Val, CopySize); memcpy((void*)&TmpBuf[0], (void*)FromValue->Val, CopySize);
NewValue = VariableAllocValueAndData(pc, Parser, CopySize, FromValue->IsLValue, FromValue->LValueFrom, OnHeap); NewValue = VariableAllocValueAndData(pc, Parser, CopySize, FromValue->IsLValue, FromValue->LValueFrom, OnHeap);
NewValue->Typ = DType; NewValue->Typ = DType;
memcpy((void *)NewValue->Val, (void *)&TmpBuf[0], CopySize); memcpy((void*)NewValue->Val, (void*)&TmpBuf[0], CopySize);
return NewValue; return NewValue;
} }
@ -163,13 +163,13 @@ int VariableScopeBegin(struct ParseState * Parser, int* OldScopeID)
{ {
struct TableEntry *Entry; struct TableEntry *Entry;
struct TableEntry *NextEntry; struct TableEntry *NextEntry;
Picoc * pc = Parser->pc; Picoc *pc = Parser->pc;
int Count; int Count;
#ifdef VAR_SCOPE_DEBUG #ifdef VAR_SCOPE_DEBUG
int FirstPrint = 0; int FirstPrint = 0;
#endif #endif
struct Table * HashTable = (pc->TopStackFrame == NULL) ? &(pc->GlobalTable) : &(pc->TopStackFrame)->LocalTable; struct Table *HashTable = (pc->TopStackFrame == NULL) ? &(pc->GlobalTable) : &(pc->TopStackFrame)->LocalTable;
if (Parser->ScopeID == -1) return -1; if (Parser->ScopeID == -1) return -1;
@ -266,7 +266,7 @@ struct Value *VariableDefine(Picoc *pc, struct ParseState *Parser, char *Ident,
AssignValue->ScopeID = ScopeID; AssignValue->ScopeID = ScopeID;
AssignValue->OutOfScope = FALSE; AssignValue->OutOfScope = FALSE;
if (!TableSet(pc, currentTable, Ident, AssignValue, Parser ? ((char *)Parser->FileName) : NULL, Parser ? Parser->Line : 0, Parser ? Parser->CharacterPos : 0)) if (!TableSet(pc, currentTable, Ident, AssignValue, Parser ? ((char*)Parser->FileName) : NULL, Parser ? Parser->Line : 0, Parser ? Parser->CharacterPos : 0))
ProgramFail(Parser, "'%s' is already defined", Ident); ProgramFail(Parser, "'%s' is already defined", Ident);
return AssignValue; return AssignValue;
@ -294,13 +294,13 @@ struct Value *VariableDefineButIgnoreIdentical(struct ParseState *Parser, char *
/* make the mangled static name (avoiding using sprintf() to minimise library impact) */ /* make the mangled static name (avoiding using sprintf() to minimise library impact) */
memset((void *)&MangledName, '\0', sizeof(MangledName)); memset((void *)&MangledName, '\0', sizeof(MangledName));
*MNPos++ = '/'; *MNPos++ = '/';
strncpy(MNPos, (char *)Parser->FileName, MNEnd - MNPos); strncpy(MNPos, (char*)Parser->FileName, MNEnd - MNPos);
MNPos += strlen(MNPos); MNPos += strlen(MNPos);
if (pc->TopStackFrame != NULL) { if (pc->TopStackFrame != NULL) {
/* we're inside a function */ /* we're inside a function */
if (MNEnd - MNPos > 0) *MNPos++ = '/'; if (MNEnd - MNPos > 0) *MNPos++ = '/';
strncpy(MNPos, (char *)pc->TopStackFrame->FuncName, MNEnd - MNPos); strncpy(MNPos, (char*)pc->TopStackFrame->FuncName, MNEnd - MNPos);
MNPos += strlen(MNPos); MNPos += strlen(MNPos);
} }
@ -312,7 +312,7 @@ struct Value *VariableDefineButIgnoreIdentical(struct ParseState *Parser, char *
if (!TableGet(&pc->GlobalTable, RegisteredMangledName, &ExistingValue, &DeclFileName, &DeclLine, &DeclColumn)) { if (!TableGet(&pc->GlobalTable, RegisteredMangledName, &ExistingValue, &DeclFileName, &DeclLine, &DeclColumn)) {
/* define the mangled-named static variable store in the global scope */ /* define the mangled-named static variable store in the global scope */
ExistingValue = VariableAllocValueFromType(Parser->pc, Parser, Typ, TRUE, NULL, TRUE); ExistingValue = VariableAllocValueFromType(Parser->pc, Parser, Typ, TRUE, NULL, TRUE);
TableSet(pc, &pc->GlobalTable, (char *)RegisteredMangledName, ExistingValue, (char *)Parser->FileName, Parser->Line, Parser->CharacterPos); TableSet(pc, &pc->GlobalTable, (char*)RegisteredMangledName, ExistingValue, (char *)Parser->FileName, Parser->Line, Parser->CharacterPos);
*FirstVisit = TRUE; *FirstVisit = TRUE;
} }
@ -401,7 +401,7 @@ void VariableStackFrameAdd(struct ParseState *Parser, const char *FuncName, int
ParserCopy(&NewFrame->ReturnParser, Parser); ParserCopy(&NewFrame->ReturnParser, Parser);
NewFrame->FuncName = FuncName; NewFrame->FuncName = FuncName;
NewFrame->Parameter = (NumParams > 0) ? ((void *)((char *)NewFrame + sizeof(struct StackFrame))) : NULL; NewFrame->Parameter = (NumParams > 0) ? ((void*)((char *)NewFrame + sizeof(struct StackFrame))) : NULL;
TableInitTable(&NewFrame->LocalTable, &NewFrame->LocalHashTable[0], LOCAL_TABLE_SIZE, FALSE); TableInitTable(&NewFrame->LocalTable, &NewFrame->LocalHashTable[0], LOCAL_TABLE_SIZE, FALSE);
NewFrame->PreviousStackFrame = Parser->pc->TopStackFrame; NewFrame->PreviousStackFrame = Parser->pc->TopStackFrame;
Parser->pc->TopStackFrame = NewFrame; Parser->pc->TopStackFrame = NewFrame;