Skip to content

Commit

Permalink
fix(zscript): allow simple types to match a template array type, for now
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Apr 5, 2024
1 parent 11b834b commit 08756b8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 35 deletions.
18 changes: 14 additions & 4 deletions src/parser/Scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,20 +553,30 @@ static int applyTemplateTypes(
if (!simpleType || simpleType->getId() != ZTID_TEMPLATE_T_ARR)
continue;

if (!parameter_types[i]->isArray())
bool is_valid_array_type = parameter_types[i]->isArray();

// This does not need to be configurable yet, as there is no way to pass around array
// types until we support arrays as parameters. So most scripts would need this on right now.
bool allow_old_ptr_compat = true;
if (allow_old_ptr_compat)
is_valid_array_type = true;

if (!is_valid_array_type)
return APPLY_TEMPLATE_RET_UNSATISFIABLE;

auto arr_type = dynamic_cast<const DataTypeArray*>(parameter_types[i]);
auto el_type = parameter_types[i]->isArray() ?
&dynamic_cast<const DataTypeArray*>(parameter_types[i])->getElementType() :
parameter_types[i];

found_template_type = true;
if (!bound_t)
{
bound_t = &arr_type->getElementType();
bound_t = el_type;
resolved_params[i] = parameter_types[i];
continue;
}

if (!arr_type->getElementType().canCastTo(*bound_t))
if (!el_type->canCastTo(*bound_t))
return APPLY_TEMPLATE_RET_UNSATISFIABLE;

resolved_params[i] = parameter_types[i];
Expand Down
27 changes: 0 additions & 27 deletions src/parser/symbols/GlobalSymbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ static AccessorTable GlobalTable[] =
{ "SwapTile", 0, ZTID_VOID, -1, 0, { ZTID_FLOAT, ZTID_FLOAT },{} },
{ "ClearTile", 0, ZTID_VOID, -1, 0, { ZTID_FLOAT },{} },
{ "SizeOfArray", 0, ZTID_FLOAT, -1, 0, { ZTID_TEMPLATE_T_ARR },{} },
// TODO: for compat, until we work out how best to handle proper strings
{ "SizeOfArray", 1, ZTID_FLOAT, -1, 0, { ZTID_CHAR },{} },
{ "ResizeArray", 0, ZTID_VOID, -1, 0, { ZTID_TEMPLATE_T_ARR, ZTID_FLOAT },{} },
// TODO: for compat, until we work out how best to handle proper strings
{ "ResizeArray", 1, ZTID_VOID, -1, 0, { ZTID_CHAR, ZTID_FLOAT },{} },
{ "OwnArray", 0, ZTID_VOID, -1, 0, { ZTID_TEMPLATE_T_ARR },{} },
{ "DestroyArray", 0, ZTID_VOID, -1, 0, { ZTID_TEMPLATE_T_ARR },{} },
{ "OwnObject", 0, ZTID_VOID, -1, 0, { ZTID_UNTYPED },{} },
Expand Down Expand Up @@ -955,17 +951,6 @@ void GlobalSymbols::generateCode()
RETURN();
function->giveCode(code);
}
//int32_t SizeOfArray(char32 val)
{
Function* function = getFunction("SizeOfArray", 1);
int32_t label = function->getLabel();
vector<shared_ptr<Opcode>> code;
addOpcode2 (code, new OPopRegister(new VarArgument(EXP1)));
LABELBACK(label);
addOpcode2 (code, new OArraySize(new VarArgument(EXP1)));
RETURN();
function->giveCode(code);
}
//void ResizeArray(T[] ptr, int sz)
{
Function* function = getFunction("ResizeArray");
Expand All @@ -978,18 +963,6 @@ void GlobalSymbols::generateCode()
RETURN();
function->giveCode(code);
}
//void ResizeArray(char32 ptr, int sz)
{
Function* function = getFunction("ResizeArray", 1);
int32_t label = function->getLabel();
vector<shared_ptr<Opcode>> code;
addOpcode2 (code, new OPopRegister(new VarArgument(EXP2)));
LABELBACK(label);
addOpcode2 (code, new OPopRegister(new VarArgument(EXP1)));
addOpcode2 (code, new OResizeArrayRegister(new VarArgument(EXP1),new VarArgument(EXP2)));
RETURN();
function->giveCode(code);
}
//void OwnArray(T[] ptr)
{
Function* function = getFunction("OwnArray");
Expand Down
11 changes: 10 additions & 1 deletion tests/scripts/errors/errors_4.zs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ global script Global
ArrayPushBack(numbers, obj);
ArrayPushBack(numbers, ""); // TODO: this should be invalid (but requires a big change for strings, should add a string type).
ArrayPushBack(numbers, 1L); // TODO: this should be invalid (else: we perform an implicit cast and multiply by 10000...)
SizeOfArray(Hero);
SizeOfArray(Hero); // TODO: this is currently allowed as a side effect of the "old ptr compat" in `applyTemplateTypes`
Object choice_1 = Choose(1, 2, 3);
auto choice_2 = Choose(1, 2, obj);
auto max_1 = Max(1, 2, obj);
Expand All @@ -34,4 +34,13 @@ global script Global
max_2 = Max(1, 2, 3);
max_2 = Max(1, 2, 3, Max(3, 4));
}

// The template checking code explicitly allows char32 as an "array" type,
// for compat.
void old_ptrs(char32 ptr)
{
// OK
ArrayPushBack(ptr, 1);
ResizeArray(ptr, 1);
}
}
1 change: 0 additions & 1 deletion tests/scripts/errors/errors_4_expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Pass 3: Registration
Pass 4: Analyzing Code
ZQ_BUFFER Line 14 @ Columns 10-36 - Error T017: Cannot cast from int to Object.
ZQ_BUFFER Line 15 @ Columns 3-30 - Error T021: Function ArrayPushBack(int[], Object) has not been declared.
ZQ_BUFFER Line 18 @ Columns 3-20 - Error T021: Function SizeOfArray(Player) has not been declared.
ZQ_BUFFER Line 19 @ Columns 10-36 - Error T017: Cannot cast from int to Object.
ZQ_BUFFER Line 20 @ Columns 19-36 - Error T021: Function Choose(int, int, Object) has not been declared.
ZQ_BUFFER Line 21 @ Columns 16-30 - Error T021: Function Max(int, int, Object) has not been declared.
Expand Down
4 changes: 2 additions & 2 deletions tests/scripts/websocket_expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ COMPAREV d2,0.0002
SETCMP d2,I==
COMPAREV d2,0
GOTOCMP 211,==; Test 'if'
LOAD d2,0.0005; InlineFunc[int SizeOfArray(char32)] Params AND if() #188 Body Start
ARRAYSIZE d2; InlineFunc[int SizeOfArray(char32)] Body
LOAD d2,0.0005; InlineFunc[int SizeOfArray(int)] Params AND if() #188 Body Start
ARRAYSIZE d2; InlineFunc[int SizeOfArray(int)] Body
STORE d2,0.0003
ALLOCATEMEMV d2,34,0
STORE d2,0.0002
Expand Down

0 comments on commit 08756b8

Please sign in to comment.