GameMonkey Script

GameMonkey Script Forums
It is currently Tue Mar 19, 2019 7:45 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Jan 21, 2009 3:38 am 
Offline

Joined: Tue Nov 11, 2008 9:03 am
Posts: 20
Hello,
I was wondering if anything in the language or grammar prevent from allowing single statement not surrounded by curly braces?
eg:
Code:
if ( foobar )
    print("Hello, world!\n");

My coding style usually rely on early return/break/continue to avoid opening recursives blocks of code, so I have a lot of those one-line statement. I know I can stick in {} on the same line but I somehow like it less and IDE formatter doesn't help. Anyway, I was wondering if there was a specific reason behind this choice.
Thanks.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 21, 2009 8:52 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
The current grammar does not support that syntax. I believe the decision was originally made based on preference and the desire to promote good programming practice. Noble as that may be, I regret it and would like to implement as much of the common C syntax as is appropriate, allowing developers to make their own decisions on coding standards for better or worse.

I believe the grammar could be modified to allow that syntax. Other statements like while and for should be modified as well.


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 24, 2010 2:49 am 
Offline

Joined: Tue Nov 11, 2008 9:03 am
Posts: 20
Hello,

I made an experimental patch to the grammar today.
DISCLAIMER: I don't think this patch is right for everyone (also I don't know much about lex/yacc so I may be taking this from the wrong angle).

There was conflict due to the {} style table-constructor which is listed as possible primary_expression. I moved it as constant_expression and added special cases for = and ?= assign operators. This change has the net effect of breaking statement like:
Code:
{x=10}.x

Which I believe aren't really useful so I am happy with the trade-off. This being changed it's now possible to allow for single statement in if/while/foreach blocks.

I couldn't add it to if/else blocks (more conflict, which I may or not investigate later).
Can you think of drawbacks or possible side-effects? (or maybe a safer/better way to do that change)

O.

Code:
179a180,190
> compound_or_single_statement
>   : compound_statement
>     {
>       $$ = $1;
>     }
>   | statement
>     {
>       $$ = $1;
>     }
>   ;
>
226c237
<   : KEYWORD_IF '(' constant_expression ')' compound_statement
---
>   : KEYWORD_IF '(' constant_expression ')' compound_or_single_statement
298c309
<   : KEYWORD_WHILE '(' constant_expression ')' compound_statement
---
>   : KEYWORD_WHILE '(' constant_expression ')' compound_or_single_statement
304c315
<   | KEYWORD_DOWHILE '(' constant_expression ')' compound_statement
---
>   | KEYWORD_DOWHILE '(' constant_expression ')' compound_or_single_statement
310c321
<   | KEYWORD_FOR '(' expression_statement constant_expression_statement ')' compound_statement
---
>   | KEYWORD_FOR '(' expression_statement constant_expression_statement ')' compound_or_single_statement
317c328
<   | KEYWORD_FOR '(' expression_statement constant_expression_statement assignment_expression ')' compound_statement
---
>   | KEYWORD_FOR '(' expression_statement constant_expression_statement assignment_expression ')' compound_or_single_statement
325c336
<   | KEYWORD_FOREACH '(' identifier KEYWORD_WITHIN argument_expression_list  ')' compound_statement
---
>   | KEYWORD_FOREACH '(' identifier KEYWORD_WITHIN argument_expression_list  ')' compound_or_single_statement
332c343
<   | KEYWORD_FOREACH '(' identifier KEYWORD_IN constant_expression ')' compound_statement
---
>   | KEYWORD_FOREACH '(' identifier KEYWORD_IN constant_expression ')' compound_or_single_statement
339c350
<   | KEYWORD_FOREACH '(' identifier KEYWORD_AND identifier KEYWORD_IN constant_expression')' compound_statement
---
>   | KEYWORD_FOREACH '(' identifier KEYWORD_AND identifier KEYWORD_IN constant_expression')' compound_or_single_statement
385a397,404
>   | postfix_expression '=' table_constructor
>     {
>       $$ = CreateOperation(CTNOT_ASSIGN, $1, $3);
>     }
>   | postfix_expression SYMBOL_ASGN_IFNULL table_constructor
>     {
>       $$ = CreateOperation(CTNOT_ASSIGN_IFNULL, $1, $3);
>     }
444a464,467
>   | table_constructor
>     {
>       $$ = $1;
>     }
809,812d831
<     {
<       $$ = $1;
<     }
<   | table_constructor


Attachments:
single_statement_20100824.zip [603 Bytes]
Downloaded 287 times
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group