Skip to content

Commit

Permalink
Merge 63e8b60 into 472ca36
Browse files Browse the repository at this point in the history
  • Loading branch information
deavmi committed Oct 27, 2023
2 parents 472ca36 + 63e8b60 commit b720750
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 76 deletions.
8 changes: 4 additions & 4 deletions source/tlang/commandline/commands.d
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ struct compileCommand
/* Begin lexing process */
File outFile;
outFile.open(outputFilename, "w");
Compiler compiler = new Compiler(sourceText, outFile);
Compiler compiler = new Compiler(sourceText, sourceFile, outFile);

/* Setup general configuration parameters */
BaseCommandInit(compiler);
Expand Down Expand Up @@ -217,7 +217,7 @@ struct lexCommand
file.close();

/* Begin lexing process */
Compiler compiler = new Compiler(sourceText, File());
Compiler compiler = new Compiler(sourceText, sourceFile, File());

/* Setup general configuration parameters */
BaseCommandInit(compiler);
Expand Down Expand Up @@ -263,7 +263,7 @@ struct parseCommand
file.close();

/* Begin lexing process */
Compiler compiler = new Compiler(sourceText, File());
Compiler compiler = new Compiler(sourceText, sourceFile, File());

/* Setup general configuration parameters */
BaseCommandInit(compiler);
Expand Down Expand Up @@ -312,7 +312,7 @@ struct typecheckCommand
file.close();

/* Begin lexing process */
Compiler compiler = new Compiler(sourceText, File());
Compiler compiler = new Compiler(sourceText, sourceFile, File());

/* Setup general configuration parameters */
BaseCommandInit(compiler);
Expand Down
22 changes: 15 additions & 7 deletions source/tlang/compiler/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public class Compiler
/* The input source code */
private string inputSource;

/* Input file path */
private string inputFilePath;

/* The lexer */
private LexerInterface lexer;

Expand Down Expand Up @@ -124,14 +127,17 @@ public class Compiler
/**
* Create a new compiler instance to compile the given
* source code
*
* Params:
* sourceCode = the source code to compile
*/
this(string sourceCode, File emitOutFile)
this(string sourceCode, string inputFilePath, File emitOutFile)
{
this.inputSource = sourceCode;
this.inputFilePath = inputFilePath;
this.emitOutFile = emitOutFile;

/* Get the default config */
this.config = CompilerConfiguration.defaultConfig();
}
Expand Down Expand Up @@ -170,7 +176,7 @@ public class Compiler
/* Spawn a new parser with the provided tokens */
this.parser = new Parser(lexer);

modulle = parser.parse();
modulle = parser.parse(this.inputFilePath);
}
}

Expand Down Expand Up @@ -247,6 +253,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 +263,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 Expand Up @@ -297,7 +305,7 @@ void beginCompilation(string[] sourceFiles)
outFile.open("tlangout.c", "w");

/* Create a new compiler */
Compiler compiler = new Compiler(cast(string)fileBytes, outFile);
Compiler compiler = new Compiler(cast(string)fileBytes, sourceFile, outFile);

/* Perform the compilation */
compiler.compile();
Expand Down Expand Up @@ -434,7 +442,7 @@ unittest
{
File tmpFile;
tmpFile.open("/tmp/bruh", "wb");
Compiler compiler = new Compiler(sourceText, tmpFile);
Compiler compiler = new Compiler(sourceText, testFileGood, tmpFile);

// Lex
compiler.doLex();
Expand Down Expand Up @@ -494,7 +502,7 @@ unittest
{
File tmpFile;
tmpFile.open("/tmp/bruh", "wb");
Compiler compiler = new Compiler(sourceText, tmpFile);
Compiler compiler = new Compiler(sourceText, testFileFail, tmpFile);

// Lex
compiler.doLex();
Expand Down
12 changes: 12 additions & 0 deletions source/tlang/compiler/modman/exceptions.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module tlang.compiler.modman.exceptions;

import misc.exceptions;
import tlang.compiler.modman.modman : ModuleManager;

public final class ModuleManagerError : TError
{
this(ModuleManager modMan, string msg)
{
super("Module manager '"~modMan.toString()~"' had error: "~msg);
}
}
121 changes: 121 additions & 0 deletions source/tlang/compiler/modman/modman.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
module tlang.compiler.modman.modman;

import gogga;

// TODO: We may want to throw an exception whilst searching
// ... as to which path is invalid
import tlang.compiler.modman.exceptions;

/**
* Manager for searching for modules etc.
*/
public final class ModuleManager
{
/**
* The search paths
*/
private string[] searchPaths;

/**
* Creates a new module manager with the
* provided paths of which it should
* consider when searching for module
* files
*
* Params:
* searchPaths = the search paths
* Throws:
* ModuleManagerError = if the
* provided search paths are incorrect
*/
this(string[] searchPaths)
{
if(!validate(searchPaths))
{
throw new ModuleManagerError(this, "An invalid path exists within the provided search paths");
}

this.searchPaths = searchPaths;
}

/**
* Validates the given paths, and only
* returns a valid verdict if all of
* the paths are valid search paths
*
* Params:
* searchPaths = the search paths
* to consider
* Returns: `true` if all paths are valid,
* `false` otherwise
*/
public static bool validate(string[] searchPaths)
{
foreach(string searchPath; searchPaths)
{
if(!validate(searchPath))
{
return false;
}
}

return true;
}

/**
* Validates a given path that it is a valid
* search path
*
* Params:
* searchPath = the path to check
* Returns: `true` if the search path is valid,
* `false` otherwise
*/
public static bool validate(string searchPath)
{
// Path cannot be empty
if(searchPath.length == 0)
{
return false;
}

import std.file : isDir;

// It should be a valid directory
return isDir(searchPath);
}
}

/**
* Tests the static methods of the `ModuleManager`
*
* In this case positive verdict is expected
*/
unittest
{
string[] goodPaths = [
"source/tlang/testing",
"source/tlang/testing/modules"
];

bool res = ModuleManager.validate(goodPaths);
assert(res);
}

/**
* Tests the static methods of the `ModuleManager`
*
* In this case negative verdict is expected
*/
unittest
{
string[] badPaths = [
"source/tlang/testing",
"source/tlang/testing/modules",
"README.md",
];

bool res = ModuleManager.validate(badPaths);
assert(!res);
}

Loading

0 comments on commit b720750

Please sign in to comment.