This commit is contained in:
Valk Richard Li 2020-07-12 03:05:12 -07:00 committed by GitHub
parent 8b4514ab2e
commit 374630b06e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 211 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#ifndef __NASAL_H__ #ifndef __NASAL_H__
#define __NASAL_H__ #define __NASAL_H__
#include <stdio.h>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <cstring> #include <cstring>
@ -21,5 +22,7 @@
#include "nasal_lexer.h" #include "nasal_lexer.h"
#include "nasal_ast.h" #include "nasal_ast.h"
#include "nasal_parse.h" #include "nasal_parse.h"
#include "nasal_vm.h"
#include "nasal_gc.h"
#endif #endif

97
version3.0/nasal_gc.h Normal file
View File

@ -0,0 +1,97 @@
#ifndef __NASAL_GC_H__
#define __NASAL_GC_H__
#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_scalar
{
private:
int type;
void* scalar_ptr;
public:
nasal_scalar()
{
type=0;
scalar_ptr=(void*)NULL;
return;
}
};
struct gc_unit
{
bool collected;
char ref_cnt;
nasal_scalar elem;
gc_unit()
{
collected=true;
ref_cnt=0;
return;
}
};
class nasal_gc
{
private:
std::queue<int> free_space;
std::vector<gc_unit*> memory;
public:
nasal_gc();
~nasal_gc();
int gc_alloc();
int add_ref(int);
int del_ref(int);
};
nasal_gc::nasal_gc()
{
memory.clear();
return;
}
nasal_gc::~nasal_gc()
{
int size=memory.size();
for(int i=0;i<size;++i)
delete []memory[i];
while(!free_space.empty())
free_space.pop();
return;
}
int nasal_gc::gc_alloc()
{
if(free_space.empty())
{
gc_unit* new_block=new gc_unit[GC_BLK_SIZE];
memory.push_back(new_block);
int mem_size=memory.size();
for(int i=(mem_size-1)*GC_BLK_SIZE;i<mem_size*GC_BLK_SIZE;++i)
free_space.push(i);
}
int ret=free_space.front();
free_space.pop();
return ret;
}
int nasal_gc::add_ref(int mem_space)
{
if(0<=mem_space && mem_space<memory.size()*GC_BLK_SIZE)
++memory[mem_space/GC_BLK_SIZE][mem_space%GC_BLK_SIZE].ref_cnt;
else
{
std::cout<<">> [gc] add_ref:unexpected memory \'"<<mem_space<<"\'."<<std::endl;
return 0;
}
return 1;
}
#endif

View File

@ -1,6 +1,41 @@
#ifndef __NASAL_PARSE_H__ #ifndef __NASAL_PARSE_H__
#define __NAsAL_PARSE_H__ #define __NAsAL_PARSE_H__
/*
_,,,_
.' `'.
/ ____ \ Fucking Nasal Parser
| .-'_ _\/ /
\_/ a a| /
(,` \ | .----.
| -' | /| '--.
\ '= / || ]| `-.
/`-.__.' || ]| ::|
.-'`-.__ \__ || ]| ::|
/ `` `. || ]| ::|
_ | \ \ \ \| ]| .-'
/ \| \ | \ L.__ .--'(
| |\ `. | \ ,---|_ \---------,
| | '. './\ \/ .--._|=- |_ /|
| \ '. `'.'. /`\/ .-' '. / |
| | `'. `;-:-;`)| |-./ |
| /_ `'--./_ ` )/'-------------')/) |
\ | `""""----"`\//`""`/,===..'`````````/ ( |
| | / `---` `===' / ) |
/ \ / / ( |
| '------. |'--------------------'| ) |
\ `-| | / |
`--...,______| | ( |
| | | | ) ,|
| | | | ( /||
| | | | )/ `"
/ \ | | (/
.' /I\ '.| | /)
.-'_.'/ \'. | | /
``` `"""` `| .-------------------.||
`"` `"`
*/
class nasal_parse class nasal_parse
{ {
#ifndef error_line #ifndef error_line

76
version3.0/nasal_vm.h Normal file
View File

@ -0,0 +1,76 @@
#ifndef __NASAL_VM_H__
#define __NASAL_VM_H__
#define MEM_BLK_SIZE 128
class nasal_vm
{
private:
std::queue<int> free_space;
std::vector<int*> memory;
public:
nasal_vm();
~nasal_vm();
int nas_alloc();
int nas_free(int);
int nas_store(int,int);
};
nasal_vm::nasal_vm()
{
memory.clear();
return;
}
nasal_vm::~nasal_vm()
{
int size=memory.size();
for(int i=0;i<size;++i)
delete []memory[i];
memory.clear();
while(!free_space.empty())
free_space.pop();
return;
}
int nasal_vm::nas_alloc()
{
if(free_space.empty())
{
int* new_block=new int[MEM_BLK_SIZE];
memory.push_back(new_block);
int mem_size=memory.size();
for(int i=(mem_size-1)*MEM_BLK_SIZE;i<mem_size*MEM_BLK_SIZE;++i)
free_space.push(i);
}
int ret=free_space.front();
free_space.pop();
return ret;
}
int nasal_vm::nas_free(int space_num)
{
if(0<=space_num && space_num<memory.size()*MEM_BLK_SIZE)
free_space.push(space_num);
else
{
std::cout<<">> [vm] nas_free:unexpected memory \'"<<space_num<<"\'."<<std::endl;
return 0;
}
return 1;
}
int nasal_vm::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)
memory[mem_space/MEM_BLK_SIZE][mem_space%MEM_BLK_SIZE]=value_space;
else
{
std::cout<<">> [vm] nas_store:unexpected memory \'"<<mem_space<<"\'."<<std::endl;
return 0;
}
return 1;
}
#endif