ref: 4be1adfd0a6da6bf6d0098238bf9343ca6d38cb3
parent: 052fc6c33c0c5a945591d08933bfeada909d1fdb
parent: 7c884e9cb6c97c99a6d3310da3feb473b15ba02a
author: Ori Bernstein <[email protected]>
date: Fri Oct 12 15:13:15 EDT 2012
Merge branch 'master' of git+ssh://git.eigenstate.org/git/ori/mc
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -4,6 +4,7 @@
chartype.myr \
die.myr \
fmt.myr \
+ option.myr \
rand.myr \
sys.myr \
types.myr \
--- /dev/null
+++ b/libstd/option.myr
@@ -1,0 +1,59 @@
+use "types.use"
+use "alloc.use"
+use "utf.use"
+
+pkg std =
+ type optctx = struct
+ /* data passed in */
+ opts : byte[:]
+ args : byte[:][:]
+
+ /* state */
+ optidx : size
+ curopts : byte[:]
+ args : byte[:][:]
+ ;;
+
+ const optinit : (opts : byte[:], opts : byte[:][:] -> optctx*)
+ const optnext : (ctx : optctx* -> char)
+;;
+
+const optinit = {opts, args
+ var ctx
+
+ ctx = alloc()
+ ctx.opts = opts
+ ctx.args = args
+ ctx.optidx = 0
+ ctx.args = [][:]
+ nextopt(ctx)
+ -> ctx
+}
+
+const optnext = {ctx
+ var c
+
+ if !ctx.curopt.len
+ if !nextopt(ctx)
+ -> Badchar
+ ;;
+ ;;
+ (c, ctx.curopt) = striter(ctx.curopt)
+ -> c
+}
+
+const nextopt = {ctx
+ var i
+
+ for i = ctx.optidx + 1; i < ctx.optidx.len; i++
+ if decode(ctx.args[i]) == '-'
+ goto foundopt
+ else
+ /* FIXME: implement slappend */
+ /* ctx.args = slappend(ctx.args, ctx.args[i]) */
+ ;;
+ ;;
+:foundopt
+ ctx.optidx = i
+ ctx.curopts = ctx.opts[i]
+}
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -129,7 +129,7 @@
%type <tydef> tydef typeid
-%type <node> exprln retexpr expr atomicexpr littok literal asnexpr lorexpr landexpr borexpr
+%type <node> exprln retexpr goto expr atomicexpr littok literal asnexpr lorexpr landexpr borexpr
%type <node> bandexpr cmpexpr unionexpr addexpr mulexpr shiftexpr prefixexpr postfixexpr
%type <node> funclit seqlit name block stmt label use
%type <node> decl declbody declcore structelt seqelt tuphead
@@ -137,7 +137,7 @@
%type <node> pat unionpat match
%type <node> castexpr
%type <ucon> unionelt
-%type <node> body
+%type <node> blkbody
%type <nodelist> arglist argdefs params matches
%type <nodelist> structbody seqbody tupbody tuprest
@@ -404,6 +404,10 @@
{$$ = NULL;}
;
+goto : Tgoto Tident
+ {$$ = mkexpr($1->line, Ojmp, mklbl($2->line, $2->str), NULL);}
+ ;
+
retexpr : Tret expr
{$$ = mkexpr($1->line, Oret, $2, NULL);}
| expr
@@ -587,9 +591,9 @@
| Tboollit {$$ = mkbool($1->line, !strcmp($1->str, "true"));}
;
-funclit : Tobrace params Tendln body Tcbrace
+funclit : Tobrace params Tendln blkbody Tcbrace
{$$ = mkfunc($1->line, $2.nl, $2.nn, mktyvar($3->line), $4);}
- | Tobrace params Tret type Tendln body Tcbrace
+ | Tobrace params Tret type Tendln blkbody Tcbrace
{$$ = mkfunc($1->line, $2.nl, $2.nn, $4, $6);}
;
@@ -625,6 +629,7 @@
;
stmt : decl
+ | goto
| retexpr
| label
| ifstmt
@@ -645,11 +650,11 @@
{$$ = mkloopstmt($1->line, NULL, $2, NULL, $3);}
;
-ifstmt : Tif exprln body elifs
+ifstmt : Tif exprln blkbody elifs
{$$ = mkifstmt($1->line, $2, $3, $4);}
;
-elifs : Telif exprln body elifs
+elifs : Telif exprln blkbody elifs
{$$ = mkifstmt($1->line, $2, $3, $4);}
| Telse block
{$$ = $2;}
@@ -686,16 +691,16 @@
{$$ = mkexpr($1->line, Oucon, mkname($2->line, $2->str), NULL);}
;
-block : body Tendblk
+block : blkbody Tendblk
;
-body : stmt
+blkbody : stmt
{$$ = mkblock(line, mkstab());
if ($1)
lappend(&$$->block.stmts, &$$->block.nstmts, $1);
if ($1 && $1->type == Ndecl)
putdcl($$->block.scope, $1);}
- | body Tendln stmt
+ | blkbody Tendln stmt
{if ($3)
lappend(&$1->block.stmts, &$1->block.nstmts, $3);
if ($3 && $3->type == Ndecl)
@@ -704,7 +709,7 @@
;
label : Tcolon Tident
- {$$ = mklbl($1->line, $1->str);}
+ {$$ = mklbl($2->line, $2->str);}
;
%%
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -738,12 +738,16 @@
static void inferseq(Inferstate *st, Node *n)
{
size_t i;
+ Type *t;
+ Node *len;
+ len = mkintlit(n->line, n->lit.nelt);
+ t = mktyarray(n->line, mktyvar(n->line), len);
for (i = 0; i < n->lit.nelt; i++) {
infernode(st, n->lit.seqval[i], NULL, NULL);
- unify(st, n, type(st, n->lit.seqval[0]), type(st, n->lit.seqval[i]));
+ unify(st, n, t->sub[0], type(st, n->lit.seqval[i]));
}
- settype(st, n, mktyarray(n->line, type(st, n->lit.seqval[0]), mkintlit(n->line, n->lit.nelt)));
+ settype(st, n, t);
}
static void inferpat(Inferstate *st, Node *n, Node *val, Node ***bind, size_t *nbind)