Skip to content

Commit

Permalink
move all doom-specific loops out of common code
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoLuis0 authored and madame-rachelle committed Nov 21, 2023
1 parent 71999e7 commit 4ec76af
Show file tree
Hide file tree
Showing 8 changed files with 550 additions and 293 deletions.
332 changes: 108 additions & 224 deletions src/common/scripting/backend/codegen.cpp

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions src/common/scripting/backend/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ enum EFxType
EFX_DoWhileLoop,
EFX_ForLoop,
EFX_ForEachLoop,
EFX_MapForEachLoop,
EFX_BlockForEachLoop,
EFX_CastForEachLoop,
EFX_TwoArgForEachLoop,
EFX_ThreeArgForEachLoop,
EFX_TypedForEachLoop,
EFX_JumpStatement,
EFX_ReturnStatement,
EFX_ClassTypeCast,
Expand Down Expand Up @@ -2073,27 +2073,28 @@ class FxForLoop : public FxLoopStatement

class FxForEachLoop : public FxLoopStatement
{
public:
FName loopVarName;
FxExpression* Array;
FxExpression* Array2;
FxExpression* Array3;
FxExpression* Array4;
FxExpression* Code;

public:
FxForEachLoop(FName vn, FxExpression* arrayvar, FxExpression* arrayvar2, FxExpression* arrayvar3, FxExpression* arrayvar4, FxExpression* code, const FScriptPosition& pos);
~FxForEachLoop();
FxExpression* DoResolve(FCompileContext&);
};

//==========================================================================
//
// FxMapForEachLoop
// FxTwoArgForEachLoop
//
//==========================================================================

class FxMapForEachLoop : public FxExpression
class FxTwoArgForEachLoop : public FxExpression
{
public:
FName keyVarName;
FName valueVarName;
FxExpression* MapExpr;
Expand All @@ -2102,50 +2103,49 @@ class FxMapForEachLoop : public FxExpression
FxExpression* MapExpr4;
FxExpression* Code;

public:
FxMapForEachLoop(FName kv, FName vv, FxExpression* mapexpr, FxExpression* mapexpr2, FxExpression* mapexpr3, FxExpression* mapexpr4, FxExpression* code, const FScriptPosition& pos);
~FxMapForEachLoop();
FxTwoArgForEachLoop(FName kv, FName vv, FxExpression* mapexpr, FxExpression* mapexpr2, FxExpression* mapexpr3, FxExpression* mapexpr4, FxExpression* code, const FScriptPosition& pos);
~FxTwoArgForEachLoop();
FxExpression *Resolve(FCompileContext&);
//ExpEmit Emit(VMFunctionBuilder *build); This node is transformed, so it won't ever be emitted itself
};

//==========================================================================
//
// FxBlockIteratorForEachLoop
// FxThreeArgForEachLoop
//
//==========================================================================

class FxBlockIteratorForEachLoop : public FxExpression
class FxThreeArgForEachLoop : public FxExpression
{
public:
FName varVarName;
FName posVarName;
FName flagsVarName;
FxExpression* BlockIteratorExpr;
FxExpression* Code;

public:
FxBlockIteratorForEachLoop(FName vv, FName pv, FName fv, FxExpression* blockiteartorexpr, FxExpression* code, const FScriptPosition& pos);
~FxBlockIteratorForEachLoop();
FxThreeArgForEachLoop(FName vv, FName pv, FName fv, FxExpression* blockiteartorexpr, FxExpression* code, const FScriptPosition& pos);
~FxThreeArgForEachLoop();
FxExpression *Resolve(FCompileContext&);
//ExpEmit Emit(VMFunctionBuilder *build); This node is transformed, so it won't ever be emitted itself
};

//==========================================================================
//
// FxCastForEachLoop
// FxTypedForEachLoop
//
//==========================================================================

class FxCastForEachLoop : public FxExpression
class FxTypedForEachLoop : public FxExpression
{
FName castClassName;
FName varVarName;
FxExpression* CastIteratorExpr;
public:
FName className;
FName varName;
FxExpression* Expr;
FxExpression* Code;

public:
FxCastForEachLoop(FName cv, FName vv, FxExpression* castiteartorexpr, FxExpression* code, const FScriptPosition& pos);
~FxCastForEachLoop();
FxTypedForEachLoop(FName cv, FName vv, FxExpression* castiteartorexpr, FxExpression* code, const FScriptPosition& pos);
~FxTypedForEachLoop();
FxExpression *Resolve(FCompileContext&);
//ExpEmit Emit(VMFunctionBuilder *build); This node is transformed, so it won't ever be emitted itself
};
Expand Down
22 changes: 11 additions & 11 deletions src/common/scripting/frontend/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,9 +983,9 @@ static void PrintArrayIterationStmt(FLispString &out, const ZCC_TreeNode *node)
out.Close();
}

static void PrintMapIterationStmt(FLispString &out, const ZCC_TreeNode *node)
static void PrintTwoArgIterationStmt(FLispString &out, const ZCC_TreeNode *node)
{
auto inode = (ZCC_MapIterationStmt *)node;
auto inode = (ZCC_TwoArgIterationStmt *)node;
out.Break();
out.Open("map-iteration-stmt");
PrintVarName(out, inode->ItKey);
Expand All @@ -998,9 +998,9 @@ static void PrintMapIterationStmt(FLispString &out, const ZCC_TreeNode *node)
out.Close();
}

static void PrintBlockIterationStmt(FLispString &out, const ZCC_TreeNode *node)
static void PrintThreeArgIterationStmt(FLispString &out, const ZCC_TreeNode *node)
{
auto inode = (ZCC_BlockIterationStmt *)node;
auto inode = (ZCC_ThreeArgIterationStmt *)node;
out.Break();
out.Open("block-iteration-stmt");
PrintVarName(out, inode->ItVar);
Expand All @@ -1015,16 +1015,16 @@ static void PrintBlockIterationStmt(FLispString &out, const ZCC_TreeNode *node)
out.Close();
}

static void PrintCastIterationStmt(FLispString &out, const ZCC_TreeNode *node)
static void PrintTypedIterationStmt(FLispString &out, const ZCC_TreeNode *node)
{
auto inode = (ZCC_CastIterationStmt *)node;
auto inode = (ZCC_TypedIterationStmt *)node;
out.Break();
out.Open("cast-iteration-stmt");
PrintVarName(out, inode->ItCast);
PrintVarName(out, inode->ItType);
out.Break();
PrintVarName(out, inode->ItVar);
out.Break();
PrintNodes(out, inode->ItIterator);
PrintNodes(out, inode->ItExpr);
out.Break();
PrintNodes(out, inode->LoopStatement);
out.Close();
Expand Down Expand Up @@ -1097,9 +1097,9 @@ static const NodePrinterFunc TreeNodePrinter[] =
PrintMixinDef,
PrintMixinStmt,
PrintArrayIterationStmt,
PrintMapIterationStmt,
PrintBlockIterationStmt,
PrintCastIterationStmt,
PrintTwoArgIterationStmt,
PrintThreeArgIterationStmt,
PrintTypedIterationStmt,
};

FString ZCC_PrintAST(const ZCC_TreeNode *root)
Expand Down
28 changes: 14 additions & 14 deletions src/common/scripting/frontend/zcc-parse.lemon
Original file line number Diff line number Diff line change
Expand Up @@ -1956,9 +1956,9 @@ statement(X) ::= expression_statement(A) SEMICOLON. { X = A; /*X-overwrites-A*/
statement(X) ::= selection_statement(X).
statement(X) ::= iteration_statement(X).
statement(X) ::= array_iteration_statement(X).
statement(X) ::= map_iteration_statement(X).
statement(X) ::= block_iteration_statement(X).
statement(X) ::= cast_iteration_statement(X).
statement(X) ::= two_arg_iteration_statement(X).
statement(X) ::= three_arg_iteration_statement(X).
statement(X) ::= typed_iteration_statement(X).
statement(X) ::= jump_statement(X).
statement(X) ::= assign_statement(A) SEMICOLON. { X = A; /*X-overwrites-A*/ }
statement(X) ::= assign_decl_statement(A) SEMICOLON.{ X = A; /*X-overwrites-A*/ }
Expand Down Expand Up @@ -2128,23 +2128,23 @@ array_iteration_statement(X) ::= FOREACH(T) LPAREN variable_name(IN) COLON expr(
X = iter;
}

%type map_iteration_statement{ZCC_Statement *}
%type two_arg_iteration_statement{ZCC_Statement *}

map_iteration_statement(X) ::= FOREACH(T) LPAREN variable_name(KEY) COMMA variable_name(VAL) COLON expr(EX) RPAREN statement(ST).
two_arg_iteration_statement(X) ::= FOREACH(T) LPAREN variable_name(KEY) COMMA variable_name(VAL) COLON expr(EX) RPAREN statement(ST).
{
NEW_AST_NODE(MapIterationStmt, iter, T);
NEW_AST_NODE(TwoArgIterationStmt, iter, T);
iter->ItKey = KEY;
iter->ItValue = VAL;
iter->ItMap = EX;
iter->LoopStatement = ST;
X = iter;
}

%type block_iteration_statement{ZCC_Statement *}
%type three_arg_iteration_statement{ZCC_Statement *}

block_iteration_statement(X) ::= FOREACH(T) LPAREN variable_name(VAR) COMMA variable_name(POS) COMMA variable_name(FLAGS) COLON expr(EX) RPAREN statement(ST).
three_arg_iteration_statement(X) ::= FOREACH(T) LPAREN variable_name(VAR) COMMA variable_name(POS) COMMA variable_name(FLAGS) COLON expr(EX) RPAREN statement(ST).
{
NEW_AST_NODE(BlockIterationStmt, iter, T);
NEW_AST_NODE(ThreeArgIterationStmt, iter, T);
iter->ItVar = VAR;
iter->ItPos = POS;
iter->ItFlags = FLAGS;
Expand All @@ -2153,14 +2153,14 @@ block_iteration_statement(X) ::= FOREACH(T) LPAREN variable_name(VAR) COMMA vari
X = iter;
}

%type cast_iteration_statement{ZCC_Statement *}
%type typed_iteration_statement{ZCC_Statement *}

cast_iteration_statement(X) ::= FOREACH(T) LPAREN variable_name(CAST) variable_name(VAR) COLON expr(EX) RPAREN statement(ST).
typed_iteration_statement(X) ::= FOREACH(T) LPAREN variable_name(TYPE) variable_name(VAR) COLON expr(EX) RPAREN statement(ST).
{
NEW_AST_NODE(CastIterationStmt, iter, T);
iter->ItCast = CAST;
NEW_AST_NODE(TypedIterationStmt, iter, T);
iter->ItType = TYPE;
iter->ItVar = VAR;
iter->ItIterator = EX;
iter->ItExpr = EX;
iter->LoopStatement = ST;
X = iter;
}
Expand Down
22 changes: 11 additions & 11 deletions src/common/scripting/frontend/zcc_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3395,38 +3395,38 @@ FxExpression *ZCCCompiler::ConvertNode(ZCC_TreeNode *ast, bool substitute)
return new FxForEachLoop(iter->ItName->Name, itArray, itArray2, itArray3, itArray4, body, *ast);
}

case AST_MapIterationStmt:
case AST_TwoArgIterationStmt:
{
auto iter = static_cast<ZCC_MapIterationStmt*>(ast);
auto iter = static_cast<ZCC_TwoArgIterationStmt*>(ast);
auto key = iter->ItKey->Name;
auto var = iter->ItValue->Name;
FxExpression* const itMap = ConvertNode(iter->ItMap);
FxExpression* const itMap2 = ConvertNode(iter->ItMap);
FxExpression* const itMap3 = ConvertNode(iter->ItMap);
FxExpression* const itMap4 = ConvertNode(iter->ItMap);
FxExpression* const body = ConvertImplicitScopeNode(ast, iter->LoopStatement);
return new FxMapForEachLoop(key, var, itMap, itMap2, itMap3, itMap4, body, *ast);
return new FxTwoArgForEachLoop(key, var, itMap, itMap2, itMap3, itMap4, body, *ast);
}

case AST_BlockIterationStmt:
case AST_ThreeArgIterationStmt:
{
auto iter = static_cast<ZCC_BlockIterationStmt*>(ast);
auto iter = static_cast<ZCC_ThreeArgIterationStmt*>(ast);
auto var = iter->ItVar->Name;
auto pos = iter->ItPos->Name;
auto flags = iter->ItFlags->Name;
FxExpression* const itBlock = ConvertNode(iter->ItBlock);
FxExpression* const body = ConvertImplicitScopeNode(ast, iter->LoopStatement);
return new FxBlockIteratorForEachLoop(var, pos, flags, itBlock, body, *ast);
return new FxThreeArgForEachLoop(var, pos, flags, itBlock, body, *ast);
}

case AST_CastIterationStmt:
case AST_TypedIterationStmt:
{
auto iter = static_cast<ZCC_CastIterationStmt*>(ast);
auto cls = iter->ItCast->Name;
auto iter = static_cast<ZCC_TypedIterationStmt*>(ast);
auto cls = iter->ItType->Name;
auto var = iter->ItVar->Name;
FxExpression* const itIterator = ConvertNode(iter->ItIterator);
FxExpression* const itExpr = ConvertNode(iter->ItExpr);
FxExpression* const body = ConvertImplicitScopeNode(ast, iter->LoopStatement);
return new FxCastForEachLoop(cls, var, itIterator, body, *ast);
return new FxTypedForEachLoop(cls, var, itExpr, body, *ast);
}

case AST_IterationStmt:
Expand Down
33 changes: 30 additions & 3 deletions src/common/scripting/frontend/zcc_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1177,11 +1177,11 @@ ZCC_TreeNode *TreeNodeDeepCopy_Internal(ZCC_AST *ast, ZCC_TreeNode *orig, bool c
break;
}

case AST_MapIterationStmt:
case AST_TwoArgIterationStmt:
{
TreeNodeDeepCopy_Start(MapIterationStmt);
TreeNodeDeepCopy_Start(TwoArgIterationStmt);

// ZCC_MapIterationStmt
// ZCC_TwoArgIterationStmt
copy->ItKey = static_cast<ZCC_VarName*>(TreeNodeDeepCopy_Internal(ast, origCasted->ItKey, true, copiedNodesList));
copy->ItValue = static_cast<ZCC_VarName*>(TreeNodeDeepCopy_Internal(ast, origCasted->ItValue, true, copiedNodesList));
copy->LoopStatement = static_cast<ZCC_Statement*>(TreeNodeDeepCopy_Internal(ast, origCasted->LoopStatement, true, copiedNodesList));
Expand All @@ -1190,6 +1190,33 @@ ZCC_TreeNode *TreeNodeDeepCopy_Internal(ZCC_AST *ast, ZCC_TreeNode *orig, bool c
break;
}

case AST_ThreeArgIterationStmt:
{
TreeNodeDeepCopy_Start(ThreeArgIterationStmt);

// ZCC_TwoArgIterationStmt
copy->ItVar = static_cast<ZCC_VarName*>(TreeNodeDeepCopy_Internal(ast, origCasted->ItVar, true, copiedNodesList));
copy->ItPos = static_cast<ZCC_VarName*>(TreeNodeDeepCopy_Internal(ast, origCasted->ItPos, true, copiedNodesList));
copy->ItFlags = static_cast<ZCC_VarName*>(TreeNodeDeepCopy_Internal(ast, origCasted->ItFlags, true, copiedNodesList));
copy->LoopStatement = static_cast<ZCC_Statement*>(TreeNodeDeepCopy_Internal(ast, origCasted->LoopStatement, true, copiedNodesList));
copy->ItBlock = static_cast<ZCC_Expression*>(TreeNodeDeepCopy_Internal(ast, origCasted->ItBlock, true, copiedNodesList));

break;
}

case AST_TypedIterationStmt:
{
TreeNodeDeepCopy_Start(TypedIterationStmt);

// ZCC_TwoArgIterationStmt
copy->ItType = static_cast<ZCC_VarName*>(TreeNodeDeepCopy_Internal(ast, origCasted->ItType, true, copiedNodesList));
copy->ItVar = static_cast<ZCC_VarName*>(TreeNodeDeepCopy_Internal(ast, origCasted->ItVar, true, copiedNodesList));
copy->LoopStatement = static_cast<ZCC_Statement*>(TreeNodeDeepCopy_Internal(ast, origCasted->LoopStatement, true, copiedNodesList));
copy->ItExpr = static_cast<ZCC_Expression*>(TreeNodeDeepCopy_Internal(ast, origCasted->ItExpr, true, copiedNodesList));

break;
}

case AST_IfStmt:
{
TreeNodeDeepCopy_Start(IfStmt);
Expand Down
16 changes: 8 additions & 8 deletions src/common/scripting/frontend/zcc_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ enum EZCCTreeNodeType
AST_MixinDef,
AST_MixinStmt,
AST_ArrayIterationStmt,
AST_MapIterationStmt,
AST_BlockIterationStmt,
AST_CastIterationStmt,
AST_TwoArgIterationStmt,
AST_ThreeArgIterationStmt,
AST_TypedIterationStmt,

NUM_AST_NODE_TYPES
};
Expand Down Expand Up @@ -535,15 +535,15 @@ struct ZCC_ArrayIterationStmt : ZCC_Statement
ZCC_Statement* LoopStatement;
};

struct ZCC_MapIterationStmt : ZCC_Statement
struct ZCC_TwoArgIterationStmt : ZCC_Statement
{
ZCC_VarName* ItKey;
ZCC_VarName* ItValue;
ZCC_Expression* ItMap;
ZCC_Statement* LoopStatement;
};

struct ZCC_BlockIterationStmt : ZCC_Statement
struct ZCC_ThreeArgIterationStmt : ZCC_Statement
{
ZCC_VarName* ItVar;
ZCC_VarName* ItPos;
Expand All @@ -552,11 +552,11 @@ struct ZCC_BlockIterationStmt : ZCC_Statement
ZCC_Statement* LoopStatement;
};

struct ZCC_CastIterationStmt : ZCC_Statement
struct ZCC_TypedIterationStmt : ZCC_Statement
{
ZCC_VarName* ItCast;
ZCC_VarName* ItType;
ZCC_VarName* ItVar;
ZCC_Expression* ItIterator;
ZCC_Expression* ItExpr;
ZCC_Statement* LoopStatement;
};

Expand Down
Loading

0 comments on commit 4ec76af

Please sign in to comment.