Refactored dependency tree generation code (and all related modules) to its own directory

This commit is contained in:
Tristan B. Velloza Kildaire 2021-11-09 19:16:51 +02:00
parent fffcc953ab
commit 60f490d8e8
9 changed files with 85 additions and 152 deletions

View File

@ -1,7 +1,7 @@
module compiler.codegen.instruction;
import std.conv : to;
import compiler.typecheck.dependency : Context;
import compiler.typecheck.dependency.core : Context;
public class Instruction
{

View File

@ -2,7 +2,7 @@ module compiler.symbols.data;
public import compiler.symbols.check;
import std.conv : to;
import compiler.typecheck.dependency : Context;
import compiler.typecheck.dependency.core : Context;
/**

View File

@ -64,7 +64,7 @@ public final class TypeChecker
* non-cyclic
*
*/
import compiler.typecheck.dependency;
import compiler.typecheck.dependency.core;
DNodeGenerator dNodeGenerator = new DNodeGenerator(this);
DNode rootNode = dNodeGenerator.generate(); /* TODO: This should make it acyclic */
@ -91,7 +91,7 @@ public final class TypeChecker
}
import compiler.typecheck.dependency;
import compiler.typecheck.dependency.core;
import std.container.slist;
import compiler.codegen.instruction;
@ -162,9 +162,9 @@ public final class TypeChecker
gprintln("typeCheckThing(): "~dnode.toString());
/* ExpressionDNodes */
if(cast(compiler.typecheck.expression.ExpressionDNode)dnode)
if(cast(compiler.typecheck.dependency.expression.ExpressionDNode)dnode)
{
compiler.typecheck.expression.ExpressionDNode expDNode = cast(compiler.typecheck.expression.ExpressionDNode)dnode;
compiler.typecheck.dependency.expression.ExpressionDNode expDNode = cast(compiler.typecheck.dependency.expression.ExpressionDNode)dnode;
Statement statement = expDNode.getEntity();
gprintln("Hdfsfdjfds"~to!(string)(statement));
@ -242,12 +242,12 @@ public final class TypeChecker
}
}
/* VariableAssigbmentDNode */
else if(cast(compiler.typecheck.variables.VariableAssignmentNode)dnode)
else if(cast(compiler.typecheck.dependency.variables.VariableAssignmentNode)dnode)
{
import compiler.typecheck.variables;
import compiler.typecheck.dependency.variables;
/* Get the variable's name */
string variableName;
VariableAssignmentNode varAssignDNode = cast(compiler.typecheck.variables.VariableAssignmentNode)dnode;
VariableAssignmentNode varAssignDNode = cast(compiler.typecheck.dependency.variables.VariableAssignmentNode)dnode;
Variable assignTo = (cast(VariableAssignment)varAssignDNode.getEntity()).getVariable();
variableName = resolver.generateName(modulle, assignTo);
@ -262,7 +262,7 @@ public final class TypeChecker
VariableAssignmentInstr varAssInstr = new VariableAssignmentInstr(variableName, valueInstr);
addInstr(varAssInstr);
}
else if(cast(compiler.typecheck.variables.ModuleVariableDeclaration)dnode)
else if(cast(compiler.typecheck.dependency.variables.ModuleVariableDeclaration)dnode)
{
/**
* Codegen
@ -314,7 +314,7 @@ public final class TypeChecker
}
/* TODO: ANy statement */
else if(cast(compiler.typecheck.dependency.DNode)dnode)
else if(cast(compiler.typecheck.dependency.core.DNode)dnode)
{
/* TODO: Get the STatement */
Statement statement = dnode.getEntity();
@ -1217,73 +1217,4 @@ unittest
/* Make sure the member y.y collided with root container (module) y */
assert(e.defined == container);
}
}
// unittest
// {
// /* TODO: Add some unit tests */
// import std.file;
// import std.stdio;
// import compiler.lexer;
// import compiler.parsing.core;
// // isUnitTest = true;
// string sourceFile = "source/tlang/testing/basic1.t";
// gprintln("Reading source file '" ~ sourceFile ~ "' ...");
// File sourceFileFile;
// sourceFileFile.open(sourceFile); /* TODO: Error handling with ANY file I/O */
// ulong fileSize = sourceFileFile.size();
// byte[] fileBytes;
// fileBytes.length = fileSize;
// fileBytes = sourceFileFile.rawRead(fileBytes);
// sourceFileFile.close();
// gprintln("Performing tokenization on '" ~ sourceFile ~ "' ...");
// /* TODO: Open source file */
// string sourceCode = cast(string) fileBytes;
// // string sourceCode = "hello \"world\"|| ";
// //string sourceCode = "hello \"world\"||"; /* TODO: Implement this one */
// // string sourceCode = "hello;";
// Lexer currentLexer = new Lexer(sourceCode);
// currentLexer.performLex();
// gprintln("Collected " ~ to!(string)(currentLexer.getTokens()));
// gprintln("Parsing tokens...");
// Parser parser = new Parser(currentLexer.getTokens());
// Module modulle = parser.parse();
// gprintln("Type checking and symbol resolution...");
// try
// {
// TypeChecker typeChecker = new TypeChecker(modulle);
// }
// // catch(CollidingNameException e)
// // {
// // gprintln(e.msg, DebugType.ERROR);
// // //gprintln("Stack trace:\n"~to!(string)(e.info));
// // }
// catch (TypeCheckerException e)
// {
// gprintln(e.msg, DebugType.ERROR);
// }
// /* Test first-level resolution */
// // assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz1").getName(), "clazz1")==0);
// // /* Test n-level resolution */
// // assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2").getName(), "clazz_2_2")==0);
// // assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2.j").getName(), "j")==0);
// // assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2.clazz_2_2_1").getName(), "clazz_2_2_1")==0);
// // assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2").getName(), "clazz_2_2")==0);
// // /* Test invalid access to j treating it as a Container (whilst it is a Variable) */
// // assert(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2.j.p") is null);
// }
}

View File

@ -1,4 +1,4 @@
module compiler.typecheck.classes.classObject;
module compiler.typecheck.dependency.classes.classObject;
import compiler.symbols.check;
import compiler.symbols.data;
@ -12,7 +12,7 @@ import compiler.typecheck.exceptions;
import compiler.typecheck.core;
import compiler.symbols.typing.core;
import compiler.symbols.typing.builtins;
import compiler.typecheck.dependency;
import compiler.typecheck.dependency.core;
/**
* (TODO) We don't init class in here, we do that when we see the type

View File

@ -1,4 +1,4 @@
module compiler.typecheck.classes.classStaticDep;
module compiler.typecheck.dependency.classes.classStaticDep;
import compiler.symbols.check;
import compiler.symbols.data;
@ -12,7 +12,7 @@ import compiler.typecheck.exceptions;
import compiler.typecheck.core;
import compiler.symbols.typing.core;
import compiler.symbols.typing.builtins;
import compiler.typecheck.dependency;
import compiler.typecheck.dependency.core;
public class ClassStaticNode : DNode
{

View File

@ -1,4 +1,4 @@
module compiler.typecheck.classes.classVirtualInit;
module compiler.typecheck.dependency.classes.classVirtualInit;
import compiler.symbols.check;
import compiler.symbols.data;
@ -12,7 +12,7 @@ import compiler.typecheck.exceptions;
import compiler.typecheck.core;
import compiler.symbols.typing.core;
import compiler.symbols.typing.builtins;
import compiler.typecheck.dependency;
import compiler.typecheck.dependency.core;
public class ClassVirtualInit : DNode
{

View File

@ -1,4 +1,4 @@
module compiler.typecheck.dependency;
module compiler.typecheck.dependency.core;
import compiler.symbols.check;
import compiler.symbols.data;
@ -52,6 +52,33 @@ public final class Context
}
}
/**
* FunctionData
*
* Contains the dependency tree for a function,
* it's name, context as to where it is declared
*
*TODO: TO getn this to work DNode and DNoeGenerator
* must become one to house `private static DNode root`
* and `private static DNode[] pool`, which means FunctionData
* may remain completely seperated from Module's DNode
*
* Of course DNode must have a FunctionData[] array irrespective
* of the sub-type of DNode as we look up data using it
* techncially it could be seperate, yeah, global function
*
* The FunctionData should, rather than Context perhaps,
* take in the DNode of the Modulle, to be able to idk
* maybe do some stuff
*/
private struct FunctionData
{
public string name;
public Context context;
public DNodeGenerator ownGenerator;
}
/**
* DNode
*
@ -305,9 +332,9 @@ public class DNodeGenerator
import compiler.typecheck.expression;
import compiler.typecheck.classes.classObject;
import compiler.typecheck.classes.classVirtualInit;
import compiler.typecheck.dependency.expression;
import compiler.typecheck.dependency.classes.classObject;
import compiler.typecheck.dependency.classes.classVirtualInit;
/* TODO: As mentioned in classObject.d we should static init the class type here */
private ClassVirtualInit virtualInit(Clazz clazz)
@ -709,7 +736,7 @@ public class DNodeGenerator
}
import compiler.typecheck.variables;
import compiler.typecheck.dependency.variables;
private ModuleVariableDeclaration pool_module_vardec(Variable entity)
{
foreach(DNode dnode; nodePool)
@ -949,73 +976,48 @@ public class DNodeGenerator
return moduleDNode;
}
/**
* FunctionData
*
* Contains the dependency tree for a function,
* it's name, context as to where it is declared
*
*TODO: TO getn this to work DNode and DNoeGenerator
* must become one to house `private static DNode root`
* and `private static DNode[] pool`, which means FunctionData
* may remain completely seperated from Module's DNode
*
* Of course DNode must have a FunctionData[] array irrespective
* of the sub-type of DNode as we look up data using it
* techncially it could be seperate, yeah, global function
*
* The FunctionData should, rather than Context perhaps,
* take in the DNode of the Modulle, to be able to idk
* maybe do some stuff
*/
private struct FunctionData
{
public string name;
public Context context;
public DNode depRoot;
}
// private FunctionData processFunction(Function funcDec)
// {
// FunctionData funcData;
// funcData.name = funcDec.getName();
// funcData.context = funcDec.context;
// // funcData.depRoot = new DNode();
private FunctionData processFunction(Function funcDec)
{
FunctionData funcData;
funcData.name = funcDec.getName();
funcData.context = funcDec.context;
// funcData.depRoot = new DNode();
// /* STart processing the internal dependencies*/
// Statement[] statements = funcDec.getStatements();
// foreach(Statement statement; statements)
// {
/* STart processing the internal dependencies*/
Statement[] statements = funcDec.getStatements();
foreach(Statement statement; statements)
{
// }
}
return funcData;
}
// return funcData;
// }
/**
* WIP: TODO: BIG ONE, this is just for now
* TODO: REMOVE THIS
*/
private DNode FunctionPass(Function func)
{
DNode dnode = pool(func);
// /**
// * WIP: TODO: BIG ONE, this is just for now
// * TODO: REMOVE THIS
// */
// private DNode FunctionPass(Function func)
// {
// DNode dnode = pool(func);
/* Statements in body */
Statement[] bodyStatements = func.getStatements();
// /* Statements in body */
// Statement[] bodyStatements = func.getStatements();
foreach(Statement statement; bodyStatements)
{
DNode dNodeStat = pool(statement);
dNodeStat.needs(dnode);
}
// foreach(Statement statement; bodyStatements)
// {
// DNode dNodeStat = pool(statement);
// dNodeStat.needs(dnode);
// }
return dnode;
}
// return dnode;
// }
import compiler.typecheck.classes.classStaticDep;
import compiler.typecheck.dependency.classes.classStaticDep;
private ClassStaticNode poolClassStatic(Clazz clazz)
{
/* Sanity check */

View File

@ -1,4 +1,4 @@
module compiler.typecheck.expression;
module compiler.typecheck.dependency.expression;
import compiler.symbols.check;
import compiler.symbols.data;
@ -12,7 +12,7 @@ import compiler.typecheck.exceptions;
import compiler.typecheck.core;
import compiler.symbols.typing.core;
import compiler.symbols.typing.builtins;
import compiler.typecheck.dependency;
import compiler.typecheck.dependency.core;
public class ExpressionDNode : DNode
{

View File

@ -1,6 +1,6 @@
module compiler.typecheck.variables;
module compiler.typecheck.dependency.variables;
import compiler.typecheck.dependency;
import compiler.typecheck.dependency.core;
import compiler.symbols.data;
/**