diff --git a/src/main/scala/TabelaSimb.scala b/src/main/scala/TabelaSimb.scala new file mode 100644 index 0000000000000000000000000000000000000000..a4a97d0f797d1eaf70b4eae9f273a3de3d92b687 --- /dev/null +++ b/src/main/scala/TabelaSimb.scala @@ -0,0 +1,54 @@ +package other +import scala.annotation.tailrec + +abstract class Entrada(val id:String) +case class Funcao (override val id:String, paramInfo:List[Param], desloc:Int, + nvLex:Int, rotEntrada:String) extends Entrada(id) +case class Procedimento(override val id:String, paramInfo:List[Param], + nvLex:Int, rotEntrada:String) extends Entrada(id) +case class Param(override val id:String, desloc: Int, byRef:Boolean, nvLex:Int) extends Entrada(id) +case class Var(override val id:String, desloc: Int, nvLex:Int) extends Entrada(id) +case class Label(override val id:String, rotulo:String, nvLex:Int, var used:Boolean = false, var put:Boolean = false) extends Entrada(id) + +object TabelaSimb { + + private var table = List[Entrada]() + + def hasNext = table.size>0 + def top = table.head + def drop = table = table.tail + + def getTable = table + def setTable(t:List[Entrada]) = table = t + + def getById(id:String) = { + table.find { e => e.id == id } + } + + def getLastProc(nvLex:Int) = { + print + table.find { + case p:Procedimento => if (p.nvLex==nvLex) true else false + case f:Funcao => if (f.nvLex==nvLex) true else false + case _ => false + }.get + } + + @annotation.tailrec + def numVarsLocais(nvLex:Int, count:Int=0, rest:List[Entrada] = table):Int = rest match { + case (v:Var) :: xs => numVarsLocais(nvLex, count+1, xs) + case (p:Procedimento) :: xs => if (p.nvLex>nvLex) numVarsLocais(nvLex,count,xs) else count + case (f:Funcao) :: xs => if (f.nvLex>nvLex) numVarsLocais(nvLex,count,xs) else count + case _ => count + } + + def insere(e:Entrada) { + table = e :: table + } + + def insere(lst:List[Entrada]) { + table = lst.reverse ::: table + } + + def print = table.reverse foreach println +} \ No newline at end of file