#[std] #[memory(512)] struct Array { let len: num, elem_size: &num, contents: &void; fn new(len: num, elem_size: num) -> Array { return [len, elem_size, alloc(len * elem_size)] } fn idx(self: &Array, n: num) -> &void { return self->contents + n * (self->elem_size) } fn print(self: &Array) -> void { prn!(self->len); prc!(','); prn!(self->elem_size); prc!(','); prn!(self->contents); prc!('.'); prend!(); } fn copy(self: &Array) -> Array { let size = (self->len) * (self->elem_size); let ptr = alloc(size) as &char; let self_ptr = (self->contents) as &char; for i in 0..size { ptr[i] = self_ptr[i]; } return [self->len, self->elem_size, self_ptr] } fn drop(self: &Array) -> void { free self->contents: ((self->len) * (self->elem_size)); } } struct Date { let month: num, day: num, year: num; fn from(ptr: &void) -> &Date { return ptr as &Date } fn new(month: num, day: num, year: num) -> Date { return [month, day, year]; } fn birthday() -> Date { return Date::new(5, 14, 2002) } fn print(self: &Date) -> void { prn!(self->month); prc!('/'); prn!(self->day); prc!('/'); prn!(self->year); prend!(); } fn yesterday(self: &Date) -> Date { self->day = self->day - 1; return *self } fn tomorrow(self: &Date) -> Date { self->day = self->day + 1; return *self } fn next_week(self: &Date) -> Date { let result: Date = *self; for (let i:num=0; i<7; i=i+1) { result = result.tomorrow(); } return result } fn next_month(self: &Date) -> Date { self->month = self->month + 1; return *self } } fn main() -> void { let arr: Array = Array::new(4, sizeof(Date)); arr.print(); *arr.idx(0) = Date::birthday(); *arr.idx(1) = Date::birthday().tomorrow(); *arr.idx(2) = Date::birthday().yesterday(); *arr.idx(3) = (Date::birthday().next_week()).next_month(); for (let i:num=0; i < arr->len; i=i+1) { Date::from(arr.idx(i)).print(); } arr.drop(); }