diff graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SimpleLanguage.atg @ 13761:7c418666c6c9

Refactoring and cleanup of Simple Language (more to come soon)
author Christian Wimmer <christian.wimmer@oracle.com>
date Fri, 24 Jan 2014 18:16:24 -0800
parents 71991b7a0f14
children b16ec83edc73
line wrap: on
line diff
--- a/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SimpleLanguage.atg	Fri Jan 24 18:13:38 2014 -0800
+++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SimpleLanguage.atg	Fri Jan 24 18:16:24 2014 -0800
@@ -24,6 +24,7 @@
 
 PRODUCTIONS
 
+
 SimpleLanguage
 =
 Function 
@@ -32,30 +33,46 @@
 }
 .
 
+
 Function
 =
-"function"                                      (. factory.startFunction(); .)
-identifier                                      (. String name = t.val; 
-                                                   List<String> parameterNames = new ArrayList<>(); .)
-["(" [identifier                                (. parameterNames.add(t.val); .)
-] {"," identifier                               (. parameterNames.add(t.val); .)
-} ")"]
-Block<out StatementNode body>                   (. factory.createFunction(body, name, parameterNames.toArray(new String[parameterNames.size()])); .)
+"function"                                      
+identifier                                      (. String name = t.val; .)
+"("                                             (. List<String> parameters = new ArrayList<>(); .)
+[
+    identifier                                  (. parameters.add(t.val); .)
+    {
+        "," 
+        identifier                              (. parameters.add(t.val); .)
+    }    
+]
+")"                                             (. factory.startFunction(name, parameters); .)
+Block<out SLStatementNode body>                 (. factory.finishFunction(body); .)
 .
 
-Block<out StatementNode result>
-=                                               (. List<StatementNode> statements = new ArrayList<>(); .)
+
+
+Block<out SLStatementNode result>
+=                                               (. factory.startBlock();
+                                                   List<SLStatementNode> statements = new ArrayList<>(); .)
 "{" 
 {
-    Statement<out StatementNode statement>      (. statements.add(statement); .)
+    Statement<out SLStatementNode statement>    (. statements.add(statement); .)
 }
-"}"                                             (. result = factory.createBlock(statements); .)
+"}"                                             (. result = factory.finishBlock(statements); .)
 .
 
-Statement<out StatementNode result>
+
+Statement<out SLStatementNode result>
 =                                               (. result = null; .)
 (
     WhileStatement<out result>
+|
+    "break"                                     (. result = factory.createBreak(); .)
+    ";"
+|
+    "continue"                                  (. result = factory.createContinue(); .)
+    ";"
 |   
     IfStatement<out result>
 |
@@ -65,109 +82,117 @@
 )
 .
 
-IfStatement<out StatementNode result>
+
+IfStatement<out SLStatementNode result>
 =
-"if" "(" Expression<out ConditionNode condition> ")"  (. StatementNode thenNode = null; StatementNode elseNode = null; .)
-Block<out thenNode>                             
-["else" Block<out elseNode>]                    (. result = factory.createIf(condition, thenNode, elseNode); .)
+"if" 
+"(" 
+Expression<out SLExpressionNode condition> 
+")"
+Block<out SLStatementNode thenPart>             (. SLStatementNode elsePart = null; .)                             
+[
+    "else" 
+    Block<out elsePart>
+]                                               (. result = factory.createIf(condition, thenPart, elsePart); .)
 .
 
-WhileStatement<out StatementNode result>
+
+WhileStatement<out SLStatementNode result>
 =
 "while"
 "("
-Expression<out ConditionNode condition>
+Expression<out SLExpressionNode condition>
 ")" 
-Block<out StatementNode body>                   (. result = factory.createWhile(condition, body); .)
+Block<out SLStatementNode body>                 (. result = factory.createWhile(condition, body); .)
+.
+
+
+ReturnStatement<out SLStatementNode result>
+=
+"return"
+Expression<out SLExpressionNode value> ";"      (. result = factory.createReturn(value); .)
 .
 
 
-ReturnStatement<out StatementNode result>
+Expression<out SLExpressionNode result>
 =
-"return"
-Expression<out TypedNode value> ";"             (. result = factory.createReturn(value); .)
+LogicTerm<out result>
+{
+    "||"                                        (. String op = t.val; .)
+    LogicTerm<out SLExpressionNode right>       (. result = factory.createBinary(op, result, right); .)
+}
 .
 
-Expression<out TypedNode result>
+
+LogicTerm<out SLExpressionNode result>
 =
-ValueExpression<out result>
+LogicFactor<out result>
+{
+    "&&"                                        (. String op = t.val; .)
+    LogicFactor<out SLExpressionNode right>     (. result = factory.createBinary(op, result, right); .)
+}
+.
+
+
+LogicFactor<out SLExpressionNode result>
+=
+Arithmetic<out result>
 [
-    ("<" | ">" | "<=" | ">=" | "==" | "!=" )    (.  String op = t.val; .)
-    ValueExpression<out TypedNode right>        (.  result = factory.createBinary(op, result, right); .)
+    ("<" | "<=" | "==" | "!=" )    (.  String op = t.val; .)
+    Arithmetic<out SLExpressionNode right>      (.  result = factory.createBinary(op, result, right); .)
 ]
 .
 
-ValueExpression<out TypedNode result>
+
+Arithmetic<out SLExpressionNode result>
 =
 Term<out result>
 {
     ("+" | "-")                                 (. String op = t.val; .)
-    Term<out TypedNode right>                   (. result = factory.createBinary(op, result, right); .)
+    Term<out SLExpressionNode right>            (. result = factory.createBinary(op, result, right); .)
 }
 .
 
-Term<out TypedNode result>
+
+Term<out SLExpressionNode result>
 =
 Factor<out result>
 {
     ("*" | "/")                                 (. String op = t.val; .)
-    Factor<out TypedNode right>                 (. result = factory.createBinary(op, result, right); .)
+    Factor<out SLExpressionNode right>          (. result = factory.createBinary(op, result, right); .)
 }
 .
 
-Factor<out TypedNode result>
+
+Factor<out SLExpressionNode result>
 =                                               (. result = null; .)
 (
-    VariableRefOrCall<out result>
-|
-    StringLiteral<out result>
+    identifier                                  (. String name = t.val; .)
+    (
+        "("                                     (. List<SLExpressionNode> parameters = new ArrayList<>();
+                                                   SLExpressionNode parameter; .)
+        [
+            Expression<out parameter>           (. parameters.add(parameter); .)
+            {
+                "," 
+                Expression<out parameter>       (. parameters.add(parameter); .)
+            }                                               
+        ]                                       (. result = factory.createCall(factory.createRead(name), parameters); .) 
+        ")"
+    |
+        "=" 
+        Expression<out SLExpressionNode value>  (. result = factory.createAssignment(name, value); .)
+    |
+                                                (. result = factory.createRead(name); .)
+    )
 |
-    NumericLiteral<out result>
+    stringLiteral                               (. result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1)); .)
 |
-    Ternary<out result>
+    numericLiteral                              (. result = factory.createNumericLiteral(t.val); .)
 |
     "(" Expression<out result> ")"
 ) 
 .
 
-Ternary<out TypedNode result>                   (. TypedNode condition, thenPart, elsePart; .)
-=
-"#" Expression<out condition> "?" Expression<out thenPart> ":" Expression<out elsePart>
-                                                (. result = factory.createTernary(condition, thenPart, elsePart); .)
-.
-
-VariableRefOrCall<out TypedNode result> 
-=
-VariableRef<out result>                  
-[
-  (Parameters<out TypedNode[] parameters>)      (. result = factory.createCall(result, parameters); .)
-| ("=" Expression<out TypedNode assignment>)    (. result = factory.createAssignment(result, assignment); .)
-]
-.
-
-Parameters<out TypedNode[] result> 
-=      
-"("                                         (. List<TypedNode> parameters = new ArrayList<>(); .)
-[Expression<out TypedNode e1>                   (. parameters.add(e1); .)
-{"," Expression<out TypedNode e2>               (. parameters.add(e2); .)
-}                                               
-]                                           (. result = parameters.toArray(new TypedNode[parameters.size()]); .) 
-")"
-.
-
-VariableRef<out TypedNode result>
-=
-identifier                                      (. result = factory.createLocal(t.val); .)
-.
-
-StringLiteral<out TypedNode result>
-=
-stringLiteral                                   (. result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1)); .)
-.
-
-NumericLiteral<out TypedNode result>
-=
-numericLiteral                                  (. result = factory.createNumericLiteral(t.val); .)
-.
 
 END SimpleLanguage.