view graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SimpleLanguage.atg @ 12752:71991b7a0f14

SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
author Christian Humer <christian.humer@gmail.com>
date Mon, 11 Nov 2013 21:34:44 +0100
parents b6a87711eca0
children 7c418666c6c9
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"                                      (. 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()])); .)
.

Block<out StatementNode result>
=                                               (. List<StatementNode> statements = new ArrayList<>(); .)
"{" 
{
    Statement<out StatementNode statement>      (. statements.add(statement); .)
}
"}"                                             (. result = factory.createBlock(statements); .)
.

Statement<out StatementNode result>
=                                               (. result = null; .)
(
    WhileStatement<out result>
|   
    IfStatement<out result>
|
    ReturnStatement<out result>
|
    Expression<out result> ";"
)
.

IfStatement<out StatementNode 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); .)
.

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


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

Expression<out TypedNode result>
=
ValueExpression<out result>
[
    ("<" | ">" | "<=" | ">=" | "==" | "!=" )    (.  String op = t.val; .)
    ValueExpression<out TypedNode right>        (.  result = factory.createBinary(op, result, right); .)
]
.

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

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

Factor<out TypedNode result>
=                                               (. result = null; .)
(
    VariableRefOrCall<out result>
|
    StringLiteral<out result>
|
    NumericLiteral<out result>
|
    Ternary<out result>
|
    "(" 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.