Skip to content

Commit

Permalink
Read the environment for alternate std.f path.
Browse files Browse the repository at this point in the history
  • Loading branch information
cstrainge committed Nov 11, 2024
1 parent 1446283 commit 22b4351
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions src/sorth.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@

#include <iostream>
#include "sorth.h"
// The main entry point for the Strange Forth, (or sorth,) interpreter. This is where the command
// line arguments are processed and the interpreter is set up to run the script or the REPL.
//
// The standard library is found in either the directory that the executable is in, or it can be
// specified by the SORTH_LIB environment variable.
//
// This project is covered by the MIT License. See the file "LICENSE" in the project root for more
// information.

#if defined(__APPLE__)

Expand All @@ -14,17 +20,27 @@

#endif

// Disable the CRT secure warnings for the deprecation warning for the function std::get_env on
// Windows.
#define _CRT_SECURE_NO_WARNINGS 1
#include <cstdlib>
#undef _CRT_SECURE_NO_WARNINGS

#include "sorth.h"



namespace
{


// Get the directory where the executable is located. This is used to find the standard library
// in the case the user hasn't specified another location for it.
std::filesystem::path get_executable_directory()
{
std::filesystem::path base_path;

// Refer to OS specific means to get the current executable's path.
#if defined(__APPLE__)

uint32_t buffer_size = 0;
Expand Down Expand Up @@ -94,35 +110,62 @@ namespace
}


// Get the directory where the standard library is located. This can either be the directory
// that the executable is in, or it can be specified by the SORTH_LIB environment variable.
std::filesystem::path get_std_lib_directory()
{
auto env_path = std::getenv("SORTH_LIB");

if (env_path != nullptr)
{
return std::filesystem::canonical(env_path);
}

return get_executable_directory();
}


}



// Let's get this interpreter started!
int main(int argc, char* argv[])
{
int exit_code;
// The exit code that we'll return to the operating system.
int exit_code = EXIT_SUCCESS;

try
{
// Create the interpreter and set up the search path to be able to find the standard
// library.
auto interpreter = sorth::create_interpreter();

interpreter->add_search_path(get_executable_directory());

// Register all of the built-in words.
sorth::register_builtin_words(interpreter);
sorth::register_terminal_words(interpreter);
sorth::register_io_words(interpreter);
sorth::register_user_words(interpreter);
sorth::register_ffi_words(interpreter);

// Load the standard library to agument the built-in words.
auto std_lib = interpreter->find_file("std.f");
interpreter->process_source(std_lib);

// Mark this context as a known good starting point. If we need to reset the interpreter it
// will be reset to this point.
interpreter->mark_context();

// Add the current directory to the search path.
interpreter->add_search_path(std::filesystem::current_path());

// Check to see if the user requested that we run a specific script.
if (argc >= 2)
{
// Looks like we have a script to run. Load up any remaining command line arguments
// into an array and make them available to the script as the word sorth.args.
sorth::ArrayPtr array = std::make_shared<sorth::Array>(argc - 2);

for (int i = 0; i < argc - 2; ++i)
Expand All @@ -131,26 +174,30 @@ int main(int argc, char* argv[])
}

ADD_NATIVE_WORD(interpreter,
"args",
"sorth.args",
[array](auto interpreter)
{
interpreter->push(array);
},
"List of command line arguments passed to the script.",
" -- arguments");

// Find the script file and run it.
auto user_source_path = interpreter->find_file(argv[1]);
interpreter->process_source(user_source_path);
}
else
{
// There was no script to run, so we'll just start the REPL from the standard library.
interpreter->execute_word("repl");
}

// Get the exit code from the interpreter so that we can return it to the operating system.
exit_code = interpreter->get_exit_code();
}
catch(const std::runtime_error& error)
{
// Something went wrong, so we'll print out the error and return a failure code.
std::cerr << "Run-Time error: " << error.what() << std::endl;
return EXIT_FAILURE;
}
Expand Down

0 comments on commit 22b4351

Please sign in to comment.