From d5811d37d553a0e6fb33978b43109f6c2c817758 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Thu, 22 Oct 2009 20:08:43 +0000 Subject: [PATCH] printf() now supports field widths, zero-padding and left/right justification. git-svn-id: http://picoc.googlecode.com/svn/trunk@342 21eae674-98b7-11dd-bd71-f92a316d2d60 --- expression.c | 24 ++++++++++++++++++++++-- picoc.h | 2 +- platform.c | 2 +- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/expression.c b/expression.c index 9074951..3b3b3c2 100644 --- a/expression.c +++ b/expression.c @@ -388,8 +388,11 @@ void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack break; case TokenSizeof: - /* XXX */ - ProgramFail(Parser, "not supported"); + /* return the size of the argument */ + if (TopValue->Typ == &TypeType) + ExpressionPushInt(Parser, StackTop, TypeSize(TopValue->Val->Typ, TopValue->Val->Typ->ArraySize, TRUE)); + else + ExpressionPushInt(Parser, StackTop, TypeSize(TopValue->Typ, TopValue->Typ->ArraySize, TRUE)); break; case TokenLeftSquareBracket: @@ -1112,6 +1115,23 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result) PrefixState = FALSE; ExpressionStackPushValue(Parser, &StackTop, LexValue); } + else if (IS_TYPE_TOKEN(Token)) + { + /* it's a type. push it on the stack like a value. this is used in sizeof() */ + struct ValueType *Typ; + char *Identifier; + struct Value *TypeValue; + + if (!PrefixState) + ProgramFail(Parser, "type not expected here"); + + PrefixState = FALSE; + *Parser = PreState; + TypeParse(Parser, &Typ, &Identifier); + TypeValue = VariableAllocValueFromType(Parser, &TypeType, FALSE, NULL, FALSE); + TypeValue->Val->Typ = Typ; + ExpressionStackPushValueNode(Parser, &StackTop, TypeValue); + } else { /* it isn't a token from an expression */ diff --git a/picoc.h b/picoc.h index 1e4b6b0..f698fe1 100644 --- a/picoc.h +++ b/picoc.h @@ -401,7 +401,7 @@ void *VariableDereferencePointer(struct ParseState *Parser, struct Value *Pointe void LibraryInit(struct Table *GlobalTable, const char *LibraryName, struct LibraryFunction (*FuncList)[]); void CLibraryInit(); void PrintCh(char OutCh, struct OutputStream *Stream); -void PrintInt(int Num, struct OutputStream *Stream); +void PrintInt(int Num, int FieldWidth, int ZeroPad, int LeftJustify, struct OutputStream *Stream); void PrintStr(const char *Str, struct OutputStream *Stream); void PrintFP(double Num, struct OutputStream *Stream); void PrintType(struct ValueType *Typ, struct OutputStream *Stream); diff --git a/platform.c b/platform.c index 301a7ae..4b88cc7 100644 --- a/platform.c +++ b/platform.c @@ -124,7 +124,7 @@ void PlatformVPrintf(const char *Format, va_list Args) switch (*FPos) { case 's': PrintStr(va_arg(Args, char *), &CStdOut); break; - case 'd': PrintInt(va_arg(Args, int), &CStdOut); break; + case 'd': PrintInt(va_arg(Args, int), 0, FALSE, FALSE, &CStdOut); break; case 'c': PrintCh(va_arg(Args, int), &CStdOut); break; case 't': PrintType(va_arg(Args, struct ValueType *), &CStdOut); break; #ifndef NO_FP