Files
c3c/resources/editor_plugins/vscode/example.c3
2021-12-21 22:52:53 +01:00

36 lines
721 B
C

module stack <Type>;
// Above: the parameterized type is applied to the entire module.
import std::mem;
struct Stack
{
usize capacity;
usize size;
Type* elems;
}
// The type methods offers dot syntax calls,
// so this function can either be called
// Stack.push(&my_stack, ...) or
// my_stack.push(...)
fn void Stack.push(Stack* this, Type element)
{
if (this.capacity == this.size)
{
this.capacity *= 2;
this.elems = mem::realloc(this.elems, $sizeof(Type) * this.capacity);
}
this.elems[this.size++] = element;
}
fn Type Stack.pop(Stack* this)
{
assert(this.size > 0);
return this.elems[--this.size];
}
fn bool Stack.empty(Stack* this)
{
return !this.size;
}