Skip to content

Commit

Permalink
Re-organize source file in bootstrap compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Nov 16, 2023
1 parent e44a7c2 commit 87e183a
Show file tree
Hide file tree
Showing 30 changed files with 84 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Std imports
import "std/type/any";

public type IAbstractAstVisitor interface {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "../CompilerPass";
import "../SourceFile";
import "SpiceVisitor";
import "../compiler-pass";
import "../source-file";
import "../ast/spice-visitor";

type ASTBuilder struct : ICompilerPass, SpiceVisitor {
CompilerPass compilerPass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import "std/type/Any";
import "std/data/Vector";

// Own imports
import "AbstractASTVisitor";
import "../util/CodeLoc";
import "../symbol/SymbolType";
import "../ast/abstract-ast-visitor";
import "../reader/code-loc";
import "../symbol/symbol-type";

/**
* Saves a constant value for an AST node to realize features like array-out-of-bounds checks
Expand Down
7 changes: 5 additions & 2 deletions src-bootstrap/bindings/llvm/llvm.spice
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![core.compiler.warnings.ignore]

// Std imports
import "std/data/vector";
import "linker-flags";

#![core.compiler.warnings.ignore]
// Own imports
import "linker-flags";

// ===== External type definitions =====
type VoidPtr alias byte*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "global/GlobalResourceManager";
import "./global/global-resource-manager";

type ICompilerPass interface {
p changeToScope(Scope*, const ScopeType&);
Expand Down
22 changes: 11 additions & 11 deletions src-bootstrap/CliInterface.spice → src-bootstrap/driver.spice
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Imports
// Std imports
import "std/text/print";
import "std/os/cmd";
import "std/io/cli-parser";
Expand Down Expand Up @@ -37,20 +37,20 @@ public type CliOptions struct {
/**
* Helper class to setup the cli interface and command line parser
*/
public type CliInterface struct {
public type Driver struct {
CliParser cliParser
public CliOptions cliOptions
public bool shouldCompile = false
public bool shouldInstall = false
public bool shouldExecute = false
}

public p CliInterface.create() {
public p Driver.create() {
this.cliParser = CliParser("Spice", "Spice programming language");
this.cliParser.setFooter("(c) Marc Auberer 2021-2023");

// Add version flag
this.cliParser.setVersion("Spice version 0.14.3\nbuilt by: GitHub Actions\n\n(c) Marc Auberer 2021-2023");
this.cliParser.setVersion("Spice version 0.18.3\nbuilt by: GitHub Actions\n\n(c) Marc Auberer 2021-2023");

// Create sub-commands
this.addBuildSubcommand();
Expand All @@ -64,7 +64,7 @@ public p CliInterface.create() {
/**
* Initialize the cli options based on the input of the user
*/
public p CliInterface.enrich() {
public p Driver.enrich() {
// Propagate target information
if this.cliOptions.targetTriple.isEmpty() && this.cliOptions.targetArch.isEmpty() {
// ToDo: Extend
Expand All @@ -81,7 +81,7 @@ public p CliInterface.enrich() {
/**
* Add build subcommand to cli interface
*/
p CliInterface.addBuildSubcommand() {
p Driver.addBuildSubcommand() {
// Create sub-command itself
CliSubcommand& subCmd = this.cliParser.createSubcommand("build", "Builds your Spice program and emits an executable");
subCmd.addAlias("b");
Expand Down Expand Up @@ -112,7 +112,7 @@ p CliInterface.addBuildSubcommand() {
/**
* Add run subcommand to cli interface
*/
p CliInterface.addRunSubcommand() {
p Driver.addRunSubcommand() {
// Create sub-command itself
CliSubcommand& subCmd = this.cliParser.createSubcommand("run", "Builds your Spice program and runs it immediately");
subCmd.addAlias("r");
Expand All @@ -132,7 +132,7 @@ p CliInterface.addRunSubcommand() {
/**
* Add install subcommand to cli interface
*/
p CliInterface.addInstallSubcommand() {
p Driver.addInstallSubcommand() {
// Create sub-command itself
CliSubcommand& subCmd = this.cliParser.createSubcommand("install", "Builds your Spice program and installs it to a directory in the PATH variable");
subCmd.addAlias("i");
Expand All @@ -143,15 +143,15 @@ p CliInterface.addInstallSubcommand() {
/**
* Add uninstall subcommand to cli interface
*/
p CliInterface.addUninstallSubcommand() {
p Driver.addUninstallSubcommand() {
// Create sub-command itself
CliSubcommand& subCmd = this.cliParser.createSubcommand("uninstall", "Builds your Spice program and runs it immediately");
subCmd.addAlias("u");

addCompileSubcommandOptions(subCmd);
}

p CliInterface.addCompileSubcommandOptions(CliSubcommand& subCmd) {
p Driver.addCompileSubcommandOptions(CliSubcommand& subCmd) {
// --debug-output
CliOption<bool>& debugOutputFlag = subCmd.addFlag("--debug-output", this.cliOptions.printDebugOutput, "Enable debug output");
debugOutputFlag.addAlias("-d");
Expand Down Expand Up @@ -200,6 +200,6 @@ p CliInterface.addCompileSubcommandOptions(CliSubcommand& subCmd) {
* @param argv Argument vector
* @return Return code
*/
public f<int> CliInterface.parse(int argc, string[] argv) {
public f<int> Driver.parse(int argc, string[] argv) {
return this.cliParser.parse(argc, argv);
}
56 changes: 0 additions & 56 deletions src-bootstrap/exception/IRError.spice

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import "../util/CodeLoc" as cl;
// Own imports
import "../reader/code-loc";

public type CliErrorType enum {
INCOMPLETE_TARGET_TRIPLE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import "../util/CodeLoc" as cl;
import "../util/code-loc";

public type LexerErrorType enum {
TOKENIZING_FAILED
Expand All @@ -19,7 +19,7 @@ public type ParserError struct {
* @param errorType Type of the error
* @param message Error message suffix
*/
public p ParserError.ctor(const cl::CodeLoc* codeLoc, const LexerErrorType errorType, const string message) {
public p ParserError.ctor(const CodeLoc* codeLoc, const LexerErrorType errorType, const string message) {
this.errorMessage = "[Error|Lexer] " + codeLoc.toPrettyString() + ": " + this.getMessagePrefix(errorType) + ": " + message;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import "../util/CodeLoc" as cl;
import "../util/code-loc";

public type LinkerErrorType enum {
LINKER_NOT_FOUND,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import "../util/CodeLoc" as cl;
import "../util/code-loc";

public type ParserErrorType enum {
PARSING_FAILED,
Expand All @@ -21,7 +21,7 @@ public type ParserError struct {
* @param errorType Type of the error
* @param message Error message suffix
*/
public p ParserError.ctor(const cl::CodeLoc* codeLoc, const ParserErrorType errorType, const string message) {
public p ParserError.ctor(const CodeLoc* codeLoc, const ParserErrorType errorType, const string message) {
this.errorMessage = "[Error|Parser] " + codeLoc.toPrettyString() + ": " + this.getMessagePrefix(errorType) + ": " + message;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Imports
import "../ast/AstNodes" as ast;
import "../util/CodeLoc" as cl;
// Own imports
import "../ast/ast-nodes";
import "../reader/code-loc";

public type SemanticErrorType enum {
REFERENCED_UNDEFINED_FUNCTION,
Expand Down Expand Up @@ -74,7 +74,7 @@ public type SemanticError struct {
string message
}

public p SemanticError.ctor(const ast::AstNode* node, const SemanticErrorType errorType, const string message) {
public p SemanticError.ctor(const AstNode* node, const SemanticErrorType errorType, const string message) {
this.errorMessage = "[Error|Semantic] " + node.codeLoc.toPrettyString() + ":\n" + this.getMessagePrefix(errorType) +
": " + message + "\n\n" + node.errorMessage;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import "../exception/ErrorManager";
import "../global/RuntimeModuleManager";
import "../linker/ExternalLinkerInterface";
import "../util/CodeLoc";
import "../util/Timer";
import "../CliInterface";
// Own imports
import "../global/runtime-module-manager";
import "../linker/external-linker-interface";
import "../reader/code-loc";
import "../util/timer";
import "../driver";

// Constants
public const string MAIN_FILE_NAME = "root";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Imports
// Std imports

// Own imports

public type Generator struct {

Expand Down
2 changes: 0 additions & 2 deletions src-bootstrap/lexer/Lexer.spice

This file was deleted.

2 changes: 2 additions & 0 deletions src-bootstrap/lexer/lexer.spice
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Imports
import "../lexer/token";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import "../util/CodeLoc";
import "../reader/code-loc";

// Enums
public type TokenType enum {
Expand Down
24 changes: 13 additions & 11 deletions src-bootstrap/main.spice
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Std imports
import "std/os/os";

import "SourceFile";
import "CliInterface";
import "global/GlobalResourceManager";
// Own imports
import "./source-file";
import "./driver";
import "./global/global-resource-manager";

/**
* Compile main source file. All files, that are included by the main source file will be resolved recursively.
Expand Down Expand Up @@ -44,28 +46,28 @@ f<bool> compileProject(const CliOptions& cliOptions) {
*/
f<int> main(int argc, string[] argv) {
// Initialize command line parser
CliInterface cli;
cli.create();
const int exitCode = cli.parse(argc, argv);
Driver driver;
driver.create();
const int exitCode = driver.parse(argc, argv);
if exitCode != EXIT_SUCCESS {
return exitCode;
}

// Cancel here if we do not have to compile
if !cli.shouldCompile {
if !driver.shouldCompile {
return EXIT_SUCCESS;
}

cli.enrich(); // Prepare the cli options
driver.enrich(); // Prepare the cli options

// Kick off the compiling process
if !compileProject(cli.cliOptions) {
if !compileProject(driver.cliOptions) {
return EXIT_FAILURE;
}

// Execute
if cli.cliOptions.execute {
cli.runBinary();
if driver.cliOptions.execute {
driver.runBinary();
}

return EXIT_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Std imports
import "std/data/vector";
import "std/data/map";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import "../lexer/Token" as tk;
// Own imports
import "../lexer/token";

public type Parser struct {

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Imports
import "std/io/filepath";
import "std/io/file";
import "../util/CodeLoc";
import "../reader/code-loc";

public type Reader struct {
FileInputStream file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Imports
import "std/text/print";
import "std/data/pair";
import "CliInterface";
import "global/GlobalResourceManager";
import "ast/AstNodes";
import "./driver";
import "./global/global-resource-manager";
import "./ast/ast-nodes";

type CompileStageType enum {
NONE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Std imports
import "std/data/map";

// Own imports

type ScopeType enum {
GLOBAL,
Expand Down
File renamed without changes.
Loading

0 comments on commit 87e183a

Please sign in to comment.