adam-mcdaniel / oakc

A portable programming language with a compact intermediate representation
Apache License 2.0
725 stars 21 forks source link

Added setup for core types #86

Open adam-mcdaniel opened 4 years ago

adam-mcdaniel commented 4 years ago

This PR implements the String type in core.ok, and adds some extensions to the core.target files to make memory management more efficient.

adam-mcdaniel commented 4 years ago

Example usage of the new additions to the standard library:

#[std]

struct Date {
    let m: num, d: num, y: num;

    fn now() -> Date {
        return [get_month_now() + 1, get_day_now(), get_year_now()];
    }

    fn puts(self: &Date) {
        putnum(self->m); putchar('\\'); putnum(self->d); putchar('\\'); putnum(self->y);
    }

    fn putln(self: &Date) { self.puts(); prend(); }
}

struct Time {
    let h: num, m: num, s: num;

    fn now() -> Time {
        return [get_hour_now(), get_minute_now(), get_second_now()];
    }

    fn puts(self: &Time) {
        putnum(self->h); putchar(':'); putnum(self->m); putchar(':'); putnum(self->s);
    }

    fn putln(self: &Time) { self.puts(); prend(); }
}

fn main() {
    putstr("Today is "); Date::now().putln();
    putstr("And the time is "); Time::now().putln();
}

Here are the actual definitions of these new functions in std.ok.

// Date
extern fn __oak_std__get_month_now as get_month_now() -> num;
extern fn __oak_std__get_day_now   as get_day_now()   -> num;
extern fn __oak_std__get_year_now  as get_year_now()  -> num;

// Time
extern fn __oak_std__get_hour_now   as get_hour_now()   -> num;
extern fn __oak_std__get_minute_now as get_minute_now() -> num;
extern fn __oak_std__get_second_now as get_second_now() -> num;

Additionally, it renames the core.ok externed functions like so:

extern fn __oak_core__memcpy as memcpy(dst: &char, src: &char, size: num);
extern fn __oak_core__memset as memset(dst: &char, val: num, size: num);
extern fn __oak_core__strlen as strlen(src: &char) -> num;
extern fn __oak_core__strcpy as strcpy(dst: &char, src: &char);