-- Test the type assertion operators

ravi.auto(true)

print "testing type assertions"
T = ravi

local opcodes_coverage = {}
opcodes_coverage.MOVE = 0
opcodes_coverage.LOADK = 0
opcodes_coverage.LOADKX = 0
opcodes_coverage.LOADBOOL = 0
opcodes_coverage.LOADNIL = 0
opcodes_coverage.GETUPVAL = 0
opcodes_coverage.GETTABUP = 0
opcodes_coverage.GETTABUP_SK = 0
opcodes_coverage.GETTABLE = 0
opcodes_coverage.SETTABUP = 0
opcodes_coverage.SETTABUP_SK = 0
opcodes_coverage.SETUPVAL = 0
opcodes_coverage.SETTABLE = 0
opcodes_coverage.NEWTABLE = 0
opcodes_coverage.SELF = 0
opcodes_coverage.ADD = 0
opcodes_coverage.SUB = 0
opcodes_coverage.MUL = 0
opcodes_coverage.MOD = 0
opcodes_coverage.POW = 0
opcodes_coverage.DIV = 0
opcodes_coverage.IDIV = 0
opcodes_coverage.BAND = 0
opcodes_coverage.BOR = 0
opcodes_coverage.BXOR = 0
opcodes_coverage.SHL = 0
opcodes_coverage.SHR = 0
opcodes_coverage.UNM = 0
opcodes_coverage.BNOT = 0
opcodes_coverage.NOT = 0
opcodes_coverage.LEN = 0
opcodes_coverage.CONCAT = 0
opcodes_coverage.JMP = 0
opcodes_coverage.EQ = 0
opcodes_coverage.LT = 0
opcodes_coverage.LE = 0
opcodes_coverage.TEST = 0
opcodes_coverage.TESTSET = 0
opcodes_coverage.CALL = 0
opcodes_coverage.TAILCALL = 0
opcodes_coverage.RETURN = 0
opcodes_coverage.FORLOOP = 0
opcodes_coverage.FORPREP = 0
opcodes_coverage.TFORCALL = 0
opcodes_coverage.TFORLOOP = 0
opcodes_coverage.SETLIST = 0
opcodes_coverage.CLOSURE = 0
opcodes_coverage.VARARG = 0
opcodes_coverage.EXTRAARG = 0
opcodes_coverage.NEW_IARRAY = 0
opcodes_coverage.NEW_FARRAY = 0
opcodes_coverage.LOADIZ = 0
opcodes_coverage.LOADFZ = 0
opcodes_coverage.UNMF = 0
opcodes_coverage.UNMI = 0
opcodes_coverage.ADDFF = 0
opcodes_coverage.ADDFI = 0
opcodes_coverage.ADDII = 0
opcodes_coverage.SUBFF = 0
opcodes_coverage.SUBFI = 0
opcodes_coverage.SUBIF = 0
opcodes_coverage.SUBII = 0
opcodes_coverage.MULFF = 0
opcodes_coverage.MULFI = 0
opcodes_coverage.MULII = 0
opcodes_coverage.DIVFF = 0
opcodes_coverage.DIVFI = 0
opcodes_coverage.DIVIF = 0
opcodes_coverage.DIVII = 0
opcodes_coverage.TOINT = 0
opcodes_coverage.TOFLT = 0
opcodes_coverage.TOIARRAY = 0
opcodes_coverage.TOFARRAY = 0
opcodes_coverage.MOVEI = 0
opcodes_coverage.MOVEF = 0
opcodes_coverage.MOVEIARRAY = 0
opcodes_coverage.MOVEFARRAY = 0
opcodes_coverage.IARRAY_GET = 0
opcodes_coverage.FARRAY_GET = 0
opcodes_coverage.IARRAY_SET = 0
opcodes_coverage.FARRAY_SET = 0
opcodes_coverage.FORLOOP_IP = 0
opcodes_coverage.FORLOOP_I1 = 0
opcodes_coverage.FORPREP_IP = 0
opcodes_coverage.FORPREP_I1 = 0
opcodes_coverage.SETUPVALI = 0
opcodes_coverage.SETUPVALF = 0
opcodes_coverage.SETUPVAL_IARRAY = 0
opcodes_coverage.SETUPVAL_FARRAY = 0
opcodes_coverage.IARRAY_SETI = 0
opcodes_coverage.FARRAY_SETF = 0
opcodes_coverage.BAND_II = 0
opcodes_coverage.BOR_II = 0
opcodes_coverage.BXOR_II = 0
opcodes_coverage.SHL_II = 0
opcodes_coverage.SHR_II = 0
opcodes_coverage.BNOT_I = 0
opcodes_coverage.EQ_II = 0
opcodes_coverage.EQ_FF = 0
opcodes_coverage.LT_II = 0
opcodes_coverage.LT_FF = 0
opcodes_coverage.LE_II = 0
opcodes_coverage.LE_FF = 0
opcodes_coverage.GETI = 0
opcodes_coverage.TABLE_GETFIELD = 0
opcodes_coverage.SETI = 0
opcodes_coverage.TABLE_SETFIELD = 0
opcodes_coverage.SETFIELD = 0
opcodes_coverage.GETFIELD = 0
opcodes_coverage.TOTAB = 0
opcodes_coverage.MOVETAB = 0
opcodes_coverage.SETUPVALT = 0
opcodes_coverage.TABLE_SELF_SK = 0
opcodes_coverage.TOTYPE = 0
opcodes_coverage.TOSTRING = 0
opcodes_coverage.TOCLOSURE = 0
opcodes_coverage.DEFER = 0

--ravi.verbosity(1)

local compile = function(f)
  if ravi.jit() then
    assert(ravi.compile(f))
  end
  return true
end

-- ================================================================
-- Following section is an extract from the code.lua test 
-- These functions test bytecode generation, and also provide
-- helper routines that we use later on in other test cases

-- testing opcodes
function check (f, arg: table)
  local c = T.listcode(f)
  for i=1, #arg do
    --print(arg[i], c[i])
    opcodes_coverage[arg[i]] = opcodes_coverage[arg[i]]+1
    assert(string.find(c[i], '- '..arg[i]..' *[AB][xs]?=%d'))
  end
  assert(c[#arg+2] == nil)
end


function checkequal (a, b)
  a = T.listcode(a)
  b = T.listcode(b)
  for i = 1, #a do
    a[i] = string.gsub(a[i], '%b()', '')   -- remove line number
    b[i] = string.gsub(b[i], '%b()', '')   -- remove line number
    assert(a[i] == b[i])
  end
end


local x,y,z

x = function()
  local i: integer = @integer 1
  local n: number = @number 1.2
  assert(i == 1 and n == 1.2)
  local j = { 1, 2, 3, 4.2, 5 }
  i = @integer( @integer (j[2]) )
  n = @number( @number (j[4]) )
  assert(i == 2)
  assert(n == 4.2)
end
check(x, {'LOADK', 'LOADK', 'GETTABUP_SK', 'EQ_II',
  'JMP', 'EQ_FF', 'JMP', 'LOADBOOL', 'LOADBOOL',
  'CALL', 'NEWTABLE', 'LOADK', 'LOADK', 'LOADK',
  'LOADK', 'LOADK', 'SETLIST', 'GETI', 'TOINT',
  'MOVEI', 'GETI', 'TOFLT', 'MOVEF', 'GETTABUP_SK',
  'EQ_II', 'JMP', 'LOADBOOL', 'LOADBOOL', 'CALL',
  'GETTABUP_SK', 'EQ_FF', 'JMP', 'LOADBOOL', 'LOADBOOL',
  'CALL', 'RETURN'})
x()
compile(x)
x()  

function x()
   local a,b = 1,2
   local c: integer,d: integer = @integer a, @integer b
   assert(c == a and b == d)
end
check(x, {'LOADK', 'LOADK', 'TOINT', 'MOVE',
  'TOINT', 'MOVE', 'GETTABUP_SK', 'EQ', 'JMP',
  'EQ', 'JMP', 'LOADBOOL', 'LOADBOOL', 'CALL',
  'RETURN'})
x()
compile(x)
x()

g = { 1, 5.5 }
local h = { 10, 4.2 }
function x() 
  local a: integer = 11
  local b: number = 6.5
  local c = 6
  a = @integer( g[1] ) + @integer( h[1] ) * @integer( c )
  c = 3.2
  b = @number( g[2] ) * @number( h[2] ) - @number( c )
  return a,b
end
y,z = x()
assert(y == 61 and math.abs(z - 19.9) < 1e-5)
compile(x)
y,z = x()
assert(y == 61 and math.abs(z - 19.9) < 1e-10)
check(x, {'LOADK', 'LOADK', 'LOADK', 'GETTABUP_SK',
  'GETI', 'TOINT', 'GETTABUP', 'TOINT', 'TOINT',
  'MULII', 'ADDII', 'LOADK', 'GETTABUP_SK', 'GETI',
  'TOFLT', 'GETTABUP', 'TOFLT', 'MULFF', 'TOFLT',
  'SUBFF', 'MOVE', 'MOVE', 'RETURN', 'RETURN'})

x=load 'local t: number[] = {}'
check(x, {'NEW_FARRAY', 'RETURN'})

x=load 'local t = @number[] {}'
check(x, {'NEW_FARRAY', 'RETURN'})

x=load 'local t: number[] = @number[] ( @number[] {} )'
check(x, {'NEW_FARRAY', 'RETURN'})

function x()
  return { @integer[] {1,2}, @number[] {42} }
end
check(x, {'NEWTABLE', 'NEW_IARRAY', 'LOADK', 'LOADK',
  'SETLIST', 'NEW_FARRAY', 'LOADK', 'SETLIST',
  'SETLIST', 'RETURN', 'RETURN'})
y = x()
assert(ravitype(y[1]) == 'integer[]')
assert(ravitype(y[2]) == 'number[]')

function x()
  return @integer[] @integer[] @integer[]{1}
end
check(x, {'NEW_IARRAY', 'LOADK', 'SETLIST', 'RETURN', 'RETURN'})
assert(ravitype(x()) == 'integer[]')

function x()
  return nil or @integer[] {1}
end
assert(ravitype(x()) == 'integer[]')
check(x, {'NEW_IARRAY', 'LOADK', 'SETLIST', 'RETURN', 'RETURN'})

function x()
  return @number[]( @integer[] {1} and @number[] {42} )
end
assert(ravitype(x()) == 'number[]')
assert(x()[1] == 42.0)
check(x, {'NEW_IARRAY', 'LOADK', 'SETLIST', 'TEST',
  'JMP', 'NEW_FARRAY', 'LOADK', 'SETLIST', 'TOFARRAY',
  'RETURN', 'RETURN'})
