Skip to content

Commit

Permalink
Adicionando funções imprima, sorteie e sementesorteio
Browse files Browse the repository at this point in the history
  • Loading branch information
sqmedeiros committed Sep 28, 2016
1 parent bce88ea commit 3df729c
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 3 deletions.
3 changes: 2 additions & 1 deletion arvore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ local function noReal (v)
end

local function noTexto (v)
return makeNoV(Tag.expTexto, TipoTag.simples, TipoBasico.texto, v)
local v = string.gsub(v, "\\t", "\t")
return makeNoV(Tag.expTexto, TipoTag.simples, TipoBasico.texto, string.gsub(v, "\\n", "\n"))
end

local function noBoolFalso ()
Expand Down
14 changes: 14 additions & 0 deletions interpretador.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ function avaliaExpChamada (exp, ambiente)
io.write(s1, " ")
end
io.write("\n")
elseif exp.nome.v == "imprima" then
for i, v in ipairs(exp.args) do
local e1 = avalia(v, ambiente)
local s1 = mytostring(e1)
io.write(s1)
end
elseif exp.nome.v == "leia" then
for i, v in ipairs(exp.args) do
local x
Expand Down Expand Up @@ -202,6 +208,14 @@ function avaliaExpChamada (exp, ambiente)
local v = exp.args[1]
local s = avalia(v, ambiente)
return math.tointeger(s)
elseif exp.nome.v == "sorteie" then
local v = exp.args[1]
local s = avalia(v, ambiente)
return math.random(s)
elseif exp.nome.v == "sementesorteio" then
local v = exp.args[1]
local s = avalia(v, ambiente)
return math.randomseed(s)
else
return avaliaExpChamadaAux(exp, ambiente)
end
Expand Down
5 changes: 4 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 / CONVINT
FuncPredef <- ENTRADA / SAIDA / TEXTOCOMP / TEXTOSUB / TEXTOPOS / CONVINT / IMPRIMA / SORTEIE / SEMENTESORTEIO
ListaExp <- (Exp (VIRG (Exp / ErroExpVirg))*)*
Var <- (Nome (ABRECOL (Exp / ErroExpArray) (FECHACOL / ErroFechaCol))*) -> noVar
Nome <- !RESERVADA {LETRA RestoNome*} -> noId Sp
Expand All @@ -133,6 +133,9 @@ local g = re.compile([[
RETORNE <- 'retorne' FimNome -> noKwRetorne
ENTRADA <- {'leia'} FimNome
SAIDA <- {'escreva'} FimNome
IMPRIMA <- {'imprima'} FimNome
SORTEIE <- {'sorteie'} FimNome
SEMENTESORTEIO <- {'sementesorteie'} FimNome
TEXTOCOMP <- {'textoComp'} FimNome
TEXTOSUB <- {'textoSub'} FimNome
TEXTOPOS <- {'textoPos'} FimNome
Expand Down
18 changes: 17 additions & 1 deletion semantica.lua
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,16 @@ function analisaExpChamada (exp, ambiente)
analisaExp(v, ambiente)
end
exp.tipo = arvore.makeTipo(TipoTag.simples, TipoBasico.vazio)
elseif exp.nome.v == "leia" then
elseif exp.nome.v == "imprima" then
for i, v in ipairs(exp.args) do
analisaExp(v, ambiente)
end
exp.tipo = arvore.makeTipo(TipoTag.simples, TipoBasico.vazio)
elseif exp.nome.v == "leia" then
for i, v in ipairs(exp.args) do
analisaExp(v, ambiente)
end
exp.tipo = arvore.makeTipo(TipoTag.simples, TipoBasico.vazio)
elseif exp.nome.v == "textoComp" then
if #exp.args ~= 1 then
erro("função textoComp espera 1 parâmetro, mas foi chamada com " .. #exp.args, exp.nome.linha)
Expand Down Expand Up @@ -335,6 +340,17 @@ function analisaExpChamada (exp, ambiente)
erro("função converteInt espera parâmetro do tipo 'numero' ou 'inteiro'", v1.linha)
end
exp.tipo = arvore.makeTipo(TipoTag.simples, TipoBasico.inteiro)
elseif exp.nome.v == "sorteie" or exp.nome.v == "sementesorteio" then
if #exp.args ~= 1 then
erro("função 'sorteie' 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) or not ehValorBasico(v1, ambiente) then
erro("função 'sorteie' espera parâmetro do tipo 'inteiro'", v1.linha)
end
exp.tipo = arvore.makeTipo(TipoTag.simples, TipoBasico.inteiro)
else
analisaExpChamadaAux(exp, ambiente)
end
Expand Down
5 changes: 5 additions & 0 deletions test/semYes/imprima.gol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
imprima("1 2 3", "\n")

imprima("1\t", "2", "3", "\n")

imprima("\\n", " eita", "\n", "imprimindo\n")
5 changes: 5 additions & 0 deletions test/semYes/imprima.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
io.write("1 2 3", "\n")

io.write("1\t", "2", "3", "\n")

io.write("\\\n", " eita", "\n", "imprimindo\n")
15 changes: 15 additions & 0 deletions test/semYes/sorteie.gol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sementesorteio(1)
inteiro i = 1

repita enquanto i <= 5
escreva(sorteie(100))
i = i + 1
fim

sementesorteio(2)
i = 1
repita enquanto i <= 5
escreva(sorteie(100))
i = i + 1
fim

13 changes: 13 additions & 0 deletions test/semYes/sorteie.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
math.randomseed(1)
local i = 1
while i <= 5 do
print(math.random(100))
i = i + 1
end

math.randomseed(2)
i = 1
while i <= 5 do
print(math.random(100))
i = i + 1
end

0 comments on commit 3df729c

Please sign in to comment.