Skip to content

Commit

Permalink
properly print large long values
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoirier committed Jan 17, 2018
1 parent e8a2a13 commit b8b4c48
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 36 deletions.
44 changes: 32 additions & 12 deletions cstdlib/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ void StdioFprintfLong(StdOutStream *Stream, const char *Format, uint64_t Value)
case 'i':
UseFormat = PRIi64;
break;
case 'o':
case 'o':
UseFormat = PRIo64;
break;
case 'u':
case 'u':
UseFormat = PRIu64;
break;
case 'x':
case 'x':
UseFormat = PRIx64;
break;
case 'X':
Expand Down Expand Up @@ -258,10 +258,20 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut,
switch (*FPos) {
case 'd':
case 'i':
ShowType = &pc->IntType;
break; /* integer decimal */
case 'o':
if (ShowLong) {
ShowLong = 0;
ShowType = &pc->LongType;
} else {
ShowType = &pc->IntType;
}
break;
case 'u':
if (ShowLong) {
ShowLong = 0;
ShowType = &pc->UnsignedLongType;
break;
}
case 'o':
case 'x':
case 'X':
ShowType = &pc->IntType;
Expand Down Expand Up @@ -350,14 +360,24 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut,
/* print this argument */
ThisArg = (struct Value*)((char*)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 */
if (IS_NUMERIC_COERCIBLE(ThisArg)) {
if (ShowLong && ShowType == &pc->IntType)
StdioFprintfLong(&SOStream, OneFormatBuf, ExpressionCoerceUnsignedInteger(ThisArg));
else
if (IS_NUMERIC_COERCIBLE(ThisArg))
StdioFprintfWord(&SOStream, OneFormatBuf, (unsigned int)ExpressionCoerceUnsignedInteger(ThisArg));
} else
else
StdioOutPuts("XXX", &SOStream);
} else if (ShowType == &pc->FPType) {
/* show a floating point number */
Expand Down
52 changes: 29 additions & 23 deletions expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ void ExpressionStackShow(Picoc *pc, struct ExpressionStack *StackTop)
printf("%ld:unsigned long", StackTop->Val->Val->UnsignedLongInteger);
break;
case TypeFP:
printf("%f:fp", StackTop->Val->Val->FP);
printf("%f:fp", StackTop->Val->Val->FP);
break;
case TypeFunction:
case TypeFunction:
printf("%s:function", StackTop->Val->Val->Identifier);
break;
case TypeMacro:
Expand Down Expand Up @@ -245,17 +245,17 @@ long ExpressionCoerceInteger(struct Value *Val)
case TypeShort:
return (long)Val->Val->ShortInteger;
case TypeLong:
return (int64_t)Val->Val->LongInteger;
return (long)Val->Val->LongInteger;
case TypeUnsignedInt:
return (unsigned long)Val->Val->UnsignedInteger;
return (long)Val->Val->UnsignedInteger;
case TypeUnsignedShort:
return (unsigned long)Val->Val->UnsignedShortInteger;
return (long)Val->Val->UnsignedShortInteger;
case TypeUnsignedLong:
return (uint64_t)Val->Val->UnsignedLongInteger;
return (long)Val->Val->UnsignedLongInteger;
case TypeUnsignedChar:
return (unsigned long)Val->Val->UnsignedCharacter;
return (long)Val->Val->UnsignedCharacter;
case TypePointer:
return (uintptr_t)Val->Val->Pointer;
return (long)Val->Val->Pointer;
case TypeFP:
return (long)Val->Val->FP;
default:
Expand All @@ -273,17 +273,17 @@ unsigned long ExpressionCoerceUnsignedInteger(struct Value *Val)
case TypeShort:
return (unsigned long)Val->Val->ShortInteger;
case TypeLong:
return (uint64_t)Val->Val->LongInteger;
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 (uint64_t)Val->Val->UnsignedLongInteger;
return (unsigned long)Val->Val->UnsignedLongInteger;
case TypeUnsignedChar:
return (unsigned long)Val->Val->UnsignedCharacter;
case TypePointer:
return (uintptr_t)Val->Val->Pointer;
return (unsigned long)Val->Val->Pointer;
case TypeFP:
return (unsigned long)Val->Val->FP;
default:
Expand Down Expand Up @@ -342,7 +342,7 @@ long ExpressionAssignInt(struct ParseState *Parser, struct Value *DestValue,
DestValue->Val->Character = (char)FromInt;
break;
case TypeLong:
DestValue->Val->LongInteger = (int64_t)FromInt;
DestValue->Val->LongInteger = (long)FromInt;
break;
case TypeUnsignedInt:
DestValue->Val->UnsignedInteger = (unsigned int)FromInt;
Expand All @@ -351,7 +351,7 @@ long ExpressionAssignInt(struct ParseState *Parser, struct Value *DestValue,
DestValue->Val->UnsignedShortInteger = (unsigned short)FromInt;
break;
case TypeUnsignedLong:
DestValue->Val->UnsignedLongInteger = (uint64_t)FromInt;
DestValue->Val->UnsignedLongInteger = (unsigned long)FromInt;
break;
case TypeUnsignedChar:
DestValue->Val->UnsignedCharacter = (unsigned char)FromInt;
Expand Down Expand Up @@ -442,6 +442,9 @@ void ExpressionPushInt(struct ParseState *Parser,
{
struct Value *ValueLoc = VariableAllocValueFromType(Parser->pc, Parser,
&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;
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
}
Expand Down Expand Up @@ -519,7 +522,7 @@ void ExpressionAssign(struct ParseState *Parser, struct Value *DestValue,
DestValue->Val->Character = (char)ExpressionCoerceInteger(SourceValue);
break;
case TypeLong:
DestValue->Val->LongInteger = ExpressionCoerceInteger(SourceValue);
DestValue->Val->LongInteger = SourceValue->Val->LongInteger;
break;
case TypeUnsignedInt:
DestValue->Val->UnsignedInteger =
Expand All @@ -530,8 +533,7 @@ void ExpressionAssign(struct ParseState *Parser, struct Value *DestValue,
(unsigned short)ExpressionCoerceUnsignedInteger(SourceValue);
break;
case TypeUnsignedLong:
DestValue->Val->UnsignedLongInteger =
ExpressionCoerceUnsignedInteger(SourceValue);
DestValue->Val->UnsignedLongInteger = SourceValue->Val->UnsignedLongInteger;
break;
case TypeUnsignedChar:
DestValue->Val->UnsignedCharacter =
Expand Down Expand Up @@ -718,8 +720,12 @@ void ExpressionPrefixOperator(struct ParseState *Parser,
ExpressionPushFP(Parser, StackTop, ResultFP);
} else if (IS_NUMERIC_COERCIBLE(TopValue)) {
/* integer prefix arithmetic */
int64_t ResultInt = 0;
int64_t TopInt = ExpressionCoerceInteger(TopValue);
long ResultInt = 0;
long TopInt = 0;
if (TopValue->Typ->Base == TypeLong)
TopInt = TopValue->Val->LongInteger;
else
TopInt = ExpressionCoerceInteger(TopValue);
switch (Op) {
case TokenPlus:
ResultInt = TopInt;
Expand Down Expand Up @@ -811,8 +817,8 @@ void ExpressionPostfixOperator(struct ParseState *Parser,
}
ExpressionPushFP(Parser, StackTop, ResultFP);
} else if (IS_NUMERIC_COERCIBLE(TopValue)) {
int64_t ResultInt = 0;
int64_t TopInt = ExpressionCoerceInteger(TopValue);
long ResultInt = 0;
long TopInt = ExpressionCoerceInteger(TopValue);
switch (Op) {
case TokenIncrement:
ResultInt = ExpressionAssignInt(Parser, TopValue, TopInt+1, true);
Expand Down Expand Up @@ -991,8 +997,8 @@ void ExpressionInfixOperator(struct ParseState *Parser,
ExpressionPushFP(Parser, StackTop, ResultFP);
} else if (IS_NUMERIC_COERCIBLE(TopValue) && IS_NUMERIC_COERCIBLE(BottomValue)) {
/* integer operation */
int64_t TopInt = ExpressionCoerceInteger(TopValue);
int64_t BottomInt = ExpressionCoerceInteger(BottomValue);
long TopInt = ExpressionCoerceInteger(TopValue);
long BottomInt = ExpressionCoerceInteger(BottomValue);
switch (Op) {
case TokenAssign:
ResultInt = ExpressionAssignInt(Parser, BottomValue, TopInt, false);
Expand Down Expand Up @@ -1080,7 +1086,7 @@ void ExpressionInfixOperator(struct ParseState *Parser,
if (BottomValue->Typ->Base == TypeUnsignedInt || BottomValue->Typ->Base == TypeUnsignedLong)
ResultInt = (uint64_t) BottomInt >> TopInt;
else
ResultInt = BottomInt >> TopInt;
ResultInt = BottomInt >> TopInt;
*/
break;
case TokenShiftRight:
Expand Down
2 changes: 1 addition & 1 deletion picoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/* VER, the git hash number, and TAG are obtained via the Makefile */
#define PICOC_VERSION TAG " r" VER
#else
#define PICOC_VERSION "v2.2"
#define PICOC_VERSION "v2.3"
#endif

#include "interpreter.h"
Expand Down

0 comments on commit b8b4c48

Please sign in to comment.