- added automatic type deduction capabilities for local variables. If you type 'let variable = value;' the type of 'variable' will be deduced from the given value. This is mostly of interest for type casting pointers, because 'let p = Classtype(objectvar);' does not require writing the class type name twice.
This commit is contained in:
parent
67bdac5227
commit
f722967abe
10 changed files with 42 additions and 3 deletions
|
|
@ -9964,17 +9964,43 @@ FxExpression *FxLocalVariableDeclaration::Resolve(FCompileContext &ctx)
|
|||
delete this;
|
||||
return nullptr;
|
||||
}
|
||||
if (ValueType->RegType == REGT_NIL)
|
||||
if (ValueType->RegType == REGT_NIL && ValueType != TypeAuto)
|
||||
{
|
||||
auto sfunc = static_cast<VMScriptFunction *>(ctx.Function->Variants[0].Implementation);
|
||||
StackOffset = sfunc->AllocExtraStack(ValueType);
|
||||
// Todo: Process the compound initializer once implemented.
|
||||
if (Init != nullptr)
|
||||
{
|
||||
ScriptPosition.Message(MSG_ERROR, "Cannot initialize non-scalar variable %s here", Name.GetChars());
|
||||
delete this;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (ValueType !=TypeAuto)
|
||||
{
|
||||
if (Init) Init = new FxTypeCast(Init, ValueType, false);
|
||||
SAFE_RESOLVE_OPT(Init, ctx);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Init == nullptr)
|
||||
{
|
||||
ScriptPosition.Message(MSG_ERROR, "Automatic type deduction requires an initializer for variable %s", Name.GetChars());
|
||||
delete this;
|
||||
return nullptr;
|
||||
}
|
||||
SAFE_RESOLVE_OPT(Init, ctx);
|
||||
if (Init->ValueType->RegType == REGT_NIL)
|
||||
{
|
||||
ScriptPosition.Message(MSG_ERROR, "Cannot initialize non-scalar variable %s here", Name.GetChars());
|
||||
delete this;
|
||||
return nullptr;
|
||||
}
|
||||
ValueType = Init->ValueType;
|
||||
// check for undersized ints and floats. These are not allowed as local variables.
|
||||
if (IsInteger() && ValueType->Align < sizeof(int)) ValueType = TypeSInt32;
|
||||
else if (IsFloat() && ValueType->Align < sizeof(double)) ValueType = TypeFloat64;
|
||||
}
|
||||
if (Name != NAME_None)
|
||||
{
|
||||
for (auto l : ctx.Block->LocalVars)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue