Skip to content

Commit

Permalink
Merge branch 'main' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
afwbkbc committed Jan 4, 2024
2 parents eb43e89 + 6728edd commit 9e860fc
Show file tree
Hide file tree
Showing 33 changed files with 372 additions and 139 deletions.
4 changes: 2 additions & 2 deletions src/config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ Config::Config( const int argc, const char* argv[] ) {
}
);
parser.AddRule(
"gse-prompt-gjs", "Open interactive GJS prompt", AH( this ) {
m_debug_flags |= DF_GSE_ONLY | DF_GSE_PROMPT_GJS;
"gse-prompt-js", "Open interactive JS prompt", AH( this ) {
m_debug_flags |= DF_GSE_ONLY | DF_GSE_PROMPT_JS;
}
);
parser.AddRule(
Expand Down
2 changes: 1 addition & 1 deletion src/config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ CLASS( Config, base::Module )
DF_QUIET = 1 << 12,
DF_GSE_ONLY = 1 << 13,
DF_GSE_TESTS = 1 << 14,
DF_GSE_PROMPT_GJS = 1 << 15,
DF_GSE_PROMPT_JS = 1 << 15,
};
#endif

Expand Down
1 change: 1 addition & 0 deletions src/gse/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ SET( SRC ${SRC}
${PWD}/GSE.cpp
${PWD}/Value.cpp
${PWD}/Context.cpp
${PWD}/Exception.cpp

)

Expand Down
51 changes: 38 additions & 13 deletions src/gse/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace gse {

Context::Context( const Context* parent_context, const source_lines_t* source_lines )
Context::Context( const Context* parent_context, const source_lines_t& source_lines, const si_t& si )
: m_parent_context( parent_context )
, m_source_lines( source_lines ) {
, m_source_lines( source_lines )
, m_si( si ) {
m_scopes.push_back( new Scope() ); // global scope
}

Expand Down Expand Up @@ -33,7 +34,7 @@ void Context::CreateVariable( const std::string& name, const Value& value ) {
scope->m_variables.insert_or_assign( name, value );
}

void Context::UpdateVariable( const std::string& name, const Value& value ) {
void Context::UpdateVariable( const std::string& name, const Value& value, bool create_if_missing ) {
Log( "UpdateVariable( " + name + ", " + value.ToString() + " )" );
Scope::variables_t::iterator it;
for ( auto scope = m_scopes.rbegin() ; scope != m_scopes.rend() ; scope++ ) {
Expand All @@ -43,7 +44,12 @@ void Context::UpdateVariable( const std::string& name, const Value& value ) {
return;
}
}
THROW( "variable '" + name + "' not found" );
if ( create_if_missing ) {
CreateVariable( name, value );
}
else {
THROW( "variable '" + name + "' not found" );
}
}

void Context::PushScope() {
Expand All @@ -66,26 +72,37 @@ const Context* Context::GetParentContext() const {
return m_parent_context;
}

void Context::SetSI( const si_t& si ) {
m_si = si;
}

const si_t& Context::GetSI() const {
return m_si;
}

void Context::AddSourceLine( const std::string& source_line ) {
m_source_lines.push_back( source_line );
}

void Context::AddSourceLines( const source_lines_t& source_lines ) {
m_source_lines.insert( m_source_lines.begin(), source_lines.begin(), source_lines.end() );
}

const std::string Context::GetSourceLine( const size_t line_num ) const {
ASSERT( line_num > 0 && line_num <= m_source_lines->size(), "source line overflow" );
return m_source_lines->at( line_num - 1 );
if ( !( line_num > 0 && line_num <= m_source_lines.size() ) ) {
return "<overflow>";
}
ASSERT( line_num > 0 && line_num <= m_source_lines.size(), "source line overflow" );
return m_source_lines.at( line_num - 1 );
}

const Context::source_lines_t& Context::GetSourceLines() const {
return m_source_lines;
}

Context* const Context::CreateFunctionScope(
const std::string& function_name,
Context* const Context::ForkContext(
const si_t& call_si,
const std::vector< std::string > parameters,
const type::Callable::function_arguments_t& arguments
) const {
ASSERT( parameters.size() == arguments.size(), "expected " + std::to_string( parameters.size() ) + " arguments, found " + std::to_string( arguments.size() ) );
auto* result = new Context( this, m_source_lines );
auto* result = new Context( this, m_source_lines, call_si );
result->m_scopes[ 0 ]->m_variables = m_scopes[ 0 ]->m_variables; // functions have access to parent variables but nothing else
result->PushScope(); // functions can have local variables
for ( size_t i = 0 ; i < parameters.size() ; i++ ) { // inject passed arguments
Expand All @@ -94,4 +111,12 @@ Context* const Context::CreateFunctionScope(
return result;
}

void Context::JoinContext( Context* const other ) const {
for ( const auto& scope : m_scopes ) {
for ( const auto& it : scope->m_variables ) {
other->UpdateVariable( it.first, it.second, true );
}
}
}

}
18 changes: 11 additions & 7 deletions src/gse/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,37 @@ CLASS( Context, base::Base )

typedef std::vector< std::string > source_lines_t;

Context( const Context* parent_context, const source_lines_t* source_lines );
Context( const Context* parent_context, const source_lines_t& source_lines, const si_t& si );
~Context();

const Value GetVariable( const std::string& name );
void CreateVariable( const std::string& name, const Value& value );
void UpdateVariable( const std::string& name, const Value& value );
void UpdateVariable( const std::string& name, const Value& value, bool create_if_missing = false );
void PushScope();
void PopScope();
const size_t GetScopeDepth() const;
const Context* GetParentContext() const;
void SetSI( const si_t& si );

const si_t& GetSI() const;

void AddSourceLine( const std::string& source_line );
void AddSourceLines( const source_lines_t& source_lines );
const std::string GetSourceLine( const size_t line_num ) const;
const source_lines_t& GetSourceLines() const;

Context* const CreateFunctionScope(
const std::string& function_name,
Context* const ForkContext(
const si_t& call_si,
const std::vector< std::string > parameters,
const type::Callable::function_arguments_t& arguments
) const;
void JoinContext( Context* const other ) const;

private:

const Context* m_parent_context;
const source_lines_t* m_source_lines;
source_lines_t m_source_lines;

si_t m_si = {};
const si_t m_si = {};

class Scope {
public:
Expand Down
23 changes: 23 additions & 0 deletions src/gse/Exception.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "Exception.h"

#include "util/String.h"

namespace gse {

const Exception::backtrace_t Exception::GetBacktraceAndCleanup( const Context* const current_ctx ) {
ASSERT_NOLOG( !contexts_freed, "contexts already freed" );
const Context* ctx = context, * pt;
#define FORMAT_SI( _si ) "\tat " + (_si).file + ":" + std::to_string( (_si).from.line ) + ": " + util::String::TrimCopy( ctx->GetSourceLine( (_si).from.line ) )
backtrace_t backtrace = { FORMAT_SI( si ) };
while ( ctx != current_ctx ) {
backtrace.push_back( FORMAT_SI( ctx->GetSI() ) );
pt = ctx->GetParentContext();
delete ctx;
ctx = pt;
}
#undef FORMAT_SI
contexts_freed = true;
return backtrace;
}

}
20 changes: 17 additions & 3 deletions src/gse/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,35 @@
#include <stdexcept>
#include <vector>

#include "program/Element.h"

namespace gse {

class Context;

class Exception : public std::runtime_error {
public:

Exception( const std::string& class_name, const std::string& reason, const std::vector< std::string >& backtrace )
typedef std::vector< std::string > backtrace_t;

Exception( const std::string& class_name, const std::string& reason, const gse::Context* context, const si_t& si )
: std::runtime_error( "[" + class_name + "] " + reason )
, class_name( class_name )
, reason( reason )
, backtrace( backtrace ) {}
, context( context )
, si( si ) {}

const std::string class_name;
const std::string reason;
const std::vector< std::string > backtrace;
const Context* context;
const si_t si;

bool contexts_freed = false;

const backtrace_t GetBacktraceAndCleanup( const Context* const current_ctx );

};

}

#include "Context.h"
19 changes: 11 additions & 8 deletions src/gse/GSE.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "GSE.h"

#include "parser/GJS.h"
#include "parser/JS.h"
#include "runner/Interpreter.h"
#include "util/FS.h"

Expand All @@ -22,13 +22,14 @@ GSE::~GSE() {
}
}

parser::Parser* GSE::GetParser( const std::string& filename, const std::string& source ) const {
parser::Parser* GSE::GetParser( const std::string& filename, const std::string& source, const size_t initial_line_num ) const {
parser::Parser* parser = nullptr;
const auto extension = util::FS::GetExtension( filename );
if ( extension == ".gjs" ) {
NEW( parser, parser::GJS, filename, source );
const auto extensions = util::FS::GetExtensions( filename );
ASSERT( extensions.size() == 2 && extensions[ 0 ] == ".gls", "unsupported file name ( " + filename + "), expected: *.gls.*" );
if ( extensions[ 1 ] == ".js" ) {
NEW( parser, parser::JS, filename, source, initial_line_num );
}
ASSERT( parser, "could not find parser for '" + extension + "' extension" );
ASSERT( parser, "could not find parser for '.gls" + extensions[ 1 ] + "' extension" );
return parser;
}

Expand All @@ -39,7 +40,7 @@ const runner::Runner* GSE::GetRunner() const {

void GSE::AddModule( const std::string& path, type::Callable* module ) {
if ( m_modules.find( path ) != m_modules.end() ) {
throw Exception( "GSE_InternalError", "module path '" + path + "' already taken", {} ); // ?
throw Exception( "GSE_InternalError", "module path '" + path + "' already taken", nullptr, {} ); // ?
}
Log( "Adding module: " + path );
m_modules[ path ] = module;
Expand All @@ -54,7 +55,9 @@ void GSE::Run() {
auto it = m_modules.find( i );
ASSERT( it != m_modules.end(), "required module missing: " + i );
Log( "Executing module: " + it->first );
it->second->Run( this, {} );
Context context( nullptr, {}, {} );
it->second->Run( &context, {}, {} );
// TODO: cleanup context properly or don't
}

Log( "GSE finished" );
Expand Down
2 changes: 1 addition & 1 deletion src/gse/GSE.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ CLASS( GSE, base::Base )
GSE();
virtual ~GSE();

parser::Parser* GetParser( const std::string& filename, const std::string& source ) const;
parser::Parser* GetParser( const std::string& filename, const std::string& source, const size_t initial_line_num = 1 ) const;
const runner::Runner* GetRunner() const;

void AddModule( const std::string& path, type::Callable* module );
Expand Down
2 changes: 1 addition & 1 deletion src/gse/builtin/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace gse {
namespace builtin {

Value Builtins::Run( GSE* gse, const Callable::function_arguments_t& arguments ) {
Value Builtins::Run( const Context* ctx, const si_t& call_si, const Callable::function_arguments_t& arguments ) {

return VALUE( type::Null );
}
Expand Down
2 changes: 1 addition & 1 deletion src/gse/builtin/Builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace gse {
namespace builtin {

class Builtins : public callable::Native {
Value Run( GSE* gse, const Callable::function_arguments_t& arguments ) override;
Value Run( const Context* ctx, const si_t& call_si, const Callable::function_arguments_t& arguments ) override;
};

}
Expand Down
2 changes: 1 addition & 1 deletion src/gse/builtin/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace gse {
namespace builtin {

class Console : public callable::Native {
Value Run( GSE* gse, const Callable::function_arguments_t& arguments ) override;
Value Run( const Context* ctx, const si_t& call_si, const Callable::function_arguments_t& arguments ) override;
};

}
Expand Down
2 changes: 1 addition & 1 deletion src/gse/parser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SET( SRC ${SRC}

${PWD}/Parser.cpp
${PWD}/GJS.cpp
${PWD}/JS.cpp

PARENT_SCOPE )
Loading

0 comments on commit 9e860fc

Please sign in to comment.