From b2c4d6ce83adcb35647038374a92caa180165e91 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Mon, 14 Feb 2011 06:05:53 +0000 Subject: [PATCH] Fixed bug108 - brackets were not collapsing the expression stack as they should have. Now more permissive in declarations of void main() git-svn-id: http://picoc.googlecode.com/svn/trunk@520 21eae674-98b7-11dd-bd71-f92a316d2d60 --- expression.c | 13 +++---------- parse.c | 6 ++++-- picoc.c | 14 +++++++------- tests/32_led.c | 4 +++- tests/49_bracket_evaluation.c | 21 +++++++++++++++++++++ tests/49_bracket_evaluation.expect | 1 + tests/Makefile | 3 ++- 7 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 tests/49_bracket_evaluation.c create mode 100644 tests/49_bracket_evaluation.expect diff --git a/expression.c b/expression.c index 7faaaa3..4041c9b 100644 --- a/expression.c +++ b/expression.c @@ -475,16 +475,6 @@ void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack ExpressionPushInt(Parser, StackTop, TypeSize(TopValue->Typ, TopValue->Typ->ArraySize, TRUE)); break; - case TokenLeftSquareBracket: - /* XXX */ - ProgramFail(Parser, "not supported"); - break; - - case TokenOpenBracket: - /* XXX - cast */ - ProgramFail(Parser, "not supported"); - break; - default: /* an arithmetic operator */ #ifndef NO_FP @@ -1065,6 +1055,9 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result) } else BracketPrecedence -= BRACKET_PRECEDENCE; + + /* collapse to the bracket precedence */ + ExpressionStackCollapse(Parser, &StackTop, BracketPrecedence); break; default: diff --git a/parse.c b/parse.c index ba9ec93..6180434 100644 --- a/parse.c +++ b/parse.c @@ -130,8 +130,9 @@ struct Value *ParseFunctionDefinition(struct ParseState *Parser, struct ValueTyp if (strcmp(Identifier, "main") == 0) { /* make sure it's int main() */ - if (FuncValue->Val->FuncDef.ReturnType != &IntType) - ProgramFail(Parser, "main() should return an int"); + if ( FuncValue->Val->FuncDef.ReturnType != &IntType && + FuncValue->Val->FuncDef.ReturnType != &VoidType ) + ProgramFail(Parser, "main() should return an int or void"); if (FuncValue->Val->FuncDef.NumParams != 0 && (FuncValue->Val->FuncDef.NumParams != 2 || FuncValue->Val->FuncDef.ParamType[0] != &IntType) ) @@ -498,6 +499,7 @@ enum ParseResult ParseStatement(struct ParseState *Parser, int CheckTrailingSemi case TokenAmpersand: case TokenIncrement: case TokenDecrement: + case TokenOpenBracket: *Parser = PreState; ExpressionParse(Parser, &CValue); if (Parser->Mode == RunModeRun) diff --git a/picoc.c b/picoc.c index 081f14d..e13b952 100644 --- a/picoc.c +++ b/picoc.c @@ -133,16 +133,16 @@ int picoc(char *SourceStr) Initialise(HEAP_SIZE); - sl = strlen(SourceStr); - if (SourceStr) - { + sl = strlen(SourceStr); + if (SourceStr) + { for (i = 0; i < sl; i++) - { + { if (SourceStr[i] == 0x1A) - { + { SourceStr[i] = 0x20; - } - } + } + } } ExitBuf[40] = 0; PlatformSetExitPoint(); diff --git a/tests/32_led.c b/tests/32_led.c index 46b4854..d1a28b6 100644 --- a/tests/32_led.c +++ b/tests/32_led.c @@ -221,11 +221,13 @@ void print_led(unsigned long x, char *buf) *buf='\0'; } -void main() +int main() { char buf[5*MAX_DIGITS]; print_led(1234567, buf); printf("%s\n",buf); + + return 0; } #ifndef NO_MAIN diff --git a/tests/49_bracket_evaluation.c b/tests/49_bracket_evaluation.c new file mode 100644 index 0000000..b4303c6 --- /dev/null +++ b/tests/49_bracket_evaluation.c @@ -0,0 +1,21 @@ +#include + +struct point +{ + double x; + double y; +}; + +struct point point_array[100]; + +int main() +{ + int my_point = 10; + + point_array[my_point].x = 12.34; + point_array[my_point].y = 56.78; + + printf("%f, %f\n", point_array[my_point].x, point_array[my_point].y); + + return 0; +} diff --git a/tests/49_bracket_evaluation.expect b/tests/49_bracket_evaluation.expect new file mode 100644 index 0000000..1da66db --- /dev/null +++ b/tests/49_bracket_evaluation.expect @@ -0,0 +1 @@ +12.340000, 56.780000 diff --git a/tests/Makefile b/tests/Makefile index 4a24acd..a80a813 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -43,7 +43,8 @@ TESTS= 00_assignment.test \ 44_scoped_declarations.test \ 45_empty_for.test \ 47_switch_return.test \ - 48_nested_break.test + 48_nested_break.test \ + 49_bracket_evaluation.test %.test: %.expect %.c @echo Test: $*...