Skip to content

Commit

Permalink
Implement the builtins system
Browse files Browse the repository at this point in the history
  • Loading branch information
quantumsheep committed Aug 11, 2020
1 parent 7639fb0 commit 8ed826b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
27 changes: 24 additions & 3 deletions src/grammar/Visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Visitor
public:
Environment env;
std::vector<std::string> include_paths;
std::string builtins_path;

ScopeStack scopes;
std::stack<fs::path> files;
Expand All @@ -82,18 +83,38 @@ class Visitor
const std::string &target_arch,
const std::string &target_cpu,
const std::string &target_features,
const std::string &builtins_path_,
const std::vector<std::string> &include_paths_ = {}) : env("output", target_os, target_arch, target_cpu, target_features),
include_paths(include_paths_),
builtins_path(builtins_path_),
scopes(this->env)
{
this->include_paths.push_back(Environment::get_std_directory().u8string());
auto std_directory = Environment::get_std_directory();

if (this->builtins_path.empty())
{
this->builtins_path = (std_directory / "builtins").u8string();
}

this->include_paths.push_back(std_directory.u8string());
}

void load_builtins()
{
for (const auto &p : fs::recursive_directory_iterator(this->builtins_path, fs::directory_options::follow_directory_symlink | fs::directory_options::skip_permission_denied))
{
if (!p.is_directory())
{
this->from_file(p.path());
}
}
}

void from_file(std::string path)
{
if (!this->files.empty())
{
if (Helpers::starts_with(path, "./"))
if (Helpers::starts_with(path, "./") || Helpers::starts_with(path, "../"))
{
auto from = this->files.top();
path = from.replace_filename(path).u8string();
Expand Down Expand Up @@ -143,7 +164,7 @@ class Visitor
std::ifstream stream;
stream.open(fullpath);

files.push(fullpath);
this->files.push(fullpath);

auto input = new ANTLRInputStream(stream);
auto lexer = new SandLexer(input);
Expand Down
11 changes: 8 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ struct Options
std::string output_file = OUTPUT_FILENAME;
std::vector<std::string> include_paths;

std::string builtins_path = "";

std::string os = CURRENT_OS;
std::string arch = CURRENT_ARCH;
std::string mode = CURRENT_MODE;
Expand Down Expand Up @@ -157,12 +159,13 @@ bool compile(const Options &options, Sand::Debugger &debug)
llvm::InitializeAllAsmParsers();
llvm::InitializeAllAsmPrinters();

Sand::Visitor visitor(options.os, options.arch, options.cpu, options.features, options.include_paths);
Sand::Visitor visitor(options.os, options.arch, options.cpu, options.features, options.builtins_path, options.include_paths);

debug.start_timer("bytecode");

try
{
visitor.load_builtins();
visitor.from_file(options.entry_file);
}
catch (Sand::CompilationException &e)
Expand Down Expand Up @@ -254,6 +257,7 @@ int main(int argc, char **argv)

build->add_option("-O", options.optimization_level, "Optimization level", true);
build->add_option("-I", options.include_paths, "Include paths", true);
build->add_option("-B,--builtins", options.builtins_path, "Builtins path", true);

build->add_option("--arch", options.arch, "Target architecture", true);
build->add_option("--os", options.os, "Target operating system", true);
Expand Down Expand Up @@ -282,8 +286,9 @@ int main(int argc, char **argv)
CLI::App *run = app.add_subcommand("run", "Run sources");
run->add_option("ENTRY", options.entry_file, "Entry file")->required()->check(CLI::ExistingFile);

run->add_option("-O", options.optimization_level, "Optimization level");
run->add_option("-I", options.include_paths, "Include paths");
run->add_option("-O", options.optimization_level, "Optimization level", true);
run->add_option("-I", options.include_paths, "Include paths", true);
run->add_option("-B,--builtins", options.builtins_path, "Builtins path", true);

run->add_option("--arch", options.arch, "Target architecture", true);
run->add_option("--os", options.os, "Target operating system", true);
Expand Down
2 changes: 1 addition & 1 deletion std/linux/crt0.sn → std/builtins/intrinsics.sn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "./syscalls"
import "../linux/syscalls"

#[target_os = "linux"]
#[target_arch = "x86_64"]
Expand Down
2 changes: 2 additions & 0 deletions std/windows/chkstk.sn → std/builtins/x86_64.sn
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
let _fltused: i32 = 0;

#[target_os = "windows"]
#[target_arch = "x86_64"]
fn __chkstk() {
Expand Down

0 comments on commit 8ed826b

Please sign in to comment.