view 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 source

COMPILER SimpleLanguage

CHARACTERS

letter = 'A'..'Z' + 'a'..'z'.
nonZeroDigit = "123456789".
digit = "0123456789".
cr = '\r'.
lf = '\n'.
tab = '\t'.
stringChar = ANY - "\"" - '\\' - cr - lf.

TOKENS

identifier = letter {letter | digit}.
stringLiteral = "\"" { stringChar } "\"".
numericLiteral = "0" | nonZeroDigit { digit }.

PRAGMAS

COMMENTS FROM "/*" TO "*/"
COMMENTS FROM "//" TO lf
IGNORE cr + lf + tab

PRODUCTIONS


SimpleLanguage
=
Function 
{
     Function
}
.


Function
=
"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 SLStatementNode result>
=                                               (. factory.startBlock();
                                                   List<SLStatementNode> statements = new ArrayList<>(); .)
"{" 
{
    Statement<out SLStatementNode statement>    (. statements.add(statement); .)
}
"}"                                             (. result = factory.finishBlock(statements); .)
.


Statement<out SLStatementNode result>
=                                               (. result = null; .)
(
    WhileStatement<out result>
|
    "break"                                     (. result = factory.createBreak(); .)
    ";"
|
    "continue"                                  (. result = factory.createContinue(); .)
    ";"
|   
    IfStatement<out result>
|
    ReturnStatement<out result>
|
    Expression<out result> ";"
)
.


IfStatement<out SLStatementNode result>
=
"if" 
"(" 
Expression<out SLExpressionNode condition> 
")"
Block<out SLStatementNode thenPart>             (. SLStatementNode elsePart = null; .)                             
[
    "else" 
    Block<out elsePart>
]                                               (. result = factory.createIf(condition, thenPart, elsePart); .)
.


WhileStatement<out SLStatementNode result>
=
"while"
"("
Expression<out SLExpressionNode condition>
")" 
Block<out SLStatementNode body>                 (. result = factory.createWhile(condition, body); .)
.


ReturnStatement<out SLStatementNode result>
=
"return"
Expression<out SLExpressionNode value> ";"      (. result = factory.createReturn(value); .)
.


Expression<out SLExpressionNode result>
=
LogicTerm<out result>
{
    "||"                                        (. String op = t.val; .)
    LogicTerm<out SLExpressionNode right>       (. result = factory.createBinary(op, result, right); .)
}
.


LogicTerm<out SLExpressionNode 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; .)
    Arithmetic<out SLExpressionNode right>      (.  result = factory.createBinary(op, result, right); .)
]
.


Arithmetic<out SLExpressionNode result>
=
Term<out result>
{
    ("+" | "-")                                 (. String op = t.val; .)
    Term<out SLExpressionNode right>            (. result = factory.createBinary(op, result, right); .)
}
.


Term<out SLExpressionNode result>
=
Factor<out result>
{
    ("*" | "/")                                 (. String op = t.val; .)
    Factor<out SLExpressionNode right>          (. result = factory.createBinary(op, result, right); .)
}
.


Factor<out SLExpressionNode result>
=                                               (. result = null; .)
(
    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); .)
    )
|
    stringLiteral                               (. result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1)); .)
|
    numericLiteral                              (. result = factory.createNumericLiteral(t.val); .)
|
    "(" Expression<out result> ")"
) 
.


END SimpleLanguage.