-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow method calls on strings (#211)
* Code improvements * Rework runtime module mechanism * Add test for string methods * Add `contains(string)` method for strings * Add `substring(int,int)` method for strings * Add tests for `substring` method * Fix #212 * Fix tests
- Loading branch information
1 parent
210f9b8
commit bc3a579
Showing
52 changed files
with
1,157 additions
and
580 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2021-2022 ChilliBits. All rights reserved. | ||
|
||
#include "RuntimeModuleManager.h" | ||
|
||
#include <dependency/SourceFile.h> | ||
#include <symbol/SymbolTable.h> | ||
#include <util/FileUtil.h> | ||
|
||
#include <ast/AstNodes.h> | ||
|
||
SourceFile *RuntimeModuleManager::requestModule(SourceFile *sourceFile, const RuntimeModuleName &moduleName) { | ||
// Make the module available | ||
if (!isModuleAvailable(moduleName)) | ||
addModule(sourceFile, moduleName); | ||
|
||
return modules.at(moduleName); | ||
} | ||
|
||
SymbolTable *RuntimeModuleManager::getModuleScope(const RuntimeModuleName &moduleName) const { | ||
assert(modules.contains(moduleName)); | ||
return modules.at(moduleName)->symbolTable.get(); | ||
} | ||
|
||
bool RuntimeModuleManager::isModuleAvailable(const RuntimeModuleName &module) const { return modules.contains(module); } | ||
|
||
void RuntimeModuleManager::addModule(SourceFile *parentSourceFile, const RuntimeModuleName &moduleName) { | ||
std::string importName; | ||
std::string fileName; | ||
switch (moduleName) { | ||
case STRING_RT: | ||
importName = "__rt_string"; | ||
fileName = "string_rt"; | ||
break; | ||
case THREAD_RT: | ||
importName = "__rt_thread"; | ||
fileName = "thread_rt"; | ||
break; | ||
default: | ||
throw std::runtime_error("Internal compiler error: Requested unknown runtime module"); | ||
} | ||
std::string filePath = FileUtil::getStdDir() + "runtime" + FileUtil::DIR_SEPARATOR + fileName + ".spice"; | ||
const auto moduleSourceFile = parentSourceFile->createSourceFile(importName, filePath, true); | ||
parentSourceFile->addDependency(moduleSourceFile, parentSourceFile->ast.get(), importName, filePath); | ||
|
||
const auto runtimeFile = parentSourceFile->dependencies.at(importName).first; | ||
runtimeFile->buildAST(); | ||
runtimeFile->preAnalyze(); | ||
runtimeFile->analyze(); | ||
runtimeFile->reAnalyze(); | ||
|
||
modules.emplace(moduleName, runtimeFile.get()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 2021-2022 ChilliBits. All rights reserved. | ||
|
||
#pragma once | ||
|
||
#include <map> | ||
|
||
// Forward declaration | ||
class SourceFile; | ||
class SymbolTable; | ||
|
||
const char *const STRING_RT_IMPORT_NAME = "__rt_string"; | ||
const char *const THREAD_RT_IMPORT_NAME = "__rt_thread"; | ||
|
||
enum RuntimeModuleName { STRING_RT, THREAD_RT }; | ||
|
||
class RuntimeModuleManager { | ||
public: | ||
// Public methods | ||
SourceFile *requestModule(SourceFile *sourceFile, const RuntimeModuleName &moduleName); | ||
[[nodiscard]] SymbolTable *getModuleScope(const RuntimeModuleName &moduleName) const; | ||
[[nodiscard]] bool isModuleAvailable(const RuntimeModuleName &moduleName) const; | ||
|
||
private: | ||
// Private methods | ||
void addModule(SourceFile *parentSourceFile, const RuntimeModuleName &moduleName); | ||
|
||
// Private fields | ||
std::map<RuntimeModuleName, SourceFile *> modules; | ||
}; |
Oops, something went wrong.