Skip to content
This repository has been archived by the owner on Jul 2, 2022. It is now read-only.

Commit

Permalink
src_t field id is now an integer value which works in combination wit…
Browse files Browse the repository at this point in the history
…h vm.srclist vector; the field id will always be unique for each source
  • Loading branch information
Electrux committed Nov 21, 2019
1 parent 32d8712 commit 22b3fe3
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 12 deletions.
6 changes: 4 additions & 2 deletions modules/eth/vm.cpp
Expand Up @@ -44,6 +44,7 @@ var_evm_t::var_evm_t( vm_state_t * vm, const int parse_ctr )
var_evm_t::~var_evm_t()
{
if( !m_copied ) {
m_vm->srclist.clear();
m_vm->srcstack.pop_back();
delete m_vm;
}
Expand Down Expand Up @@ -87,13 +88,14 @@ var_base_t * vm_create( vm_state_t & vm, func_call_data_t & fcd )
src_t * src = new src_t( true );
src->ptree = new parse_tree_t;
src->dir = vm.srcstack.back()->dir;
src->id = 0;
src->file = fcd.args.size() > 1 ? fcd.args[ 0 ]->to_str() : "<repl>";
src->id = src->file;

vm_state_t * v = new vm_state_t();

v->flags = vm.flags;
v->srcstack.push_back( src );
v->srclist.push_back( src->file );
v->srcs[ src->id ] = src;
v->vars->add( "nil", v->nil );

Expand Down Expand Up @@ -143,7 +145,7 @@ var_base_t * vm_exec_code( vm_state_t & vm, func_call_data_t & fcd )
src_t * s = v->srcstack.back();
src_t src( false );
src.file = s->file;
src.id = s->file;
src.id = s->id;

size_t code_size = s->code.size();
size_t toks_size = s->toks.size();
Expand Down
9 changes: 5 additions & 4 deletions src/FE/Ethereal.hpp
Expand Up @@ -20,9 +20,10 @@

struct src_t
{
// id is unique and used as the srcs map's key
// file is the name (no dir part) of the source file
std::string id;
// id is unique and used for mapping variables to sources
// file is the name (no dir part) of the source file and is used
// as key for srcs map.
int id;
std::string file;
std::string dir;
std::vector< std::string > code;
Expand Down Expand Up @@ -57,6 +58,6 @@ struct src_t
src.bcode.push_back( { m_tok_ctr, line, col, IC_REM_SCOPE, { OP_INT, "1" } } ); \
if( src.block_depth.back().size() > 0 ) --src.block_depth.back().back()

typedef std::unordered_map< std::string, src_t * > srcs_t;
typedef std::unordered_map< int, src_t * > srcs_t;

#endif // ETHEREAL_HPP
4 changes: 3 additions & 1 deletion src/FE/Main.cpp
Expand Up @@ -48,8 +48,8 @@ int main( int argc, char ** argv )
const std::string main_src_str = args[ 0 ].substr( last_slash_loc + 1 );

src_t * main_src = new src_t( true );
main_src->id = 0;
main_src->file = main_src_str;
main_src->id = "main_src_" + main_src->file;
main_src->dir = src_dir;
err = tokenize( * main_src );
if( err != E_OK ) goto cleanup;
Expand Down Expand Up @@ -98,6 +98,7 @@ int main( int argc, char ** argv )
vm_state_t vm;
vm.flags = flags;
vm.srcstack.push_back( main_src );
vm.srclist.push_back( main_src->file );
vm.srcs[ main_src->id ] = main_src;
if( !set_init_mods( vm ) ) { err = E_VM_FAIL; goto end; }
std::vector< var_base_t * > arg_vec;
Expand All @@ -113,6 +114,7 @@ int main( int argc, char ** argv )
vm.vars->add( "false", new var_bool_t( false, 0 ) );
vm.vars->add( "nil", vm.nil );
err = vm_exec( vm );
vm.srclist.clear();
vm.srcstack.pop_back();
goto end;
}
Expand Down
2 changes: 2 additions & 0 deletions src/VM/Core.hpp
Expand Up @@ -18,6 +18,7 @@
extern const char * LIB_EXT;

typedef std::vector< src_t * > src_stack_t;
typedef std::vector< std::string > src_list_t;

typedef std::unordered_map< std::string, functions_t > type_funcs_t;

Expand All @@ -36,6 +37,7 @@ struct vm_state_t
var_nil_t * nil;

src_stack_t srcstack;
src_list_t srclist;
srcs_t srcs;

vars_t * vars;
Expand Down
4 changes: 2 additions & 2 deletions src/VM/ExecInternal.cpp
Expand Up @@ -445,7 +445,7 @@ int exec_internal( vm_state_t & vm, long begin, long end, var_base_t * ret )
goto fail;
}
vm.typefuncs[ member_of ].add( function_t{ name, args_min, args_max, args, FnType::LANG,
{ .langfn = { src.id.c_str(), blk.beg, blk.end } },
{ .langfn = { src.id, blk.beg, blk.end } },
false } );
}
if( member_ofs.size() == 0 ) {
Expand All @@ -455,7 +455,7 @@ int exec_internal( vm_state_t & vm, long begin, long end, var_base_t * ret )
goto fail;
}
vm.funcs.add( function_t{ name, args_min, args_max, args, FnType::LANG,
{ .langfn = { src.id.c_str(), blk.beg, blk.end } },
{ .langfn = { src.id, blk.beg, blk.end } },
false } );
}
break;
Expand Down
2 changes: 1 addition & 1 deletion src/VM/Functions.hpp
Expand Up @@ -22,7 +22,7 @@ struct func_call_data_t;
typedef var_base_t * ( * modfnptr_t )( vm_state_t & vm, func_call_data_t & fcd );
struct langfn_t
{
const char * src;
int src;
int beg, end;
};

Expand Down
15 changes: 13 additions & 2 deletions src/VM/LoadFile.cpp
Expand Up @@ -12,6 +12,8 @@

#include "LoadFile.hpp"

static bool src_exists( const srcs_t & srcs, const std::string & file );

int load_src( vm_state_t & vm, const std::string & file )
{
src_t & src = * vm.srcstack.back();
Expand All @@ -20,7 +22,7 @@ int load_src( vm_state_t & vm, const std::string & file )

const std::string new_src_file = file.substr( last_slash_loc + 1 );

if( vm.srcs.find( new_src_file ) != vm.srcs.end() ) {
if( src_exists( vm.srcs, new_src_file ) ) {
return E_OK;
}

Expand All @@ -30,8 +32,8 @@ int load_src( vm_state_t & vm, const std::string & file )

parse_tree_t * ptree = nullptr;
src_t * new_src = new src_t( false );
new_src->id = vm.srclist.size();
new_src->file = new_src_file;
new_src->id = new_src_file;
new_src->dir = src_dir;
err = tokenize( * new_src );
if( err != E_OK ) goto cleanup;
Expand Down Expand Up @@ -78,6 +80,7 @@ int load_src( vm_state_t & vm, const std::string & file )
// actual code execution
if( !( vm.flags & OPT_C ) && !( vm.flags & OPT_D ) ) {
vm.srcstack.push_back( new_src );
vm.srclist.push_back( new_src_file );
vm.srcs[ new_src->id ] = new_src;
vm.vars->add_zero( vm.vars->layer_size() );
err = vm_exec( vm );
Expand All @@ -89,4 +92,12 @@ int load_src( vm_state_t & vm, const std::string & file )
cleanup:
if( err != E_OK ) delete new_src;
return err;
}

static bool src_exists( const srcs_t & srcs, const std::string & file )
{
for( auto & item : srcs ) {
if( item.second->file == file && !item.second->is_main_src ) return true;
}
return false;
}

0 comments on commit 22b3fe3

Please sign in to comment.