Code: Select all
#include once "jsb_function.bas"
#include once "jsb_lexer.bas"
#include once "jsb_error.bi"
#include once "crt.bi"
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' Variable Instantiation & Assignment '
' '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function _VAR( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 2, ILLEGAL_FUNCTION_CALL, "var" )
func_assert( args[0].typ = varType._STRING_, ILLEGAL_FUNCTION_CALL, args[0].getType )
dim as string lbl = args[0].strng
if args[1].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[1].strng )
if check then
dim as variable n = *check
n.lbl = lbl
varMap->add( n )
else
args[1].lbl = lbl
varMap->add( args[1] )
endif
else
args[1].lbl = lbl
varMap->add( args[1] )
endif
return "true"
end function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function _ASSIGN( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
'func_assert( args[0].typ = varType._STRING_, ILLEGAL_FUNCTION_CALL, args[0].getType )
dim as string lbl = args[0].strng
dim as variable ptr lhs = varMap->find( lbl )
func_assert( lhs, VARIABLE_NOT_DECLARED, lbl )
dim as variable ptr rhs
if args.size = 2 then
if args[1].typ = varType._STRING_ then
rhs = varMap->find( args[1].strng )
if rhs then
*lhs = *rhs
lhs->lbl = lbl
else
*lhs = args[1]
lhs->lbl = lbl
endif
else
*lhs = args[1]
lhs->lbl = lbl
endif
elseif args.size = 3 then
dim as variable ptr idx
dim as integer idx_val = 0
dim as string idx_str = ""
if args[1].typ = varType._STRING_ then
idx = varMap->find( args[1].strng )
if idx then
if idx->typ = varType._NUMBER_ then
idx_val = idx->number
elseif idx->typ = varType._STRING_ then
idx_str = idx->strng
endif
else
idx_str = args[1].strng
endif
else
'func_assert( args[1].typ = _NUMBER_, TYPE_MISMATCH, args[1].tostring )
if args[1].typ = varType._NUMBER_ then
idx_val = args[1].number
elseif args[1].typ = varType._STRING_ then
idx_str = args[1].strng
endif
endif
dim as variable ptr value
dim as variable assign_val
if args[2].typ = varType._STRING_ then
value = varMap->find( args[2].strng )
if value then
assign_val = *value
else
assign_val = args[2]
endif
else
assign_val = args[2]
endif
if lhs->typ = varType._ARRAY_ then
func_assert( idx_val < lhs->array->size, ARRAY_OUT_OF_BOUNDS, idx_val )
*lhs->array->items( idx_val ) = assign_val
elseif lhs->typ = varType._OBJECT_ then
dim as variable ptr check2 = lhs->object->get( idx_str )
func_assert( check2, INVALID_OBJECT_KEY, idx_str )
*check2 = assign_val
endif
endif
return "true"
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' Console Output '
' '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function _PRINT( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 1, ILLEGAL_FUNCTION_CALL, "PRINT" )
dim as string value = args[0].tostring
dim as variable ptr check = varMap->find( value )
if check then
print check->toString
else
print value
endif
return "true"
end function
declare function sound alias "Beep" ( byval frequency as integer, byval duration as integer ) as integer
function _jsBEEP( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
if args.size = 0 then sound( 800, 700 ) : return "true"
if args.size = 1 then
dim as variable ptr check = varMap->find( args[0].strng )
if check then
sound( check->number, 1 )
return "true"
else
sound( args[0].number, 1 )
return "true"
endif
endif
if args.size = 2 then
dim as integer p1,p2
dim as variable ptr check = varMap->find( args[0].strng )
if check then
p1 = check->number
else
p1 = args[0].number
endif
check = varMap->find( args[1].strng )
if check then
p2 = check->number
else
p2 = args[1].number
endif
sound( p1,p2 )
return "true"
endif
return "false"
end function
function _jsbSLEEP( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
sleep : return "true"
end function
function _CLS( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
cls : return "true"
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' Math Functions '
' '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function _ADD( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 2, SYNTAX_ERROR, "Parameter count all wrong")
for i as integer = 0 to 1
if args[i].typ = varType._STRING_ then
dim as variable ptr chk = varMap->find( args[i].strng )
if chk then args[i] = *chk
endif
next i
if args[0].typ = varType._NUMBER_ andalso args[1].typ = varType._NUMBER_ then
return args[0].number + args[1].number
else
return DBLQUOTE + args[0].tostring + args[1].tostring + DBLQUOTE
endif
return "null"
end function
function _SUB( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then vRes = args[0].number
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = check->number
end if
if args[1].typ = varType._NUMBER_ then vRes -= args[1].number
if args[1].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[1].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[1].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[1].getType )
vRes -= check->number
end if
return vRes
end function
function _MUL( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then vRes = args[0].number
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = check->number
end if
if args[1].typ = varType._NUMBER_ then vRes *= args[1].number
if args[1].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[1].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[1].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[1].getType )
vRes *= check->number
end if
return vRes
end function
function _DIV( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then vRes = args[0].number
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = check->number
end if
if args[1].typ = varType._NUMBER_ then vRes /= args[1].number
if args[1].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[1].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[1].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[1].getType )
vRes /= check->number
end if
return vRes
end function
function _MOD( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then vRes = args[0].number
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = check->number
end if
if args[1].typ = varType._NUMBER_ then vRes = vRes MOD args[1].number
if args[1].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[1].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[1].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[1].getType )
vRes = vRes MOD check->number
end if
return vRes
end function
function _EXP( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then vRes = args[0].number
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = check->number
end if
if args[1].typ = varType._NUMBER_ then vRes ^= args[1].number
if args[1].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[1].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[1].strng )
func_assert( check->getTypeNum = varType._NUMBER_, TYPE_MISMATCH, args[1].getType )
vRes ^= check->number
end if
return vRes
end function
function _SIN( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then return sin( args[0].number )
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = check->number
end if
return sin( vRes )
end function
function _COS( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then return cos( args[0].number )
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = check->number
end if
return cos( vRes )
end function
function _TAN( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then return tan( args[0].number )
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = check->number
end if
return tan( vRes )
end function
function _ATAN( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then return atn( args[0].number )
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = check->number
end if
return atn( vRes )
end function
function _ATAN2( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
'Atan2(y,x)
dim as double x,y
if args[0].typ = varType._NUMBER_ then x = args[0].number
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
y = check->number
end if
if args[1].typ = varType._NUMBER_ then y = args[1].number
if args[1].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[1].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[1].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[1].getType )
x = check->number
end if
return atan2( y, x )
end function
function _ACOS( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then return acos( args[0].number )
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = check->number
end if
return acos( vRes )
end function
function _ASIN( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then return asin( args[0].number )
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = check->number
end if
return asin( vRes )
end function
function _SQR( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then return sqr( args[0].number )
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = check->number
end if
return sqr( vRes )
end function
function _ABS( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then return abs( args[0].number )
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = abs( check->number )
end if
return vRes
end function
function _LOG( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then return log( args[0].number )
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = log( check->number )
end if
return vRes
end function
function _FIX( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then return fix( args[0].number )
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = fix( check->number )
end if
return vRes
end function
function _INT( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then return int( args[0].number )
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = int( check->number )
end if
return vRes
end function
function _FRAC( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then return frac( args[0].number )
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = frac( check->number )
end if
return vRes
end function
function _SGN( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
if args[0].typ = varType._NUMBER_ then return sgn( args[0].number )
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = sgn( check->number )
end if
return vRes
end function
function _RND( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as double vRes
return rnd
if args.size then
if args[0].typ = varType._NUMBER_ then return rnd * args[0].number
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
func_assert( check->typ = varType._NUMBER_, TYPE_MISMATCH, args[0].getType )
vRes = check->number*rnd
end if
endif
return vRes
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' String Functions '
' '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function _CAT( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as string vRes
dim as variable ptr check = varMap->find( args[0].strng )
if check then
vRes = check->toString
else
vRes = args[0].toString
endif
check = varMap->find( args[1].strng )
if check then
vRes += check->toString
else
vRes += args[1].toString
endif
return ascii(34)+vRes+ascii(34)
end function
function _LEN( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as integer vRes
dim as variable ptr check = varMap->find( args[0].strng )
if check then
select case check->typ
case varType._STRING_
vRes = len( check->strng )
case varType._ARRAY_
vRes = check->getArray->size
end select
elseif args[0].typ = varType._STRING_ then
vRes = len( args[0].strng )
elseif args[0].typ = varType._ARRAY_ then
vRes = args[0].array->size
endif
return vRes
end function
function _ASC( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as ubyte vRes
dim as variable ptr check = varMap->find( args[0].strng )
if check then
vRes = check->strng[0]
else
vRes = args[0].strng[0]
endif
return vRes
end function
function _CHR( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as string vRes = ""
dim as variable ptr check = varMap->find( args[0].strng )
if check then
vRes = ascii( check->number )
else
vRes = ascii( args[0].number )
endif
if vRes = "" then return ""
return DBLQUOTE+vRes+DBLQUOTE
end function
function _LEFT( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as string p1 = "", vRes = ""
dim as integer p2 = 0
dim as variable arr
func_assert( args.size = 2, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr check = varMap->find( args[0].strng )
dim as variable ptr checkNum = varMap->find( args[1].strng )
if check = 0 then
check = new variable
*check = args[0]
endif
if checkNum = 0 then
checkNum = new variable
*checkNum = args[1]
endif
if check->typ = varType._STRING_ then
p1 = check->strng
elseif check->typ = varType._ARRAY_ then
arr = *check
endif
p2 = args[1].number
if check->typ = varType._STRING_ then
vRes = left( p1, p2 )
return DBLQUOTE+vRes+DBLQUOTE
elseif check->typ = varType._ARRAY_ then
dim as variable vRes
vRes.array = new varArray
vRes.typ = varType._ARRAY_
for i as integer = 0 to p2-1
vRes.array->push_back( arr.array->items(i) )
next i
return vRes
endif
end function
function _RIGHT( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as string p1 = "", vRes = ""
dim as integer p2 = 0
dim as variable arr
func_assert( args.size = 2, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr check = varMap->find( args[0].strng )
dim as variable ptr checkNum = varMap->find( args[1].strng )
if check = 0 then
check = new variable
*check = args[0]
endif
if checknum = 0 then
checkNum = new variable
*checkNum = args[1]
endif
if check->typ = varType._STRING_ then
p1 = check->strng
elseif check->typ = varType._ARRAY_ then
arr = *check
endif
p2 = checkNum->number
if check->typ = varType._STRING_ then
vRes = right( p1, p2 )
return DBLQUOTE+vRes+DBLQUOTE
elseif check->typ = varType._ARRAY_ then
dim as variable vRes
vRes.array = new varArray
vRes.typ = varType._ARRAY_
for i as integer = p2 to arr.array->size-1
vRes.array->push_back( arr.array->items(i) )
next i
return vRes
endif
end function
function _MID( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as string p1 = ""
dim as integer p2,p3
dim as variable ptr check1,check2,check3
dim as variable arr
check1 = varMap->find( args[0].strng )
check2 = varMap->find( args[1].strng )
check3 = varMap->find( args[2].strng )
if check1 then
if check1->typ = varType._STRING_ then p1 = check1->strng
if check1->typ = varType._ARRAY_ then arr = *check1
else
if args[0].typ = varType._STRING_ then p1 = args[0].strng
if args[0].typ = varType._ARRAY_ then arr = args[0]
endif
if check2 then p2 = check2->number else p2 = args[1].number
if check3 then p3 = check3->number else p3 = args[2].number
if (check1 andalso check1->typ = varType._ARRAY_) orelse args[0].typ = varType._ARRAY_ then
dim as integer p4 = p2+p3-1
if p4 > arr.array->size-1 then p4 = arr.array->size-1
dim as variable res
res.array = new varArray
res.typ = varType._ARRAY_
for i as integer = p2-1 to p4-1
res.array->push_back( arr.array->items(i) )
next i
return res
else
return DBLQUOTE+ mid(p1,p2,p3) +DBLQUOTE
endif
return "false"
end function
function _LCASE( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as string vRes = ""
dim as variable ptr check = varMap->find( args[0].strng )
if check then
vRes = check->strng
else
vRes = args[0].strng
endif
vRes = lcase( vRes )
if vRes = "" then return ""
return DBLQUOTE+vRes+DBLQUOTE
end function
function _UCASE( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as string vRes = ""
dim as variable ptr check = varMap->find( args[0].strng )
if check then
vRes = check->strng
else
vRes = args[0].strng
endif
vRes = ucase( vRes )
if vRes = "" then return ""
return DBLQUOTE+vRes+DBLQUOTE
end function
function _INSTR( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as integer vRes = 0
dim as string p1 = "", p2 = ""
dim as integer p3 = 1
func_assert( args.size >= 2, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr check = varMap->find( args[0].strng )
if check then
p1 = check->strng
else
p1 = args[0].strng
endif
check = 0
check = varMap->find( args[1].strng )
if check then
p2 = check->strng
else
p2 = args[1].strng
endif
check = 0
if args.size = 3 then
check = varMap->find( args[2].strng )
if check then
p3 = check->number
else
p3 = args[2].number
endif
endif
if p3 = 0 then p3 = 1
vRes = instr( p3,p1,p2 )
return vRes
end function
function _LOADFILEASSTRING( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as string vRes = "", p1 = ""
dim as variable ptr check = varMap->find( args[0].strng )
if check then
p1 = check->strng
else
p1 = args[0].strng
endif
vRes = LoadFileAsString( p1 )
dim as variable res
res.typ = varType._STRING_
res.strng = vRes
return res
end function
function _SPLIT( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 3, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as string txt = "", delim = ""
dim as variable ptr check1 = varMap->find( args[0].strng )
dim as variable ptr check2 = varMap->find( args[1].strng )
dim as variable ptr check3 = varMap->find( args[2].strng )
if check1 then txt = check1->strng else txt = args[0].strng
if check2 then delim = check2->strng else delim = args[1].strng
if check3 = 0 then check3 = @args[2]
func_assert( check3, VARIABLE_NOT_DECLARED, args[2].strng )
func_assert( len(delim) andalso len(txt), SYNTAX_ERROR, "Empty string" )
txt &= ascii(delim[0])
dim as integer nFields=0, bgn=0, lentxt = len(txt)-1
dim as string newstring = ""
for i as integer = 0 to lentxt
for ii as integer = 0 to len(delim)-1
if txt[i] = delim[ii] OR i = lentxt then
newstring = RIGHT ( LEFT ( txt, i ), i-bgn )
if newstring <> "" then
nFields+=1
dim as variable tmp = DBLQUOTE+newstring+DBLQUOTE
check3->array->push_back( @tmp )
endif
bgn = i+1
endif
next
next
return "true"
end function
function _SLICE( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 3, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr check1 = varMap->find( args[0].strng )
dim as variable ptr check2 = varMap->find( args[1].strng )
dim as variable ptr check3 = varMap->find( args[2].strng )
dim as integer idx1=0, idx2=0
dim as string s = ""
dim as variable arr
if check1 then
if check1->typ = varType._STRING_ then s = check1->strng
if check1->typ = varType._ARRAY_ then arr = *check1
else
if args[0].typ = varType._STRING_ then s = args[0].strng
if args[0].typ = varType._ARRAY_ then arr = args[0]
endif
if check2 then idx1 = check2->number else idx1 = args[1].number
if check3 then idx2 = check3->number else idx2 = args[2].number
if s <> "" then
dim as integer l = idx2-idx1
return DBLQUOTE+ mid(s,idx1,l) +DBLQUOTE
elseif arr.typ = varType._ARRAY_ then
dim as variable res
res.typ = varType._ARRAY_
res.array = new varArray
for i as integer = idx1 to idx2
if i > arr.array->size-1 then exit for
res.array->push_back( arr.array->items(i) )
next i
return res
endif
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' Array Functions '
' '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function _PLUCK( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
dim as string vRes = ""
dim as string p1 = ""
dim as integer p2 = 0
func_assert( args.size = 2, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr check = varMap->find( args[0].strng )
if check then
if check->typ = varType._STRING_ then
p1 = check->strng
endif
if check->typ = varType._ARRAY_ then
dim as variable ptr check2 = varMap->find( args[1].strng )
if check2 then
dim as variable ptr pluck = check->array->get( check2->number )
func_assert( pluck, ARRAY_OUT_OF_BOUNDS, check2->number )
return *pluck
else
dim as variable ptr pluck = check->array->get( args[1].number )
func_assert( pluck, ARRAY_OUT_OF_BOUNDS, args[1].number )
return *pluck
endif
endif
if check->typ = varType._OBJECT_ then
dim as variable ptr check2 = varMap->find( args[1].strng )
if check2 then
dim as variable ptr pluck = check->object->get( check->strng )
func_assert( pluck, INVALID_OBJECT_KEY, check2->strng )
return *pluck
else
dim as variable ptr pluck = check->object->get( args[1].strng )
func_assert( pluck, INVALID_OBJECT_KEY, args[1].strng )
return *pluck
endif
endif
else
select case args[0].typ
case varType._OBJECT_
dim as variable ptr check2 = varMap->find( args[1].strng )
dim as string obkey = ""
if check2 then
obkey = check2->strng
else
obkey = args[1].strng
endif
dim as variable ptr pluck = args[0].object->get( obkey )
func_assert( pluck, INVALID_OBJECT_KEY, p2 )
return *pluck
case varType._ARRAY_
dim as variable ptr check2 = varMap->find( args[1].strng )
if check2 then
p2 = check2->number
else
p2 = args[1].number
endif
dim as variable ptr pluck = args[0].array->get( p2 )
func_assert( pluck, ARRAY_OUT_OF_BOUNDS, p2 )
return *pluck
case else
'i dunno
end select
endif
func_assert( p1 <> "", ILLEGAL_FUNCTION_CALL, "Empty string" )
check = varMap->find( args[1].strng )
if check then
p2 = check->number
else
p2 = args[1].number
endif
func_assert( (p2 < len(p1) andalso p2 >= 0), ARRAY_OUT_OF_BOUNDS, p1 + "[" & p2 & "]" )
return DBLQUOTE+ascii( p1[ p2 ] )+DBLQUOTE
end function
function _POP( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 2, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr array_name = varMap->find( args[0].strng )
dim as variable ptr check_idx = varMap->find( args[1].strng )
dim as integer idx = 0
if array_name = 0 then array_name = @args[0]
if check_idx then
func_assert( check_idx->typ = varType._NUMBER_, TYPE_MISMATCH, check_idx->lbl )
idx = check_idx->number
else
func_assert( args[1].typ = varType._NUMBER_, TYPE_MISMATCH, args[1].lbl )
idx = args[1].number
endif
array_name->array->pop( idx )
return "true"
end function
function _PUSH( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 3, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr array_name = varMap->find( args[0].strng )
dim as variable ptr check_idx = varMap->find( args[1].strng )
dim as variable ptr check_var = varMap->find( args[2].strng )
dim as variable new_var
dim as integer idx = 0
if array_name = 0 then array_name = @args[0]
if check_idx then
func_assert( check_idx->typ = varType._NUMBER_, TYPE_MISMATCH, check_idx->lbl )
idx = check_idx->number
else
func_assert( args[1].typ = varType._NUMBER_, TYPE_MISMATCH, args[1].lbl )
idx = args[1].number
endif
if check_var then
new_var = *check_var
else
new_var = args[2]
endif
array_name->getArray()->push( idx, @new_var )
return "true"
end function
function _PUSH_BACK( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 2, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr array_name = varMap->find( args[0].strng )
dim as variable ptr add_var = varMap->find( args[1].strng )
dim as integer idx = 0
if array_name = 0 then array_name = @args[0]
if add_var then
array_name->array->push_back( add_var )
else
array_name->array->push_back( @args[1] )
endif
return "true"
end function
function _FIRST( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 1, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr array_name = varMap->find( args[0].strng )
if array_name = 0 then array_name = @args[0]
func_assert( array_name->array->size, ARRAY_OUT_OF_BOUNDS, args[0].strng )
return *array_name->array->items(0)
end function
function _LAST( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 1, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr array_name = varMap->find( args[0].strng )
if array_name = 0 then array_name = @args[0]
func_assert( array_name->array->size, ARRAY_OUT_OF_BOUNDS, args[0].strng )
return *array_name->array->items( array_name->array->size - 1 )
end function
function _SIZE( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 1, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr ob_name = varMap->find( args[0].strng )
if ob_name = 0 then
func_assert( args[0].typ = varType._OBJECT_, ILLEGAL_FUNCTION_CALL, "Expected Object" )
ob_name = @args[0]
else
func_assert( ob_name->typ = varType._OBJECT_, ILLEGAL_FUNCTION_CALL, "Expected Object" )
endif
return ob_name->object->size
end function
function _CLEAR( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
if args[0].typ = varType._STRING_ then
dim as variable ptr check = varMap->find( args[0].strng )
func_assert( check, VARIABLE_NOT_DECLARED, args[0].strng )
check->array->clear
elseif args[0].typ = varType._ARRAY_ then
args[0].array->clear
endif
return "true"
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' Bitwise Functions '
' '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function _ISEQUAL( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 2, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr check1 = varMap->find( args[0].strng )
dim as variable ptr check2 = varMap->find( args[1].strng )
dim as variable p1,p2
if check1 then p1 = *check1 else p1 = args[0]
if check2 then p2 = *check2 else p2 = args[1]
if p1.typ <> p2.typ then return "false"
select case p1.typ
case varType._STRING_
if p1.strng = p2.strng then return "true"
case varType._NUMBER_
if p1.number = p2.number then return "true"
case varType._BOOL_
if p1.bool = p2.bool then return "true"
end select
return "false"
end function
function _GREATERTHAN( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 2, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr check1 = varMap->find( args[0].strng )
dim as variable ptr check2 = varMap->find( args[1].strng )
dim as variable p1,p2
if check1 then p1 = *check1 else p1 = args[0]
if check2 then p2 = *check2 else p2 = args[1]
if p1.typ <> p2.typ then return "false"
select case p1.typ
case varType._STRING_ : if strcmp( p1.strng, p2.strng ) = 1 then return "true" else return "false"
case varType._NUMBER_ : if p1.number > p2.number then return "true" else return "false"
case varType._BOOL_ : if p1.bool > p2.bool then return "true" else return "false"
end select
return "false"
end function
function _LESSTHAN( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 2, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr check1 = varMap->find( args[0].strng )
dim as variable ptr check2 = varMap->find( args[1].strng )
dim as variable p1,p2
if check1 then p1 = *check1 else p1 = args[0]
if check2 then p2 = *check2 else p2 = args[1]
if p1.typ <> p2.typ then return "false"
select case p1.typ
case varType._STRING_ : if strcmp( p1.strng, p2.strng ) = -1 then return "true" else return "false"
case varType._NUMBER_ : if p1.number < p2.number then return "true" else return "false"
case varType._BOOL_ : if p1.bool < p2.bool then return "true" else return "false"
end select
return "false"
end function
function _GTEQ( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 2, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr check1 = varMap->find( args[0].strng )
dim as variable ptr check2 = varMap->find( args[1].strng )
dim as variable p1,p2
if check1 then p1 = *check1 else p1 = args[0]
if check2 then p2 = *check2 else p2 = args[1]
if p1.typ <> p2.typ then return "false"
select case p1.typ
case varType._STRING_ : if strcmp( p1.strng, p2.strng ) >= 0 then return "true"
case varType._NUMBER_ : if p1.number >= p2.number then return "true"
case varType._BOOL_ : if p1.bool >= p2.bool then return "true"
end select
return "false"
end function
function _LTEQ( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 2, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr check1 = varMap->find( args[0].strng )
dim as variable ptr check2 = varMap->find( args[1].strng )
dim as variable p1,p2
if check1 then p1 = *check1 else p1 = args[0]
if check2 then p2 = *check2 else p2 = args[1]
if p1.typ <> p2.typ then return "false"
select case p1.typ
case varType._STRING_ : if strcmp( p1.strng, p2.strng ) <= 0 then return "true"
case varType._NUMBER_ : if p1.number <= p2.number then return "true"
case varType._BOOL_ : if p1.bool <= p2.bool then return "true"
end select
return "false"
end function
function _UNEQUAL( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 2, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr check1 = varMap->find( args[0].strng )
dim as variable ptr check2 = varMap->find( args[1].strng )
dim as variable p1,p2
if check1 then p1 = *check1 else p1 = args[0]
if check2 then p2 = *check2 else p2 = args[1]
if p1.typ <> p2.typ then return "true"
select case p1.typ
case varType._STRING_
if p1.strng = p2.strng then return "false"
case varType._NUMBER_
if p1.number = p2.number then return "false"
case varType._BOOL_
if p1.bool = p2.bool then return "false"
end select
return "true"
end function
function _NOT( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 1, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr check1 = varMap->find( args[0].strng )
dim as variable p1
if check1 then p1 = *check1 else p1 = args[0]
select case p1.typ
case varType._NUMBER_
return not( p1.number )
case varType._BOOL_
if p1.bool = JSON_BOOL._TRUE_ then return "false" else return "true"
case else
func_assert( 0, TYPE_MISMATCH, p1.toString )
end select
end function
function _AND( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
for i as integer = 0 to args.size-1
if args[i].bool = JSON_BOOL._FALSE_ then return "false"
next i
return "true"
end function
function _OR( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
for i as integer = 0 to args.size-1
if args[i].bool = JSON_BOOL._TRUE_ then return "true"
next i
return "false"
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' Parsing Functions '
' '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function _TYPEOF( byval varMap as jsb_var_map ptr, byref args as vectorVariable ) as variable
func_assert( args.size = 1, ILLEGAL_FUNCTION_CALL, "Parameter count all wrong" )
dim as variable ptr check1 = varMap->find( args[0].strng )
if check1 then
return DBLQUOTE+ check1->getType +DBLQUOTE
else
return DBLQUOTE+ args[0].getType +DBLQUOTE
endif
end function