Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding foreach support on C# scripting #65

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CSharpVersion/CSharp_CPPCompiler/CompilerProject/Dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,10 @@ void Statement::dumpStatement(bool processBrother) {
}
} else { // Finally
hasFinally = true;
tab(); printf("FINALLY\n");
if (!firstCatch) {
// This line should not be executed for try-finally (no catches) pattern.
tab(); printf("FINALLY\n");
}
}
p->m_pChild->dumpStatement();

Expand Down
91 changes: 86 additions & 5 deletions CSharpVersion/CSharp_CPPCompiler/CompilerProject/csharp.y
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

// Prototypes to keep the compiler happy
void yyerror (const char *error);
const char* makeUniqueId(const char* body);

extern "C" {
int yyparse(void);
Expand Down Expand Up @@ -697,11 +698,84 @@ statement_expression_list
;
foreach_statement
: FOREACH '(' type IDENTIFIER IN expression ')' embedded_statement
{ R(); $$.statement = CreateStatement(STM_FOREACH, NULL, NULL)
->setExpression($6.expr)
->addType($3.type)
->addChild($8.statement);
compilerError(ERR_NOT_SUPPORTED_YET, "Foreach not supported yet. Please use 'for' with array or list.");
{
R();
const char* enumVar = makeUniqueId("enumerator");
PushGenericType(GetGenericType());
addGenericType($3.type);
// NOTE: Requires System.Collections.Generic;
TypeObject* enumType =
TypeObject::getTypeObject("IEnumerator", TYPE_UNRESOLVED)
->setGeneric(GetGenericType());
PopGenericType();

$$.statement =
/* IEnumerator<T> enumerator = $6.GetEnumerator() */
CreateVarStatement(
STM_LOCALVAR,
NULL, // next
NULL, // child
CreateVarInstance(
enumVar
)->setInitializer(
CreateDoubleExpr(
EXPR_INVOKE,
CreateSingleExpr(EXPR_DOT, $6.expr)->setIdentifier("GetEnumerator"),
NULL
)->patchSubInvoke()
),
enumType
) ->addNext(
CreateStatement(STM_TRY, NULL, NULL)
->addChild(
CreateStatement(
STM_BLOCK,
NULL,
CreateStatement(
STM_WHILE,
NULL,
CreateStatement(
STM_BLOCK,
NULL,
CreateVarStatement(
STM_LOCALVAR,
$8.statement, // next
NULL, // child
CreateVarInstance(
$4.text
)->setInitializer(
CreateSingleExpr(EXPR_DOT, CreateLeafExpr(EXPR_IDENT, enumVar))->setIdentifier("Current")
),
$3.type
)
)
) -> setExpression(
/* enumerator.MoveNext() */
CreateDoubleExpr(
EXPR_INVOKE,
CreateSingleExpr(EXPR_DOT, CreateLeafExpr(EXPR_IDENT, enumVar))->setIdentifier("MoveNext"),
NULL // no args
)->patchSubInvoke()
)
)
) -> addNext (
CreateStatement(STM_FINALLY, NULL, NULL)
->addChild(
CreateStatement(
STM_BLOCK,
NULL,
CreateStatement(STM_WRAP_EXP, NULL, NULL) -> setExpression(
/* enumerator.Dispose() */
CreateDoubleExpr(
EXPR_INVOKE,
CreateSingleExpr(EXPR_DOT, CreateLeafExpr(EXPR_IDENT, enumVar))->setIdentifier("Dispose"),
NULL // no args
)->patchSubInvoke()
)
)
)
)
);
}
;
jump_statement
Expand Down Expand Up @@ -1525,3 +1599,10 @@ void error (const char* msg,...) {

printf(log);
}

int uniqueIdCount = 0;
const char* makeUniqueId(const char* body) {
char buf[10];
_itoa(uniqueIdCount++, buf, 10);
return concat3("__", body, buf);
}
Loading