Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example programs #115

Closed
mhyee opened this issue May 10, 2017 · 1 comment
Closed

Example programs #115

mhyee opened this issue May 10, 2017 · 1 comment

Comments

@mhyee
Copy link
Member

mhyee commented May 10, 2017

For now, this is for record-keeping and discussion. I don't want to implement too many examples just yet, because our arrays/variables are still in flux. For now, I'll just sketch them out.

My goal is to keep these examples minimal, but still interesting. Some of these examples might require us to implement more features. For example, I think we want the other arithmetic and comparison operations (which should be straightforward to implement). Other types (e.g. floats or strings) might be nice, but probably not necessary.

2D Point

Very simple example for testing function inlining, and maybe branch speculation. This is so simple it's boring, but that's why we have other examples.

Fields
[x, y] - x and y are the coordinates

Functions
init(x, y) - returns a reference to a new point, initialized to x and y
getX(p), getY(p), setX(p, x), setY(p, y) - getters and setters
equals(p1, p2) - compares two points, returns a boolean

Example program
User provides a list of points as inputs, check if any of those points equals some predefined point.

Vector

An array that automatically grows as needed. Again, we can test inlining and branch speculation, but we also get bounds check elimination and maybe code motion.

Note: We need multiplication and comparison for this.

Fields
[size, length, arr] - size is the physical size of the allocated array, length is the portion that is being used, arr is a reference to an array of size size

Functions
init() - returns a reference to a new vector of size 1 and length 0
set(v, i, x) - sets v[i] to x, after ensuring 0 <= i. Grows the internal array (by doubling its size) if necessary, and zeros everything between the old length and the new length.
get(v, i) - returns v[i], after ensuring 0 <= i < length
length(v) - returns the length of v
iter(v, f) - iterates over all (used) elements, applying f to each element

List

A doubly-linked list. We can test inlining and branch speculation. On second thought, this example doesn't really give us anything new.

Fields
[val, next, prev] - val is the integer stored in the cell, next and prev are references to the next and previous cells

Functions
cons(v, l), head(l), tail(l), append(l1, l2), fold(f, acc, l) - self-explanatory
length(l), iter(f, l), map(f, l), filter(p, l) - implemented with fold

Object System

TODO. Still thinking about this, will probably start with the example in #83.

This is where strings would be nice, to represent method names. We could work around this by using (unique) numbers to represent method names, but it would be pretty unreadable. An alternative might be to implement symbols, like in Racket or Ruby. Basically immutable strings that you can only compare for equality.

@mhyee
Copy link
Member Author

mhyee commented Jun 6, 2017

Closed. I think #131 is more than good enough for our purposes.

@mhyee mhyee closed this as completed Jun 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant