Compare commits

...

4 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 33473f5f2f Test cases
- Updated `simple_aliases.t`
2024-05-08 18:21:14 +02:00
Tristan B. Velloza Kildaire a659cb510c Meta
- Cleaned up
2024-05-08 18:20:57 +02:00
Tristan B. Velloza Kildaire 1877799285 Meta
- Implemented alias replacement such that it now looks for the nearest alias declaration
2024-05-08 18:18:48 +02:00
Tristan B. Velloza Kildaire bfb822b892 Program
- Implemented replacement
2024-05-08 18:18:02 +02:00
3 changed files with 59 additions and 19 deletions

View File

@ -73,8 +73,42 @@ public final class Program : Container
public bool replace(Statement thiz, Statement that)
{
// TODO: Implement me
return false;
/* We cannot replace ourselves */
if(thiz == this)
{
return false;
}
/* If not ourselves, then search our body */
else
{
/* Check for module replacement */
for(size_t i = 0; i < this.modules.length; i++)
{
Module mod = this.modules[i];
/* If we are replacing a module */
if(thiz == mod)
{
this.modules[i] = cast(Module)that;
that.parentTo(this);
return true;
}
}
/* Check for replacement WITHIN each module */
for(size_t i = 0; i < this.modules.length; i++)
{
Module mod = this.modules[i];
/* If we are replacing WITHIN a module */
if(mod.replace(thiz, that))
{
return true;
}
}
return false;
}
}
public void addStatement(Statement statement)

View File

@ -129,24 +129,22 @@ public class MetaProcessor
import tlang.compiler.symbols.data : VariableExpression;
import std.string : cmp;
import tlang.compiler.typecheck.resolution : Resolver;
private AliasDeclaration[] findAliasesFrom(Container from)
{
Resolver resolver = tc.getResolver(); // TODO: Remove from here, make a field
// Predicate to only find aliases (amongst all the different types of statements)
bool isAliasDecl(Statement stmt) { return cast(AliasDeclaration)stmt !is null; }
Statement[] declaredAliasesStmts;
resolver.collectUpwards(from, predicateOf!(isAliasDecl), declaredAliasesStmts);
return cast(AliasDeclaration[])declaredAliasesStmts;
}
private void doAliasExpression(Container container, Statement curStmt)
{
bool oldCode = false;
import tlang.compiler.typecheck.resolution : Resolver;
Resolver resolver = tc.getResolver();
// Discover all declared aliases in current container
// TODO: Ordering might be a parameter to set (as this discovers alias declared even AFTER they are used)
bool isAliasDecl(Statement stmt)
{
return cast(AliasDeclaration)stmt !is null;
}
Statement[] declaredAliasesStmts;
resolver.collectUpwards(container, predicateOf!(isAliasDecl), declaredAliasesStmts);
AliasDeclaration[] declaredAliases = cast(AliasDeclaration[])declaredAliasesStmts;
DEBUG(format("All aliases available from (cntnr:%s, stmt=%s) upwards: %s", container, curStmt, declaredAliases));
DEBUG(format("doAliasExpression(cntnr:%s, stmt=%s)", container, curStmt));
// Find any VariableExpression(s) from curStmt (TODO: should be container or nah?)
MStatementSearchable searchableStmt = cast(MStatementSearchable)curStmt;
@ -163,6 +161,10 @@ public class MetaProcessor
// furthest when obtained from the resolver, so any
// tie breaking would be the logically closest
// alias with the same name)
//
// Achor the search to start from the VarExp
AliasDeclaration[] declaredAliases = findAliasesFrom(varExp.parentOf());
DEBUG("DeclAlis: ", declaredAliases);
AliasDeclaration[] matched;
string varExpIdent = varExp.getName();
bool filterAliasesToName(AliasDeclaration aliasDecl)

View File

@ -9,9 +9,13 @@ int cnt()
alias expr = cnt();
alias inner = 1;
int main()
{
alias inner = 0;
int i = expr;
int p = expr;
return i+p;
int o = inner;
return i+p+o;
}