Move the option type to its own module

This commit is contained in:
Patrick Walton 2010-11-03 17:10:37 -07:00
parent e399926776
commit adb1754e4d
14 changed files with 115 additions and 93 deletions

View File

@ -5,9 +5,9 @@ import front.token;
import middle.trans;
import middle.resolve;
import std.util.option;
import std.util.some;
import std.util.none;
import std.option;
import std.option.some;
import std.option.none;
import std._str;
import std._vec;
@ -39,8 +39,8 @@ fn usage(session.session sess, str argv0) {
impure fn main(vec[str] args) {
auto sess = session.session();
let option[str] input_file = none[str];
let option[str] output_file = none[str];
let option.t[str] input_file = none[str];
let option.t[str] output_file = none[str];
let bool do_warn = true;
auto i = 1u;

View File

@ -1,7 +1,6 @@
import util.common.option;
import std.map.hashmap;
import std.util.option;
import std.option;
import util.common.span;
import util.common.spanned;
@ -71,16 +70,16 @@ tag unop {
type stmt = spanned[stmt_];
tag stmt_ {
stmt_decl(@decl);
stmt_ret(option[@expr]);
stmt_ret(option.t[@expr]);
stmt_log(@expr);
stmt_check_expr(@expr);
stmt_expr(@expr);
}
type local = rec(option[@ty] ty,
type local = rec(option.t[@ty] ty,
bool infer,
ident ident,
option[@expr] init,
option.t[@expr] init,
def_id id);
type decl = spanned[decl_];
@ -99,14 +98,14 @@ tag expr_ {
expr_unary(unop, @expr, ann);
expr_lit(@lit, ann);
expr_cast(@expr, @ty, ann);
expr_if(@expr, block, option[block], ann);
expr_if(@expr, block, option.t[block], ann);
expr_while(@expr, block, ann);
expr_do_while(block, @expr, ann);
expr_block(block, ann);
expr_assign(@expr /* TODO: @expr|is_lval */, @expr, ann);
expr_field(@expr, ident, ann);
expr_index(@expr, @expr, ann);
expr_name(name, option[def], ann);
expr_name(name, option.t[def], ann);
}
type lit = spanned[lit_];
@ -131,7 +130,7 @@ tag ty_ {
ty_box(@ty);
ty_vec(@ty);
ty_tup(vec[tup(bool /* mutability */, @ty)]);
ty_path(path, option[def]);
ty_path(path, option.t[def]);
}
tag mode {

View File

@ -1,7 +1,7 @@
import std._io;
import std.util.option;
import std.util.some;
import std.util.none;
import std.option;
import std.option.some;
import std.option.none;
import std.map.hashmap;
import driver.session;
@ -157,7 +157,7 @@ impure fn parse_arg(parser p) -> ast.arg {
impure fn parse_seq[T](token.token bra,
token.token ket,
option[token.token] sep,
option.t[token.token] sep,
(impure fn(parser) -> T) f,
parser p) -> util.common.spanned[vec[T]] {
let bool first = true;
@ -185,7 +185,7 @@ impure fn parse_seq[T](token.token bra,
ret spanned(lo, hi, v);
}
impure fn parse_lit(parser p) -> option[ast.lit] {
impure fn parse_lit(parser p) -> option.t[ast.lit] {
auto lo = p.get_span();
let ast.lit_ lit;
alt (p.peek()) {
@ -600,7 +600,7 @@ impure fn parse_if_expr(parser p) -> @ast.expr {
auto cond = parse_expr(p);
expect(p, token.RPAREN);
auto thn = parse_block(p);
let option[ast.block] els = none[ast.block];
let option.t[ast.block] els = none[ast.block];
hi = thn.span;
alt (p.peek()) {
case (token.ELSE) {
@ -664,7 +664,7 @@ impure fn parse_expr(parser p) -> @ast.expr {
}
}
impure fn parse_initializer(parser p) -> option[@ast.expr] {
impure fn parse_initializer(parser p) -> option.t[@ast.expr] {
if (p.peek() == token.EQ) {
p.bump();
ret some(parse_expr(p));

View File

@ -1,7 +1,7 @@
import std.map.hashmap;
import std.util.option;
import std.util.some;
import std.util.none;
import std.option;
import std.option.some;
import std.option.none;
import util.common.new_str_hash;
import util.common.spanned;
@ -47,7 +47,7 @@ type ast_fold[ENV] =
vec[tup(bool, @ty)] elts) -> @ty) fold_ty_tup,
(fn(&ENV e, &span sp, ast.path p,
&option[def] d) -> @ty) fold_ty_path,
&option.t[def] d) -> @ty) fold_ty_path,
// Expr folds.
(fn(&ENV e, &span sp,
@ -79,7 +79,7 @@ type ast_fold[ENV] =
(fn(&ENV e, &span sp,
@expr cond, &block thn,
&option[block] els,
&option.t[block] els,
ann a) -> @expr) fold_expr_if,
(fn(&ENV e, &span sp,
@ -107,7 +107,7 @@ type ast_fold[ENV] =
(fn(&ENV e, &span sp,
&name n,
&option[def] d,
&option.t[def] d,
ann a) -> @expr) fold_expr_name,
// Decl folds.
@ -123,7 +123,7 @@ type ast_fold[ENV] =
@decl decl) -> @stmt) fold_stmt_decl,
(fn(&ENV e, &span sp,
&option[@expr] rv) -> @stmt) fold_stmt_ret,
&option.t[@expr] rv) -> @stmt) fold_stmt_ret,
(fn(&ENV e, &span sp,
@expr e) -> @stmt) fold_stmt_log,
@ -568,7 +568,7 @@ fn identity_fold_ty_tup[ENV](&ENV env, &span sp, vec[tup(bool,@ty)] elts)
}
fn identity_fold_ty_path[ENV](&ENV env, &span sp, ast.path p,
&option[def] d) -> @ty {
&option.t[def] d) -> @ty {
ret @respan(sp, ast.ty_path(p, d));
}
@ -614,7 +614,7 @@ fn identity_fold_expr_lit[ENV](&ENV env, &span sp, @ast.lit lit,
fn identity_fold_expr_if[ENV](&ENV env, &span sp,
@expr cond, &block thn,
&option[block] els, ann a) -> @expr {
&option.t[block] els, ann a) -> @expr {
ret @respan(sp, ast.expr_if(cond, thn, els, a));
}
@ -650,7 +650,7 @@ fn identity_fold_expr_index[ENV](&ENV env, &span sp,
}
fn identity_fold_expr_name[ENV](&ENV env, &span sp,
&name n, &option[def] d,
&name n, &option.t[def] d,
ann a) -> @expr {
ret @respan(sp, ast.expr_name(n, d, a));
}
@ -675,7 +675,7 @@ fn identity_fold_stmt_decl[ENV](&ENV env, &span sp, @decl d) -> @stmt {
}
fn identity_fold_stmt_ret[ENV](&ENV env, &span sp,
&option[@expr] rv) -> @stmt {
&option.t[@expr] rv) -> @stmt {
ret @respan(sp, ast.stmt_ret(rv));
}

View File

@ -8,9 +8,9 @@ import std.map.hashmap;
import std.list.list;
import std.list.nil;
import std.list.cons;
import std.util.option;
import std.util.some;
import std.util.none;
import std.option;
import std.option.some;
import std.option.none;
import std._str;
tag scope {
@ -22,11 +22,11 @@ tag scope {
type env = rec(list[scope] scopes,
session.session sess);
fn lookup_name(&env e, ast.ident i) -> option[def] {
fn lookup_name(&env e, ast.ident i) -> option.t[def] {
// log "resolving name " + i;
fn found_def_item(@ast.item i) -> option[def] {
fn found_def_item(@ast.item i) -> option.t[def] {
alt (i.node) {
case (ast.item_fn(_, _, ?id)) {
ret some[def](ast.def_fn(id));
@ -40,7 +40,7 @@ fn lookup_name(&env e, ast.ident i) -> option[def] {
}
}
fn found_decl_stmt(@ast.stmt s) -> option[def] {
fn found_decl_stmt(@ast.stmt s) -> option.t[def] {
alt (s.node) {
case (ast.stmt_decl(?d)) {
alt (d.node) {
@ -56,7 +56,7 @@ fn lookup_name(&env e, ast.ident i) -> option[def] {
ret none[def];
}
fn check_mod(ast.ident i, ast._mod m) -> option[def] {
fn check_mod(ast.ident i, ast._mod m) -> option.t[def] {
alt (m.index.find(i)) {
case (some[uint](?ix)) {
ret found_def_item(m.items.(ix));
@ -66,7 +66,7 @@ fn lookup_name(&env e, ast.ident i) -> option[def] {
}
fn in_scope(ast.ident i, &scope s) -> option[def] {
fn in_scope(ast.ident i, &scope s) -> option.t[def] {
alt (s) {
case (scope_crate(?c)) {
@ -103,7 +103,7 @@ fn lookup_name(&env e, ast.ident i) -> option[def] {
}
fn fold_expr_name(&env e, &span sp, &ast.name n,
&option[def] d, ann a) -> @ast.expr {
&option.t[def] d, ann a) -> @ast.expr {
auto d_ = lookup_name(e, n.node.ident);

View File

@ -3,9 +3,9 @@ import std._vec;
import std._str.rustrt.sbuf;
import std._vec.rustrt.vbuf;
import std.map.hashmap;
import std.util.option;
import std.util.some;
import std.util.none;
import std.option;
import std.option.some;
import std.option.none;
import front.ast;
import driver.session;
@ -613,7 +613,7 @@ impure fn trans_binary(@block_ctxt cx, ast.binop op,
}
impure fn trans_if(@block_ctxt cx, &ast.expr cond,
&ast.block thn, &option[ast.block] els) -> result {
&ast.block thn, &option.t[ast.block] els) -> result {
auto cond_res = trans_expr(cx, cond);
@ -868,7 +868,7 @@ impure fn trans_check_expr(@block_ctxt cx, &ast.expr e) -> result {
ret res(next_cx, C_nil());
}
impure fn trans_ret(@block_ctxt cx, &option[@ast.expr] e) -> result {
impure fn trans_ret(@block_ctxt cx, &option.t[@ast.expr] e) -> result {
auto r = res(cx, C_nil());
alt (e) {
case (some[@ast.expr](?x)) {

View File

@ -1,5 +1,5 @@
import vbuf = rustrt.vbuf;
import op = util.operator;
import std.option;
native "rust" mod rustrt {
type vbuf;
@ -129,7 +129,7 @@ fn grow[T](&mutable vec[T] v, int n, &T initval) {
}
}
fn map[T, U](&op[T,U] f, &vec[T] v) -> vec[U] {
fn map[T, U](&option.operator[T,U] f, &vec[T] v) -> vec[U] {
let vec[U] u = alloc[U](len[T](v));
for (T ve in v) {
u += vec(f(ve));

View File

@ -2,7 +2,7 @@
* A deque, for fun. Untested as of yet. Likely buggy.
*/
import std.util;
import std.option;
import std._vec;
import std._int;
@ -23,7 +23,7 @@ type t[T] = obj {
fn create[T]() -> t[T] {
type cell[T] = mutable util.option[T];
type cell[T] = mutable option.t[T];
let uint initial_capacity = 32u; // 2^5
@ -39,7 +39,7 @@ fn create[T]() -> t[T] {
if (i < nelts) {
ret old.((lo + i) % nelts);
} else {
ret util.none[T];
ret option.none[T];
}
}
@ -50,7 +50,7 @@ fn create[T]() -> t[T] {
fn get[T](vec[cell[T]] elts, uint i) -> T {
alt (elts.(i)) {
case (util.some[T](?t)) { ret t; }
case (option.some[T](?t)) { ret t; }
case (_) { fail; }
}
}
@ -77,7 +77,7 @@ fn create[T]() -> t[T] {
hi = nelts;
}
elts.(lo) = util.some[T](t);
elts.(lo) = option.some[T](t);
nelts += 1u;
}
@ -88,7 +88,7 @@ fn create[T]() -> t[T] {
hi = nelts;
}
elts.(hi) = util.some[T](t);
elts.(hi) = option.some[T](t);
hi = (hi + 1u) % _vec.len[cell[T]](elts);
nelts += 1u;
}
@ -99,7 +99,7 @@ fn create[T]() -> t[T] {
*/
fn pop_front() -> T {
let T t = get[T](elts, lo);
elts.(lo) = util.none[T];
elts.(lo) = option.none[T];
lo = (lo + 1u) % _vec.len[cell[T]](elts);
nelts -= 1u;
ret t;
@ -113,7 +113,7 @@ fn create[T]() -> t[T] {
}
let T t = get[T](elts, hi);
elts.(hi) = util.none[T];
elts.(hi) = option.none[T];
nelts -= 1u;
ret t;
}
@ -132,7 +132,7 @@ fn create[T]() -> t[T] {
}
}
let vec[cell[T]] v = _vec.init_elt[cell[T]](util.none[T],
let vec[cell[T]] v = _vec.init_elt[cell[T]](option.none[T],
initial_capacity);
ret deque[T](0u, 0u, 0u, v);

View File

@ -1,7 +1,7 @@
import util.option;
import util.some;
import util.none;
import std.option;
import option.some;
import option.none;
// FIXME: It would probably be more appealing to define this as
// type list[T] = rec(T hd, option[@list[T]] tl), but at the moment
@ -27,7 +27,7 @@ fn foldl[T,U](&list[T] ls, &U u, fn(&T t, U u) -> U f) -> U {
}
fn find[T,U](&list[T] ls,
(fn(&T) -> option[U]) f) -> option[U] {
(fn(&T) -> option.t[U]) f) -> option.t[U] {
alt(ls) {
case (cons[T](?hd, ?tl)) {
alt (f(hd)) {

View File

@ -5,7 +5,7 @@
import std._int;
import std.sys;
import std.util;
import std.option;
import std._vec;
@ -17,8 +17,8 @@ abs state type hashmap[K, V] = state obj {
fn insert(&K key, &V val) -> bool;
fn contains_key(&K key) -> bool;
fn get(&K key) -> V;
fn find(&K key) -> util.option[V];
fn remove(&K key) -> util.option[V];
fn find(&K key) -> option.t[V];
fn remove(&K key) -> option.t[V];
fn rehash();
iter items() -> tup(K,V);
};
@ -103,7 +103,7 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
vec[mutable bucket[K, V]] bkts,
uint nbkts,
&K key)
-> util.option[V]
-> option.t[V]
{
let uint i = 0u;
while (i < nbkts) {
@ -111,17 +111,17 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
alt (bkts.(j)) {
case (some[K, V](?k, ?v)) {
if (eqer(key, k)) {
ret util.some[V](v);
ret option.some[V](v);
}
}
case (nil[K, V]) {
ret util.none[V];
ret option.none[V];
}
case (deleted[K, V]) { }
}
i += 1u;
}
ret util.none[V];
ret option.none[V];
}
@ -173,25 +173,25 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
fn contains_key(&K key) -> bool {
alt (find_common[K, V](hasher, eqer, bkts, nbkts, key)) {
case (util.some[V](_)) { ret true; }
case (option.some[V](_)) { ret true; }
case (_) { ret false; }
}
}
fn get(&K key) -> V {
alt (find_common[K, V](hasher, eqer, bkts, nbkts, key)) {
case (util.some[V](?val)) { ret val; }
case (option.some[V](?val)) { ret val; }
case (_) { fail; }
}
}
fn find(&K key) -> util.option[V] {
fn find(&K key) -> option.t[V] {
// FIXME: should be 'be' but parametric tail-calls don't
// work at the moment.
ret find_common[K, V](hasher, eqer, bkts, nbkts, key);
}
fn remove(&K key) -> util.option[V] {
fn remove(&K key) -> option.t[V] {
let uint i = 0u;
while (i < nbkts) {
let uint j = (hash[K](hasher, nbkts, key, i));
@ -200,17 +200,17 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
if (eqer(key, k)) {
bkts.(j) = deleted[K, V];
nelts -= 1u;
ret util.some[V](v);
ret option.some[V](v);
}
}
case (deleted[K, V]) { }
case (nil[K, V]) {
ret util.none[V];
ret option.none[V];
}
}
i += 1u;
}
ret util.none[V];
ret option.none[V];
}
fn rehash() {

40
src/lib/option.rs Normal file
View File

@ -0,0 +1,40 @@
// lib/option.rs
tag t[T] {
none;
some(T);
}
type operator[T, U] = fn(&T) -> U;
fn get[T](&t[T] opt) -> T {
alt (opt) {
case (some[T](?x)) {
ret x;
}
case (none[T]) {
fail;
}
}
}
fn map[T, U](&operator[T, U] f, &t[T] opt) -> t[U] {
alt (opt) {
case (some[T](?x)) {
ret some[U](f(x));
}
case (none[T]) {
ret none[U];
}
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View File

@ -20,6 +20,7 @@ mod _task;
// Utility modules.
mod option;
mod util;
// Authorize various rule-bendings.

View File

@ -1,21 +1,3 @@
tag option[T] {
none;
some(T);
}
type operator[T, U] = fn(&T) -> U;
fn option_map[T, U](&operator[T, U] f, &option[T] opt) -> option[U] {
alt (opt) {
case (some[T](?x)) {
ret some[U](f(x));
}
case (none[T]) {
ret none[U];
}
}
}
fn id[T](&T x) -> T {
ret x;
}

View File

@ -32,7 +32,7 @@ fn test_slice() {
fn test_map() {
fn square(&int x) -> int { ret x * x; }
let std.util.operator[int, int] op = square;
let std.option.operator[int, int] op = square;
let vec[int] v = vec(1, 2, 3, 4, 5);
let vec[int] s = std._vec.map[int, int](op, v);
let int i = 0;