Skip to content

Instantly share code, notes, and snippets.

@lrlucena
Last active December 6, 2016 21:51
Show Gist options
  • Save lrlucena/f3cf6049cfab3ff5ed9e2eae0ab7fee0 to your computer and use it in GitHub Desktop.
Save lrlucena/f3cf6049cfab3ff5ed9e2eae0ab7fee0 to your computer and use it in GitHub Desktop.
package whilelang
import org.antlr.v4.runtime.ParserRuleContext
import org.antlr.v4.runtime.tree._
import whilelang.{ WhilelangParser => C }
import plp.enquanto.linguagem.Linguagem._
trait Antlr2Scala {
protected val values = new ParseTreeProperty[Any]
protected implicit class rule2scala(rule: ParserRuleContext) {
def apply(i: Int) = rule.getChild(i)
def value[T]: T = values.get(rule).asInstanceOf[T]
def value_=(v: Any) = values.put(rule, v)
}
protected implicit class tree2scala(tree: ParseTree) {
def text = tree.getText
}
}
class MyListener extends WhilelangBaseListener with Antlr2Scala {
object Read extends Leia
object Skip extends Skip
override def exitProgram(ctx: C.ProgramContext) {}
override def exitSeqCommand(ctx: C.SeqCommandContext) {}
override def exitAttrib(ctx: C.AttribContext) {}
override def exitSkip(ctx: C.SkipContext) {
ctx.value = Skip
}
override def exitIf(ctx: C.IfContext) = {
val bool: Booleano = ctx.bool.value
def cmd(i: Int): Comando = ctx.command(i).value
ctx.value = new Se(bool, cmd(0), cmd(1))
}
override def exitWhile(ctx: C.WhileContext) {
val condicao: Booleano = ctx.bool.value
val comando: Comando = ctx.command.value
ctx.value = new Enquanto(condicao, comando);
}
override def exitPrint(ctx: C.PrintContext) {
}
override def exitWrite(ctx: C.WriteContext) {
ctx.value = new Escreva(ctx.expression.value[Expressao])
}
override def exitBlock(ctx: C.BlockContext) {}
override def exitRead(ctx: C.ReadContext) = {
ctx.value = Read
}
override def exitId(ctx: C.IdContext) {
ctx.value = new Id(ctx.ID.text)
}
override def exitExpParen(ctx: C.ExpParenContext) {}
override def exitInt(ctx: C.IntContext) {
ctx.value = new Inteiro(ctx.text.toInt)
}
override def exitBinOp(ctx: C.BinOpContext) {}
override def exitNot(ctx: C.NotContext) {}
override def exitBoolean(ctx: C.BooleanContext) = {
ctx.value = new Booleano(ctx.text == "true")
}
override def exitAnd(ctx: C.AndContext) {
def bool(i: Int) = ctx.bool(i).value[Booleano]
ctx.value = new ELogico(bool(0), bool(1))
}
override def exitBoolParen(ctx: C.BoolParenContext) {}
override def exitRelOp(ctx: C.RelOpContext) {}
override def exitEveryRule(ctx: ParserRuleContext) {}
override def visitErrorNode(node: ErrorNode) {}
}
grammar Whilelang;
program : seqCommand;
seqCommand: command (';' command)* ;
command: ID ':=' expression # attrib
| 'skip' # skip
| 'if' bool 'then' command 'else' command # if
| 'while' bool 'do' command # while
| 'print' Text # print
| 'write' expression # write
| '{' seqCommand '}' # bloco
;
expression: INT # int
| 'read' # read
| ID # id
| expression '*' expression # binOp
| expression '+' expression # binOp
| expression '-' expression # binOp
| '(' expression ')' # expParen
;
bool: ('true'|'false') # boolean
| expression '=' expression # relOp
| expression '<=' expression # relOp
| 'not' bool # not
| bool 'and' bool # and
| '(' bool ')' # boolParen
;
INT: ('0'..'9')+ ;
ID: ('a'..'z')+;
Text: '"' .*? '"';
Space: [ \t\n\r] -> skip;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment