Skip to content

Commit

Permalink
Adicionando "converteInt"
Browse files Browse the repository at this point in the history
  • Loading branch information
sqmedeiros committed Jul 26, 2016
1 parent 7dd9a07 commit 132e089
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 1 deletion.
4 changes: 4 additions & 0 deletions interpretador.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ function avaliaExpChamada (exp, ambiente)
exp.nome.v = "textoSub"
exp.args[3] = exp.args[2]
return avaliaExpChamada(exp, ambiente)
elseif exp.nome.v == "converteInt" then
local v = exp.args[1]
local s = avalia(v, ambiente)
return math.tointeger(s)
else
return avaliaExpChamadaAux(exp, ambiente)
end
Expand Down
3 changes: 2 additions & 1 deletion parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ local g = re.compile([[
ABREPAR (Exp / ErroExpPar) (FECHAPAR / ErroFechaPar) /
ChamadaFunc / Numero / Var / Cadeia / VERDADEIRO / FALSO
ChamadaFunc <- ((FuncPredef -> noId (ABREPAR / ErroFuncPredef) / Nome ABREPAR) ListaExp (FECHAPAR / ErroFechaPar)) -> noChamadaFunc
FuncPredef <- ENTRADA / SAIDA / TEXTOCOMP / TEXTOSUB / TEXTOPOS
FuncPredef <- ENTRADA / SAIDA / TEXTOCOMP / TEXTOSUB / TEXTOPOS / CONVINT
ListaExp <- (Exp (VIRG (Exp / ErroExpVirg))*)*
Var <- (Nome (ABRECOL (Exp / ErroExpArray) (FECHACOL / ErroFechaCol))*) -> noVar
Nome <- !RESERVADA {LETRA RestoNome*} -> noId Sp
Expand Down Expand Up @@ -136,6 +136,7 @@ local g = re.compile([[
TEXTOCOMP <- {'textoComp'} FimNome
TEXTOSUB <- {'textoSub'} FimNome
TEXTOPOS <- {'textoPos'} FimNome
CONVINT <- {'converteInt'} FimNome
REPITA <- 'repita' FimNome
ENQUANTO <- 'enquanto' FimNome
INTEIRO <- 'inteiro' -> getTipoBasico FimNome
Expand Down
11 changes: 11 additions & 0 deletions semantica.lua
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,17 @@ function analisaExpChamada (exp, ambiente)
end
end
exp.tipo = arvore.makeTipo(TipoTag.simples, TipoBasico.texto)
elseif exp.nome.v == "converteInt" then
if #exp.args ~= 1 then
erro("função converteInt espera 1 parâmetro, mas foi chamada com " .. #exp.args, exp.nome.linha)
return
end
local v1 = exp.args[1]
analisaExp(v1, ambiente)
if (v1.tipo.basico ~= TipoBasico.inteiro and v1.tipo.basico ~= TipoBasico.numero) or not ehValorBasico(v1, ambiente) then
erro("função converteInt espera parâmetro do tipo 'numero' ou 'inteiro'", v1.linha)
end
exp.tipo = arvore.makeTipo(TipoTag.simples, TipoBasico.inteiro)
else
analisaExpChamadaAux(exp, ambiente)
end
Expand Down
16 changes: 16 additions & 0 deletions test/semNo/converteInt.gol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
inteiro x

x = converteInt(3)

x = converteInt(3.0)

//Erro
escreva(converteInt("3.0"))

//Erro
escreva(converteInt(verdadeiro))

escreva(converteInt(3.0 + 4.2))

//Erro
escreva(converteInt(3.0 + 4.2, 3))
9 changes: 9 additions & 0 deletions test/semYes/converte.gol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
numero x = 8.0

escreva(converteInt(3 + 4.2))

escreva(converteInt(4.5 + 4.5))

escreva(converteInt(x))

escreva(converteInt(44))
9 changes: 9 additions & 0 deletions test/semYes/converte.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local x = 8.0

print(math.tointeger(3 + 4.2))

print(math.tointeger(4.5 + 4.5))

print(math.tointeger(x))

print(math.tointeger(44))
1 change: 1 addition & 0 deletions testsemantico.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ novoTeste("erroTextoPos.gol", 8)
novoTeste("funcaoSoma.gol", 16)
novoTeste("funcaoArray.gol", 4)
novoTeste("funcaoArray2.gol", 6)
novoTeste("converteInt.gol", 3)

local dir = "./test/semNo"
makeTeste(arqTeste, dir)
Expand Down

0 comments on commit 132e089

Please sign in to comment.