Skip to content

Commit

Permalink
Merge ab4ddc7 into e1d034c
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan B. Velloza Kildaire committed Aug 27, 2023
2 parents e1d034c + ab4ddc7 commit b85deae
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 66 deletions.
4 changes: 3 additions & 1 deletion source/tlang/compiler/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ public class Compiler
}
}

// TODO: Move the below to utils
// TODO: Make it do error checking on the path provided and file-access rights
/**
* Opens the source file at the given path, reads the data
* and returns it
Expand All @@ -255,7 +257,7 @@ public class Compiler
* sourceFile = the path to the file to open
* Returns: the source data
*/
private string gibFileData(string sourceFile)
public string gibFileData(string sourceFile)
{
File sourceFileFile;
sourceFileFile.open(sourceFile); /* TODO: Error handling with ANY file I/O */
Expand Down
54 changes: 53 additions & 1 deletion source/tlang/compiler/parsing/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -2203,6 +2203,49 @@ public final class Parser
return externStmt;
}

/**
* Parses an `import <moduleName>;` statement
*/
private void parseImport()
{
gprintln("parseImport(): Enter", DebugType.WARNING);

/* Consume the `import` keyword */
lexer.nextToken();

/* Get the module's name */
expect(SymbolType.IDENT_TYPE, lexer.getCurrentToken());
string moduleName = lexer.getCurrentToken().getToken();

/* Consume the token */
lexer.nextToken();

/* Expect a semi-colon and consume it */
expect(SymbolType.SEMICOLON, lexer.getCurrentToken());
lexer.nextToken();
// TODO: Add support for multi-imports on one line (i.e. `import <module1>, <module2>;`)

import tlang.compiler.core : gibFileData;

// TODO: Read in here
string workDir = getWorkingDirectory();
gprintln("Current working directory is: '"~workDir~"'");




gprintln("parseImport(): Leave", DebugType.WARNING);
}

import std.path;

private static string getWorkingDirectory()
{
// TOOD: look at lazy, seems cool
string workDir = absolutePath(".");
return workDir;
}


/* Almost like parseBody but has more */
/**
Expand All @@ -2211,7 +2254,7 @@ public final class Parser
* one to define classes within functions
*/
/* TODO: Variables should be allowed to have letters in them and underscores */
public Module parse()
public Module parse(string moduleFilePath = "")
{
gprintln("parse(): Enter", DebugType.WARNING);

Expand All @@ -2232,6 +2275,9 @@ public final class Parser
/* Initialize Module */
modulle = new Module(programName);

/* Set the file system path of this module */
modulle.setFilePath(moduleFilePath);

/* TODO: do `lexer.hasTokens()` check */
/* TODO: We should add `lexer.hasTokens()` to the `lexer.nextToken()` */
/* TODO: And too the `getCurrentTokem()` and throw an error when we have ran out rather */
Expand Down Expand Up @@ -2303,6 +2349,12 @@ public final class Parser

modulle.addStatement(externStatement);
}
/* If it is an import */
else if(symbol == SymbolType.IMPORT)
{
// TODO: Figure out exactly what to do
parseImport();
}
else
{
expect("parse(): Unknown '" ~ tok.getToken() ~ "'");
Expand Down
10 changes: 10 additions & 0 deletions source/tlang/compiler/symbols/check.d
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ public enum SymbolType
*/
GENERIC_TYPE_DECLARE,

/**
* `import` keyword
*/
IMPORT,

/**
* Unknown symbol
*/
Expand Down Expand Up @@ -695,6 +700,11 @@ public SymbolType getSymbolType(Token tokenIn)
{
return SymbolType.GENERIC_TYPE_DECLARE;
}
/* import keyword */
else if(cmp(token, "import") == 0)
{
return SymbolType.IMPORT;
}
/* An identifier/type (of some sorts) - further inspection in parser is needed */
else if(isPathIdentifier(token) || isIdentifier(token))
{
Expand Down
27 changes: 27 additions & 0 deletions source/tlang/compiler/symbols/containers.d
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,38 @@ public interface Container : MStatementSearchable, MStatementReplaceable

public class Module : Entity, Container
{
/**
* Path to the module on disk
*/
private string moduleFilePath;

this(string moduleName)
{
super(moduleName);
}

/**
* Returns the file system path where
* this module was parsed from
*
* Returns: the path as a `string`
*/
public string getFilePath()
{
return this.moduleFilePath;
}

/**
* Sets the file system path to the module
*
* Params:
* filePath = path to the module on disk
*/
public void setFilePath(string filePath)
{
this.moduleFilePath = filePath;
}

private Statement[] statements;


Expand Down
81 changes: 17 additions & 64 deletions source/tlang/compiler/symbols/data.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,78 +10,31 @@ import gogga;
// AST manipulation interfaces
import tlang.compiler.symbols.mcro : MStatementSearchable, MStatementReplaceable, MCloneable;

/**
* TODO: Implement the blow and use them
*
* These are just to use for keeping track of what
* valid identifiers are.
*
* Actually it might be, yeah it will
*/

/**
* Represents a program made up of one or more
* module(s)
*/
public class Program
{
private string moduleName;
private Program[] importedModules;

private Statement[] statements;

this(string moduleName)
{
this.moduleName = moduleName;
}

public void addStatement(Statement statement)
{
statements ~= statement;
}

public static StatementType[] getAllOf(StatementType)(StatementType, Statement[] statements)
{
StatementType[] statementsMatched;

foreach(Statement statement; statements)
{
/* TODO: Remove null, this is for unimpemented */
if(statement !is null && cast(StatementType)statement)
{
statementsMatched ~= cast(StatementType)statement;
}
}

return statementsMatched;
}
/**
* Modules this program is made up of
*/
private Module[] modules;

public Variable[] getGlobals()
this()
{
Variable[] variables;

foreach(Statement statement; statements)
{
if(typeid(statement) == typeid(Variable))
{
variables ~= cast(Variable)statement;
}
}

return variables;
}

/* TODO: Make this use weights */
public Statement[] getStatements()
/**
* Adds a new `Module` to this program
*
* Params:
* newModule = the new `Module` to add
*/
public void addModule(Module newModule)
{
/* Re-ordered by lowest wieght first */
Statement[] stmntsRed;

bool wCmp(Statement lhs, Statement rhs)
{
return lhs.weight < rhs.weight;
}
import std.algorithm.sorting;
stmntsRed = sort!(wCmp)(statements).release;


return stmntsRed;
this.modules ~= newModule;
}
}

Expand Down
3 changes: 3 additions & 0 deletions source/tlang/testing/modules/a.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module a;

import b;
2 changes: 2 additions & 0 deletions source/tlang/testing/modules/b.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module b;

0 comments on commit b85deae

Please sign in to comment.