Skip to content

Instantly share code, notes, and snippets.

@heat
Created May 18, 2012 17:18
Show Gist options
  • Save heat/2726513 to your computer and use it in GitHub Desktop.
Save heat/2726513 to your computer and use it in GitHub Desktop.
Korg Language
/**
versao aceita definicao de variaveis;
- atribuicao a variavel
definiçoes de funcoes
- corpo da funcao
- retorno da funcao
expressoes literais
- operadores
- parenteses de operacao
- podem ser funcao
Comparacao
- comparadores
- concatenacoa de comparacao
- comparacao em expressao
*/
PARSER_BEGIN(NewKorg)
public class NewKorg {
/** Main entry point. */
public static void main(String args[]) throws ParseException {
try {
NewKorg parser = new NewKorg(System.in);
parser.Input();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
PARSER_END(NewKorg)
SKIP :
{
" "
| "\t"
| "\n"
| "\r"
}
TOKEN : /* SEPARATORS */
{
< LPAREN: "(" >
| < RPAREN: ")" >
| < LBRACE: "{" >
| < RBRACE: "}" >
| < LBRACKET: "[" >
| < RBRACKET: "]" >
| < SEMICOLON: ";" >
| < DOUBLEDOT: ":" >
| <ATT: "=">
| <COMP: ("eq"|"ne"|"gt"|"lt")>
| <CATCOMP: ("or"|"and")>
}
TOKEN :
{
<VAR: "var" >
| <DEF: "def" >
| <DIF: "if">
| <SET: "set" >
| <DELSE: "else">
| <RETURN: "return">
| <ID: (["a"-"z"])+ >
| <NUM:(["0"-"9"])+("."(["0"-"9"])+)?>
| <OPERATOR: ["*","-","+","/"]>
}
/** Root production. */
void Input() :
{}
{
(DefVar())*
(Functions())*
(Statement())*<EOF>
}
void Statement() :
{}
{
<SET> <ID> <ATT> Expression() <SEMICOLON>
| IfThenStatement()
| Expression()<SEMICOLON>
}
void IfThenStatement() :
{}
{
<DIF> "(" BooleanExpression() ")"<LBRACE>(Statement())*<RBRACE>(<DELSE><LBRACE>(Statement())*<RBRACE>)?
}
void BooleanExpression():
{}
{
Expression() <COMP> Expression() (<CATCOMP> BooleanExpression())*
}
void Expression() :
{}
{
"("BooleanExpression()")""?" Expression() "|" Expression()
|Term() (<OPERATOR> Term())*
}
void Term() :
{}
{
<ID> ("(" (Expression())* ")")?
| <NUM>
| "(" Expression() ")"
}
void Functions() :
{}
{
<DEF> <ID> <LPAREN> (Parameters())? <RPAREN><LBRACE>FunctionBody()<RBRACE>
}
void FunctionBody() :
{}
{
(DefVar())* (Statement())* <RETURN> Expression() <SEMICOLON>
}
void Parameters() :
{}
{
<ID> ("," <ID>)*
}
void DefVar():
{}
{
<VAR> <ID> (<ATT> Expression())? <SEMICOLON>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment