added class pointer casts. Due to grammar problems the type has to be put into parentheses to get the class token out of the global parsing namespace:

class<Actor> myclass = (class<Actor>)(GetClass());
This commit is contained in:
Christoph Oelckers 2016-11-17 20:31:53 +01:00
commit 3bcd85eb8a
8 changed files with 167 additions and 0 deletions

View file

@ -585,6 +585,16 @@ static void PrintExprFuncCall(FLispString &out, ZCC_TreeNode *node)
out.Close();
}
static void PrintExprClassCast(FLispString &out, ZCC_TreeNode *node)
{
ZCC_ClassCast *enode = (ZCC_ClassCast *)node;
assert(enode->Operation == PEX_ClassCast);
out.Open("expr-class-cast");
out.AddName(enode->ClassName);
PrintNodes(out, enode->Parameters, false);
out.Close();
}
static void PrintExprMemberAccess(FLispString &out, ZCC_TreeNode *node)
{
ZCC_ExprMemberAccess *enode = (ZCC_ExprMemberAccess *)node;
@ -913,6 +923,7 @@ void (* const TreeNodePrinter[NUM_AST_NODE_TYPES])(FLispString &, ZCC_TreeNode *
PrintPropertyStmt,
PrintVectorInitializer,
PrintDeclFlags,
PrintExprClassCast,
};
FString ZCC_PrintAST(ZCC_TreeNode *root)

View file

@ -1080,6 +1080,14 @@ primary(X) ::= primary(A) LPAREN func_expr_list(B) RPAREN. [DOT] // Function ca
expr->Parameters = B;
X = expr;
}
primary(X) ::= LPAREN CLASS LT IDENTIFIER(A) GT RPAREN LPAREN func_expr_list(B) RPAREN. [DOT] // class type cast
{
NEW_AST_NODE(ClassCast, expr, A);
expr->Operation = PEX_ClassCast;
expr->ClassName = ENamedName(A.Int);
expr->Parameters = B;
X = expr;
}
primary(X) ::= primary(A) LBRACKET expr(B) RBRACKET. [DOT] // Array access
{
NEW_AST_NODE(ExprBinary, expr, B);

View file

@ -2766,6 +2766,24 @@ FxExpression *ZCCCompiler::ConvertNode(ZCC_TreeNode *ast)
break;
}
case AST_ClassCast:
{
auto cc = static_cast<ZCC_ClassCast *>(ast);
if (cc->Parameters == nullptr || cc->Parameters->SiblingNext != cc->Parameters)
{
Error(cc, "Class type cast requires exactly one parameter");
return new FxNop(*ast); // return something so that the compiler can continue.
}
auto cls = PClass::FindClass(cc->ClassName);
if (cls == nullptr)
{
Error(cc, "Unknown class %s", FName(cc->ClassName).GetChars());
return new FxNop(*ast); // return something so that the compiler can continue.
}
return new FxClassPtrCast(cls, ConvertNode(cc->Parameters));
}
case AST_ExprMemberAccess:
{
auto memaccess = static_cast<ZCC_ExprMemberAccess *>(ast);

View file

@ -8,6 +8,7 @@ xx(ConstValue, TK_Const)
xx(FuncCall, '(')
xx(ArrayAccess, TK_Array)
xx(MemberAccess, '.')
xx(ClassCast, TK_Class)
xx(TypeRef, TK_Class)
xx(Vector, TK_Vector2)

View file

@ -102,6 +102,7 @@ enum EZCCTreeNodeType
AST_PropertyStmt,
AST_VectorValue,
AST_DeclFlags,
AST_ClassCast,
NUM_AST_NODE_TYPES
};
@ -366,6 +367,12 @@ struct ZCC_ExprFuncCall : ZCC_Expression
ZCC_FuncParm *Parameters;
};
struct ZCC_ClassCast : ZCC_Expression
{
ENamedName ClassName;
ZCC_FuncParm *Parameters;
};
struct ZCC_ExprMemberAccess : ZCC_Expression
{
ZCC_Expression *Left;