#[std] #[memory(512)] fn realloc(old_ptr: &char, old_size: num, new_size: num) -> &char { let new_ptr = alloc(new_size) as &char; for (let i=0; i num { for (let i=0; str[i] != '\0'; i=i+1) {} return i } fn strsize(str: &char) -> num { return strlen(str) + 1 } struct String { let ptr: &char, len: num; fn new() -> String { return [alloc(1), 0]; } fn from(str: &char) -> String { let result = String::new(); for (let i=0; str[i] != '\0'; i=i+1) { result.push(str[i]); } return result } fn push(self: &String, ch: char) { self->len = self->len + 1; self->ptr = realloc(self->ptr, self->len, self->len + 1); (self->ptr)[self->len - 1] = ch; (self->ptr)[self->len] = '\0'; } fn copy(self: &String) -> String { let ptr = alloc(self->len+1) as &char; strcpy(ptr, self->ptr); return [ptr, self->len] } fn drop(self: &String) { free self->ptr: self->len + 1; } } fn putstring(s: String) { putstr(s->ptr); } struct Person { let first_name: String, middle_name: String, last_name: String; fn new(first: &char, middle: &char, last: &char) -> Person { return [ String::from(first), String::from(middle), String::from(last), ]; } fn print(self: &Person) { putstring(self->last_name); putstr(", "); putstring(self->first_name); putchar(' '); putchar( ((self->middle_name)->ptr)[0] ); putcharln('.'); } fn copy(self: &Person) -> Person { putstrln("copy!"); return [ self->first_name, self->middle_name, self->last_name ] } fn drop(self: &Person) { putstrln("drop!"); (self->first_name).drop(); (self->middle_name).drop(); (self->last_name).drop(); } } fn putperson(p: Person) { p.print(); } fn main() { let p = Person::new("Adam", "Ryan", "McDaniel"); p.print(); putperson(p); p.print(); }