minor format changes
This commit is contained in:
parent
e318530710
commit
6248fb2fb6
97
expression.c
97
expression.c
|
@ -443,7 +443,7 @@ void ExpressionAssignToPointer(struct ParseState *Parser, struct Value *ToValue,
|
||||||
FromValue->Typ == Parser->pc->VoidPtrType ||
|
FromValue->Typ == Parser->pc->VoidPtrType ||
|
||||||
(ToValue->Typ == Parser->pc->VoidPtrType &&
|
(ToValue->Typ == Parser->pc->VoidPtrType &&
|
||||||
FromValue->Typ->Base == TypePointer))
|
FromValue->Typ->Base == TypePointer))
|
||||||
ToValue->Val->Pointer = FromValue->Val->Pointer; /* plain old pointer assignment */
|
ToValue->Val->Pointer = FromValue->Val->Pointer; /* plain old pointer assignment */
|
||||||
else if (FromValue->Typ->Base == TypeArray &&
|
else if (FromValue->Typ->Base == TypeArray &&
|
||||||
(PointedToType == FromValue->Typ->FromType ||
|
(PointedToType == FromValue->Typ->FromType ||
|
||||||
ToValue->Typ == Parser->pc->VoidPtrType)) {
|
ToValue->Typ == Parser->pc->VoidPtrType)) {
|
||||||
|
@ -482,7 +482,8 @@ void ExpressionAssign(struct ParseState *Parser, struct Value *DestValue,
|
||||||
|
|
||||||
if (IS_NUMERIC_COERCIBLE(DestValue) &&
|
if (IS_NUMERIC_COERCIBLE(DestValue) &&
|
||||||
!IS_NUMERIC_COERCIBLE_PLUS_POINTERS(SourceValue, AllowPointerCoercion))
|
!IS_NUMERIC_COERCIBLE_PLUS_POINTERS(SourceValue, AllowPointerCoercion))
|
||||||
AssignFail(Parser, "%t from %t", DestValue->Typ, SourceValue->Typ, 0, 0, FuncName, ParamNo);
|
AssignFail(Parser, "%t from %t", DestValue->Typ, SourceValue->Typ, 0, 0,
|
||||||
|
FuncName, ParamNo);
|
||||||
|
|
||||||
switch (DestValue->Typ->Base) {
|
switch (DestValue->Typ->Base) {
|
||||||
case TypeInt:
|
case TypeInt:
|
||||||
|
@ -685,10 +686,12 @@ void ExpressionPrefixOperator(struct ParseState *Parser,
|
||||||
ResultFP = -TopValue->Val->FP;
|
ResultFP = -TopValue->Val->FP;
|
||||||
break;
|
break;
|
||||||
case TokenIncrement:
|
case TokenIncrement:
|
||||||
ResultFP = ExpressionAssignFP(Parser, TopValue, TopValue->Val->FP+1);
|
ResultFP = ExpressionAssignFP(Parser, TopValue,
|
||||||
|
TopValue->Val->FP+1);
|
||||||
break;
|
break;
|
||||||
case TokenDecrement:
|
case TokenDecrement:
|
||||||
ResultFP = ExpressionAssignFP(Parser, TopValue, TopValue->Val->FP-1);
|
ResultFP = ExpressionAssignFP(Parser, TopValue,
|
||||||
|
TopValue->Val->FP-1);
|
||||||
break;
|
break;
|
||||||
case TokenUnaryNot:
|
case TokenUnaryNot:
|
||||||
ResultFP = !TopValue->Val->FP;
|
ResultFP = !TopValue->Val->FP;
|
||||||
|
@ -872,14 +875,16 @@ void ExpressionInfixOperator(struct ParseState *Parser,
|
||||||
case TypeArray:
|
case TypeArray:
|
||||||
Result = VariableAllocValueFromExistingData(Parser,
|
Result = VariableAllocValueFromExistingData(Parser,
|
||||||
BottomValue->Typ->FromType,
|
BottomValue->Typ->FromType,
|
||||||
(union AnyValue*)(&BottomValue->Val->ArrayMem[0]+TypeSize(BottomValue->Typ,
|
(union AnyValue*)(&BottomValue->Val->ArrayMem[0] +
|
||||||
ArrayIndex, true)),
|
TypeSize(BottomValue->Typ,
|
||||||
|
ArrayIndex, true)),
|
||||||
BottomValue->IsLValue, BottomValue->LValueFrom);
|
BottomValue->IsLValue, BottomValue->LValueFrom);
|
||||||
break;
|
break;
|
||||||
case TypePointer: Result = VariableAllocValueFromExistingData(Parser,
|
case TypePointer: Result = VariableAllocValueFromExistingData(Parser,
|
||||||
BottomValue->Typ->FromType,
|
BottomValue->Typ->FromType,
|
||||||
(union AnyValue*)((char*)BottomValue->Val->Pointer+TypeSize(BottomValue->Typ->FromType,
|
(union AnyValue*)((char*)BottomValue->Val->Pointer +T
|
||||||
0, true) * ArrayIndex),
|
ypeSize(BottomValue->Typ->FromType,
|
||||||
|
0, true) * ArrayIndex),
|
||||||
BottomValue->IsLValue, BottomValue->LValueFrom);
|
BottomValue->IsLValue, BottomValue->LValueFrom);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -970,7 +975,8 @@ void ExpressionInfixOperator(struct ParseState *Parser,
|
||||||
ResultInt = ExpressionAssignInt(Parser, BottomValue, TopInt, false);
|
ResultInt = ExpressionAssignInt(Parser, BottomValue, TopInt, false);
|
||||||
break;
|
break;
|
||||||
case TokenAddAssign:
|
case TokenAddAssign:
|
||||||
ResultInt = ExpressionAssignInt(Parser, BottomValue, BottomInt + TopInt, false);
|
ResultInt = ExpressionAssignInt(Parser, BottomValue,
|
||||||
|
BottomInt + TopInt, false);
|
||||||
break;
|
break;
|
||||||
case TokenSubtractAssign:
|
case TokenSubtractAssign:
|
||||||
ResultInt = ExpressionAssignInt(Parser, BottomValue,
|
ResultInt = ExpressionAssignInt(Parser, BottomValue,
|
||||||
|
@ -1196,9 +1202,12 @@ void ExpressionStackCollapse(struct ParseState *Parser,
|
||||||
#endif
|
#endif
|
||||||
TopValue = TopStackNode->Val;
|
TopValue = TopStackNode->Val;
|
||||||
|
|
||||||
/* pop the value and then the prefix operator - assume they'll still be there until we're done */
|
/* pop the value and then the prefix operator - assume
|
||||||
|
they'll still be there until we're done */
|
||||||
HeapPopStack(Parser->pc, NULL,
|
HeapPopStack(Parser->pc, NULL,
|
||||||
sizeof(struct ExpressionStack)+sizeof(struct Value)+TypeStackSizeValue(TopValue));
|
sizeof(struct ExpressionStack) +
|
||||||
|
sizeof(struct Value) +
|
||||||
|
TypeStackSizeValue(TopValue));
|
||||||
HeapPopStack(Parser->pc, TopOperatorNode,
|
HeapPopStack(Parser->pc, TopOperatorNode,
|
||||||
sizeof(struct ExpressionStack));
|
sizeof(struct ExpressionStack));
|
||||||
*StackTop = TopOperatorNode->Next;
|
*StackTop = TopOperatorNode->Next;
|
||||||
|
@ -1224,7 +1233,9 @@ void ExpressionStackCollapse(struct ParseState *Parser,
|
||||||
/* 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, NULL, sizeof(struct ExpressionStack));
|
||||||
HeapPopStack(Parser->pc, TopValue,
|
HeapPopStack(Parser->pc, TopValue,
|
||||||
sizeof(struct ExpressionStack)+sizeof(struct Value)+TypeStackSizeValue(TopValue));
|
sizeof(struct ExpressionStack) +
|
||||||
|
sizeof(struct Value) +
|
||||||
|
TypeStackSizeValue(TopValue));
|
||||||
*StackTop = TopStackNode->Next->Next;
|
*StackTop = TopStackNode->Next->Next;
|
||||||
|
|
||||||
/* do the postfix operation */
|
/* do the postfix operation */
|
||||||
|
@ -1247,13 +1258,18 @@ void ExpressionStackCollapse(struct ParseState *Parser,
|
||||||
if (TopValue != NULL) {
|
if (TopValue != NULL) {
|
||||||
BottomValue = TopOperatorNode->Next->Val;
|
BottomValue = TopOperatorNode->Next->Val;
|
||||||
|
|
||||||
/* pop a value, the operator and another value - assume they'll still be there until we're done */
|
/* pop a value, the operator and another value - assume
|
||||||
|
they'll still be there until we're done */
|
||||||
HeapPopStack(Parser->pc, NULL,
|
HeapPopStack(Parser->pc, NULL,
|
||||||
sizeof(struct ExpressionStack)+sizeof(struct Value)+TypeStackSizeValue(TopValue));
|
sizeof(struct ExpressionStack) +
|
||||||
|
sizeof(struct Value) +
|
||||||
|
TypeStackSizeValue(TopValue));
|
||||||
HeapPopStack(Parser->pc, NULL,
|
HeapPopStack(Parser->pc, NULL,
|
||||||
sizeof(struct ExpressionStack));
|
sizeof(struct ExpressionStack));
|
||||||
HeapPopStack(Parser->pc, BottomValue,
|
HeapPopStack(Parser->pc, BottomValue,
|
||||||
sizeof(struct ExpressionStack)+sizeof(struct Value)+TypeStackSizeValue(BottomValue));
|
sizeof(struct ExpressionStack) +
|
||||||
|
sizeof(struct Value) +
|
||||||
|
TypeStackSizeValue(BottomValue));
|
||||||
*StackTop = TopOperatorNode->Next->Next;
|
*StackTop = TopOperatorNode->Next->Next;
|
||||||
|
|
||||||
/* do the infix operation */
|
/* do the infix operation */
|
||||||
|
@ -1276,7 +1292,8 @@ void ExpressionStackCollapse(struct ParseState *Parser,
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if we've returned above the ignored precedence level turn ignoring off */
|
/* if we've returned above the ignored precedence level
|
||||||
|
turn ignoring off */
|
||||||
if (FoundPrecedence <= *IgnorePrecedence)
|
if (FoundPrecedence <= *IgnorePrecedence)
|
||||||
*IgnorePrecedence = DEEP_PRECEDENCE;
|
*IgnorePrecedence = DEEP_PRECEDENCE;
|
||||||
}
|
}
|
||||||
|
@ -1352,7 +1369,9 @@ void ExpressionGetStructElement(struct ParseState *Parser,
|
||||||
|
|
||||||
/* pop the value - assume it'll still be there until we're done */
|
/* pop the value - assume it'll still be there until we're done */
|
||||||
HeapPopStack(Parser->pc, ParamVal,
|
HeapPopStack(Parser->pc, ParamVal,
|
||||||
sizeof(struct ExpressionStack)+sizeof(struct Value)+TypeStackSizeValue(StructVal));
|
sizeof(struct ExpressionStack) +
|
||||||
|
sizeof(struct Value) +
|
||||||
|
TypeStackSizeValue(StructVal));
|
||||||
*StackTop = (*StackTop)->Next;
|
*StackTop = (*StackTop)->Next;
|
||||||
|
|
||||||
/* make the result value for this member only */
|
/* make the result value for this member only */
|
||||||
|
@ -1412,7 +1431,8 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
if (LexGetToken(Parser, &LexValue, true) != TokenCloseBracket)
|
if (LexGetToken(Parser, &LexValue, true) != TokenCloseBracket)
|
||||||
ProgramFail(Parser, "brackets not closed");
|
ProgramFail(Parser, "brackets not closed");
|
||||||
|
|
||||||
/* scan and collapse the stack to the precedence of this infix cast operator, then push */
|
/* scan and collapse the stack to the precedence of
|
||||||
|
this infix cast operator, then push */
|
||||||
Precedence = BracketPrecedence +
|
Precedence = BracketPrecedence +
|
||||||
OperatorPrecedence[(int)TokenCast].PrefixPrecedence;
|
OperatorPrecedence[(int)TokenCast].PrefixPrecedence;
|
||||||
|
|
||||||
|
@ -1429,9 +1449,11 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
BracketPrecedence += BRACKET_PRECEDENCE;
|
BracketPrecedence += BRACKET_PRECEDENCE;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* scan and collapse the stack to the precedence of this operator, then push */
|
/* scan and collapse the stack to the precedence of
|
||||||
|
this operator, then push */
|
||||||
|
|
||||||
/* take some extra care for double prefix operators, e.g. x = - -5, or x = **y */
|
/* take some extra care for double prefix operators,
|
||||||
|
e.g. x = - -5, or x = **y */
|
||||||
int NextToken = LexGetToken(Parser, NULL, false);
|
int NextToken = LexGetToken(Parser, NULL, false);
|
||||||
int TempPrecedenceBoost = 0;
|
int TempPrecedenceBoost = 0;
|
||||||
if (NextToken > TokenComma && NextToken < TokenOpenBracket) {
|
if (NextToken > TokenComma && NextToken < TokenOpenBracket) {
|
||||||
|
@ -1458,7 +1480,8 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
case TokenCloseBracket:
|
case TokenCloseBracket:
|
||||||
case TokenRightSquareBracket:
|
case TokenRightSquareBracket:
|
||||||
if (BracketPrecedence == 0) {
|
if (BracketPrecedence == 0) {
|
||||||
/* assume this bracket is after the end of the expression */
|
/* assume this bracket is after the end of the
|
||||||
|
expression */
|
||||||
ParserCopy(Parser, &PreState);
|
ParserCopy(Parser, &PreState);
|
||||||
Done = true;
|
Done = true;
|
||||||
} else {
|
} else {
|
||||||
|
@ -1469,7 +1492,8 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
/* scan and collapse the stack to the precedence of this operator, then push */
|
/* scan and collapse the stack to the precedence of
|
||||||
|
this operator, then push */
|
||||||
Precedence = BracketPrecedence +
|
Precedence = BracketPrecedence +
|
||||||
OperatorPrecedence[(int)Token].PostfixPrecedence;
|
OperatorPrecedence[(int)Token].PostfixPrecedence;
|
||||||
ExpressionStackCollapse(Parser, &StackTop, Precedence,
|
ExpressionStackCollapse(Parser, &StackTop, Precedence,
|
||||||
|
@ -1483,8 +1507,10 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
Precedence = BracketPrecedence +
|
Precedence = BracketPrecedence +
|
||||||
OperatorPrecedence[(int)Token].InfixPrecedence;
|
OperatorPrecedence[(int)Token].InfixPrecedence;
|
||||||
|
|
||||||
/* for right to left order, only go down to the next higher precedence so we evaluate it in reverse order */
|
/* for right to left order, only go down to the next
|
||||||
/* for left to right order, collapse down to this precedence so we evaluate it in forward order */
|
higher precedence so we evaluate it in reverse order */
|
||||||
|
/* for left to right order, collapse down to this precedence
|
||||||
|
so we evaluate it in forward order */
|
||||||
if (IS_LEFT_TO_RIGHT(OperatorPrecedence[(int)Token].InfixPrecedence))
|
if (IS_LEFT_TO_RIGHT(OperatorPrecedence[(int)Token].InfixPrecedence))
|
||||||
ExpressionStackCollapse(Parser, &StackTop, Precedence,
|
ExpressionStackCollapse(Parser, &StackTop, Precedence,
|
||||||
&IgnorePrecedence);
|
&IgnorePrecedence);
|
||||||
|
@ -1493,10 +1519,12 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
&IgnorePrecedence);
|
&IgnorePrecedence);
|
||||||
|
|
||||||
if (Token == TokenDot || Token == TokenArrow) {
|
if (Token == TokenDot || Token == TokenArrow) {
|
||||||
/* this operator is followed by a struct element so handle it as a special case */
|
/* this operator is followed by a struct element so
|
||||||
|
handle it as a special case */
|
||||||
ExpressionGetStructElement(Parser, &StackTop, Token);
|
ExpressionGetStructElement(Parser, &StackTop, Token);
|
||||||
} else {
|
} else {
|
||||||
/* if it's a && or || operator we may not need to evaluate the right hand side of the expression */
|
/* if it's a && or || operator we may not need to
|
||||||
|
evaluate the right hand side of the expression */
|
||||||
if ((Token == TokenLogicalOr || Token == TokenLogicalAnd) &&
|
if ((Token == TokenLogicalOr || Token == TokenLogicalAnd) &&
|
||||||
IS_NUMERIC_COERCIBLE(StackTop->Val)) {
|
IS_NUMERIC_COERCIBLE(StackTop->Val)) {
|
||||||
long LHSInt = ExpressionCoerceInteger(StackTop->Val);
|
long LHSInt = ExpressionCoerceInteger(StackTop->Val);
|
||||||
|
@ -1553,7 +1581,8 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
ProgramFail(&MacroParser, "macro arguments missing");
|
ProgramFail(&MacroParser, "macro arguments missing");
|
||||||
|
|
||||||
if (!ExpressionParse(&MacroParser, &MacroResult) ||
|
if (!ExpressionParse(&MacroParser, &MacroResult) ||
|
||||||
LexGetToken(&MacroParser, NULL, false) != TokenEndOfFunction)
|
LexGetToken(&MacroParser, NULL, false) !=
|
||||||
|
TokenEndOfFunction)
|
||||||
ProgramFail(&MacroParser, "expression expected");
|
ProgramFail(&MacroParser, "expression expected");
|
||||||
|
|
||||||
ExpressionStackPushValueNode(Parser, &StackTop, MacroResult);
|
ExpressionStackPushValueNode(Parser, &StackTop, MacroResult);
|
||||||
|
@ -1572,7 +1601,8 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
IgnorePrecedence = DEEP_PRECEDENCE;
|
IgnorePrecedence = DEEP_PRECEDENCE;
|
||||||
|
|
||||||
PrefixState = false;
|
PrefixState = false;
|
||||||
} else if ((int)Token > TokenCloseBracket && (int)Token <= TokenCharacterConstant) {
|
} else if ((int)Token > TokenCloseBracket &&
|
||||||
|
(int)Token <= TokenCharacterConstant) {
|
||||||
/* it's a value of some sort, push it */
|
/* it's a value of some sort, push it */
|
||||||
if (!PrefixState)
|
if (!PrefixState)
|
||||||
ProgramFail(Parser, "value not expected here");
|
ProgramFail(Parser, "value not expected here");
|
||||||
|
@ -1580,7 +1610,8 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
PrefixState = false;
|
PrefixState = false;
|
||||||
ExpressionStackPushValue(Parser, &StackTop, LexValue);
|
ExpressionStackPushValue(Parser, &StackTop, LexValue);
|
||||||
} else if (IsTypeToken(Parser, Token, LexValue)) {
|
} else if (IsTypeToken(Parser, Token, LexValue)) {
|
||||||
/* it's a type. push it on the stack like a value. this is used in sizeof() */
|
/* it's a type. push it on the stack like a value.
|
||||||
|
this is used in sizeof() */
|
||||||
struct ValueType *Typ;
|
struct ValueType *Typ;
|
||||||
char *Identifier;
|
char *Identifier;
|
||||||
struct Value *TypeValue;
|
struct Value *TypeValue;
|
||||||
|
@ -1620,7 +1651,9 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
HeapPopStack(Parser->pc, StackTop, sizeof(struct ExpressionStack));
|
HeapPopStack(Parser->pc, StackTop, sizeof(struct ExpressionStack));
|
||||||
} else
|
} else
|
||||||
HeapPopStack(Parser->pc, StackTop->Val,
|
HeapPopStack(Parser->pc, StackTop->Val,
|
||||||
sizeof(struct ExpressionStack)+sizeof(struct Value)+TypeStackSizeValue(StackTop->Val));
|
sizeof(struct ExpressionStack) +
|
||||||
|
sizeof(struct Value) +
|
||||||
|
TypeStackSizeValue(StackTop->Val));
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_EXPRESSIONS
|
#ifdef DEBUG_EXPRESSIONS
|
||||||
|
@ -1687,7 +1720,9 @@ void ExpressionParseMacroCall(struct ParseState *Parser,
|
||||||
ProgramFail(Parser, "not enough arguments to '%s'", MacroName);
|
ProgramFail(Parser, "not enough arguments to '%s'", MacroName);
|
||||||
|
|
||||||
if (MDef->Body.Pos == NULL)
|
if (MDef->Body.Pos == NULL)
|
||||||
ProgramFail(Parser, "ExpressionParseMacroCall MacroName: '%s' is undefined", MacroName);
|
ProgramFail(Parser,
|
||||||
|
"ExpressionParseMacroCall MacroName: '%s' is undefined",
|
||||||
|
MacroName);
|
||||||
|
|
||||||
ParserCopy(&MacroParser, &MDef->Body);
|
ParserCopy(&MacroParser, &MDef->Body);
|
||||||
MacroParser.Mode = Parser->Mode;
|
MacroParser.Mode = Parser->Mode;
|
||||||
|
|
6
heap.c
6
heap.c
|
@ -1,6 +1,7 @@
|
||||||
/* picoc heap memory allocation. */
|
/* picoc heap memory allocation. */
|
||||||
|
|
||||||
/* stack grows up from the bottom and heap grows down from the top of heap space */
|
/* stack grows up from the bottom and heap grows down from
|
||||||
|
the top of heap space */
|
||||||
#include "interpreter.h"
|
#include "interpreter.h"
|
||||||
|
|
||||||
#ifdef DEBUG_HEAP
|
#ifdef DEBUG_HEAP
|
||||||
|
@ -103,7 +104,8 @@ void HeapPushStackFrame(Picoc *pc)
|
||||||
MEM_ALIGN(sizeof(ALIGN_TYPE)));
|
MEM_ALIGN(sizeof(ALIGN_TYPE)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* pop the current stack frame, freeing all memory in the frame. can return NULL */
|
/* pop the current stack frame, freeing all memory in the
|
||||||
|
frame. can return NULL */
|
||||||
int HeapPopStackFrame(Picoc *pc)
|
int HeapPopStackFrame(Picoc *pc)
|
||||||
{
|
{
|
||||||
if (*(void **)pc->StackFrame != NULL) {
|
if (*(void **)pc->StackFrame != NULL) {
|
||||||
|
|
Loading…
Reference in a new issue