From 8df85029fe362d4bf702320c48361ac3d37310be Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Sat, 2 Nov 2024 14:50:24 +0800 Subject: [PATCH] :memo: std lib update --- src/main.colgm | 3 +- src/package.colgm | 2 +- src/std/list.colgm | 104 +++++++++++++++++++++++++++ src/std/{hashmap.colgm => map.colgm} | 0 src/std/vec.colgm | 3 + 5 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/std/list.colgm rename src/std/{hashmap.colgm => map.colgm} (100%) create mode 100644 src/std/vec.colgm diff --git a/src/main.colgm b/src/main.colgm index 9ae732e..be5a4d2 100644 --- a/src/main.colgm +++ b/src/main.colgm @@ -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(); diff --git a/src/package.colgm b/src/package.colgm index deee282..f7f53f4 100644 --- a/src/package.colgm +++ b/src/package.colgm @@ -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, diff --git a/src/std/list.colgm b/src/std/list.colgm new file mode 100644 index 0000000..0d75efb --- /dev/null +++ b/src/std/list.colgm @@ -0,0 +1,104 @@ +use std::libc::{ free }; + +struct node { + elem: T*, + next: node*, + prev: node* +} + +struct primitive_node { + elem: T, + next: primitive_node*, + prev: primitive_node* +} + +pub struct list { + head: node*, + tail: node*, + size: u64 +} + +pub struct primitive_list { + head: primitive_node*, + tail: primitive_node*, + size: u64 +} + +impl list { + pub func new() -> list* { + var res = list::__alloc__(); + res->head = nil => node*; + res->tail = nil => node*; + res->size = 0; + return res; + } + + pub func delete(self) { + var curr = self->head; + while (curr != nil => node*) { + var next = curr->next; + curr->elem->delete(); + free(curr => i8*); + curr = next; + } + self->head = nil => node*; + self->tail = nil => node*; + self->size = 0; + } + + pub func insert(self, elem: T*) { + var new_node = node::__alloc__(); + new_node->elem = elem->copy(); + new_node->next = nil => node*; + new_node->prev = self->tail; + + if (self->tail != nil => node*) { + self->tail->next = new_node; + } else { + self->head = new_node; + } + + self->tail = new_node; + self->size += 1; + return; + } +} + +impl primitive_list { + pub func new() -> primitive_list* { + var res = primitive_list::__alloc__(); + res->head = nil => primitive_node*; + res->tail = nil => primitive_node*; + res->size = 0; + return res; + } + + pub func delete(self) { + var curr = self->head; + while (curr != nil => primitive_node*) { + var next = curr->next; + free(curr => i8*); + curr = next; + } + self->head = nil => primitive_node*; + self->tail = nil => primitive_node*; + self->size = 0; + } + + pub func insert(self, elem: T) { + var new_node = node::__alloc__(); + new_node->elem = elem; + new_node->next = nil => node*; + new_node->prev = self->tail; + + if (self->tail != nil => node*) { + self->tail->next = new_node; + } else { + self->head = new_node; + } + + self->tail = new_node; + self->size += 1; + return; + } +} \ No newline at end of file diff --git a/src/std/hashmap.colgm b/src/std/map.colgm similarity index 100% rename from src/std/hashmap.colgm rename to src/std/map.colgm diff --git a/src/std/vec.colgm b/src/std/vec.colgm new file mode 100644 index 0000000..7e5e803 --- /dev/null +++ b/src/std/vec.colgm @@ -0,0 +1,3 @@ +struct vec {} + +struct primitive_vec {} \ No newline at end of file