GameMonkey Script

GameMonkey Script Forums
It is currently Sun Jan 20, 2019 10:58 pm

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Mon Aug 16, 2010 9:31 am 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
I made this for a project that is using GM to emulate a similar language and thought i'd also post it here since some people seem to think that not having this is a downfall of GM.

Code:
//Standard way still works
global lolwat = function(a, b)
{
  return a * b;
};

//Global function
global function DoThat(a, b)
{
  return lolwat(a, b);
}

//Defaults to local.
//Open gmConfig.h and change "#define GMMACHINE_DEFAULT_FUNCTION" to
//CTVT_LOCAL or CTVT_GLOBAL or CTVT_MEMBER
function DoThis(a, b)
{
  return DoThat(a, b);
}


print(DoThis(4, 4));


Built with gm 1.25. I couldn't get 1.27 to compile....?? (with or without this change)

I havn't tested it too much though.


Attachments:
gmFunctionThingy.zip [35.6 KiB]
Downloaded 231 times
Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 16, 2010 10:17 am 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
Not sure why but i included gmScanner.* stuff. You don't need to replace those. Just gmParser.* and gmConfig.h


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 16, 2010 11:16 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
Thanks 39ster, I like it! This should have been in from day one (in fact it was on the original paper spec under 'syntax sugar'). I've integrated your changes into my 1.27.2 build. Seems to work well for me. Let us know if there are any known or potential issues.


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 16, 2010 3:34 pm 
Offline

Joined: Fri Jan 14, 2005 2:28 am
Posts: 439
What exactly is the change in behavior?


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 16, 2010 3:49 pm 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
DrEvil-OB wrote:
What exactly is the change in behavior?


I'm hoping none.

It's just a small mod to gmParser.y. It just declares a variable using the function name and assigns the function to that variable in the same way "local MyFunction = function(){};" would.
I know it sort of breaks the logic of the scripting language, but as Greg said, it's syntax sugar. I needed to do it to emulate another scripting language and thought i'd share it ;)


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 16, 2010 10:33 pm 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
The syntax is currently limited to single names in the main scope.
For example, this works with standard syntax:
Code:
myTable = {};
myTable.myFunc = function(a)
{
  return a;
};

But not this:
Code:
myTable = {};
function myTable.myFunc(a)
{
  return a;
}


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 17, 2010 5:49 am 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
Yea that's true. Not too sure how to do it though.


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 12, 2012 11:29 am 
Offline

Joined: Wed Jan 11, 2012 10:10 pm
Posts: 3
Here's a quick fix I made for that:
Code:
function_statement
  :
    KEYWORD_FUNCTION identifier '(' ')' compound_statement
    {
      gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
      func->SetChild(1, $5);
      

      $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_VARIABLE, gmlineno, (int)GMMACHINE_DEFAULT_FUNCTION);
      $$->SetChild(0, $2);
      ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $2, func));
   }
  |
    KEYWORD_FUNCTION identifier '(' parameter_list ')' compound_statement
    {
      gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
      func->SetChild(0, $4);
      func->SetChild(1, $6);
      

      $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_VARIABLE, gmlineno, (int)GMMACHINE_DEFAULT_FUNCTION);
      $$->SetChild(0, $2);
      ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $2, func));
   }
  |
    KEYWORD_FUNCTION tablemember_expression '(' ')' compound_statement
    {
      gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
      func->SetChild(1, $5);
      
      ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $2, func));
   }
  |
    KEYWORD_FUNCTION tablemember_expression '(' parameter_list ')' compound_statement
    {
      gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
      func->SetChild(0, $4);
      func->SetChild(1, $6);
      
      ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $2, func));
   }
  ;
 
tablemember_expression
  : identifier '.' identifier
    {
      $$ = CreateOperation(CTNOT_DOT, $1, $3);
    }
  | tablemember_expression '.' identifier
    {
      $$ = CreateOperation(CTNOT_DOT, $1, $3);
    }
  ;
 


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 13, 2012 6:47 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
HenryTran wrote:
Here's a quick fix I made for that: ...

That seems to work well HenryTran. Thanks for sharing and welcome to the forum :)


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 06, 2012 12:57 pm 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
HenryTran wrote:
Here's a quick fix I made for that:
Code:
function_statement
  :
    KEYWORD_FUNCTION identifier '(' ')' compound_statement
    {
      gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
      func->SetChild(1, $5);
      

      $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_VARIABLE, gmlineno, (int)GMMACHINE_DEFAULT_FUNCTION);
      $$->SetChild(0, $2);
      ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $2, func));
   }
  |
    KEYWORD_FUNCTION identifier '(' parameter_list ')' compound_statement
    {
      gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
      func->SetChild(0, $4);
      func->SetChild(1, $6);
      

      $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_VARIABLE, gmlineno, (int)GMMACHINE_DEFAULT_FUNCTION);
      $$->SetChild(0, $2);
      ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $2, func));
   }
  |
    KEYWORD_FUNCTION tablemember_expression '(' ')' compound_statement
    {
      gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
      func->SetChild(1, $5);
      
      ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $2, func));
   }
  |
    KEYWORD_FUNCTION tablemember_expression '(' parameter_list ')' compound_statement
    {
      gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
      func->SetChild(0, $4);
      func->SetChild(1, $6);
      
      ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $2, func));
   }
  ;
 
tablemember_expression
  : identifier '.' identifier
    {
      $$ = CreateOperation(CTNOT_DOT, $1, $3);
    }
  | tablemember_expression '.' identifier
    {
      $$ = CreateOperation(CTNOT_DOT, $1, $3);
    }
  ;
 

Nice. So GM fully supports this syntax now?


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 06, 2012 11:56 pm 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
39ster wrote:
Nice. So GM fully supports this syntax now?

I'm including it in my next build.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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