📝 std lib update

This commit is contained in:
ValKmjolnir 2024-11-02 14:50:24 +08:00
parent 8ab9bc958b
commit 8df85029fe
5 changed files with 110 additions and 2 deletions

View File

@ -4,7 +4,8 @@ use cli::*;
use std::io::{ readfile_into_string, io };
use std::fs::{ fs };
use compiler::{ compiler };
use std::hashmap::{ hashmap };
use std::list::{ list, primitive_list };
use std::map::{ hashmap };
func compile(option: cli_option) {
var err = io::stderr();

View File

@ -1,7 +1,7 @@
use std::str::{ str };
use err::report::{ report };
use std::io::{ io };
use std::hashmap::{ hashmap };
use std::map::{ hashmap };
struct file_info_t {
name: str,

104
src/std/list.colgm Normal file
View File

@ -0,0 +1,104 @@
use std::libc::{ free };
struct node<T> {
elem: T*,
next: node<T>*,
prev: node<T>*
}
struct primitive_node<T> {
elem: T,
next: primitive_node<T>*,
prev: primitive_node<T>*
}
pub struct list<T> {
head: node<T>*,
tail: node<T>*,
size: u64
}
pub struct primitive_list<T> {
head: primitive_node<T>*,
tail: primitive_node<T>*,
size: u64
}
impl list<T> {
pub func new() -> list<T>* {
var res = list<T>::__alloc__();
res->head = nil => node<T>*;
res->tail = nil => node<T>*;
res->size = 0;
return res;
}
pub func delete(self) {
var curr = self->head;
while (curr != nil => node<T>*) {
var next = curr->next;
curr->elem->delete();
free(curr => i8*);
curr = next;
}
self->head = nil => node<T>*;
self->tail = nil => node<T>*;
self->size = 0;
}
pub func insert(self, elem: T*) {
var new_node = node<T>::__alloc__();
new_node->elem = elem->copy();
new_node->next = nil => node<T>*;
new_node->prev = self->tail;
if (self->tail != nil => node<T>*) {
self->tail->next = new_node;
} else {
self->head = new_node;
}
self->tail = new_node;
self->size += 1;
return;
}
}
impl primitive_list<T> {
pub func new() -> primitive_list<T>* {
var res = primitive_list<T>::__alloc__();
res->head = nil => primitive_node<T>*;
res->tail = nil => primitive_node<T>*;
res->size = 0;
return res;
}
pub func delete(self) {
var curr = self->head;
while (curr != nil => primitive_node<T>*) {
var next = curr->next;
free(curr => i8*);
curr = next;
}
self->head = nil => primitive_node<T>*;
self->tail = nil => primitive_node<T>*;
self->size = 0;
}
pub func insert(self, elem: T) {
var new_node = node<T>::__alloc__();
new_node->elem = elem;
new_node->next = nil => node<T>*;
new_node->prev = self->tail;
if (self->tail != nil => node<T>*) {
self->tail->next = new_node;
} else {
self->head = new_node;
}
self->tail = new_node;
self->size += 1;
return;
}
}

3
src/std/vec.colgm Normal file
View File

@ -0,0 +1,3 @@
struct vec<T> {}
struct primitive_vec<T> {}