Skip to content

Commit

Permalink
Implement more bootstrap parts (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer authored May 24, 2024
1 parent 50f8d33 commit 568b535
Show file tree
Hide file tree
Showing 29 changed files with 578 additions and 158 deletions.
21 changes: 10 additions & 11 deletions media/test-project/test.spice
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import "std/io/file";
import "std/os/env";
import "bootstrap/ast/ast-nodes";
import "bootstrap/lexer/lexer";
import "bootstrap/parser/parser";

f<int> main() {
string filename = "./test-file.txt";
deleteFile(filename); // Ensure that the file is not present in the beginning

assert !fileExists(filename);
assert createFile(filename);
assert fileExists(filename);
assert deleteFile(filename);
assert !fileExists(filename);

printf("All assertions passed!");
String filePath = getEnv("SPICE_STD_DIR") + "/../test/test-files/bootstrap-compiler/standalone-parser-test/test-file.spice";
Lexer lexer = Lexer(filePath.getRaw());
Parser parser = Parser(lexer);
ASTEntryNode* ast = parser.parse();
assert ast != nil<ASTEntryNode*>;
printf("All assertions passed!\n");
}
6 changes: 3 additions & 3 deletions src-bootstrap/ast/ast-nodes.spice
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 "../ast/abstract-ast-visitor";
import "../reader/code-loc";
//import "../symboltablebuilder/type";
//import "bootstrap/ast/abstract-ast-visitor";
import "bootstrap/reader/code-loc";
//import "bootstrap/symboltablebuilder/type";

/**
* Saves a constant value for an AST node to realize features like array-out-of-bounds checks
Expand Down
2 changes: 1 addition & 1 deletion src-bootstrap/bindings/llvm/llvm.spice
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import "std/data/vector";

// Own imports
import "linker-flags";
import "bootstrap/bindings/llvm/linker-flags";

// ===== External type definitions =====
type VoidPtr alias byte*;
Expand Down
2 changes: 1 addition & 1 deletion src-bootstrap/compiler-pass.spice
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "./global/global-resource-manager";
import "bootstrap/global/global-resource-manager";

type ICompilerPass interface {
p changeToScope(Scope*, const ScopeType&);
Expand Down
4 changes: 2 additions & 2 deletions src-bootstrap/exception/cli-error.spice
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Own imports
import "../reader/code-loc";
import "bootstrap/reader/code-loc";

public type CliErrorType enum {
INCOMPLETE_TARGET_TRIPLE,
Expand Down Expand Up @@ -39,6 +39,6 @@ f<string> CliError.getMessagePrefix(const CliErrorType errorType) {
case CliErrorType::OPT_DEBUG_INFO_INCOMPATIBILITY: { return "Cannot emit debug info with optimization enabled"; }
case CliErrorType::NON_ZERO_EXIT_CODE: { return "Non-zero exit code"; }
case CliErrorType::COMING_SOON_CLI: { return "Coming soon"; }
default: { return "Unknown error"; }
default: { panic(Error("Unknown error")); }
}
}
4 changes: 2 additions & 2 deletions src-bootstrap/exception/lexer-error.spice
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Own imports
import "../util/code-loc";
import "bootstrap/util/code-loc";

public type LexerErrorType enum {
TOKENIZING_FAILED
Expand Down Expand Up @@ -32,6 +32,6 @@ public p ParserError.ctor(const CodeLoc* codeLoc, const LexerErrorType errorType
f<string> ParserError.getMessagePrefix(const LexerErrorType errorType) {
switch errorType {
case LexerErrorType::TOKENIZING_FAILED: { return "Tokenizing failed"; }
default: { return "Unknown error"; }
default: { panic(Error("Unknown error")); }
}
}
4 changes: 2 additions & 2 deletions src-bootstrap/exception/linker-error.spice
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Own imports
import "../util/code-loc";
import "bootstrap/util/code-loc";

public type LinkerErrorType enum {
LINKER_NOT_FOUND,
Expand Down Expand Up @@ -31,6 +31,6 @@ f<string> LinkerError.getMessagePrefix(const LinkerErrorType errorType) {
switch errorType {
case LinkerErrorType::LINKER_NOT_FOUND: { return "Linker not found"; }
case LinkerErrorType::LINKER_ERROR: { return "Linker error occurred"; }
default: { return "Unknown error"; }
default: { panic(Error("Unknown error")); }
}
}
4 changes: 2 additions & 2 deletions src-bootstrap/exception/parser-error.spice
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import "../util/code-loc";
import "bootstrap/util/code-loc";

public type ParserErrorType enum {
PARSING_FAILED,
Expand Down Expand Up @@ -42,6 +42,6 @@ f<string> ParserError.getMessagePrefix(const ParserErrorType errorType) {
case ParserErrorType::INVALID_CHAR_LITERAL: { return "Invalid char literal"; }
case ParserErrorType::INVALID_ATTR_VALUE_TYPE: { return "Invalid attribute value type"; }
case ParserErrorType::RESERVED_KEYWORD: { return "Usage of reserved keyword"; }
default: { return "Unknown error"; }
default: { panic(Error("Unknown error")); }
}
}
6 changes: 3 additions & 3 deletions src-bootstrap/exception/semantic-error.spice
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Own imports
import "../ast/ast-nodes";
import "../reader/code-loc";
import "bootstrap/ast/ast-nodes";
import "bootstrap/reader/code-loc";

public type SemanticErrorType enum {
REFERENCED_UNDEFINED_FUNCTION,
Expand Down Expand Up @@ -125,6 +125,6 @@ f<string> SemanticError.getMessagePrefix(const SemanticErrorType errorType) {
case SemanticErrorType::STRUCT_AMBIGUITY: { return "Struct ambiguity"; }
case SemanticErrorType::VARIABLE_DECLARED_TWICE: { return "Multiple declarations of the same variable"; }
case SemanticErrorType::FUNCTION_DECLARED_TWICE: { return "Multiple declarations of a function/procedure"; }
default: { return "Unknown error"; }
default: { panic(Error("Unknown error")); }
}
}
16 changes: 8 additions & 8 deletions src-bootstrap/global/global-resource-manager.spice
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import "std/time/timer";
import "std/io/filepath";

// Own imports
import "../global/runtime-module-manager";
import "../linker/external-linker-interface";
import "../reader/code-loc";
import "../util/memory";
import "../util/block-allocator";
import "../bindings/llvm/llvm";
import "../source-file";
import "../driver";
import "bootstrap/global/runtime-module-manager";
import "bootstrap/linker/external-linker-interface";
import "bootstrap/reader/code-loc";
import "bootstrap/util/memory";
import "bootstrap/util/block-allocator";
import "bootstrap/bindings/llvm/llvm";
import "bootstrap/source-file";
import "bootstrap/driver";

// Constants
public const string MAIN_FILE_NAME = "root";
Expand Down
29 changes: 20 additions & 9 deletions src-bootstrap/global/runtime-module-manager.spice
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Std imports
import "std/data/unordered-map";

// Own imports
import "bootstrap/util/file-util";

const string STRING_RT_IMPORT_NAME = "__rt_string";
const string RESULT_RT_IMPORT_NAME = "__rt_result";
Expand All @@ -9,8 +14,8 @@ public type RuntimeModule enum {
STRING_RT = 1,
RESULT_RT = 2,
ERROR_RT = 4,
ARRAY_RT = 8,
OBJECT_RT = 16
MEMORY_RT = 8,
RTTI_RT = 16
}

type ModuleNamePair struct {
Expand Down Expand Up @@ -59,7 +64,7 @@ public f<bool> RuntimeModuleManager.isModuleAvailable(RuntimeModule requestedMod

public f<SourceFile *> RuntimeModuleManager.loadModule(SourceFile* parentSourceFile, RuntimeModule requestedModule) {
const ModuleNamePair namePair = resolveNamePair(requestedModule);
const string filePath = FileUtil::getStdDir() / "runtime" / fileName / ".spice";
const string filePath = getStdDir() / "runtime" / fileName / ".spice";
assert filePath != parentSourceFile.filePath;

// Instruct the global resource manager to create a new source file
Expand All @@ -75,17 +80,23 @@ public f<SourceFile *> RuntimeModuleManager.loadModule(SourceFile* parentSourceF

public f<ModuleNamePair> RuntimeModuleManager.resolveNamePair(RuntimeModule requestedModule) {
switch requestedModule {
case RuntimeModule::STRING_RT:
case RuntimeModule::STRING_RT: {
return ModuleNamePair{STRING_RT_IMPORT_NAME, "string_rt"};
case RuntimeModule::RESULT_RT:
}
case RuntimeModule::RESULT_RT: {
return ModuleNamePair{RESULT_RT_IMPORT_NAME, "result_rt"};
case RuntimeModule::ERROR_RT:
}
case RuntimeModule::ERROR_RT: {
return ModuleNamePair{ERROR_RT_IMPORT_NAME, "error_rt"};
case RuntimeModule::ARRAY_RT:
}
case RuntimeModule::ARRAY_RT: {
return ModuleNamePair{MEMORY_RT_IMPORT_NAME, "memory_rt"};
case RuntimeModule::OBJECT_RT:
}
case RuntimeModule::OBJECT_RT: {
return ModuleNamePair{RTTI_RT_IMPORT_NAME, "rtti_rt"};
default:
}
default: {
panic(Error("Requested unknown runtime module"));
}
}
}
10 changes: 5 additions & 5 deletions src-bootstrap/lexer/lexer.spice
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import "std/text/format";
import "std/text/analysis";

// Own imports
//import "../source-file";
//import "../compiler-pass";
import "../lexer/token";
import "../reader/reader";
import "../reader/code-loc";
//import "bootstrap/source-file";
//import "bootstrap/compiler-pass";
import "bootstrap/lexer/token";
import "bootstrap/reader/reader";
import "bootstrap/reader/code-loc";

public type Lexer struct/* : ICompilerPass*/ {
//compose CompilerPass compilerPass
Expand Down
2 changes: 1 addition & 1 deletion src-bootstrap/lexer/token.spice
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import "../reader/code-loc";
import "bootstrap/reader/code-loc";

// Enums
public type TokenType enum {
Expand Down
2 changes: 1 addition & 1 deletion src-bootstrap/linker/external-linker-interface.spice
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "std/data/vector";
import "std/io/filepath";

// Own imports
import "../driver";
import "bootstrap/driver";

public type ExternalLinkerInterface struct {
CliOptions& cliOptions
Expand Down
8 changes: 4 additions & 4 deletions src-bootstrap/parser/parser.spice
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import "std/data/stack";

// Own imports
import "../compiler-pass";
import "../lexer/lexer";
import "../lexer/token";
import "../ast/ast-nodes";
import "bootstrap/compiler-pass";
import "bootstrap/lexer/lexer";
import "bootstrap/lexer/token";
import "bootstrap/ast/ast-nodes";

// Generic types
type T dyn;
Expand Down
2 changes: 1 addition & 1 deletion src-bootstrap/reader/reader.spice
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "std/io/file";
import "std/os/os";

// Own imports
import "../reader/code-loc";
import "bootstrap/reader/code-loc";

public type Reader struct {
File file
Expand Down
26 changes: 20 additions & 6 deletions src-bootstrap/source-file.spice
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
// Imports
// Std imports
import "std/text/print";
import "std/data/pair";
import "std/data/unordered-map";
import "std/data/vector";
import "std/io/filepath";
import "./driver";
import "./global/global-resource-manager";
import "./ast/ast-nodes";

// Own imports
import "bootstrap/driver";
//import "bootstrap/global/global-resource-manager";
import "bootstrap/ast/ast-nodes";
import "bootstrap/bindings/llvm/llvm";
import "bootstrap/util/compiler-warning";
import "bootstrap/symboltablebuilder/symbol-table-entry";
import "bootstrap/symboltablebuilder/scope";

type CompileStageType enum {
NONE,
Expand Down Expand Up @@ -74,6 +82,7 @@ type NameRegistryEntry struct {
* Represents a single source file
*/
public type SourceFile struct {
// Public fields
public String name
public String fileName
public FilePath filePath
Expand All @@ -89,13 +98,18 @@ public type SourceFile struct {
public bool restoredFromCache = false
public EntryNode* ast = nil<EntryNode*>
public heap Scope* globalScope
//public llvm::Module llvmModule
public llvm::LLVMContext context
public llvm::IRBuilder builder
public heap llvm::TargetMachine* targetMachine
public heap llvm::Module* llvmModule
public UnorderedMap<String, SourceFile*> dependencies
public Vector<const SourceFile*> dependants
public Map<String, NameRegistryEntry> exportedNameRegistry
public Vector<const Function*> testFunctions

// Private fields
GlobalResourceManager& resourceManager
CliOptions& cliOptions
UnorderedMap<const Type*, llvm::Type*> llvmTypeMapping
unsigned short importedRuntimeModules = 0s
unsigned short totalTypeCheckerRuns = 0s
}
Expand Down
Loading

0 comments on commit 568b535

Please sign in to comment.