minor formatting, add message on how to terminate interactive mode

This commit is contained in:
Joseph Poirier 2015-06-14 15:48:18 -05:00
parent 7c21d0a54f
commit 1ff1ef9cfb
5 changed files with 70 additions and 77 deletions

View file

@ -220,7 +220,7 @@ void ExpressionStackShow(Picoc *pc, struct ExpressionStack *StackTop)
} }
#endif #endif
int IsTypeToken(struct ParseState * Parser, enum LexToken t, int IsTypeToken(struct ParseState *Parser, enum LexToken t,
struct Value * LexValue) struct Value * LexValue)
{ {
if (t >= TokenIntType && t <= TokenUnsignedType) if (t >= TokenIntType && t <= TokenUnsignedType)
@ -697,7 +697,6 @@ void ExpressionPrefixOperator(struct ParseState *Parser,
if (TopValue->Typ == &Parser->pc->FPType) { if (TopValue->Typ == &Parser->pc->FPType) {
/* floating point prefix arithmetic */ /* floating point prefix arithmetic */
double ResultFP = 0.0; double ResultFP = 0.0;
switch (Op) { switch (Op) {
case TokenPlus: case TokenPlus:
ResultFP = TopValue->Val->FP; ResultFP = TopValue->Val->FP;
@ -722,68 +721,65 @@ void ExpressionPrefixOperator(struct ParseState *Parser,
} }
ExpressionPushFP(Parser, StackTop, ResultFP); ExpressionPushFP(Parser, StackTop, ResultFP);
} else if (IS_NUMERIC_COERCIBLE(TopValue)) { } else if (IS_NUMERIC_COERCIBLE(TopValue)) {
/* integer prefix arithmetic */ /* integer prefix arithmetic */
long ResultInt = 0; long ResultInt = 0;
long TopInt = ExpressionCoerceInteger(TopValue); long TopInt = ExpressionCoerceInteger(TopValue);
switch (Op) { switch (Op) {
case TokenPlus: case TokenPlus:
ResultInt = TopInt; ResultInt = TopInt;
break; break;
case TokenMinus: case TokenMinus:
ResultInt = -TopInt; ResultInt = -TopInt;
break; break;
case TokenIncrement: case TokenIncrement:
ResultInt = ExpressionAssignInt(Parser, TopValue, ResultInt = ExpressionAssignInt(Parser, TopValue,
TopInt+1, false); TopInt+1, false);
break; break;
case TokenDecrement: case TokenDecrement:
ResultInt = ExpressionAssignInt(Parser, TopValue, ResultInt = ExpressionAssignInt(Parser, TopValue,
TopInt-1, false); TopInt-1, false);
break; break;
case TokenUnaryNot: case TokenUnaryNot:
ResultInt = !TopInt; ResultInt = !TopInt;
break; break;
case TokenUnaryExor: case TokenUnaryExor:
ResultInt = ~TopInt; ResultInt = ~TopInt;
break; break;
default: default:
ProgramFail(Parser, "invalid operation");
break;
}
ExpressionPushInt(Parser, StackTop, ResultInt);
} else if (TopValue->Typ->Base == TypePointer) {
/* pointer prefix arithmetic */
int Size = TypeSize(TopValue->Typ->FromType, 0, true);
struct Value *StackValue;
void *ResultPtr;
if (TopValue->Val->Pointer == NULL)
ProgramFail(Parser, "invalid use of a NULL pointer");
if (!TopValue->IsLValue)
ProgramFail(Parser, "can't assign to this");
switch (Op) {
case TokenIncrement:
TopValue->Val->Pointer =
(void*)((char*)TopValue->Val->Pointer+Size);
break;
case TokenDecrement:
TopValue->Val->Pointer =
(void*)((char*)TopValue->Val->Pointer-Size);
break;
default:
ProgramFail(Parser, "invalid operation");
break;
}
ResultPtr = TopValue->Val->Pointer;
StackValue = ExpressionStackPushValueByType(Parser, StackTop,
TopValue->Typ);
StackValue->Val->Pointer = ResultPtr;
} else
ProgramFail(Parser, "invalid operation"); ProgramFail(Parser, "invalid operation");
break; break;
}
ExpressionPushInt(Parser, StackTop, ResultInt);
} else if (TopValue->Typ->Base == TypePointer) {
/* pointer prefix arithmetic */
int Size = TypeSize(TopValue->Typ->FromType, 0, true);
struct Value *StackValue;
void *ResultPtr;
if (TopValue->Val->Pointer == NULL)
ProgramFail(Parser, "a. invalid use of a NULL pointer");
if (!TopValue->IsLValue)
ProgramFail(Parser, "can't assign to this");
switch (Op) {
case TokenIncrement:
TopValue->Val->Pointer =
(void*)((char*)TopValue->Val->Pointer+Size);
break;
case TokenDecrement:
TopValue->Val->Pointer =
(void*)((char*)TopValue->Val->Pointer-Size);
break;
default:
ProgramFail(Parser, "invalid operation");
break;
}
ResultPtr = TopValue->Val->Pointer;
StackValue = ExpressionStackPushValueByType(Parser, StackTop,
TopValue->Typ);
StackValue->Val->Pointer = ResultPtr;
} else {
ProgramFail(Parser, "invalid operation");
}
break;
} }
} }
@ -811,8 +807,7 @@ void ExpressionPostfixOperator(struct ParseState *Parser,
break; break;
} }
ExpressionPushFP(Parser, StackTop, ResultFP); ExpressionPushFP(Parser, StackTop, ResultFP);
} } else if (IS_NUMERIC_COERCIBLE(TopValue)) {
else if (IS_NUMERIC_COERCIBLE(TopValue)) {
long ResultInt = 0; long ResultInt = 0;
long TopInt = ExpressionCoerceInteger(TopValue); long TopInt = ExpressionCoerceInteger(TopValue);
switch (Op) { switch (Op) {
@ -840,7 +835,7 @@ void ExpressionPostfixOperator(struct ParseState *Parser,
void *OrigPointer = TopValue->Val->Pointer; void *OrigPointer = TopValue->Val->Pointer;
if (TopValue->Val->Pointer == NULL) if (TopValue->Val->Pointer == NULL)
ProgramFail(Parser, "invalid use of a NULL pointer"); ProgramFail(Parser, "b. invalid use of a NULL pointer");
if (!TopValue->IsLValue) if (!TopValue->IsLValue)
ProgramFail(Parser, "can't assign to this"); ProgramFail(Parser, "can't assign to this");
@ -859,8 +854,7 @@ void ExpressionPostfixOperator(struct ParseState *Parser,
StackValue = ExpressionStackPushValueByType(Parser, StackTop, StackValue = ExpressionStackPushValueByType(Parser, StackTop,
TopValue->Typ); TopValue->Typ);
StackValue->Val->Pointer = OrigPointer; StackValue->Val->Pointer = OrigPointer;
} } else
else
ProgramFail(Parser, "invalid operation"); ProgramFail(Parser, "invalid operation");
} }
@ -1121,7 +1115,7 @@ void ExpressionInfixOperator(struct ParseState *Parser,
Pointer = BottomValue->Val->Pointer; Pointer = BottomValue->Val->Pointer;
if (Pointer == NULL) if (Pointer == NULL)
ProgramFail(Parser, "invalid use of a NULL pointer"); ProgramFail(Parser, "c. invalid use of a NULL pointer");
if (Op == TokenPlus) if (Op == TokenPlus)
Pointer = (void*)((char*)Pointer + TopInt * Size); Pointer = (void*)((char*)Pointer + TopInt * Size);
@ -1142,7 +1136,7 @@ void ExpressionInfixOperator(struct ParseState *Parser,
Pointer = BottomValue->Val->Pointer; Pointer = BottomValue->Val->Pointer;
if (Pointer == NULL) if (Pointer == NULL)
ProgramFail(Parser, "invalid use of a NULL pointer"); ProgramFail(Parser, "d. invalid use of a NULL pointer");
if (Op == TokenAddAssign) if (Op == TokenAddAssign)
Pointer = (void*)((char*)Pointer + TopInt * Size); Pointer = (void*)((char*)Pointer + TopInt * Size);

View file

@ -40,6 +40,7 @@ typedef FILE IOFILE;
#define IS_FP(v) ((v)->Typ->Base == TypeFP) #define IS_FP(v) ((v)->Typ->Base == TypeFP)
#define FP_VAL(v) ((v)->Val->FP) #define FP_VAL(v) ((v)->Val->FP)
/* ap -> AllowPointerCoercion = true | false*/
#define IS_POINTER_COERCIBLE(v, ap) ((ap) ? ((v)->Typ->Base == TypePointer) : 0) #define IS_POINTER_COERCIBLE(v, ap) ((ap) ? ((v)->Typ->Base == TypePointer) : 0)
#define POINTER_COERCE(v) ((int)(v)->Val->Pointer) #define POINTER_COERCE(v) ((int)(v)->Val->Pointer)
@ -281,7 +282,7 @@ struct Value
char ValOnStack; /* the AnyValue is on the stack along with this 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 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 */ char IsLValue; /* is modifiable and is allocated somewhere we can usefully modify it */
int ScopeID; /* to know when it goes out of scope */ int ScopeID; /* to know when it goes out of scope */
char OutOfScope; char OutOfScope;
}; };

View file

@ -583,13 +583,11 @@ enum ParseResult ParseStatement(struct ParseState *Parser,
struct Value *VarValue; struct Value *VarValue;
struct ParseState PreState; struct ParseState PreState;
#ifdef DEBUGGER
/* if we're debugging, check for a breakpoint */ /* if we're debugging, check for a breakpoint */
if (Parser->DebugMode && Parser->Mode == RunModeRun) if (Parser->DebugMode && Parser->Mode == RunModeRun)
#ifdef DEBUGGER DebugCheckStatement(Parser);
DebugCheckStatement(Parser)
#endif #endif
;
/* take note of where we are and then grab a token to see what /* take note of where we are and then grab a token to see what
statement we have */ statement we have */

View file

@ -25,7 +25,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 (Ctrl+D to exit)\n");
exit(1); exit(1);
} }

View file

@ -46,7 +46,7 @@
#define LOCAL_TABLE_SIZE (11) /* size of local variable table (can expand) */ #define LOCAL_TABLE_SIZE (11) /* size of local variable table (can expand) */
#define STRUCT_TABLE_SIZE (11) /* size of struct/union member table (can expand) */ #define STRUCT_TABLE_SIZE (11) /* size of struct/union member table (can expand) */
#define INTERACTIVE_PROMPT_START "starting picoc " PICOC_VERSION "\n" #define INTERACTIVE_PROMPT_START "starting picoc " PICOC_VERSION " (Ctrl+D to exit)\n"
#define INTERACTIVE_PROMPT_STATEMENT "picoc> " #define INTERACTIVE_PROMPT_STATEMENT "picoc> "
#define INTERACTIVE_PROMPT_LINE " > " #define INTERACTIVE_PROMPT_LINE " > "