Skip to content
Snippets Groups Projects
Commit 16a27f0a authored by Lior Spach's avatar Lior Spach
Browse files

Better version, but still things left to do.

parent dfc1f89b
No related branches found
No related tags found
No related merge requests found
......@@ -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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment