Browse Source

Added syntactical sugar for functions

Greg 15 years ago
parent
commit
135e3997b4
3 changed files with 55 additions and 0 deletions
  1. 4 0
      gmsrc/doc/ChangeLog.txt
  2. 1 0
      gmsrc/src/gm/gmConfig.h
  3. 50 0
      gmsrc/src/gm/gmParser.y

+ 4 - 0
gmsrc/doc/ChangeLog.txt

@@ -365,3 +365,7 @@ o Improved Minimal example.  Not all errors were logged. Also enabled debug mode
 09/07/10
 o Updated code base for 64bit address target. (64bit builds of current binaries not made or tested yet.) 
 o Updated system lib for 64bit Win OS compatibility.
+
+16/08/10
+o Added syntactical sugar for functions.  'function <name>(<parms>){<statements>}' Thanks 39ster.
+

+ 1 - 0
gmsrc/src/gm/gmConfig.h

@@ -58,6 +58,7 @@ enum gmEndian
 
 // MACHINE
 
+#define GMMACHINE_DEFAULT_FUNCTION  CTVT_LOCAL // CTVT_LOCAL, CTVT_GLOBAL or CTVT_MEMBER for default scope of function using sugar syntax
 #define GMMACHINE_REMOVECOMPILER    0         // Remove compiler code, will only be able to run precompiled libs
 #define GMMACHINE_GMCHECKDIVBYZERO  0         // Let GM operator check for divide by zero and possibly cause GM run time exception (rather than OS exception)
 #define GMMACHINE_NULL_VAR_CTOR     0         // Nullify gmVariable in constructor.  Not recommended for real-time / time critical applications.

+ 50 - 0
gmsrc/src/gm/gmParser.y

@@ -158,6 +158,10 @@ statement
     {
       $$ = $1;
     }
+  | function_statement
+    {
+	  $$ = $1;
+	}
   ;
 
 compound_statement
@@ -172,6 +176,33 @@ compound_statement
     }
   ;
 
+  
+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));
+	}
+  ;
+  
 var_statement
   : var_type identifier ';'
     {
@@ -184,6 +215,25 @@ var_statement
       $$->SetChild(0, $2);
       ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $2, $4));
     }
+  | var_type KEYWORD_FUNCTION identifier '(' ')' compound_statement
+    {
+      gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
+      func->SetChild(1, $6);
+
+      $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_VARIABLE, gmlineno, (int) $1);
+      $$->SetChild(0, $3);
+      ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $3, func));
+    }
+  | var_type KEYWORD_FUNCTION identifier '(' parameter_list ')' compound_statement
+    {
+      gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
+      func->SetChild(0, $5);
+      func->SetChild(1, $7);
+
+      $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_VARIABLE, gmlineno, (int) $1);
+      $$->SetChild(0, $3);
+      ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $3, func));
+    }
   ;
 
 var_type