Skip to content

Commit

Permalink
A função sementesorteio sem parâmetros usa o tempo atual como semente
Browse files Browse the repository at this point in the history
  • Loading branch information
sqmedeiros committed Sep 30, 2016
1 parent 3df729c commit e81c864
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 7 additions & 2 deletions interpretador.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,13 @@ function avaliaExpChamada (exp, ambiente)
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)
local s
if #exp.args == 1 then
local v = exp.args[1]
s = avalia(v, ambiente)
else
s = os.time()
end
return math.randomseed(s)
else
return avaliaExpChamadaAux(exp, ambiente)
Expand Down
15 changes: 14 additions & 1 deletion semantica.lua
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ 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
elseif exp.nome.v == "sorteie" then
if #exp.args ~= 1 then
erro("função 'sorteie' espera 1 parâmetro, mas foi chamada com " .. #exp.args, exp.nome.linha)
return
Expand All @@ -351,6 +351,19 @@ function analisaExpChamada (exp, ambiente)
erro("função 'sorteie' espera parâmetro do tipo 'inteiro'", v1.linha)
end
exp.tipo = arvore.makeTipo(TipoTag.simples, TipoBasico.inteiro)
elseif exp.nome.v == "sementesorteio" then
if #exp.args ~= 1 and #exp.args ~= 0 then
erro("função 'sementesorteio' espera 0 ou 1 parâmetro, mas foi chamada com " .. #exp.args, exp.nome.linha)
return
end
if #exp.args == 1 then
local v1 = exp.args[1]
analisaExp(v1, ambiente)
if (v1.tipo.basico ~= TipoBasico.inteiro) or not ehValorBasico(v1, ambiente) then
erro("função 'sementesorteio' espera parâmetro do tipo 'inteiro'", v1.linha)
end
exp.tipo = arvore.makeTipo(TipoTag.simples, TipoBasico.inteiro)
end
else
analisaExpChamadaAux(exp, ambiente)
end
Expand Down

0 comments on commit e81c864

Please sign in to comment.