properly print large long values

This commit is contained in:
Joseph Poirier 2018-01-16 21:06:52 -06:00
parent e8a2a13595
commit b8b4c48637
No known key found for this signature in database
GPG key ID: 65858A2540EBDA40
3 changed files with 62 additions and 36 deletions

View file

@ -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':
if (ShowLong) {
ShowLong = 0;
ShowType = &pc->LongType;
} else {
ShowType = &pc->IntType; ShowType = &pc->IntType;
break; /* integer decimal */ }
case 'o': 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) {
/* show a signed integer */ if (ShowType == &pc->LongType) {
if (IS_NUMERIC_COERCIBLE(ThisArg)) { /* show a signed long */
if (ShowLong && ShowType == &pc->IntType) if (IS_NUMERIC_COERCIBLE(ThisArg))
StdioFprintfLong(&SOStream, OneFormatBuf, ExpressionCoerceUnsignedInteger(ThisArg)); StdioFprintfLong(&SOStream, OneFormatBuf, ThisArg->Val->LongInteger);
else 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 */
if (IS_NUMERIC_COERCIBLE(ThisArg))
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

@ -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);

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"