Dependency

- Bug fix (#57) - Undid hack (of #46) for Context setting of VariableStdAloneAssignments
- Bug fix (#54) in VariableExpression whereby global lookups failed because they used `resolveWithin()` and not `resolveBest()`

Resolution

- Implemented `generateNameBest(Entity)` which can generate the full absolute path of the given Entity
- Added debug statements to `isDescendant(Container, Entity)`
- Added a TODO for when isDescendant fails, the asserts should be removed and placed there

Mapper

- The `symbolLookup()` method now takes in only the Entity and provides a hash (by making use of `generateNameBest()`)

DGen

- Switched to using the new `symbolLookup(Entity)` in `transform()` wherever it was being used

Test cases

- Updated test case `simple_function_decls.t` to use a global variable reference in a VariableExpression to test the fix for #54
This commit is contained in:
Tristan B. Velloza Kildaire 2022-12-17 13:41:00 +02:00
parent 2a12c310a6
commit ddea68a73d
5 changed files with 68 additions and 20 deletions

View File

@ -48,9 +48,8 @@ public final class DCodeEmitter : CodeEmitter
gprintln("Is ContextNull?: "~to!(string)(context is null));
gprintln("Wazza contect: "~to!(string)(context.container));
auto typedEntityVariable = context.tc.getResolver().resolveBest(context.getContainer(), varAs.varName); //TODO: Remove `auto`
string typedEntityVariableName = context.tc.getResolver().generateName(context.getContainer(), typedEntityVariable);
string renamedSymbol = SymbolMapper.symbolLookup(context.getContainer(), typedEntityVariableName);
string renamedSymbol = SymbolMapper.symbolLookup(typedEntityVariable);
// If we are needed as part of a VariabvleDeclaration-with-assignment
@ -77,7 +76,6 @@ public final class DCodeEmitter : CodeEmitter
Context context = varDecInstr.getContext();
Variable typedEntityVariable = cast(Variable)context.tc.getResolver().resolveBest(context.getContainer(), varDecInstr.varName); //TODO: Remove `auto`
string typedEntityVariableName = context.tc.getResolver().generateName(context.getContainer(), typedEntityVariable);
//NOTE: We should remove all dots from generated symbol names as it won't be valid C (I don't want to say C because
// a custom CodeEmitter should be allowed, so let's call it a general rule)
@ -86,7 +84,7 @@ public final class DCodeEmitter : CodeEmitter
//NOTE: We may need to create a symbol table actually and add to that and use that as these names
//could get out of hand (too long)
// NOTE: Best would be identity-mapping Entity's to a name
string renamedSymbol = SymbolMapper.symbolLookup(context.getContainer(), varDecInstr.varName);
string renamedSymbol = SymbolMapper.symbolLookup(typedEntityVariable);
// Check to see if this declaration has an assignment attached
@ -126,9 +124,11 @@ public final class DCodeEmitter : CodeEmitter
Context context = fetchValueVarInstr.getContext();
Variable typedEntityVariable = cast(Variable)context.tc.getResolver().resolveBest(context.getContainer(), fetchValueVarInstr.varName); //TODO: Remove `auto`
string typedEntityVariableName = context.tc.getResolver().generateName(context.getContainer(), typedEntityVariable);
string renamedSymbol = SymbolMapper.symbolLookup(context.getContainer(), typedEntityVariableName);
//TODO: THis is giving me kak (see issue #54), it's generating name but trying to do it for the given container, relative to it
//TODO: We might need a version of generateName that is like generatenamebest (currently it acts like generatename, within)
string renamedSymbol = SymbolMapper.symbolLookup(typedEntityVariable);
return renamedSymbol;
}

View File

@ -24,13 +24,20 @@ public final class SymbolMapper
// this.tc = tc;
// }
public static string symbolLookup(Container container, string entityNameIn)
/**
* Given an Entity this will generate a unique (but consistent)
* symbol name for it by taking the md5 hash of the full absolute
* path to the Entity and finally prefixing it with <code>t_</code>.
*
* Params:
* entityIn = The Entity to generate a hash for
*
* Returns: The symbol hash
*/
public static string symbolLookup(Entity entityIn)
{
// Firstly translate the entity name to the full absolute path
auto entity = tc.getResolver().resolveBest(container, entityNameIn); //TODO: Remove `auto`
string entityNameAbsolute = tc.getResolver().generateName(tc.getModule(), entity);
gprintln("symbolLookup(): "~to!(string)(container));
// Generate the absolute full path of the Entity
string absoluteFullPath = tc.getResolver().generateNameBest(entityIn);
// Hash the absolute path name
// FIXME: Ensure these hashes are unique (use the smbyol table!)
@ -38,11 +45,9 @@ public final class SymbolMapper
import std.digest.md : md5Of;
// Generate the name as `_<hexOfAbsPath>`
string symbolName = toHexString!(LetterCase.lower)(md5Of(entityNameAbsolute));
string symbolName = toHexString!(LetterCase.lower)(md5Of(absoluteFullPath));
symbolName="t_"~symbolName;
return symbolName;
}
}

View File

@ -723,7 +723,7 @@ public class DNodeGenerator
nearestName = path;
/* Resolve the Entity */
Entity namedEntity = tc.getResolver().resolveWithin(context.getContainer(), nearestName);
Entity namedEntity = tc.getResolver().resolveBest(context.getContainer(), nearestName);
@ -1284,6 +1284,7 @@ public class DNodeGenerator
else if(cast(VariableAssignmentStdAlone)entity)
{
VariableAssignmentStdAlone vAsStdAl = cast(VariableAssignmentStdAlone)entity;
vAsStdAl.setContext(context);
/* TODO: CHeck avriable name even */
gprintln("YEAST ENJOYER");
@ -1297,8 +1298,6 @@ public class DNodeGenerator
Variable variable = cast(Variable)tc.getResolver().resolveBest(c, vAsStdAl.getVariableName());
assert(variable);
/* Set the context of the assignment to the Context of the Variable being assigned to */
vAsStdAl.setContext(variable.getContext());
/* Pool the variable */
DNode varDecDNode = pool(variable);

View File

@ -16,9 +16,48 @@ public final class Resolver
this.typeChecker = typeChecker;
}
/**
* Generate the absolute full path of the given Entity
*
* Params:
* entity = The Entity to generate the full absolute path for
*
* Returns: The absolute full path
*/
public string generateNameBest(Entity entity)
{
string absoluteFullPath;
assert(entity);
/**
* Search till we get to the top-most Container
* then generate a name relative to that with `generateName(topMostContainer, entity)`
*/
Entity parentingEntity = entity;
while(true)
{
parentingEntity = cast(Entity)parentingEntity.parentOf();
if(parentingEntity.parentOf() is null)
{
break;
}
}
absoluteFullPath = generateName(cast(Container)parentingEntity, entity);
return absoluteFullPath;
}
/**
* Given an Entity generate it's full path relative to a given
* container
* container, this is akin to `generateNameWithin()` in the
* sense that it will fail if the entity prvided is not
* contained by `relativeTo` - returning null
*/
public string generateName(Container relativeTo, Entity entity)
{
@ -89,6 +128,7 @@ public final class Resolver
/* If not */
else
{
//TODO: technically an assert should be here and the one in isDescdant removed
return null;
}
}
@ -115,6 +155,10 @@ public final class Resolver
do
{
gprintln("c isdecsenat: "~to!(string)(c));
gprintln("currentEntity: "~to!(string)(currentEntity));
gprintln("currentEntity(parent): "~to!(string)(currentEntity.parentOf()));
/**
* TODO: Make sure this condition holds
*

View File

@ -12,6 +12,6 @@ int banana(int j)
{
int h = 64;
k=1+h+apple(1, apple(2, 3));
k=1+h+apple(1, apple(2, 3))+k;
}