Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoirier committed Jun 14, 2015
1 parent 3ba3fa5 commit 890694a
Show file tree
Hide file tree
Showing 9 changed files with 340 additions and 142 deletions.
59 changes: 44 additions & 15 deletions clibrary.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 changes: 103 additions & 49 deletions expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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:
Expand Down Expand Up @@ -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) +
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions include.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Loading

0 comments on commit 890694a

Please sign in to comment.