This commit is contained in:
Valk Richard Li 2020-08-02 01:01:48 -07:00 committed by GitHub
parent 0b0b0daf11
commit b701637db6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 28 deletions

View File

@ -4,6 +4,7 @@ nasal_resource resource;
nasal_lexer lexer;
nasal_parse parse;
std::string command;
nasal_runtime runtime;
void help()
{
@ -81,6 +82,25 @@ void ast_func()
std::cout<<">> [lexer] error(s) occurred,stop.\n";
return;
}
void runtime_start()
{
lexer.scanner(resource.get_file());
if(!lexer.get_error())
{
parse.set_toklist(lexer.get_token_list());
parse.main_process();
if(parse.get_error())
std::cout<<">> [parse] error(s) occurred,stop.\n";
else
{
runtime.set_root(parse.get_root());
runtime.run();
}
}
else
std::cout<<">> [lexer] error(s) occurred,stop.\n";
return;
}
int main()
{
@ -135,9 +155,7 @@ int main()
else if(command=="ast")
ast_func();
else if(command=="run")
{
;
}
runtime_start();
else if(command=="logo")
logo();
else if(command=="exit")

View File

@ -29,8 +29,8 @@ public:
class nasal_function
{
private:
// this int points to the space in nasal_vm::memory_manager_memory
std::list<int> closures;
// this int points to the space in nasal_vm::garbage_collector_memory
int closure_addr;
nasal_ast function_tree;
public:
nasal_function();
@ -180,15 +180,13 @@ void nasal_hash::del_elem(std::string key)
/*functions of nasal_function*/
nasal_function::nasal_function()
{
closures.clear();
closure_addr=-1;
function_tree.clear();
return;
}
nasal_function::~nasal_function()
{
for(std::list<int>::iterator iter=closures.begin();iter!=closures.end();++iter)
nasal_vm.mem_free(*iter);
closures.clear();
nasal_vm.del_reference(closure_addr);
function_tree.clear();
return;
}

View File

@ -1,46 +1,56 @@
#ifndef __NASAL_RUNTIME_H__
#define __NASAL_RUNTIME_H__
enum runtime_returned_state
{
rt_return=1,
rt_break,
rt_continue,
rt_error,
rt_exit_without_error
};
class nasal_runtime
{
private:
// global_scope_address is address in garbage_collector_memory,NOT memory_manager_memory
// global_scope_address and function_return_address are addresses in garbage_collector_memory
int function_returned_address;
int global_scope_address;
nasal_ast root;
std::stack<int> function_position;
std::stack<int> running_stack;
std::stack<int> closure_stack;
// process functions are private
int main_progress();
int block_progress();
int loop_progress();
int conditional_progress();
int function_progress();
int calculation();
public:
nasal_runtime();
~nasal_runtime();
void set_root(nasal_ast&);
void run();
};
nasal_runtime::nasal_runtime()
{
root.clear();
while(!function_position.empty())
function_position.pop();
while(!running_stack.empty())
running_stack.pop();
while(!closure_stack.empty())
closure_stack.pop();
this->root.clear();
this->global_scope_address=-1;
return;
}
nasal_runtime::~nasal_runtime()
{
root.clear();
while(!function_position.empty())
function_position.pop();
while(!running_stack.empty())
running_stack.pop();
while(!closure_stack.empty())
closure_stack.pop();
this->root.clear();
this->global_scope_address=-1;
return;
}
void nasal_runtime::set_root(nasal_ast& parse_result)
{
this->root=parse_result;
return;
}
void nasal_runtime::run()
{
global_scope_address=nasal_vm.gc_alloc();
this->global_scope_address=nasal_vm.gc_alloc();
nasal_vm.gc_get(global_scope_address).set_type(vm_closure);
nasal_vm.gc_get(global_scope_address).get_closure().add_scope();
@ -48,5 +58,35 @@ void nasal_runtime::run()
nasal_vm.del_reference(global_scope_address);
return;
}
int nasal_runtime::main_progress()
{
int ret_state=rt_exit_without_error;
return ret_state;
}
int nasal_runtime::block_progress()
{
int ret_state=rt_exit_without_error;
return ret_state;
}
int nasal_runtime::loop_progress()
{
int ret_state=rt_exit_without_error;
return ret_state;
}
int nasal_runtime::conditional_progress()
{
int ret_state=rt_exit_without_error;
return ret_state;
}
int nasal_runtime::function_progress()
{
int ret_state=rt_exit_without_error;
return ret_state;
}
int nasal_runtime::calculation()
{
int ret_state=rt_exit_without_error;
return ret_state;
}
#endif