formatting
This commit is contained in:
parent
3ba3fa5997
commit
890694a14a
59
clibrary.c
59
clibrary.c
|
@ -57,22 +57,47 @@ void LibraryAdd(Picoc *pc, struct LibraryFunction *FuncList)
|
|||
void PrintType(struct ValueType *Typ, IOFILE *Stream)
|
||||
{
|
||||
switch (Typ->Base) {
|
||||
case TypeVoid: PrintStr("void", Stream); break;
|
||||
case TypeInt: PrintStr("int", Stream); break;
|
||||
case TypeShort: PrintStr("short", Stream); break;
|
||||
case TypeChar: PrintStr("char", Stream); break;
|
||||
case TypeLong: PrintStr("long", Stream); break;
|
||||
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;
|
||||
case TypeFP: PrintStr("double", Stream); break;
|
||||
case TypeFunction: PrintStr("function", Stream); break;
|
||||
case TypeMacro: PrintStr("macro", Stream); break;
|
||||
case TypeVoid:
|
||||
PrintStr("void", Stream);
|
||||
break;
|
||||
case TypeInt:
|
||||
PrintStr("int", Stream);
|
||||
break;
|
||||
case TypeShort:
|
||||
PrintStr("short", Stream);
|
||||
break;
|
||||
case TypeChar:
|
||||
PrintStr("char", Stream);
|
||||
break;
|
||||
case TypeLong:
|
||||
PrintStr("long", Stream);
|
||||
break;
|
||||
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;
|
||||
case TypeFP:
|
||||
PrintStr("double", Stream);
|
||||
break;
|
||||
case TypeFunction:
|
||||
PrintStr("function", Stream);
|
||||
break;
|
||||
case TypeMacro:
|
||||
PrintStr("macro", Stream);
|
||||
break;
|
||||
case TypePointer:
|
||||
if (Typ->FromType)
|
||||
PrintType(Typ->FromType, Stream);
|
||||
PrintCh('*', Stream); break;
|
||||
PrintCh('*', Stream);
|
||||
break;
|
||||
case TypeArray:
|
||||
PrintType(Typ->FromType, Stream);
|
||||
PrintCh('[', Stream);
|
||||
|
@ -92,8 +117,12 @@ void PrintType(struct ValueType *Typ, IOFILE *Stream)
|
|||
PrintStr("enum ", Stream);
|
||||
PrintStr(Typ->Identifier, Stream);
|
||||
break;
|
||||
case TypeGotoLabel: PrintStr("goto label ", Stream); break;
|
||||
case Type_Type: PrintStr("type ", Stream); break;
|
||||
case TypeGotoLabel:
|
||||
PrintStr("goto label ", Stream);
|
||||
break;
|
||||
case Type_Type:
|
||||
PrintStr("type ", Stream);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
152
expression.c
152
expression.c
|
@ -243,34 +243,56 @@ int IsTypeToken(struct ParseState * Parser, enum LexToken t,
|
|||
long ExpressionCoerceInteger(struct Value *Val)
|
||||
{
|
||||
switch (Val->Typ->Base) {
|
||||
case TypeInt: return (long)Val->Val->Integer;
|
||||
case TypeChar: return (long)Val->Val->Character;
|
||||
case TypeShort: return (long)Val->Val->ShortInteger;
|
||||
case TypeLong: return (long)Val->Val->LongInteger;
|
||||
case TypeUnsignedInt: return (long)Val->Val->UnsignedInteger;
|
||||
case TypeUnsignedShort: return (long)Val->Val->UnsignedShortInteger;
|
||||
case TypeUnsignedLong: return (long)Val->Val->UnsignedLongInteger;
|
||||
case TypeUnsignedChar: return (long)Val->Val->UnsignedCharacter;
|
||||
case TypePointer: return (long)Val->Val->Pointer;
|
||||
case TypeFP: return (long)Val->Val->FP;
|
||||
default: return 0;
|
||||
case TypeInt:
|
||||
return (long)Val->Val->Integer;
|
||||
case TypeChar:
|
||||
return (long)Val->Val->Character;
|
||||
case TypeShort:
|
||||
return (long)Val->Val->ShortInteger;
|
||||
case TypeLong:
|
||||
return (long)Val->Val->LongInteger;
|
||||
case TypeUnsignedInt:
|
||||
return (long)Val->Val->UnsignedInteger;
|
||||
case TypeUnsignedShort:
|
||||
return (long)Val->Val->UnsignedShortInteger;
|
||||
case TypeUnsignedLong:
|
||||
return (long)Val->Val->UnsignedLongInteger;
|
||||
case TypeUnsignedChar:
|
||||
return (long)Val->Val->UnsignedCharacter;
|
||||
case TypePointer:
|
||||
return (long)Val->Val->Pointer;
|
||||
case TypeFP:
|
||||
return (long)Val->Val->FP;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long ExpressionCoerceUnsignedInteger(struct Value *Val)
|
||||
{
|
||||
switch (Val->Typ->Base) {
|
||||
case TypeInt: return (unsigned long)Val->Val->Integer;
|
||||
case TypeChar: return (unsigned long)Val->Val->Character;
|
||||
case TypeShort: return (unsigned long)Val->Val->ShortInteger;
|
||||
case TypeLong: return (unsigned long)Val->Val->LongInteger;
|
||||
case TypeUnsignedInt: return (unsigned long)Val->Val->UnsignedInteger;
|
||||
case TypeUnsignedShort: return (unsigned long)Val->Val->UnsignedShortInteger;
|
||||
case TypeUnsignedLong: return (unsigned long)Val->Val->UnsignedLongInteger;
|
||||
case TypeUnsignedChar: return (unsigned long)Val->Val->UnsignedCharacter;
|
||||
case TypePointer: return (unsigned long)Val->Val->Pointer;
|
||||
case TypeFP: return (unsigned long)Val->Val->FP;
|
||||
default: return 0;
|
||||
case TypeInt:
|
||||
return (unsigned long)Val->Val->Integer;
|
||||
case TypeChar:
|
||||
return (unsigned long)Val->Val->Character;
|
||||
case TypeShort:
|
||||
return (unsigned long)Val->Val->ShortInteger;
|
||||
case TypeLong:
|
||||
return (unsigned long)Val->Val->LongInteger;
|
||||
case TypeUnsignedInt:
|
||||
return (unsigned long)Val->Val->UnsignedInteger;
|
||||
case TypeUnsignedShort:
|
||||
return (unsigned long)Val->Val->UnsignedShortInteger;
|
||||
case TypeUnsignedLong:
|
||||
return (unsigned long)Val->Val->UnsignedLongInteger;
|
||||
case TypeUnsignedChar:
|
||||
return (unsigned long)Val->Val->UnsignedCharacter;
|
||||
case TypePointer:
|
||||
return (unsigned long)Val->Val->Pointer;
|
||||
case TypeFP:
|
||||
return (unsigned long)Val->Val->FP;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -315,15 +337,32 @@ long ExpressionAssignInt(struct ParseState *Parser, struct Value *DestValue,
|
|||
Result = FromInt;
|
||||
|
||||
switch (DestValue->Typ->Base) {
|
||||
case TypeInt: DestValue->Val->Integer = (int)FromInt; break;
|
||||
case TypeShort: DestValue->Val->ShortInteger = (short)FromInt; break;
|
||||
case TypeChar: DestValue->Val->Character = (char)FromInt; break;
|
||||
case TypeLong: DestValue->Val->LongInteger = (long)FromInt; break;
|
||||
case TypeUnsignedInt: DestValue->Val->UnsignedInteger = (unsigned int)FromInt; break;
|
||||
case TypeUnsignedShort: DestValue->Val->UnsignedShortInteger = (unsigned short)FromInt; break;
|
||||
case TypeUnsignedLong: DestValue->Val->UnsignedLongInteger = (unsigned long)FromInt; break;
|
||||
case TypeUnsignedChar: DestValue->Val->UnsignedCharacter = (unsigned char)FromInt; break;
|
||||
default: break;
|
||||
case TypeInt:
|
||||
DestValue->Val->Integer = (int)FromInt;
|
||||
break;
|
||||
case TypeShort:
|
||||
DestValue->Val->ShortInteger = (short)FromInt;
|
||||
break;
|
||||
case TypeChar:
|
||||
DestValue->Val->Character = (char)FromInt;
|
||||
break;
|
||||
case TypeLong:
|
||||
DestValue->Val->LongInteger = (long)FromInt;
|
||||
break;
|
||||
case TypeUnsignedInt:
|
||||
DestValue->Val->UnsignedInteger = (unsigned int)FromInt;
|
||||
break;
|
||||
case TypeUnsignedShort:
|
||||
DestValue->Val->UnsignedShortInteger = (unsigned short)FromInt;
|
||||
break;
|
||||
case TypeUnsignedLong:
|
||||
DestValue->Val->UnsignedLongInteger = (unsigned long)FromInt;
|
||||
break;
|
||||
case TypeUnsignedChar:
|
||||
DestValue->Val->UnsignedCharacter = (unsigned char)FromInt;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
@ -909,22 +948,28 @@ void ExpressionInfixOperator(struct ParseState *Parser,
|
|||
ASSIGN_FP_OR_INT(BottomFP / TopFP);
|
||||
break;
|
||||
case TokenEqual:
|
||||
ResultInt = BottomFP == TopFP; ResultIsInt = true;
|
||||
ResultInt = BottomFP == TopFP;
|
||||
ResultIsInt = true;
|
||||
break;
|
||||
case TokenNotEqual:
|
||||
ResultInt = BottomFP != TopFP; ResultIsInt = true;
|
||||
ResultInt = BottomFP != TopFP;
|
||||
ResultIsInt = true;
|
||||
break;
|
||||
case TokenLessThan:
|
||||
ResultInt = BottomFP < TopFP; ResultIsInt = true;
|
||||
ResultInt = BottomFP < TopFP;
|
||||
ResultIsInt = true;
|
||||
break;
|
||||
case TokenGreaterThan:
|
||||
ResultInt = BottomFP > TopFP; ResultIsInt = true;
|
||||
ResultInt = BottomFP > TopFP;
|
||||
ResultIsInt = true;
|
||||
break;
|
||||
case TokenLessEqual:
|
||||
ResultInt = BottomFP <= TopFP; ResultIsInt = true;
|
||||
ResultInt = BottomFP <= TopFP;
|
||||
ResultIsInt = true;
|
||||
break;
|
||||
case TokenGreaterEqual:
|
||||
ResultInt = BottomFP >= TopFP; ResultIsInt = true;
|
||||
ResultInt = BottomFP >= TopFP;
|
||||
ResultIsInt = true;
|
||||
break;
|
||||
case TokenPlus:
|
||||
ResultFP = BottomFP + TopFP;
|
||||
|
@ -1079,9 +1124,9 @@ void ExpressionInfixOperator(struct ParseState *Parser,
|
|||
ProgramFail(Parser, "invalid use of a NULL pointer");
|
||||
|
||||
if (Op == TokenPlus)
|
||||
Pointer = (void *)((char *)Pointer + TopInt * Size);
|
||||
Pointer = (void*)((char*)Pointer + TopInt * Size);
|
||||
else
|
||||
Pointer = (void *)((char *)Pointer - TopInt * Size);
|
||||
Pointer = (void*)((char*)Pointer - TopInt * Size);
|
||||
|
||||
StackValue = ExpressionStackPushValueByType(Parser, StackTop,
|
||||
BottomValue->Typ);
|
||||
|
@ -1100,9 +1145,9 @@ void ExpressionInfixOperator(struct ParseState *Parser,
|
|||
ProgramFail(Parser, "invalid use of a NULL pointer");
|
||||
|
||||
if (Op == TokenAddAssign)
|
||||
Pointer = (void *)((char *)Pointer + TopInt * Size);
|
||||
Pointer = (void*)((char*)Pointer + TopInt * Size);
|
||||
else
|
||||
Pointer = (void *)((char *)Pointer - TopInt * Size);
|
||||
Pointer = (void*)((char*)Pointer - TopInt * Size);
|
||||
|
||||
HeapUnpopStack(Parser->pc, sizeof(struct Value));
|
||||
BottomValue->Val->Pointer = Pointer;
|
||||
|
@ -1112,8 +1157,8 @@ void ExpressionInfixOperator(struct ParseState *Parser,
|
|||
} else if (BottomValue->Typ->Base == TypePointer &&
|
||||
TopValue->Typ->Base == TypePointer && Op != TokenAssign) {
|
||||
/* pointer/pointer operations */
|
||||
char *TopLoc = (char *)TopValue->Val->Pointer;
|
||||
char *BottomLoc = (char *)BottomValue->Val->Pointer;
|
||||
char *TopLoc = (char*)TopValue->Val->Pointer;
|
||||
char *BottomLoc = (char*)BottomValue->Val->Pointer;
|
||||
|
||||
switch (Op) {
|
||||
case TokenEqual:
|
||||
|
@ -1210,7 +1255,8 @@ void ExpressionStackCollapse(struct ParseState *Parser,
|
|||
#endif
|
||||
TopValue = TopStackNode->Next->Val;
|
||||
|
||||
/* pop the postfix operator and then the value - assume they'll still be there until we're done */
|
||||
/* pop the postfix operator and then the value - assume
|
||||
they'll still be there until we're done */
|
||||
HeapPopStack(Parser->pc, NULL, sizeof(struct ExpressionStack));
|
||||
HeapPopStack(Parser->pc, TopValue,
|
||||
sizeof(struct ExpressionStack) +
|
||||
|
@ -1337,7 +1383,8 @@ void ExpressionGetStructElement(struct ParseState *Parser,
|
|||
NULL, &StructType, NULL);
|
||||
|
||||
if (StructType->Base != TypeStruct && StructType->Base != TypeUnion)
|
||||
ProgramFail(Parser, "can't use '%s' on something that's not a struct or union %s : it's a %t",
|
||||
ProgramFail(Parser,
|
||||
"can't use '%s' on something that's not a struct or union %s : it's a %t",
|
||||
(Token == TokenDot) ? "." : "->",
|
||||
(Token == TokenArrow) ? "pointer" : "", ParamVal->Typ);
|
||||
|
||||
|
@ -1519,8 +1566,12 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
|||
PrefixState = true;
|
||||
|
||||
switch (Token) {
|
||||
case TokenQuestionMark: TernaryDepth++; break;
|
||||
case TokenColon: TernaryDepth--; break;
|
||||
case TokenQuestionMark:
|
||||
TernaryDepth++;
|
||||
break;
|
||||
case TokenColon:
|
||||
TernaryDepth--;
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
@ -1655,7 +1706,8 @@ void ExpressionParseMacroCall(struct ParseState *Parser,
|
|||
|
||||
if (Parser->Mode == RunModeRun) {
|
||||
/* create a stack frame for this macro */
|
||||
ExpressionStackPushValueByType(Parser, StackTop, &Parser->pc->FPType); /* largest return type there is */
|
||||
/* largest return type there is */
|
||||
ExpressionStackPushValueByType(Parser, StackTop, &Parser->pc->FPType);
|
||||
ReturnValue = (*StackTop)->Val;
|
||||
HeapPushStackFrame(Parser->pc);
|
||||
ParamArray = HeapAllocStack(Parser->pc,
|
||||
|
@ -1803,7 +1855,9 @@ void ExpressionParseFunctionCall(struct ParseState *Parser,
|
|||
struct ParseState FuncParser;
|
||||
|
||||
if (FuncValue->Val->FuncDef.Body.Pos == NULL)
|
||||
ProgramFail(Parser, "ExpressionParseFunctionCall FuncName: '%s' is undefined", FuncName);
|
||||
ProgramFail(Parser,
|
||||
"ExpressionParseFunctionCall FuncName: '%s' is undefined",
|
||||
FuncName);
|
||||
|
||||
ParserCopy(&FuncParser, &FuncValue->Val->FuncDef.Body);
|
||||
VariableStackFrameAdd(Parser, FuncName,
|
||||
|
|
|
@ -66,8 +66,10 @@ void IncludeFile(Picoc *pc, char *FileName)
|
|||
{
|
||||
struct IncludeLibrary *LInclude;
|
||||
|
||||
/* scan for the include file name to see if it's in our list of predefined includes */
|
||||
for (LInclude = pc->IncludeLibList; LInclude != NULL; LInclude = LInclude->NextLib) {
|
||||
/* scan for the include file name to see if it's in our list
|
||||
of predefined includes */
|
||||
for (LInclude = pc->IncludeLibList; LInclude != NULL;
|
||||
LInclude = LInclude->NextLib) {
|
||||
if (strcmp(LInclude->IncludeName, FileName) == 0) {
|
||||
/* found it - protect against multiple inclusion */
|
||||
if (!VariableDefined(pc, FileName)) {
|
||||
|
|
134
lex.c
134
lex.c
|
@ -83,7 +83,8 @@ void LexInit(Picoc *pc)
|
|||
TableInitTable(&pc->ReservedWordTable, &pc->ReservedWordHashTable[0],
|
||||
sizeof(ReservedWords) / sizeof(struct ReservedWord) * 2, true);
|
||||
|
||||
for (Count = 0; Count < sizeof(ReservedWords) / sizeof(struct ReservedWord); Count++) {
|
||||
for (Count = 0; Count < sizeof(ReservedWords) / sizeof(struct ReservedWord);
|
||||
Count++) {
|
||||
TableSet(pc, &pc->ReservedWordTable,
|
||||
TableStrRegister(pc, ReservedWords[Count].Word),
|
||||
(struct Value *)&ReservedWords[Count], NULL, 0, 0);
|
||||
|
@ -105,7 +106,8 @@ void LexCleanup(Picoc *pc)
|
|||
|
||||
LexInteractiveClear(pc, NULL);
|
||||
|
||||
for (Count = 0; Count < sizeof(ReservedWords) / sizeof(struct ReservedWord); Count++)
|
||||
for (Count = 0; Count < sizeof(ReservedWords) / sizeof(struct ReservedWord);
|
||||
Count++)
|
||||
TableDelete(pc, &pc->ReservedWordTable,
|
||||
TableStrRegister(pc, ReservedWords[Count].Word));
|
||||
}
|
||||
|
@ -149,7 +151,8 @@ 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))
|
||||
for (; Lexer->Pos != Lexer->End && IS_BASE_DIGIT(*Lexer->Pos, Base);
|
||||
LEXER_INC(Lexer))
|
||||
Result = Result * Base + GET_BASE_DIGIT(*Lexer->Pos);
|
||||
|
||||
if (*Lexer->Pos == 'u' || *Lexer->Pos == 'U') {
|
||||
|
@ -229,9 +232,14 @@ enum LexToken LexGetWord(Picoc *pc, struct LexState *Lexer, struct Value *Value)
|
|||
|
||||
Token = LexCheckReservedWord(pc, Value->Val->Identifier);
|
||||
switch (Token) {
|
||||
case TokenHashInclude: Lexer->Mode = LexModeHashInclude; break;
|
||||
case TokenHashDefine: Lexer->Mode = LexModeHashDefine; break;
|
||||
default: break;
|
||||
case TokenHashInclude:
|
||||
Lexer->Mode = LexModeHashInclude;
|
||||
break;
|
||||
case TokenHashDefine:
|
||||
Lexer->Mode = LexModeHashDefine;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (Token != TokenNone)
|
||||
|
@ -692,7 +700,7 @@ enum LexToken LexGetRawToken(struct ParseState *Parser, struct Value **Value,
|
|||
|
||||
if (Parser->FileName != pc->StrEmpty || pc->InteractiveHead != NULL) {
|
||||
/* skip leading newlines */
|
||||
while ((Token = (enum LexToken)*(unsigned char *)Parser->Pos) == TokenEndOfLine) {
|
||||
while ((Token = (enum LexToken)*(unsigned char*)Parser->Pos) == TokenEndOfLine) {
|
||||
Parser->Line++;
|
||||
Parser->Pos += TOKEN_DATA_OFFSET;
|
||||
}
|
||||
|
@ -876,7 +884,8 @@ void LexHashIf(struct ParseState *Parser)
|
|||
void LexHashElse(struct ParseState *Parser)
|
||||
{
|
||||
if (Parser->HashIfEvaluateToLevel == Parser->HashIfLevel - 1)
|
||||
Parser->HashIfEvaluateToLevel++; /* #if was not active, make this next section active */
|
||||
Parser->HashIfEvaluateToLevel++; /* #if was not active, make
|
||||
this next section active */
|
||||
else if (Parser->HashIfEvaluateToLevel == Parser->HashIfLevel) {
|
||||
/* #if was active, now go inactive */
|
||||
if (Parser->HashIfLevel == 0)
|
||||
|
@ -903,32 +912,101 @@ 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",
|
||||
/* 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",
|
||||
/* 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"
|
||||
/* 0x5c */ "EOF",
|
||||
"EndOfLine",
|
||||
"EndOfFunction"
|
||||
};
|
||||
printf("{%s}", TokenNames[Token]);
|
||||
}
|
||||
|
|
27
parse.c
27
parse.c
|
@ -93,7 +93,8 @@ struct Value *ParseFunctionDefinition(struct ParseState *Parser,
|
|||
FuncValue->Val->FuncDef.ParamType =
|
||||
(struct ValueType**)((char*)FuncValue->Val+sizeof(struct FuncDef));
|
||||
FuncValue->Val->FuncDef.ParamName =
|
||||
(char**)((char*)FuncValue->Val->FuncDef.ParamType+sizeof(struct ValueType*)*ParamCount);
|
||||
(char**)((char*)FuncValue->Val->FuncDef.ParamType +
|
||||
sizeof(struct ValueType*)*ParamCount);
|
||||
|
||||
for (ParamCount = 0; ParamCount < FuncValue->Val->FuncDef.NumParams; ParamCount++) {
|
||||
/* harvest the parameters into the function definition */
|
||||
|
@ -140,7 +141,8 @@ struct Value *ParseFunctionDefinition(struct ParseState *Parser,
|
|||
/* look for a function body */
|
||||
Token = LexGetToken(Parser, NULL, false);
|
||||
if (Token == TokenSemicolon)
|
||||
LexGetToken(Parser, NULL, true); /* it's a prototype, absorb the trailing semicolon */
|
||||
LexGetToken(Parser, NULL, true); /* it's a prototype, absorb
|
||||
the trailing semicolon */
|
||||
else {
|
||||
/* it's a full function definition with a body */
|
||||
if (Token != TokenLeftBrace)
|
||||
|
@ -214,7 +216,8 @@ int ParseArrayInitialiser(struct ParseState *Parser, struct Value *NewVariable,
|
|||
NewVariable->Typ->FromType->ArraySize, true);
|
||||
SubArray = VariableAllocValueFromExistingData(Parser,
|
||||
NewVariable->Typ->FromType,
|
||||
(union AnyValue*)(&NewVariable->Val->ArrayMem[0]+SubArraySize*ArrayIndex),
|
||||
(union AnyValue*)(&NewVariable->Val->ArrayMem[0] +
|
||||
SubArraySize*ArrayIndex),
|
||||
true, NewVariable);
|
||||
#ifdef DEBUG_ARRAY_INITIALIZER
|
||||
int FullArraySize = TypeSize(NewVariable->Typ,
|
||||
|
@ -237,12 +240,14 @@ int ParseArrayInitialiser(struct ParseState *Parser, struct Value *NewVariable,
|
|||
int TotalSize = 1;
|
||||
int ElementSize = 0;
|
||||
|
||||
/* int x[3][3] = {1,2,3,4} => handle it just like int x[9] = {1,2,3,4} */
|
||||
/* int x[3][3] = {1,2,3,4} => handle it
|
||||
just like int x[9] = {1,2,3,4} */
|
||||
while (ElementType->Base == TypeArray) {
|
||||
TotalSize *= ElementType->ArraySize;
|
||||
ElementType = ElementType->FromType;
|
||||
|
||||
/* char x[10][10] = {"abc", "def"} => assign "abc" to x[0], "def" to x[1] etc */
|
||||
/* char x[10][10] = {"abc", "def"} => assign "abc" to
|
||||
x[0], "def" to x[1] etc */
|
||||
if (LexGetToken(Parser, NULL, false) == TokenStringConstant &&
|
||||
ElementType->FromType->Base == TypeChar)
|
||||
break;
|
||||
|
@ -257,7 +262,8 @@ int ParseArrayInitialiser(struct ParseState *Parser, struct Value *NewVariable,
|
|||
ProgramFail(Parser, "too many array elements");
|
||||
ArrayElement = VariableAllocValueFromExistingData(Parser,
|
||||
ElementType,
|
||||
(union AnyValue*)(&NewVariable->Val->ArrayMem[0]+ElementSize*ArrayIndex),
|
||||
(union AnyValue*)(&NewVariable->Val->ArrayMem[0] +
|
||||
ElementSize*ArrayIndex),
|
||||
true, NewVariable);
|
||||
}
|
||||
|
||||
|
@ -266,7 +272,8 @@ int ParseArrayInitialiser(struct ParseState *Parser, struct Value *NewVariable,
|
|||
ProgramFail(Parser, "expression expected");
|
||||
|
||||
if (Parser->Mode == RunModeRun && DoAssignment) {
|
||||
ExpressionAssign(Parser, ArrayElement, CValue, false, NULL, 0, false);
|
||||
ExpressionAssign(Parser, ArrayElement, CValue, false, NULL, 0,
|
||||
false);
|
||||
VariableStackPop(Parser, CValue);
|
||||
VariableStackPop(Parser, ArrayElement);
|
||||
}
|
||||
|
@ -389,7 +396,8 @@ void ParseMacroDefinition(struct ParseState *Parser)
|
|||
sizeof(struct MacroDef) + sizeof(const char*) * NumParams,
|
||||
false, NULL, true);
|
||||
MacroValue->Val->MacroDef.NumParams = NumParams;
|
||||
MacroValue->Val->MacroDef.ParamName = (char**)((char*)MacroValue->Val+sizeof(struct MacroDef));
|
||||
MacroValue->Val->MacroDef.ParamName = (char**)((char*)MacroValue->Val +
|
||||
sizeof(struct MacroDef));
|
||||
|
||||
Token = LexGetToken(Parser, &ParamName, true);
|
||||
|
||||
|
@ -595,7 +603,8 @@ enum ParseResult ParseStatement(struct ParseState *Parser,
|
|||
/* might be a typedef-typed variable declaration or it might
|
||||
be an expression */
|
||||
if (VariableDefined(Parser->pc, LexerValue->Val->Identifier)) {
|
||||
VariableGet(Parser->pc, Parser, LexerValue->Val->Identifier, &VarValue);
|
||||
VariableGet(Parser->pc, Parser, LexerValue->Val->Identifier,
|
||||
&VarValue);
|
||||
if (VarValue->Typ->Base == Type_Type) {
|
||||
*Parser = PreState;
|
||||
ParseDeclaration(Parser, Token);
|
||||
|
|
31
platform.c
31
platform.c
|
@ -221,14 +221,29 @@ void PlatformVPrintf(IOFILE *Stream, const char *Format, va_list Args)
|
|||
if (*FPos == '%') {
|
||||
FPos++;
|
||||
switch (*FPos) {
|
||||
case 's': PrintStr(va_arg(Args, char *), Stream); break;
|
||||
case 'd': PrintSimpleInt(va_arg(Args, int), Stream); break;
|
||||
case 'c': PrintCh(va_arg(Args, int), Stream); break;
|
||||
case 't': PrintType(va_arg(Args, struct ValueType *), Stream); break;
|
||||
case 'f': PrintFP(va_arg(Args, double), Stream); break;
|
||||
case '%': PrintCh('%', Stream); break;
|
||||
case '\0': FPos--; break;
|
||||
default: break;
|
||||
case 's':
|
||||
PrintStr(va_arg(Args, char *), Stream);
|
||||
break;
|
||||
case 'd':
|
||||
PrintSimpleInt(va_arg(Args, int), Stream);
|
||||
break;
|
||||
case 'c':
|
||||
PrintCh(va_arg(Args, int), Stream);
|
||||
break;
|
||||
case 't':
|
||||
PrintType(va_arg(Args, struct ValueType *), Stream);
|
||||
break;
|
||||
case 'f':
|
||||
PrintFP(va_arg(Args, double), Stream);
|
||||
break;
|
||||
case '%':
|
||||
PrintCh('%', Stream);
|
||||
break;
|
||||
case '\0':
|
||||
FPos--;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
9
table.c
9
table.c
|
@ -130,7 +130,8 @@ static struct TableEntry *TableSearchIdentifier(struct Table *Tbl,
|
|||
struct TableEntry *Entry;
|
||||
|
||||
for (Entry = Tbl->HashTable[HashValue]; Entry != NULL; Entry = Entry->Next) {
|
||||
if (strncmp(&Entry->p.Key[0], (char *)Key, Len) == 0 && Entry->p.Key[Len] == '\0')
|
||||
if (strncmp(&Entry->p.Key[0], (char *)Key, Len) == 0 &&
|
||||
Entry->p.Key[Len] == '\0')
|
||||
return Entry; /* found */
|
||||
}
|
||||
|
||||
|
@ -139,10 +140,12 @@ static struct TableEntry *TableSearchIdentifier(struct Table *Tbl,
|
|||
}
|
||||
|
||||
/* set an identifier and return the identifier. share if possible */
|
||||
char *TableSetIdentifier(Picoc *pc, struct Table *Tbl, const char *Ident, int IdentLen)
|
||||
char *TableSetIdentifier(Picoc *pc, struct Table *Tbl, const char *Ident,
|
||||
int IdentLen)
|
||||
{
|
||||
int AddAt;
|
||||
struct TableEntry *FoundEntry = TableSearchIdentifier(Tbl, Ident, IdentLen, &AddAt);
|
||||
struct TableEntry *FoundEntry = TableSearchIdentifier(Tbl, Ident, IdentLen,
|
||||
&AddAt);
|
||||
|
||||
if (FoundEntry != NULL)
|
||||
return &FoundEntry->p.Key[0];
|
||||
|
|
15
type.c
15
type.c
|
@ -65,7 +65,8 @@ struct ValueType *TypeGetMatching(Picoc *pc, struct ParseState *Parser,
|
|||
break;
|
||||
default:
|
||||
Sizeof = 0; AlignBytes = 0;
|
||||
break; /* structs and unions will get bigger when we add members to them */
|
||||
break; /* structs and unions will get bigger
|
||||
when we add members to them */
|
||||
}
|
||||
|
||||
return TypeAdd(pc, Parser, ParentType, Base, ArraySize, Identifier, Sizeof,
|
||||
|
@ -150,10 +151,12 @@ void TypeInit(Picoc *pc)
|
|||
TypeAddBaseType(pc, &pc->UnsignedCharType, TypeUnsignedChar,
|
||||
sizeof(unsigned char), (char*)&ca.y - &ca.x);
|
||||
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->GotoLabelType, TypeGotoLabel, 0, 1);
|
||||
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 */
|
||||
pc->CharArrayType = TypeAdd(pc, NULL, &pc->CharType, TypeArray, 0,
|
||||
|
@ -173,7 +176,8 @@ void TypeCleanupNode(Picoc *pc, struct ValueType *Typ)
|
|||
struct ValueType *NextSubType;
|
||||
|
||||
/* clean up and free all the sub-nodes */
|
||||
for (SubType = Typ->DerivedTypeList; SubType != NULL; SubType = NextSubType) {
|
||||
for (SubType = Typ->DerivedTypeList; SubType != NULL;
|
||||
SubType = NextSubType) {
|
||||
NextSubType = SubType->Next;
|
||||
TypeCleanupNode(pc, SubType);
|
||||
if (SubType->OnHeap) {
|
||||
|
@ -257,7 +261,8 @@ void TypeParseStruct(struct ParseState *Parser, struct ValueType **Typ,
|
|||
/* allocate this member's location in the struct */
|
||||
AlignBoundary = MemberValue->Typ->AlignBytes;
|
||||
if (((*Typ)->Sizeof & (AlignBoundary-1)) != 0)
|
||||
(*Typ)->Sizeof += AlignBoundary - ((*Typ)->Sizeof & (AlignBoundary-1));
|
||||
(*Typ)->Sizeof +=
|
||||
AlignBoundary - ((*Typ)->Sizeof & (AlignBoundary-1));
|
||||
|
||||
MemberValue->Val->Integer = (*Typ)->Sizeof;
|
||||
(*Typ)->Sizeof += TypeSizeValue(MemberValue, true);
|
||||
|
|
17
variable.c
17
variable.c
|
@ -169,11 +169,13 @@ struct Value *VariableAllocValueShared(struct ParseState *Parser,
|
|||
struct Value *FromValue)
|
||||
{
|
||||
return VariableAllocValueFromExistingData(Parser, FromValue->Typ,
|
||||
FromValue->Val, FromValue->IsLValue, FromValue->IsLValue ? FromValue : NULL);
|
||||
FromValue->Val, FromValue->IsLValue,
|
||||
FromValue->IsLValue ? FromValue : NULL);
|
||||
}
|
||||
|
||||
/* reallocate a variable so its data has a new size */
|
||||
void VariableRealloc(struct ParseState *Parser, struct Value *FromValue, int NewSize)
|
||||
void VariableRealloc(struct ParseState *Parser, struct Value *FromValue,
|
||||
int NewSize)
|
||||
{
|
||||
if (FromValue->AnyValOnHeap)
|
||||
HeapFreeMem(Parser->pc, FromValue->Val);
|
||||
|
@ -241,8 +243,8 @@ void VariableScopeEnd(struct ParseState *Parser, int ScopeID, int PrevScopeID)
|
|||
&(Parser->pc->GlobalTable) : &(Parser->pc->TopStackFrame)->LocalTable;
|
||||
|
||||
for (Count = 0; Count < HashTable->Size; Count++) {
|
||||
for (Entry = HashTable->HashTable[Count];
|
||||
Entry != NULL; Entry = NextEntry) {
|
||||
for (Entry = HashTable->HashTable[Count]; Entry != NULL;
|
||||
Entry = NextEntry) {
|
||||
NextEntry = Entry->Next;
|
||||
if ((Entry->p.v.Val->ScopeID == ScopeID) &&
|
||||
(Entry->p.v.Val->OutOfScope == false)) {
|
||||
|
@ -270,8 +272,8 @@ int VariableDefinedAndOutOfScope(Picoc *pc, const char* Ident)
|
|||
&(pc->GlobalTable) : &(pc->TopStackFrame)->LocalTable;
|
||||
|
||||
for (Count = 0; Count < HashTable->Size; Count++) {
|
||||
for (Entry = HashTable->HashTable[Count];
|
||||
Entry != NULL; Entry = Entry->Next) {
|
||||
for (Entry = HashTable->HashTable[Count]; Entry != NULL;
|
||||
Entry = Entry->Next) {
|
||||
if (Entry->p.v.Val->OutOfScope == true &&
|
||||
(char*)((intptr_t)Entry->p.v.Key & ~1) == Ident)
|
||||
return true;
|
||||
|
@ -343,7 +345,8 @@ struct Value *VariableDefineButIgnoreIdentical(struct ParseState *Parser,
|
|||
|
||||
if (pc->TopStackFrame != NULL) {
|
||||
/* we're inside a function */
|
||||
if (MNEnd - MNPos > 0) *MNPos++ = '/';
|
||||
if (MNEnd - MNPos > 0)
|
||||
*MNPos++ = '/';
|
||||
strncpy(MNPos, (char*)pc->TopStackFrame->FuncName, MNEnd - MNPos);
|
||||
MNPos += strlen(MNPos);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue