Skip to content

Commit

Permalink
MetaProcessor
Browse files Browse the repository at this point in the history
- Cleaned up
  • Loading branch information
deavmi committed May 8, 2024
1 parent 7e1caad commit fc1135a
Showing 1 changed file with 41 additions and 139 deletions.
180 changes: 41 additions & 139 deletions source/tlang/compiler/typecheck/meta.d
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,6 @@ public class MetaProcessor
}
}

// FIXME: This should climb upwards and search for the stuff
private AliasDeclaration[] obtainAliases(Container container)
{
AliasDeclaration[] aliases;
foreach(Statement stmt; container.getStatements())
{
AliasDeclaration aliasDecl = cast(AliasDeclaration)stmt;
if(aliasDecl)
{
DEBUG(format("Found an alias declared as '%s'", aliasDecl));
aliases ~= aliasDecl;
}
}

return aliases;
}

import niknaks.arrays : filter;
import niknaks.functional : predicateOf, Predicate;
import tlang.compiler.symbols.data : VariableExpression;
Expand All @@ -165,139 +148,58 @@ public class MetaProcessor

DEBUG(format("All aliases available from (cntnr:%s, stmt=%s) upwards: %s", container, curStmt, declaredAliases));

if(!oldCode)
// Find any VariableExpression(s) from curStmt (TODO: should be container or nah?)
MStatementSearchable searchableStmt = cast(MStatementSearchable)curStmt;
if(searchableStmt)
{
// Find any VariableExpression(s) from curStmt (TODO: should be container or nah?)
MStatementSearchable searchableStmt = cast(MStatementSearchable)curStmt;
if(searchableStmt)
{
VariableExpression[] foundStmts = cast(VariableExpression[])searchableStmt.search(VariableExpression.classinfo);
VariableExpression[] foundStmts = cast(VariableExpression[])searchableStmt.search(VariableExpression.classinfo);

// Now, for all VariableExpressions, do
foreach(VariableExpression varExp; foundStmts)
// Now, for all VariableExpressions, do
foreach(VariableExpression varExp; foundStmts)
{
// Extract the name/referent, then match all aliases
// that have the same name
// (these should have been generated from closest to
// furthest when obtained from the resolver, so any
// tie breaking would be the logically closest
// alias with the same name)
AliasDeclaration[] matched;
string varExpIdent = varExp.getName();
bool filterAliasesToName(AliasDeclaration aliasDecl)
{
// Extract the name/referent, then match all aliases
// that have the same name
// (these should have been generated from closest to
// furthest when obtained from the resolver, so any
// tie breaking would be the logically closest
// alias with the same name)
AliasDeclaration[] matched;
string varExpIdent = varExp.getName();
bool filterAliasesToName(AliasDeclaration aliasDecl)
{
return cmp(aliasDecl.getName(), varExpIdent) == 0;
}
filter!(AliasDeclaration)(declaredAliases, predicateOf!(filterAliasesToName), matched);
return cmp(aliasDecl.getName(), varExpIdent) == 0;
}
filter!(AliasDeclaration)(declaredAliases, predicateOf!(filterAliasesToName), matched);

DEBUG(format("Matched aliases for VarExp '%s': %s", varExpIdent, matched));
DEBUG(format("Matched aliases for VarExp '%s': %s", varExpIdent, matched));

// If there is no match then it isn't an alias referent
// hence we only care IF it IS an alias referent
if(matched.length)
{
// Nearest matched alias
AliasDeclaration nearestAlias = matched[0];

// Now extract the alias's expression and clone it
// and make its parent the VariableExpression's
// (as it will take its exact place)
MCloneable cloneableExpr = cast(MCloneable)nearestAlias.getExpr();
assert(cloneableExpr);
Expression clonedExpr = cast(Expression)cloneableExpr.clone(varExp.parentOf());

// Now, from the current container, replace the
// VariableExpression with the cloned expression
MStatementReplaceable containerRepl = cast(MStatementReplaceable)container;
assert(containerRepl);
assert(containerRepl.replace(varExp, clonedExpr));
// If there is no match then it isn't an alias referent
// hence we only care IF it IS an alias referent
if(matched.length)
{
// Nearest matched alias
AliasDeclaration nearestAlias = matched[0];

// Now extract the alias's expression and clone it
// and make its parent the VariableExpression's
// (as it will take its exact place)
MCloneable cloneableExpr = cast(MCloneable)nearestAlias.getExpr();
assert(cloneableExpr);
Expression clonedExpr = cast(Expression)cloneableExpr.clone(varExp.parentOf());

// Now, from the current container, replace the
// VariableExpression with the cloned expression
MStatementReplaceable containerRepl = cast(MStatementReplaceable)container;
assert(containerRepl);
assert(containerRepl.replace(varExp, clonedExpr));

}
}
}
else
{
DEBUG("Skipping non MStatementSearchable node");
}
}
else
{
// TODO: After finding we must then remove the aliss declaration itself
DEBUG(format("doAliasExpression(%s, %s): Enter", container, curStmt));

/**
* Search for any `VariableExpression` expressions
* and replace them with an `Expression` derived
* from one of our aliases
*/
if(cast(MStatementSearchable)curStmt && cast(MStatementReplaceable)curStmt)
{
MStatementSearchable searchableStmt = cast(MStatementSearchable)curStmt;
VariableExpression[] foundStmts = cast(VariableExpression[])searchableStmt.search(VariableExpression.classinfo);

DEBUG("All VarExp in '", curStmt, "': ", foundStmts);

foreach(VariableExpression varExp; foundStmts)
{
ERROR("Sherlock is inspecting VarExp: ", varExp, cast(void*)varExp);
// TODO: Cache the list of declared aliases (instead of searching for them every-single-darn-time)
AliasDeclaration[] aliases = obtainAliases(container);

string varExpIdent = varExp.getName();
bool filterAliasesToName(AliasDeclaration aliasDecl)
{
return cmp(aliasDecl.getName(), varExpIdent) == 0;
}

AliasDeclaration[] matched;
filter!(AliasDeclaration)(aliases, predicateOf!(filterAliasesToName), matched);

// TODO: ENsure there is only ever one
if(matched.length)
{
AliasDeclaration aliasDeclMatch = matched[0];
DEBUG("Filtered down aliases to: ", matched);
DEBUG("Proccing: ", curStmt);
DEBUG("VariableExpression with name: ", varExp);

/**
* Traverse down from the `Container` we are processing
* and then apply the replacement by swapping out the
* `VariableExpression` with the `Expression` referred
* to by the alias.
*
* Also, before this, make sure you clone said expression
*/
MCloneable cloneable = cast(MCloneable)aliasDeclMatch.getExpr();
DEBUG("Cloned alias expression to: ", cloneable);
assert(cloneable);
MStatementReplaceable containerRepl = cast(MStatementReplaceable)container;
assert(containerRepl.replace(varExp, cloneable.clone(varExp.parentOf())));
DEBUG("Parent: ", varExp.parentOf());

}
else
{
WARN("Skipping?");
}
}
}

if(cast(Function)curStmt)
{
import tlang.compiler.symbols.data : VariableAssignment;
MStatementSearchable searchableStmt = cast(MStatementSearchable)curStmt;
VariableAssignment[] foundStmts = cast(VariableAssignment[])searchableStmt.search(VariableAssignment.classinfo);
DEBUG("Vars now: ", foundStmts);
foreach(i; foundStmts)
{
DEBUG(i.getExpression());
}
}
}



DEBUG("Skipping non MStatementSearchable node");
}
}

/**
Expand Down

0 comments on commit fc1135a

Please sign in to comment.