diff --git a/compiler/Lexer.scala b/compiler/Lexer.scala index 6590f57df75fc4f3fb8ce51c03bd495eef93650c..d36d6e083150d2093ae801786a1c77885f6fbe54 100644 --- a/compiler/Lexer.scala +++ b/compiler/Lexer.scala @@ -1,23 +1,47 @@ -import io.{Source,BufferedSource} +#!/usr/bin/scala +!# +println("started...") +case class Token(val value:String, val func:String) -case class Atomo(val valor:String, val token:String) +val palavrasReservadas = List("begin", "end", "function", "procedure", "var", "program") + +// Conjuntos +val num = (c:Char) => ('0' to '9').contains(c) +val op = (c:Char) => "+-*/=:".contains(c) +val letra = (c:Char) => ('a' to 'z').contains(c) || ('A' to 'Z').contains(c) || c=='_' +val espacoLinTab = (c:Char) => c==' ' || c=='\t' || c=='\n' +val palavraReservada = (s:String) => palavrasReservadas.contains(s) + +//Acoes +val erro = (msg:String) => {println("Erro: "+msg); sys.exit(1)} -object Lexer { - def getTokens(stream:Iterator[Char]):List[atomo] = { - var proximo: Char = '' - var acc = "" - - def PROX = if (stream.hasNext) proximo = stream.next - def ADD = acc + proximo - PROX - proximo match { //TODO: Pattern Matching - case ' ' || '\t' || '\n' => - case letra => - case numero => - case simbolo_especial => +def atomo(str:String) { + val iter = str.iterator + while (iter.hasNext) { + def take(iter:Iterator[Char]) = iter.takeWhile{(c) => ! espacoLinTab(c)}.mkString + iter.dropWhile(espacoLinTab) + val c = iter.next + var acc = "" + println( + c match { + case c if num(c) => + acc = c + take(iter) + if ( acc.exists{(c) => !num(c)} ) erro(acc+" nao eh numero") + (acc,"numero") + case c if op(c) => (c, "op") + case c if letra(c) => (c, "letra") + acc = c + take(iter) + if ( acc.exists{(c) => !(num(c) || letra(c)) } ) erro(acc+" nao eh identificador valido") + if (palavraReservada(acc)) (acc,"reservado") + else (acc, "identificador") + case c if espacoLinTab(c) => /*pula*/ + case erroCh => erro(erroCh+"") } - + ) } - } + + +while (true) + atomo(readLine) \ No newline at end of file