Skip to content

Commit

Permalink
Merged in a pile of changes from broscutamaker
Browse files Browse the repository at this point in the history
  • Loading branch information
zik.saleeba committed Mar 16, 2013
1 parent fbc6bf5 commit 483ff3b
Show file tree
Hide file tree
Showing 14 changed files with 479 additions and 85 deletions.
1 change: 1 addition & 0 deletions clibrary.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void PrintType(struct ValueType *Typ, IOFILE *Stream)
case TypeUnsignedInt: PrintStr("unsigned int", Stream); break;
case TypeUnsignedShort: PrintStr("unsigned short", Stream); break;
case TypeUnsignedLong: PrintStr("unsigned long", Stream); break;
case TypeUnsignedChar: PrintStr("unsigned char", Stream); break;
#ifndef NO_FP
case TypeFP: PrintStr("double", Stream); break;
#endif
Expand Down
6 changes: 3 additions & 3 deletions cstdlib/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define MAX_FORMAT 80
#define MAX_SCANF_ARGS 10

static int ZeroValue = 0;
static int Stdio_ZeroValue = 0;
static int EOFValue = EOF;
static int SEEK_SETValue = SEEK_SET;
static int SEEK_CURValue = SEEK_CUR;
Expand Down Expand Up @@ -103,7 +103,7 @@ void StdioOutPuts(const char *Str, StdOutStream *Stream)
}

/* printf-style format of an int or other word-sized object */
void StdioFprintfWord(StdOutStream *Stream, const char *Format, unsigned int Value)
void StdioFprintfWord(StdOutStream *Stream, const char *Format, unsigned long Value)
{
if (Stream->FilePtr != NULL)
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
Expand Down Expand Up @@ -712,7 +712,7 @@ void StdioSetupFunc(Picoc *pc)

/* define NULL, TRUE and FALSE */
if (!VariableDefined(pc, TableStrRegister(pc, "NULL")))
VariableDefinePlatformVar(pc, NULL, "NULL", &pc->IntType, (union AnyValue *)&ZeroValue, FALSE);
VariableDefinePlatformVar(pc, NULL, "NULL", &pc->IntType, (union AnyValue *)&Stdio_ZeroValue, FALSE);
}

/* portability-related I/O calls */
Expand Down
4 changes: 2 additions & 2 deletions cstdlib/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#ifndef BUILTIN_MINI_STDLIB

static int ZeroValue = 0;
static int Stdlib_ZeroValue = 0;

#ifndef NO_FP
void StdlibAtof(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
Expand Down Expand Up @@ -168,7 +168,7 @@ void StdlibSetupFunc(Picoc *pc)
{
/* define NULL, TRUE and FALSE */
if (!VariableDefined(pc, TableStrRegister(pc, "NULL")))
VariableDefinePlatformVar(pc, NULL, "NULL", &pc->IntType, (union AnyValue *)&ZeroValue, FALSE);
VariableDefinePlatformVar(pc, NULL, "NULL", &pc->IntType, (union AnyValue *)&Stdlib_ZeroValue, FALSE);
}

#endif /* !BUILTIN_MINI_STDLIB */
4 changes: 2 additions & 2 deletions cstdlib/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#ifndef BUILTIN_MINI_STDLIB

static int ZeroValue = 0;
static int String_ZeroValue = 0;

void StringStrcpy(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
Expand Down Expand Up @@ -180,7 +180,7 @@ void StringSetupFunc(Picoc *pc)
{
/* define NULL */
if (!VariableDefined(pc, TableStrRegister(pc, "NULL")))
VariableDefinePlatformVar(pc, NULL, "NULL", &pc->IntType, (union AnyValue *)&ZeroValue, FALSE);
VariableDefinePlatformVar(pc, NULL, "NULL", &pc->IntType, (union AnyValue *)&String_ZeroValue, FALSE);
}

#endif /* !BUILTIN_MINI_STDLIB */
115 changes: 102 additions & 13 deletions expression.c

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

#define GETS_BUF_MAX 256

/* for debugging */
#define PRINT_SOURCE_POS ({ PrintSourceTextErrorLine(Parser->pc->CStdOut, Parser->FileName, Parser->SourceText, Parser->Line, Parser->CharacterPos); PlatformPrintf(Parser->pc->CStdOut, "\n"); })
#define PRINT_TYPE(typ) PlatformPrintf(Parser->pc->CStdOut, "%t\n", typ);

/* small processors use a simplified FILE * for stdio, otherwise use the system FILE * */
#ifdef BUILTIN_MINI_STDLIB
typedef struct OutputStream IOFILE;
Expand Down Expand Up @@ -123,6 +127,7 @@ struct ParseState
short int HashIfLevel; /* how many "if"s we're nested down */
short int HashIfEvaluateToLevel; /* if we're not evaluating an if branch, what the last evaluated level was */
char DebugMode; /* debugging mode */
int ScopeID; /* for keeping track of local variables (free them after they go out of scope) */
};

/* values */
Expand All @@ -131,10 +136,11 @@ enum BaseType
TypeVoid, /* no type */
TypeInt, /* integer */
TypeShort, /* short integer */
TypeChar, /* a single character (unsigned) */
TypeChar, /* a single character (signed) */
TypeLong, /* long integer */
TypeUnsignedInt, /* unsigned integer */
TypeUnsignedShort, /* unsigned short integer */
TypeUnsignedChar, /* unsigned 8-bit number */ /* must be before unsigned long */
TypeUnsignedLong, /* unsigned long integer */
#ifndef NO_FP
TypeFP, /* floating point */
Expand Down Expand Up @@ -189,13 +195,14 @@ struct MacroDef
/* values */
union AnyValue
{
unsigned char Character;
char Character;
short ShortInteger;
int Integer;
long LongInteger;
unsigned short UnsignedShortInteger;
unsigned int UnsignedInteger;
unsigned long UnsignedLongInteger;
unsigned char UnsignedCharacter;
char *Identifier;
char ArrayMem[2]; /* placeholder for where the data starts, doesn't point to it */
struct ValueType *Typ;
Expand All @@ -216,6 +223,8 @@ struct Value
char ValOnStack; /* the AnyValue is on the stack along with this Value */
char AnyValOnHeap; /* the AnyValue is separately allocated from the Value on the heap */
char IsLValue; /* is modifiable and is allocated somewhere we can usefully modify it */
int ScopeID; /* to know when it goes out of scope */
char OutOfScope;
};

/* hash table data structure */
Expand Down Expand Up @@ -414,6 +423,7 @@ struct Picoc_Struct
struct ValueType UnsignedIntType;
struct ValueType UnsignedShortType;
struct ValueType UnsignedLongType;
struct ValueType UnsignedCharType;
#ifndef NO_FP
struct ValueType FPType;
#endif
Expand Down Expand Up @@ -543,6 +553,7 @@ struct Value *VariableAllocValueShared(struct ParseState *Parser, struct Value *
struct Value *VariableDefine(Picoc *pc, struct ParseState *Parser, char *Ident, struct Value *InitValue, struct ValueType *Typ, int MakeWritable);
struct Value *VariableDefineButIgnoreIdentical(struct ParseState *Parser, char *Ident, struct ValueType *Typ, int IsStatic, int *FirstVisit);
int VariableDefined(Picoc *pc, const char *Ident);
int VariableDefinedAndOutOfScope(Picoc *pc, const char *Ident);
void VariableRealloc(struct ParseState *Parser, struct Value *FromValue, int NewSize);
void VariableGet(Picoc *pc, struct ParseState *Parser, const char *Ident, struct Value **LVal);
void VariableDefinePlatformVar(Picoc *pc, struct ParseState *Parser, char *Ident, struct ValueType *Typ, union AnyValue *FromValue, int IsWritable);
Expand All @@ -551,6 +562,8 @@ void VariableStackFramePop(struct ParseState *Parser);
struct Value *VariableStringLiteralGet(Picoc *pc, char *Ident);
void VariableStringLiteralDefine(Picoc *pc, char *Ident, struct Value *Val);
void *VariableDereferencePointer(struct ParseState *Parser, struct Value *PointerValue, struct Value **DerefVal, int *DerefOffset, struct ValueType **DerefType, int *DerefIsLValue);
int VariableScopeBegin(struct ParseState * Parser, int* PrevScopeID);
void VariableScopeEnd(struct ParseState * Parser, int ScopeID, int PrevScopeID);

/* clibrary.c */
void BasicIOInit(Picoc *pc);
Expand Down
116 changes: 86 additions & 30 deletions lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,18 @@ enum LexToken LexCheckReservedWord(Picoc *pc, const char *Word)
/* get a numeric literal - used while scanning */
enum LexToken LexGetNumber(Picoc *pc, struct LexState *Lexer, struct Value *Value)
{
int Result = 0;
int Base = 10;
long Result = 0;
long Base = 10;
enum LexToken ResultToken;
#ifndef NO_FP
double FPResult;
double FPDiv;
#endif
/* long/unsigned flags */
#if 0 /* unused for now */
char IsLong = 0;
char IsUnsigned = 0;
#endif

if (*Lexer->Pos == '0')
{
Expand All @@ -155,46 +160,57 @@ enum LexToken LexGetNumber(Picoc *pc, struct LexState *Lexer, struct Value *Valu
/* get the value */
for (; Lexer->Pos != Lexer->End && IS_BASE_DIGIT(*Lexer->Pos, Base); LEXER_INC(Lexer))
Result = Result * Base + GET_BASE_DIGIT(*Lexer->Pos);
if (Result >= 0 && Result <= MAX_CHAR_VALUE)

if (*Lexer->Pos == 'u' || *Lexer->Pos == 'U')
{
Value->Typ = &pc->CharType;
Value->Val->Character = Result;
ResultToken = TokenCharacterConstant;
LEXER_INC(Lexer);
/* IsUnsigned = 1; */
}
else
if (*Lexer->Pos == 'l' || *Lexer->Pos == 'L')
{
Value->Typ = &pc->IntType;
Value->Val->Integer = Result;
ResultToken = TokenIntegerConstant;
LEXER_INC(Lexer);
/* IsLong = 1; */
}

Value->Typ = &pc->LongType; /* ignored? */
Value->Val->LongInteger = Result;

ResultToken = TokenIntegerConstant;

if (Lexer->Pos == Lexer->End)
return ResultToken;

if (*Lexer->Pos == 'l' || *Lexer->Pos == 'L')
#ifndef NO_FP
if (Lexer->Pos == Lexer->End)
{
LEXER_INC(Lexer);
return ResultToken;
}
#ifndef NO_FP
if (Lexer->Pos == Lexer->End || *Lexer->Pos != '.')

if (*Lexer->Pos != '.' && *Lexer->Pos != 'e' && *Lexer->Pos != 'E')
{
return ResultToken;

}

Value->Typ = &pc->FPType;
LEXER_INC(Lexer);
for (FPDiv = 1.0/Base, FPResult = (double)Result; Lexer->Pos != Lexer->End && IS_BASE_DIGIT(*Lexer->Pos, Base); LEXER_INC(Lexer), FPDiv /= (double)Base)
FPResult += GET_BASE_DIGIT(*Lexer->Pos) * FPDiv;
FPResult = (double)Result;

if (*Lexer->Pos == '.')
{
LEXER_INC(Lexer);
for (FPDiv = 1.0/Base; Lexer->Pos != Lexer->End && IS_BASE_DIGIT(*Lexer->Pos, Base); LEXER_INC(Lexer), FPDiv /= (double)Base)
{
FPResult += GET_BASE_DIGIT(*Lexer->Pos) * FPDiv;
}
}

if (Lexer->Pos != Lexer->End && (*Lexer->Pos == 'e' || *Lexer->Pos == 'E'))
{
double ExponentMultiplier = 1.0;
int ExponentSign = 1;

LEXER_INC(Lexer);
if (Lexer->Pos != Lexer->End && *Lexer->Pos == '-')
{
ExponentMultiplier = -1.0;
ExponentSign = -1;
LEXER_INC(Lexer);
}

Expand All @@ -204,12 +220,15 @@ enum LexToken LexGetNumber(Picoc *pc, struct LexState *Lexer, struct Value *Valu
Result = Result * Base + GET_BASE_DIGIT(*Lexer->Pos);
LEXER_INC(Lexer);
}
FPResult *= pow((double)Base, (double)Result * ExponentMultiplier);

FPResult *= pow((double)Base, (double)Result * ExponentSign);
}

Value->Val->FP = FPResult;


if (*Lexer->Pos == 'f' || *Lexer->Pos == 'F')
LEXER_INC(Lexer);

return TokenFPConstant;
#else
return ResultToken;
Expand Down Expand Up @@ -496,7 +515,7 @@ int LexTokenSize(enum LexToken Token)
switch (Token)
{
case TokenIdentifier: case TokenStringConstant: return sizeof(char *);
case TokenIntegerConstant: return sizeof(int);
case TokenIntegerConstant: return sizeof(long);
case TokenCharacterConstant: return sizeof(unsigned char);
case TokenFPConstant: return sizeof(double);
default: return 0;
Expand All @@ -523,7 +542,7 @@ void *LexTokenise(Picoc *pc, struct LexState *Lexer, int *TokenLen)
{
/* store the token at the end of the stack area */
Token = LexScanGetToken(pc, Lexer, &GotValue);

#ifdef DEBUG_LEXER
printf("Token: %02x\n", Token);
#endif
Expand Down Expand Up @@ -699,7 +718,7 @@ enum LexToken LexGetRawToken(struct ParseState *Parser, struct Value **Value, in
{
case TokenStringConstant: pc->LexValue.Typ = pc->CharPtrType; break;
case TokenIdentifier: pc->LexValue.Typ = NULL; break;
case TokenIntegerConstant: pc->LexValue.Typ = &pc->IntType; break;
case TokenIntegerConstant: pc->LexValue.Typ = &pc->LongType; break;
case TokenCharacterConstant: pc->LexValue.Typ = &pc->CharType; break;
#ifndef NO_FP
case TokenFPConstant: pc->LexValue.Typ = &pc->FPType; break;
Expand Down Expand Up @@ -766,7 +785,7 @@ void LexHashIf(struct ParseState *Parser)
{
/* get symbol to check */
struct Value *IdentValue;
struct Value *SavedValue;
struct Value *SavedValue = NULL;
struct ParseState MacroParser;
enum LexToken Token = LexGetRawToken(Parser, &IdentValue, TRUE);

Expand All @@ -783,7 +802,7 @@ void LexHashIf(struct ParseState *Parser)
Token = LexGetRawToken(&MacroParser, &IdentValue, TRUE);
}

if (Token != TokenCharacterConstant)
if (Token != TokenCharacterConstant && Token != TokenIntegerConstant)
ProgramFail(Parser, "value expected");

/* is the identifier defined? */
Expand Down Expand Up @@ -823,6 +842,43 @@ void LexHashEndif(struct ParseState *Parser)
Parser->HashIfEvaluateToLevel = Parser->HashIfLevel;
}

#if 0 /* useful for debug */
void LexPrintToken(enum LexToken Token)
{
char* TokenNames[] = {
/* 0x00 */ "None",
/* 0x01 */ "Comma",
/* 0x02 */ "Assign", "AddAssign", "SubtractAssign", "MultiplyAssign", "DivideAssign", "ModulusAssign",
/* 0x08 */ "ShiftLeftAssign", "ShiftRightAssign", "ArithmeticAndAssign", "ArithmeticOrAssign", "ArithmeticExorAssign",
/* 0x0d */ "QuestionMark", "Colon",
/* 0x0f */ "LogicalOr",
/* 0x10 */ "LogicalAnd",
/* 0x11 */ "ArithmeticOr",
/* 0x12 */ "ArithmeticExor",
/* 0x13 */ "Ampersand",
/* 0x14 */ "Equal", "NotEqual",
/* 0x16 */ "LessThan", "GreaterThan", "LessEqual", "GreaterEqual",
/* 0x1a */ "ShiftLeft", "ShiftRight",
/* 0x1c */ "Plus", "Minus",
/* 0x1e */ "Asterisk", "Slash", "Modulus",
/* 0x21 */ "Increment", "Decrement", "UnaryNot", "UnaryExor", "Sizeof", "Cast",
/* 0x27 */ "LeftSquareBracket", "RightSquareBracket", "Dot", "Arrow",
/* 0x2b */ "OpenBracket", "CloseBracket",
/* 0x2d */ "Identifier", "IntegerConstant", "FPConstant", "StringConstant", "CharacterConstant",
/* 0x32 */ "Semicolon", "Ellipsis",
/* 0x34 */ "LeftBrace", "RightBrace",
/* 0x36 */ "IntType", "CharType", "FloatType", "DoubleType", "VoidType", "EnumType",
/* 0x3c */ "LongType", "SignedType", "ShortType", "StaticType", "AutoType", "RegisterType", "ExternType", "StructType", "UnionType", "UnsignedType", "Typedef",
/* 0x46 */ "Continue", "Do", "Else", "For", "Goto", "If", "While", "Break", "Switch", "Case", "Default", "Return",
/* 0x52 */ "HashDefine", "HashInclude", "HashIf", "HashIfdef", "HashIfndef", "HashElse", "HashEndif",
/* 0x59 */ "New", "Delete",
/* 0x5b */ "OpenMacroBracket",
/* 0x5c */ "EOF", "EndOfLine", "EndOfFunction"
};
printf("{%s}", TokenNames[Token]);
}
#endif

/* get the next token given a parser state, pre-processing as we go */
enum LexToken LexGetToken(struct ParseState *Parser, struct Value **Value, int IncPos)
{
Expand Down
Loading

0 comments on commit 483ff3b

Please sign in to comment.