Compare commits

...

8 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 657fe07b90 Compiler
- Added test case for `"source/tlang/testing/simple_aliases.t"`
2024-05-08 13:55:52 +02:00
Tristan B. Velloza Kildaire f75028c215 Merge branch 'vardec_varass_dependency' into feature/aliases 2024-05-08 13:55:22 +02:00
Tristan B. Velloza Kildaire ce32cfe00d Compiler
- Added test case for `"source/tlang/testing/modules/a.t"`
2024-05-08 13:54:56 +02:00
Tristan B. Velloza Kildaire fc1135a470 MetaProcessor
- Cleaned up
2024-05-08 13:53:30 +02:00
Tristan B. Velloza Kildaire 7e1caad6f7 MetaProcessor
- Re-worked the alias replacement code
2024-05-08 13:48:32 +02:00
Tristan B. Velloza Kildaire e05e20d211 FunctionCall
- Made replacement return `false` for now (ass it is not implemented)
- Addec `MCloneable` support
2024-05-08 13:47:27 +02:00
Tristan B. Velloza Kildaire a98166aa76 Test cases
- Fixed `simple_aliases.t`
2024-05-08 13:45:25 +02:00
Tristan B. Velloza Kildaire 80c2ea5154 Pipelines
- Added test
2024-05-08 13:45:13 +02:00
5 changed files with 139 additions and 5 deletions

View File

@ -542,6 +542,23 @@ jobs:
exit 1
fi
- name: Alias expression test that returns 3
run: |
set -e
./tlang compile source/tlang/testing/simple_aliases.t
set +e
./tlang.out
if [ $? = 3 ]
then
set -e
exit 0
else
set -e
exit 1
fi
##################################

View File

@ -494,7 +494,9 @@ unittest
"source/tlang/testing/simple_literals6.t",
"source/tlang/testing/universal_coerce/simple_coerce_literal_good.t",
"source/tlang/testing/universal_coerce/simple_coerce_literal_good_stdalo.t",
"source/tlang/testing/simple_function_return_type_check_good.t"
"source/tlang/testing/simple_function_return_type_check_good.t",
"source/tlang/testing/modules/a.t",
"source/tlang/testing/simple_aliases.t"
];
foreach(string testFileGood; testFilesGood)

View File

@ -1016,7 +1016,7 @@ public class Call : IdentExpression
}
// FIXME: Finish adding proper `MStatementSearchable` and `MStatementReplaceable` to `FunctionCall`
public final class FunctionCall : Call, MStatementSearchable, MStatementReplaceable
public final class FunctionCall : Call, MStatementSearchable, MStatementReplaceable, MCloneable
{
/* Whether this is statement-level function call or not */
@ -1117,7 +1117,39 @@ public final class FunctionCall : Call, MStatementSearchable, MStatementReplacea
// {
// return false;
// }
return true;
return false;
}
/**
* Clones this integer literal
*
* Param:
* newParent = the `Container` to re-parent the
* cloned `Statement`'s self to
*
* Returns: the cloned `Statement`
*/
public override Statement clone(Container newParent = null)
{
// Clone arguments
Expression[] clonedArgs;
foreach(Expression arg; clonedArgs)
{
MCloneable argClonable = cast(MCloneable)arg;
if(argClonable)
{
clonedArgs ~= cast(Expression)argClonable.clone();
}
}
FunctionCall clonedFuncCall = new FunctionCall(this.name, clonedArgs);
DEBUG("haram");
// Parent outselves to the given parent
clonedFuncCall.parentTo(newParent);
return clonedFuncCall;
}
}

View File

@ -9,6 +9,8 @@ import tlang.compiler.typecheck.core;
import tlang.misc.logging;
import std.conv : to;
import tlang.compiler.configuration;
import tlang.compiler.symbols.aliases : AliasDeclaration;
import std.string : format;
/**
* The `MetaProcessor` is used to do a pass over a `Container`
@ -58,6 +60,9 @@ public class MetaProcessor
// Perform replacement of all type alises to concrete types, such as `size_t`
doTypeAlias(container, curStmt);
// Perform alias expression replacement (TODO: Should this be here?)
doAliasExpression(container, curStmt);
/**
* Search for any `sizeof(<ident_type>)` expressions
* and replace them with a `NumberLiteral`
@ -119,6 +124,84 @@ public class MetaProcessor
}
}
import niknaks.arrays : filter;
import niknaks.functional : predicateOf, Predicate;
import tlang.compiler.symbols.data : VariableExpression;
import std.string : cmp;
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));
// 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);
// 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)
{
return cmp(aliasDecl.getName(), varExpIdent) == 0;
}
filter!(AliasDeclaration)(declaredAliases, predicateOf!(filterAliasesToName), 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));
}
}
}
else
{
DEBUG("Skipping non MStatementSearchable node");
}
}
/**
* Re-writes the types for things such as `size_t`, `ssize_t` and so forth
*

View File

@ -11,7 +11,7 @@ alias expr = cnt();
int main()
{
int i = cnt();
int p = cnt();
int i = expr;
int p = expr;
return i+p;
}