Merge pull request #18 from jpoirier/develop

properly print large long values
This commit is contained in:
Joseph Poirier 2018-01-16 21:08:39 -06:00 committed by GitHub
commit 345b1defbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 36 deletions

View file

@ -132,13 +132,13 @@ void StdioFprintfLong(StdOutStream *Stream, const char *Format, uint64_t Value)
case 'i': case 'i':
UseFormat = PRIi64; UseFormat = PRIi64;
break; break;
case 'o': case 'o':
UseFormat = PRIo64; UseFormat = PRIo64;
break; break;
case 'u': case 'u':
UseFormat = PRIu64; UseFormat = PRIu64;
break; break;
case 'x': case 'x':
UseFormat = PRIx64; UseFormat = PRIx64;
break; break;
case 'X': case 'X':
@ -258,10 +258,20 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut,
switch (*FPos) { switch (*FPos) {
case 'd': case 'd':
case 'i': case 'i':
ShowType = &pc->IntType; if (ShowLong) {
break; /* integer decimal */ ShowLong = 0;
case 'o': ShowType = &pc->LongType;
} else {
ShowType = &pc->IntType;
}
break;
case 'u': case 'u':
if (ShowLong) {
ShowLong = 0;
ShowType = &pc->UnsignedLongType;
break;
}
case 'o':
case 'x': case 'x':
case 'X': case 'X':
ShowType = &pc->IntType; ShowType = &pc->IntType;
@ -350,14 +360,24 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut,
/* print this argument */ /* print this argument */
ThisArg = (struct Value*)((char*)ThisArg + ThisArg = (struct Value*)((char*)ThisArg +
MEM_ALIGN(sizeof(struct Value)+TypeStackSizeValue(ThisArg))); MEM_ALIGN(sizeof(struct Value)+TypeStackSizeValue(ThisArg)));
if (ShowType == &pc->IntType) {
if (ShowType == &pc->LongType) {
/* show a signed long */
if (IS_NUMERIC_COERCIBLE(ThisArg))
StdioFprintfLong(&SOStream, OneFormatBuf, ThisArg->Val->LongInteger);
else
StdioOutPuts("XXX", &SOStream);
} else if (ShowType == &pc->UnsignedLongType) {
/* show a unsigned long */
if (IS_NUMERIC_COERCIBLE(ThisArg))
StdioFprintfLong(&SOStream, OneFormatBuf, ThisArg->Val->UnsignedLongInteger);
else
StdioOutPuts("XXX", &SOStream);
} else if (ShowType == &pc->IntType) {
/* show a signed integer */ /* show a signed integer */
if (IS_NUMERIC_COERCIBLE(ThisArg)) { if (IS_NUMERIC_COERCIBLE(ThisArg))
if (ShowLong && ShowType == &pc->IntType)
StdioFprintfLong(&SOStream, OneFormatBuf, ExpressionCoerceUnsignedInteger(ThisArg));
else
StdioFprintfWord(&SOStream, OneFormatBuf, (unsigned int)ExpressionCoerceUnsignedInteger(ThisArg)); StdioFprintfWord(&SOStream, OneFormatBuf, (unsigned int)ExpressionCoerceUnsignedInteger(ThisArg));
} else else
StdioOutPuts("XXX", &SOStream); StdioOutPuts("XXX", &SOStream);
} else if (ShowType == &pc->FPType) { } else if (ShowType == &pc->FPType) {
/* show a floating point number */ /* show a floating point number */

View file

@ -160,9 +160,9 @@ void ExpressionStackShow(Picoc *pc, struct ExpressionStack *StackTop)
printf("%ld:unsigned long", StackTop->Val->Val->UnsignedLongInteger); printf("%ld:unsigned long", StackTop->Val->Val->UnsignedLongInteger);
break; break;
case TypeFP: case TypeFP:
printf("%f:fp", StackTop->Val->Val->FP); printf("%f:fp", StackTop->Val->Val->FP);
break; break;
case TypeFunction: case TypeFunction:
printf("%s:function", StackTop->Val->Val->Identifier); printf("%s:function", StackTop->Val->Val->Identifier);
break; break;
case TypeMacro: case TypeMacro:
@ -245,17 +245,17 @@ long ExpressionCoerceInteger(struct Value *Val)
case TypeShort: case TypeShort:
return (long)Val->Val->ShortInteger; return (long)Val->Val->ShortInteger;
case TypeLong: case TypeLong:
return (int64_t)Val->Val->LongInteger; return (long)Val->Val->LongInteger;
case TypeUnsignedInt: case TypeUnsignedInt:
return (unsigned long)Val->Val->UnsignedInteger; return (long)Val->Val->UnsignedInteger;
case TypeUnsignedShort: case TypeUnsignedShort:
return (unsigned long)Val->Val->UnsignedShortInteger; return (long)Val->Val->UnsignedShortInteger;
case TypeUnsignedLong: case TypeUnsignedLong:
return (uint64_t)Val->Val->UnsignedLongInteger; return (long)Val->Val->UnsignedLongInteger;
case TypeUnsignedChar: case TypeUnsignedChar:
return (unsigned long)Val->Val->UnsignedCharacter; return (long)Val->Val->UnsignedCharacter;
case TypePointer: case TypePointer:
return (uintptr_t)Val->Val->Pointer; return (long)Val->Val->Pointer;
case TypeFP: case TypeFP:
return (long)Val->Val->FP; return (long)Val->Val->FP;
default: default:
@ -273,17 +273,17 @@ unsigned long ExpressionCoerceUnsignedInteger(struct Value *Val)
case TypeShort: case TypeShort:
return (unsigned long)Val->Val->ShortInteger; return (unsigned long)Val->Val->ShortInteger;
case TypeLong: case TypeLong:
return (uint64_t)Val->Val->LongInteger; return (unsigned long)Val->Val->LongInteger;
case TypeUnsignedInt: case TypeUnsignedInt:
return (unsigned long)Val->Val->UnsignedInteger; return (unsigned long)Val->Val->UnsignedInteger;
case TypeUnsignedShort: case TypeUnsignedShort:
return (unsigned long)Val->Val->UnsignedShortInteger; return (unsigned long)Val->Val->UnsignedShortInteger;
case TypeUnsignedLong: case TypeUnsignedLong:
return (uint64_t)Val->Val->UnsignedLongInteger; return (unsigned long)Val->Val->UnsignedLongInteger;
case TypeUnsignedChar: case TypeUnsignedChar:
return (unsigned long)Val->Val->UnsignedCharacter; return (unsigned long)Val->Val->UnsignedCharacter;
case TypePointer: case TypePointer:
return (uintptr_t)Val->Val->Pointer; return (unsigned long)Val->Val->Pointer;
case TypeFP: case TypeFP:
return (unsigned long)Val->Val->FP; return (unsigned long)Val->Val->FP;
default: default:
@ -342,7 +342,7 @@ long ExpressionAssignInt(struct ParseState *Parser, struct Value *DestValue,
DestValue->Val->Character = (char)FromInt; DestValue->Val->Character = (char)FromInt;
break; break;
case TypeLong: case TypeLong:
DestValue->Val->LongInteger = (int64_t)FromInt; DestValue->Val->LongInteger = (long)FromInt;
break; break;
case TypeUnsignedInt: case TypeUnsignedInt:
DestValue->Val->UnsignedInteger = (unsigned int)FromInt; DestValue->Val->UnsignedInteger = (unsigned int)FromInt;
@ -351,7 +351,7 @@ long ExpressionAssignInt(struct ParseState *Parser, struct Value *DestValue,
DestValue->Val->UnsignedShortInteger = (unsigned short)FromInt; DestValue->Val->UnsignedShortInteger = (unsigned short)FromInt;
break; break;
case TypeUnsignedLong: case TypeUnsignedLong:
DestValue->Val->UnsignedLongInteger = (uint64_t)FromInt; DestValue->Val->UnsignedLongInteger = (unsigned long)FromInt;
break; break;
case TypeUnsignedChar: case TypeUnsignedChar:
DestValue->Val->UnsignedCharacter = (unsigned char)FromInt; DestValue->Val->UnsignedCharacter = (unsigned char)FromInt;
@ -442,6 +442,9 @@ void ExpressionPushInt(struct ParseState *Parser,
{ {
struct Value *ValueLoc = VariableAllocValueFromType(Parser->pc, Parser, struct Value *ValueLoc = VariableAllocValueFromType(Parser->pc, Parser,
&Parser->pc->IntType, false, NULL, false); &Parser->pc->IntType, false, NULL, false);
// jdp: ugly hack to properly print long values
ValueLoc->Val->UnsignedLongInteger = IntValue;
ValueLoc->Val->LongInteger = IntValue;
ValueLoc->Val->Integer = IntValue; ValueLoc->Val->Integer = IntValue;
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc); ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
} }
@ -519,7 +522,7 @@ void ExpressionAssign(struct ParseState *Parser, struct Value *DestValue,
DestValue->Val->Character = (char)ExpressionCoerceInteger(SourceValue); DestValue->Val->Character = (char)ExpressionCoerceInteger(SourceValue);
break; break;
case TypeLong: case TypeLong:
DestValue->Val->LongInteger = ExpressionCoerceInteger(SourceValue); DestValue->Val->LongInteger = SourceValue->Val->LongInteger;
break; break;
case TypeUnsignedInt: case TypeUnsignedInt:
DestValue->Val->UnsignedInteger = DestValue->Val->UnsignedInteger =
@ -530,8 +533,7 @@ void ExpressionAssign(struct ParseState *Parser, struct Value *DestValue,
(unsigned short)ExpressionCoerceUnsignedInteger(SourceValue); (unsigned short)ExpressionCoerceUnsignedInteger(SourceValue);
break; break;
case TypeUnsignedLong: case TypeUnsignedLong:
DestValue->Val->UnsignedLongInteger = DestValue->Val->UnsignedLongInteger = SourceValue->Val->UnsignedLongInteger;
ExpressionCoerceUnsignedInteger(SourceValue);
break; break;
case TypeUnsignedChar: case TypeUnsignedChar:
DestValue->Val->UnsignedCharacter = DestValue->Val->UnsignedCharacter =
@ -718,8 +720,12 @@ 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 */
int64_t ResultInt = 0; long ResultInt = 0;
int64_t TopInt = ExpressionCoerceInteger(TopValue); long TopInt = 0;
if (TopValue->Typ->Base == TypeLong)
TopInt = TopValue->Val->LongInteger;
else
TopInt = ExpressionCoerceInteger(TopValue);
switch (Op) { switch (Op) {
case TokenPlus: case TokenPlus:
ResultInt = TopInt; ResultInt = TopInt;
@ -811,8 +817,8 @@ void ExpressionPostfixOperator(struct ParseState *Parser,
} }
ExpressionPushFP(Parser, StackTop, ResultFP); ExpressionPushFP(Parser, StackTop, ResultFP);
} else if (IS_NUMERIC_COERCIBLE(TopValue)) { } else if (IS_NUMERIC_COERCIBLE(TopValue)) {
int64_t ResultInt = 0; long ResultInt = 0;
int64_t TopInt = ExpressionCoerceInteger(TopValue); long TopInt = ExpressionCoerceInteger(TopValue);
switch (Op) { switch (Op) {
case TokenIncrement: case TokenIncrement:
ResultInt = ExpressionAssignInt(Parser, TopValue, TopInt+1, true); ResultInt = ExpressionAssignInt(Parser, TopValue, TopInt+1, true);
@ -991,8 +997,8 @@ void ExpressionInfixOperator(struct ParseState *Parser,
ExpressionPushFP(Parser, StackTop, ResultFP); ExpressionPushFP(Parser, StackTop, ResultFP);
} else if (IS_NUMERIC_COERCIBLE(TopValue) && IS_NUMERIC_COERCIBLE(BottomValue)) { } else if (IS_NUMERIC_COERCIBLE(TopValue) && IS_NUMERIC_COERCIBLE(BottomValue)) {
/* integer operation */ /* integer operation */
int64_t TopInt = ExpressionCoerceInteger(TopValue); long TopInt = ExpressionCoerceInteger(TopValue);
int64_t BottomInt = ExpressionCoerceInteger(BottomValue); long BottomInt = ExpressionCoerceInteger(BottomValue);
switch (Op) { switch (Op) {
case TokenAssign: case TokenAssign:
ResultInt = ExpressionAssignInt(Parser, BottomValue, TopInt, false); ResultInt = ExpressionAssignInt(Parser, BottomValue, TopInt, false);
@ -1080,7 +1086,7 @@ void ExpressionInfixOperator(struct ParseState *Parser,
if (BottomValue->Typ->Base == TypeUnsignedInt || BottomValue->Typ->Base == TypeUnsignedLong) if (BottomValue->Typ->Base == TypeUnsignedInt || BottomValue->Typ->Base == TypeUnsignedLong)
ResultInt = (uint64_t) BottomInt >> TopInt; ResultInt = (uint64_t) BottomInt >> TopInt;
else else
ResultInt = BottomInt >> TopInt; ResultInt = BottomInt >> TopInt;
*/ */
break; break;
case TokenShiftRight: case TokenShiftRight:

View file

@ -9,7 +9,7 @@
/* VER, the git hash number, and TAG are obtained via the Makefile */ /* VER, the git hash number, and TAG are obtained via the Makefile */
#define PICOC_VERSION TAG " r" VER #define PICOC_VERSION TAG " r" VER
#else #else
#define PICOC_VERSION "v2.2" #define PICOC_VERSION "v2.3"
#endif #endif
#include "interpreter.h" #include "interpreter.h"