Mercurial > hg > truffle
view graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SimpleLanguage.atg @ 13821:b16ec83edc73
Documentation and more refactoring of Simple Language
author | Christian Wimmer <christian.wimmer@oracle.com> |
---|---|
date | Wed, 29 Jan 2014 20:45:43 -0800 |
parents | 7c418666c6c9 |
children | 64c77f0577bb |
line wrap: on
line source
/* * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ 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 (. factory.startFunction(t); .) "(" [ identifier (. factory.addFormalParameter(t); .) { "," identifier (. factory.addFormalParameter(t); .) } ] ")" Block<out SLStatementNode body> (. factory.finishFunction(body); .) . Block<out SLStatementNode result> = (. factory.startBlock(); List<SLStatementNode> body = new ArrayList<>(); .) "{" { Statement<out SLStatementNode s> (. body.add(s); .) } "}" (. result = factory.finishBlock(body); .) . Statement<out SLStatementNode result> = (. result = null; .) ( WhileStatement<out result> | "break" (. result = factory.createBreak(t); .) ";" | "continue" (. result = factory.createContinue(t); .) ";" | IfStatement<out result> | ReturnStatement<out result> | Expression<out result> ";" ) . WhileStatement<out SLStatementNode result> = "while" "(" (. Token whileToken = t; .) Expression<out SLExpressionNode condition> ")" Block<out SLStatementNode body> (. result = factory.createWhile(whileToken, condition, body); .) . IfStatement<out SLStatementNode result> = "if" "(" (. Token ifToken = t; .) Expression<out SLExpressionNode condition> ")" Block<out SLStatementNode thenPart> (. SLStatementNode elsePart = null; .) [ "else" Block<out elsePart> ] (. result = factory.createIf(ifToken, condition, thenPart, elsePart); .) . ReturnStatement<out SLStatementNode result> = "return" (. Token returnToken = t; SLExpressionNode value = null; .) [ Expression<out value> ] (. result = factory.createReturn(returnToken, value); .) ";" . Expression<out SLExpressionNode result> = LogicTerm<out result> { "||" (. Token op = t; .) LogicTerm<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) } . LogicTerm<out SLExpressionNode result> = LogicFactor<out result> { "&&" (. Token op = t; .) LogicFactor<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) } . LogicFactor<out SLExpressionNode result> = Arithmetic<out result> [ ("<" | "<=" | ">" | ">=" | "==" | "!=" ) (. Token op = t; .) Arithmetic<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) ] . Arithmetic<out SLExpressionNode result> = Term<out result> { ("+" | "-") (. Token op = t; .) Term<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) } . Term<out SLExpressionNode result> = Factor<out result> { ("*" | "/") (. Token op = t; .) Factor<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) } . Factor<out SLExpressionNode result> = (. result = null; .) ( identifier (. Token nameToken = t; .) ( "(" (. List<SLExpressionNode> parameters = new ArrayList<>(); SLExpressionNode parameter; .) [ Expression<out parameter> (. parameters.add(parameter); .) { "," Expression<out parameter> (. parameters.add(parameter); .) } ] (. result = factory.createCall(nameToken, parameters); .) ")" | "=" Expression<out SLExpressionNode value> (. result = factory.createAssignment(nameToken, value); .) | (. result = factory.createRead(nameToken); .) ) | stringLiteral (. result = factory.createStringLiteral(t); .) | numericLiteral (. result = factory.createNumericLiteral(t); .) | "(" Expression<out result> ")" ) . END SimpleLanguage.