Skip to content

Commit 577152e

Browse files
committed
[update] the Lamscript runtime to return program results that will better allow the language to be used as a library.
1 parent 0656ae5 commit 577152e

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

src/Lamscript/runtime/Lamscript.cpp

+14-15
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ bool Lamscript::had_error_ = false;
1919
bool Lamscript::had_runtime_error_ = false;
2020

2121
/// @brief Run the given source.
22-
void Lamscript::Run(const std::string& source) {
22+
ProgramResult Lamscript::Run(const std::string& source) {
2323
parsing::Scanner scanner = parsing::Scanner(source);
2424
std::vector<parsing::Token> tokens = scanner.ScanTokens();
2525

@@ -28,24 +28,30 @@ void Lamscript::Run(const std::string& source) {
2828

2929
if (had_error_) {
3030
had_error_ = false;
31-
return;
31+
return ProgramResult{ProgramStatus::FailedAtParser, 65};
3232
}
3333

3434
parsing::Resolver resolver = parsing::Resolver(interpreter_);
3535
resolver.Resolve(statements);
3636

3737
if (had_error_) {
3838
had_error_ = false;
39-
return;
39+
return ProgramResult{ProgramStatus::FailedAtResolver, 65};
4040
}
4141

4242
interpreter_->Interpret(statements);
43+
44+
if (had_runtime_error_) {
45+
return ProgramResult{ProgramStatus::FailedAtInterpeter, 70};
46+
}
47+
48+
return ProgramResult{ProgramStatus::Success, 0};
4349
}
4450

4551
/// @brief Run a given file.
4652
///
4753
/// This functions throws if it cannot successfully open the file.
48-
void Lamscript::RunFile(const std::string& file_path) {
54+
ProgramResult Lamscript::RunFile(const std::string& file_path) {
4955
std::ifstream source_file(file_path, std::ios::in | std::ios::binary);
5056
std::string source_code;
5157

@@ -55,22 +61,14 @@ void Lamscript::RunFile(const std::string& file_path) {
5561
source_file.seekg(0, std::ios::beg);
5662
source_file.read(&source_code[0], source_code.size());
5763
} else {
58-
throw "The Input file could not be read";
59-
}
60-
61-
Run(source_code);
62-
63-
if (had_error_) {
64-
exit(65);
64+
return ProgramResult{ProgramStatus::FailedAtReadingFile, 1};
6565
}
6666

67-
if (had_runtime_error_) {
68-
exit(70);
69-
}
67+
return Run(source_code);
7068
}
7169

7270
/// @brief Runs the prompt for the interpreter.
73-
void Lamscript::RunPrompt() {
71+
ProgramResult Lamscript::RunPrompt() {
7472
bool running = true;
7573
std::string source_line;
7674

@@ -83,6 +81,7 @@ void Lamscript::RunPrompt() {
8381
}
8482
source_line.clear();
8583
}
84+
return ProgramResult{ProgramStatus::Success, 0};
8685
}
8786

8887
/// @brief Report an error

src/Lamscript/runtime/Lamscript.h

+21-4
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,35 @@
66
#include <iostream>
77
#include <vector>
88

9-
#include <Lamscript/runtime/Interpreter.h>
109
#include <Lamscript/errors/RuntimeError.h>
1110
#include <Lamscript/parsing/Token.h>
11+
#include <Lamscript/runtime/Interpreter.h>
1212

1313
namespace lamscript {
1414
namespace runtime {
1515

16+
/// @brief The status of a programs execution.
17+
enum class ProgramStatus {
18+
Success,
19+
FailedAtReadingFile,
20+
FailedAtScanner,
21+
FailedAtParser,
22+
FailedAtResolver,
23+
FailedAtInterpeter
24+
};
25+
26+
/// @brief Stores the result of any given programs execution result.
27+
struct ProgramResult{
28+
ProgramStatus Status;
29+
int ReturnCode;
30+
std::string Message;
31+
};
32+
1633
class Lamscript {
1734
public:
18-
static void Run(const std::string& source);
19-
static void RunFile(const std::string& file_path);
20-
static void RunPrompt();
35+
static ProgramResult Run(const std::string& source);
36+
static ProgramResult RunFile(const std::string& file_path);
37+
static ProgramResult RunPrompt();
2138
static void Error(int line, const std::string& message);
2239
static void Error(parsing::Token token, const std::string& message);
2340
static void RuntimeError(lamscript::RuntimeError error);

0 commit comments

Comments
 (0)