update old data structures

This commit is contained in:
ValKmjolnir 2024-11-22 00:36:41 +08:00
parent 563617b6d3
commit c6e5e2eb29
5 changed files with 100 additions and 169 deletions

View File

@ -3,80 +3,25 @@ use std::str::{ str };
use std::io::{ readfile_into_string, io };
use std::libc::{ malloc, realloc, free, itoa, strlen };
use std::fs::{ fs };
struct file_lines {
source: str*,
size: i64,
capacity: i64
}
impl file_lines {
pub func init(self) {
self->source = malloc((128 => u64)*str::__size__()) => str*;
self->size = 0;
self->capacity = 128;
for (var index = 0; index < self->capacity; index += 1) {
self->source[index].init();
}
}
pub func delete(self) {
for (var index = 0; index < self->size; index += 1) {
self->source[index].delete();
}
free(self->source => i8*);
}
func clear(self) {
for (var index = 0; index < self->size; index += 1) {
self->source[index].clear();
}
self->size = 0;
}
func expand_capacity(self) {
self->capacity *= 2;
self->source = realloc(
self->source => i8*,
(self->capacity => u64)*str::__size__()
) => str*;
for (var index = self->size; index < self->capacity; index += 1) {
self->source[index].init();
}
}
pub func push(self, line: str*) {
if (self->size == self->capacity) {
self->expand_capacity();
}
self->source[self->size].append_str(line);
self->size += 1;
}
}
use std::vec::{ vec };
pub struct report {
filename: str,
source: file_lines,
source: vec<str>,
error_count: i64
}
impl report {
pub func new() -> report* {
var res = report::__alloc__();
res->filename.init();
res->source.init();
res->filename = str::instance();
res->source = vec<str>::instance();
res->error_count = 0;
return res;
}
pub func delete(self) {
io::stderr().out("[").green().out("report").reset()
.out("] delete report::filename\n");
self->filename.delete();
io::stderr().out("[").green().out("report").reset()
.out("] delete report::source\n");
self->source.delete();
}
}
@ -94,27 +39,25 @@ impl report {
self->filename.clear();
self->filename.append_i8_vec(filename);
var source = str::new();
readfile_into_string(filename, source);
var source = str::instance();
readfile_into_string(filename, source.__ptr__());
var tmp = str::new();
for (var pos = 0 => u64; pos < source->size; pos += 1 => u64) {
if (source->get(pos)=='\n') {
self->source.push(tmp);
tmp->clear();
var tmp = str::instance();
for (var pos = 0 => u64; pos < source.size; pos += 1 => u64) {
if (source.get(pos)=='\n') {
self->source.push(tmp.__ptr__());
tmp.clear();
continue;
}
tmp->append_char(source->get(pos));
tmp.append_char(source.get(pos));
}
if (tmp->size > (0 => u64)) {
self->source.push(tmp);
tmp->clear();
if (tmp.size > (0 => u64)) {
self->source.push(tmp.__ptr__());
tmp.clear();
}
source->delete();
free(source => i8*);
tmp->delete();
free(tmp => i8*);
source.delete();
tmp.delete();
}
}
@ -140,14 +83,13 @@ impl report {
}
func to_str(num: i64, padding: i64) -> str* {
var tmp = malloc(256 => u64);
var tmp = [i8; 256];
itoa(num, tmp, 10);
var res = str::new();
for (var index = strlen(tmp); index < padding; index += 1) {
res->append_char(' ');
}
res->append_i8_vec(tmp);
free(tmp);
return res;
}
@ -158,12 +100,12 @@ impl report {
io::stderr().cyan().out(pad_str->c_str).out(" | ").reset().endln();
while(index <= location->end_line && index < self->source.size) {
var tmp_str_const = self->source.source[index].c_str;
var len = strlen(tmp_str_const);
while(index <= location->end_line && (index => u64) < self->source.size) {
var this_line = self->source.get(index => u64)->c_str;
var len = self->source.get(index => u64)->size => i64;
var pad_num = report::to_str(index + 1, max_line_number_length);
io::stderr().cyan().out(pad_num->c_str).out(" | ")
.reset().out(tmp_str_const).endln();
.reset().out(this_line).endln();
var underline = str::new();
if (index == location->begin_line) {
@ -172,16 +114,16 @@ impl report {
i == location->end_column) {
break;
}
if (i < location->begin_column && tmp_str_const[i]!='\t') {
if (i < location->begin_column && this_line[i]!='\t') {
underline->append_char(' ');
}
if (i < location->begin_column && tmp_str_const[i]=='\t') {
if (i < location->begin_column && this_line[i]=='\t') {
underline->append_char('\t');
}
if (i >= location->begin_column && tmp_str_const[i]!='\t') {
if (i >= location->begin_column && this_line[i]!='\t') {
underline->append_char('^');
}
if (i >= location->begin_column && tmp_str_const[i]=='\t') {
if (i >= location->begin_column && this_line[i]=='\t') {
underline->append_i8_vec("^^^^");
}
}
@ -192,24 +134,24 @@ impl report {
i < location->begin_column) {
continue;
}
if (i < location->end_column && tmp_str_const[i]!='\t') {
if (i < location->end_column && this_line[i]!='\t') {
underline->append_char('^');
}
if (i < location->end_column && tmp_str_const[i]=='\t') {
if (i < location->end_column && this_line[i]=='\t') {
underline->append_i8_vec("^^^^");
}
if (i >= location->end_column && tmp_str_const[i]!='\t') {
if (i >= location->end_column && this_line[i]!='\t') {
underline->append_char(' ');
}
if (i >= location->end_column && tmp_str_const[i]=='\t') {
if (i >= location->end_column && this_line[i]=='\t') {
underline->append_char('\t');
}
}
} else {
for (var i = 0; i < len; i += 1) {
if (tmp_str_const[i]=='\t') {
if (this_line[i]=='\t') {
underline->append_i8_vec("^^^^");
} elsif (tmp_str_const[i]!='\r' && tmp_str_const[i]!='\n') {
} elsif (this_line[i]!='\r' && this_line[i]!='\n') {
underline->append_char('^');
}
}

View File

@ -2,8 +2,8 @@ use std::str::str;
use std::libc::{ malloc, realloc, free, exit };
use std::io::{ readfile_into_string, io };
use std::fs::{ fs };
use err::report::*;
use err::span::span;
use err::report::{ report };
use err::span::{ span };
pub enum tok_kind {
tok_null, // reserved
@ -180,11 +180,7 @@ impl lexer {
}
pub func delete(self) {
io::stderr().out("[").green().out("lexer").reset().out("]")
.out(" delete lexer::filename\n");
self->filename.delete();
io::stderr().out("[").green().out("lexer").reset().out("]")
.out(" delete lexer::toks\n");
self->toks->delete();
free(self->toks => i8*);
}

View File

@ -26,13 +26,6 @@ func compile(option: cli_option) {
return;
}
err.out("[").green().out("colgm").reset().out("]");
err.out(" source file : ").out(option.input_file).out("\n");
err.out("[").green().out("colgm").reset().out("]");
err.out(" output file : ").out(option.output_file).out("\n");
err.out("[").green().out("colgm").reset().out("]");
err.out(" library path : ").out(option.library_path).out("\n");
var cc = compiler::instance();
cc.pkg->library_path = option.library_path;

View File

@ -25,8 +25,8 @@ impl pair<K, V> {
pub struct hashmap<K, V> {
size: u64,
data_capacity: u64,
data: list<pair<K, V>>*
bucket_capacity: u64,
bucket: list<pair<K, V>>*
}
impl hashmap<K, V> {
@ -44,30 +44,30 @@ impl hashmap<K, V> {
func init(self) {
self->size = 0 => u64;
self->data_capacity = 1024 => u64;
self->data = malloc(self->data_capacity
* list<pair<K, V>>::__size__())
=> list<pair<K, V>>*;
for (var i = 0 => u64; i < self->data_capacity; i += 1 => u64) {
self->data[i] = list<pair<K, V>>::instance();
self->bucket_capacity = 1024 => u64;
self->bucket = malloc(self->bucket_capacity
* list<pair<K, V>>::__size__())
=> list<pair<K, V>>*;
for (var i = 0 => u64; i < self->bucket_capacity; i += 1 => u64) {
self->bucket[i] = list<pair<K, V>>::instance();
}
}
pub func delete(self) {
for (var i = 0 => u64; i < self->data_capacity; i += 1 => u64) {
self->data[i].delete();
for (var i = 0 => u64; i < self->bucket_capacity; i += 1 => u64) {
self->bucket[i].delete();
}
free(self->data => i8*);
free(self->bucket => i8*);
self->size = 0 => u64;
self->data_capacity = 0 => u64;
self->data = nil => list<pair<K, V>>*;
self->bucket_capacity = 0 => u64;
self->bucket = nil => list<pair<K, V>>*;
}
}
impl hashmap<K, V> {
pub func has(self, key: K*) -> bool {
var hash = key->hash() % (1024 => u64);
var bucket = self->data[hash].__ptr__();
var bucket = self->bucket[hash].__ptr__();
for (var i = bucket->iter(); !i.is_end(); i = i.next()) {
if (i.elem()->key->eq(key)) {
return true;
@ -78,7 +78,7 @@ impl hashmap<K, V> {
pub func get(self, key: K*) -> V* {
var hash = key->hash() % (1024 => u64);
var bucket = self->data[hash].__ptr__();
var bucket = self->bucket[hash].__ptr__();
for (var i = bucket->iter(); !i.is_end(); i = i.next()) {
if (i.elem()->key->eq(key)) {
return i.elem()->value;
@ -88,8 +88,8 @@ impl hashmap<K, V> {
}
pub func insert(self, key: K*, value: V*) {
var hash = key->hash() % self->data_capacity;
var bucket = self->data[hash].__ptr__();
var hash = key->hash() % self->bucket_capacity;
var bucket = self->bucket[hash].__ptr__();
for (var i = bucket->iter(); !i.is_end(); i = i.next()) {
if (i.elem()->key->eq(key)) {
var tmp = i.elem();
@ -113,7 +113,7 @@ impl hashmap<K, V> {
pub struct hashmap_iter<K, V> {
map: hashmap<K, V>*,
bucket_index: u64,
bucket_list_iter: list_iter<pair<K, V>>
bucket_iter: list_iter<pair<K, V>>
}
impl hashmap_iter<K, V> {
@ -121,49 +121,49 @@ impl hashmap_iter<K, V> {
var res = hashmap_iter<K, V> {
map: map,
bucket_index: 0 => u64,
bucket_list_iter: map->data[0].iter()
bucket_iter: map->bucket[0].iter()
};
while (res.bucket_list_iter.is_end()) {
while (res.bucket_iter.is_end()) {
res.bucket_index += 1 => u64;
if (res.bucket_index >= map->data_capacity) {
if (res.bucket_index >= map->bucket_capacity) {
return res;
}
res.bucket_list_iter = map->data[res.bucket_index].iter();
res.bucket_iter = map->bucket[res.bucket_index].iter();
}
return res;
}
pub func next(self) -> hashmap_iter<K, V> {
if (!self->bucket_list_iter.is_end()) {
self->bucket_list_iter = self->bucket_list_iter.next();
if (!self->bucket_iter.is_end()) {
self->bucket_iter = self->bucket_iter.next();
}
while (self->bucket_list_iter.is_end()) {
while (self->bucket_iter.is_end()) {
self->bucket_index += 1 => u64;
if (self->bucket_index >= self->map->data_capacity) {
if (self->bucket_index >= self->map->bucket_capacity) {
break;
}
self->bucket_list_iter = self->map->data[self->bucket_index].iter();
self->bucket_iter = self->map->bucket[self->bucket_index].iter();
}
return hashmap_iter<K, V> {
map: self->map,
bucket_index: self->bucket_index,
bucket_list_iter: self->bucket_list_iter,
bucket_iter: self->bucket_iter,
};
}
pub func is_end(self) -> bool {
return self->bucket_index >= self->map->data_capacity &&
self->bucket_list_iter.is_end();
return self->bucket_index >= self->map->bucket_capacity &&
self->bucket_iter.is_end();
}
pub func key(self) -> K* {
return self->bucket_list_iter.elem()->key;
return self->bucket_iter.elem()->key;
}
pub func value(self) -> V* {
return self->bucket_list_iter.elem()->value;
return self->bucket_iter.elem()->value;
}
}

View File

@ -5,18 +5,18 @@ use std::io::{ io };
pub struct hashset<T> {
size: u64,
data_capacity: u64,
data: list<T>*
bucket_capacity: u64,
bucket: list<T>*
}
impl hashset<T> {
pub func new() -> hashset<T>* {
var res = hashset<T>::__alloc__();
res->size = 0 => u64;
res->data_capacity = 1024 => u64;
res->data = malloc(res->data_capacity * list<T>::__size__()) => list<T>*;
for (var i = 0 => u64; i < res->data_capacity; i += 1 => u64) {
res->data[i] = list<T>::instance();
res->bucket_capacity = 1024 => u64;
res->bucket = malloc(res->bucket_capacity * list<T>::__size__()) => list<T>*;
for (var i = 0 => u64; i < res->bucket_capacity; i += 1 => u64) {
res->bucket[i] = list<T>::instance();
}
return res;
}
@ -24,31 +24,31 @@ impl hashset<T> {
pub func instance() -> hashset<T> {
var res = hashset<T> {
size: 0 => u64,
data_capacity: 1024 => u64,
data: nil => list<T>*
bucket_capacity: 1024 => u64,
bucket: nil => list<T>*
};
res.data = malloc(res.data_capacity * list<T>::__size__()) => list<T>*;
for (var i = 0 => u64; i < res.data_capacity; i += 1 => u64) {
res.data[i] = list<T>::instance();
res.bucket = malloc(res.bucket_capacity * list<T>::__size__()) => list<T>*;
for (var i = 0 => u64; i < res.bucket_capacity; i += 1 => u64) {
res.bucket[i] = list<T>::instance();
}
return res;
}
pub func delete(self) {
for (var i = 0 => u64; i < self->data_capacity; i += 1 => u64) {
self->data[i].delete();
for (var i = 0 => u64; i < self->bucket_capacity; i += 1 => u64) {
self->bucket[i].delete();
}
free(self->data => i8*);
free(self->bucket => i8*);
self->size = 0 => u64;
self->data_capacity = 0 => u64;
self->data = nil => list<T>*;
self->bucket_capacity = 0 => u64;
self->bucket = nil => list<T>*;
}
}
impl hashset<T> {
pub func has(self, item: T*) -> bool {
var hash = item->hash() % self->data_capacity;
var bucket = self->data[hash].__ptr__();
var hash = item->hash() % self->bucket_capacity;
var bucket = self->bucket[hash].__ptr__();
var tmp = bucket->head;
while (tmp => i8* != nil) {
if (tmp->elem->eq(item)) {
@ -60,10 +60,10 @@ impl hashset<T> {
}
pub func insert(self, item: T*) {
var hash = item->hash() % self->data_capacity;
var hash = item->hash() % self->bucket_capacity;
// must use pointer/reference, otherwise changes will not
// be reflected in the hashset
var bucket = self->data[hash].__ptr__();
var bucket = self->bucket[hash].__ptr__();
var tmp = bucket->head;
while (tmp => i8* != nil) {
if (tmp->elem->eq(item)) {
@ -79,53 +79,53 @@ impl hashset<T> {
pub struct hashset_iter<T> {
set: hashset<T>*,
bucket_index: u64,
bucket_list_iter: list_iter<T>
bucket_iter: list_iter<T>
}
impl hashset_iter<T> {
func instance(set: hashset<T>*) -> hashset_iter<T> {
var index = 0 => u64;
var iter = set->data[0].iter();
var iter = set->bucket[0].iter();
while (iter.is_end()) {
index += 1 => u64;
if (index >= set->data_capacity) {
if (index >= set->bucket_capacity) {
break;
}
iter = set->data[index].iter();
iter = set->bucket[index].iter();
}
return hashset_iter<T> {
set: set,
bucket_index: index,
bucket_list_iter: iter
bucket_iter: iter
};
}
pub func is_end(self) -> bool {
return self->bucket_index >= self->set->data_capacity &&
self->bucket_list_iter.is_end();
return self->bucket_index >= self->set->bucket_capacity &&
self->bucket_iter.is_end();
}
pub func elem(self) -> T* {
return self->bucket_list_iter.elem();
return self->bucket_iter.elem();
}
pub func next(self) -> hashset_iter<T> {
if (!self->bucket_list_iter.is_end()) {
self->bucket_list_iter = self->bucket_list_iter.next();
if (!self->bucket_iter.is_end()) {
self->bucket_iter = self->bucket_iter.next();
}
while (self->bucket_list_iter.is_end()) {
while (self->bucket_iter.is_end()) {
self->bucket_index += 1 => u64;
if (self->bucket_index >= self->set->data_capacity) {
if (self->bucket_index >= self->set->bucket_capacity) {
break;
}
self->bucket_list_iter = self->set->data[self->bucket_index].iter();
self->bucket_iter = self->set->bucket[self->bucket_index].iter();
}
return hashset_iter<T> {
set: self->set,
bucket_index: self->bucket_index,
bucket_list_iter: self->bucket_list_iter,
bucket_iter: self->bucket_iter,
};
}
}