Fix of shadowing bug. Allow pointer and subarrays to be constant initialized. Compile time values may now pass around anything considered compile time constant. It's possible to index into an initializer list at compile time. (Some work still remains on this)

This commit is contained in:
Christoffer Lerno
2021-09-14 11:14:51 +02:00
committed by Christoffer Lerno
parent 1b103a3e22
commit e4c7dde30b
30 changed files with 1209 additions and 343 deletions

View File

@@ -0,0 +1,86 @@
module tmem;
import std::mem;
import std::io;
struct String
{
Allocator allocator;
usize len;
usize capacity;
char* ptr;
}
func void String.init(String *s, char[] c)
{
s.capacity = c.len + 16;
s.ptr = mem::_malloc(s.capacity);
s.len = c.len;
mem::copy(s.ptr, (char*)(c), c.len);
}
func char* String.zstr(String *s)
{
char* c = mem::_malloc(s.len + 1);
mem::copy(c, s.ptr, s.len);
c[s.len] = 0;
return c;
}
func void String.appendc(String *s, char c)
{
if (s.capacity == s.len)
{
s.capacity *= 2;
char* new_ptr = mem::_malloc(s.capacity);
mem::copy(new_ptr, s.ptr, s.len);
s.ptr = new_ptr;
}
s.ptr[s.len++] = c;
}
func void String.append(String *s, char[] other_string)
{
if (s.capacity < s.len + other_string.len)
{
do
{
s.capacity *= 2;
}
while (s.capacity < s.len + other_string.len);
char* new_ptr = mem::_malloc(s.capacity);
mem::copy(new_ptr, s.ptr, s.len);
s.ptr = new_ptr;
}
mem::copy(s.ptr + s.len, (char*)(other_string), other_string.len);
s.len += other_string.len;
}
func void String.concat(String *s, String* other_string)
{
if (s.capacity < s.len + other_string.len)
{
do
{
s.capacity *= 2;
}
while (s.capacity < s.len + other_string.len);
char* new_ptr = mem::_malloc(s.capacity);
mem::copy(new_ptr, s.ptr, s.len);
s.ptr = new_ptr;
}
mem::copy(s.ptr + s.len, other_string.ptr, other_string.len);
s.len += other_string.len;
}
func void main()
{
String s;
s.init("Hello");
s.appendc(' ');
s.appendc('W');
s.append("orld!");
String w;
w.init("Yo man!");
s.concat(&w);
io::printf("Message was: %s\n", s.zstr());
}