This commit is contained in:
Valk Richard Li 2020-07-27 02:30:39 -07:00 committed by GitHub
parent 78683ffbd4
commit 580a5b264d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 109 additions and 16 deletions

View File

@ -170,4 +170,15 @@ void error_info(int line,int error_type,std::string error_str="")
return;
}
enum runtime_scalar_type
{
vm_nil=0,
vm_number,
vm_string,
vm_closure,
vm_function,
vm_vector,
vm_hash
};
#endif

View File

@ -3,12 +3,55 @@
#define GC_BLK_SIZE 128
class nasal_number;
class nasal_string;
class nasal_vector;
class nasal_hash;
class nasal_function;
class nasal_closure;
class nasal_vector
{
private:
// this int points to the space in nasal_vm_memory_manager
std::vector<int> elems;
public:
nasal_vector()
{
elems.clear();
return;
}
};
class nasal_hash
{
private:
// this int points to the space in nasal_vm_memory_manager
std::map<std::string,int> elems;
public:
nasal_hash()
{
elems.clear();
return;
}
};
class nasal_function
{
private:
// this int points to the space in nasal_vm_memory_manager
std::list<int> closures;
nasal_ast function_tree;
public:
nasal_function()
{
closures.clear();
function_tree.clear();
return;
}
};
class nasal_closure
{
private:
std::map<std::string,int> elems;
public:
nasal_closure()
{
elems.clear();
return;
}
};
class nasal_scalar
{
@ -18,10 +61,49 @@ private:
public:
nasal_scalar()
{
type=0;
scalar_ptr=(void*)NULL;
this->type=vm_nil;
this->scalar_ptr=(void*)NULL;
return;
}
~nasal_scalar()
{
switch(this->type)
{
case vm_nil:break;
case vm_number: delete (double*)(this->scalar_ptr); break;
case vm_string: delete (std::string*)(this->scalar_ptr); break;
case vm_vector: delete (nasal_vector*)(this->scalar_ptr); break;
case vm_hash: delete (nasal_hash*)(this->scalar_ptr); break;
case vm_function: delete (nasal_function*)(this->scalar_ptr); break;
case vm_closure: delete (nasal_closure*)(this->scalar_ptr); break;
}
return;
}
bool set_type(int nasal_scalar_type)
{
bool ret=true;
this->type=nasal_scalar_type;
switch(nasal_scalar_type)
{
case vm_nil: this->scalar_ptr=(void*)NULL; break;
case vm_number: this->scalar_ptr=(void*)(new double); break;
case vm_string: this->scalar_ptr=(void*)(new std::string); break;
case vm_vector: this->scalar_ptr=(void*)(new nasal_vector); break;
case vm_hash: this->scalar_ptr=(void*)(new nasal_hash); break;
case vm_function: this->scalar_ptr=(void*)(new nasal_function); break;
case vm_closure: this->scalar_ptr=(void*)(new nasal_closure); break;
default:
std::cout<<">> [scalar] error scalar type: "<<nasal_scalar_type<<std::endl;
this->type=vm_nil;
this->scalar_ptr=(void*)NULL;
ret=false;
break;
}
return ret;
}
// +-*/~
// =
// unary - !
};
struct gc_unit

View File

@ -3,26 +3,26 @@
#define MEM_BLK_SIZE 128
class nasal_vm
class nasal_vm_memory_manager
{
private:
std::queue<int> free_space;
std::vector<int*> memory;
public:
nasal_vm();
~nasal_vm();
nasal_vm_memory_manager();
~nasal_vm_memory_manager();
int nas_alloc();
int nas_free(int);
int nas_store(int,int);
};
nasal_vm::nasal_vm()
nasal_vm_memory_manager::nasal_vm_memory_manager()
{
memory.clear();
return;
}
nasal_vm::~nasal_vm()
nasal_vm_memory_manager::~nasal_vm_memory_manager()
{
int size=memory.size();
for(int i=0;i<size;++i)
@ -33,7 +33,7 @@ nasal_vm::~nasal_vm()
return;
}
int nasal_vm::nas_alloc()
int nasal_vm_memory_manager::nas_alloc()
{
if(free_space.empty())
{
@ -48,7 +48,7 @@ int nasal_vm::nas_alloc()
return ret;
}
int nasal_vm::nas_free(int space_num)
int nasal_vm_memory_manager::nas_free(int space_num)
{
if(0<=space_num && space_num<memory.size()*MEM_BLK_SIZE)
free_space.push(space_num);
@ -60,7 +60,7 @@ int nasal_vm::nas_free(int space_num)
return 1;
}
int nasal_vm::nas_store(int mem_space,int value_space)
int nasal_vm_memory_manager::nas_store(int mem_space,int value_space)
{
// be careful! this process doesn't check if this mem_space is in use.
if(0<=mem_space && mem_space<memory.size()*MEM_BLK_SIZE)