diff --git a/compiler/Lexer.scala b/compiler/Lexer.scala index d36d6e083150d2093ae801786a1c77885f6fbe54..786124e87cc74d85063c3fa6ae5d608eea3e92d1 100644 --- a/compiler/Lexer.scala +++ b/compiler/Lexer.scala @@ -12,36 +12,58 @@ val letra = (c:Char) => ('a' to 'z').contains(c) || ('A' to 'Z').contains val espacoLinTab = (c:Char) => c==' ' || c=='\t' || c=='\n' val palavraReservada = (s:String) => palavrasReservadas.contains(s) +//Verificadores +def verificaIdentificador(str:String) = if ( str.exists{(c) => !(num(c) || letra(c)) } ) erro(str+" nao eh identificador valido") +def verificaNumero(str:String) = if ( str.exists{(c) => !num(c)} ) erro(str+" nao eh numero") + //Acoes val erro = (msg:String) => {println("Erro: "+msg); sys.exit(1)} +def take(iter:BufferedIterator[Char]) = { + var acc="" + while ( //TODO + val c = iter.head + if (! espacoLinTab(c) && ! op(c)) { + acc += c + } + } +} + def atomo(str:String) { - val iter = str.iterator + val iter = str.iterator.buffered 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") + println( c match { + + case c if num(c) => //NUMERO + val acc = c + take(iter) + println("prox: "+iter.head) + verificaNumero(acc) (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 op(c) => //OPERADOR + if ((c==':') && (iter.head == '=')) + (c+""+iter.next, "op") + else + (c, "op") + + case c if letra(c) => //IDENTIFICADOR + val acc = c + take(iter) + verificaIdentificador(acc) + if (palavraReservada(acc)) + (acc,"reservado") + else + (acc, "identificador") + case c if espacoLinTab(c) => /*pula*/ + iter.dropWhile(espacoLinTab) + "" case erroCh => erro(erroCh+"") - } - ) + }) } } while (true) - atomo(readLine) \ No newline at end of file + atomo(readLine)