ref: 8bd18e2d1caef67781ae69d761bdfca8d2bcf41d
parent: 11103467867dfd1416f7328b1facbe63da5c0cf5
parent: 0022c21553124df1dc02948b4fc27bd3906e4d37
author: Ori Bernstein <[email protected]>
date: Tue Nov 12 11:43:33 EST 2013
Merge branch 'master' of git+ssh://git.eigenstate.org/git/ori/mc
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -11,6 +11,7 @@
fmt.myr \
hashfuncs.myr \
htab.myr \
+ ipaddr.myr \
intparse.myr \
now.myr \
option.myr \
--- a/libstd/resolve.myr
+++ b/libstd/resolve.myr
@@ -73,8 +73,8 @@
;;
/* hardcode Google DNS for now */
sa.fam = Afinet
- sa.port = hosttonet(53)
- sa.addr = 0x08080808
+ sa.port = hosttonet(53) /* port 53 */
+ sa.addr = [8,8,8,8] /* 8.8.8.8 */
status = connect(s, (&sa) castto(sockaddr#), sizeof(sockaddr_in))
if status < 0
put("Warning: Failed to connect to server: %l\n", status)
@@ -122,12 +122,7 @@
const rquery = {srv
var pktbuf : byte[1024]
var pkt
- var off
- var nans
- var nquery
var n
- var v
- var i
put("Waiting for response...\n")
n = read(srv, pktbuf[:])
@@ -135,8 +130,18 @@
put("Warning: Failed to read from %z: %i\n", srv, n)
;;
pkt = pktbuf[:n]
+ dumpresponse(pkt)
+}
- put("packet size = %z\n", n)
+
+const dumpresponse = {pkt
+ var nquery
+ var nans
+ var off
+ var v
+ var i
+
+ put("packet size = %z\n", pkt.len)
(v, off) = unpack16(pkt, 0)
put("hdr.id = %w\n", v)
(v, off) = unpack16(pkt, off)
--- a/libstd/sys-osx.myr
+++ b/libstd/sys-osx.myr
@@ -70,7 +70,7 @@
len : byte
fam : sockfam
port : uint16
- addr : uint32
+ addr : byte[4]
zero : byte[8]
;;
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -24,6 +24,8 @@
/* nodes that need post-inference checking/unification */
Node **postcheck;
size_t npostcheck;
+ Stab **postcheckscope;
+ size_t npostcheckscope;
/* the type params bound at the current point */
Htab **tybindings;
size_t ntybindings;
@@ -99,8 +101,14 @@
return s;
}
-void typeerror(Inferstate *st, Type *a, Type *b, Node *ctx, char *msg)
+static void delayedcheck(Inferstate *st, Node *n, Stab *s)
{
+ lappend(&st->postcheck, &st->npostcheck, n);
+ lappend(&st->postcheckscope, &st->npostcheckscope, s);
+}
+
+static void typeerror(Inferstate *st, Type *a, Type *b, Node *ctx, char *msg)
+{
char *t1, *t2, *c;
t1 = tystr(a);
@@ -317,8 +325,11 @@
* match the instantiation */
if (orig->type == Tyunres && t->isgeneric) {
t = tyfreshen(st, t);
- for (i = 0; i < t->narg; i++)
- unify(st, NULL, t->arg[i], orig->arg[i]);
+ for (i = 0; i < t->narg; i++) {
+ unify(st, NULL, t->arg[i], orig->arg[i]);
+ if (orig->arg[i]->type == Typaram || isgeneric(st, t->arg[i]))
+ t->isgeneric = 1;
+ }
}
st->ingeneric -= t->isgeneric;
return t;
@@ -875,7 +886,7 @@
*isconst = 0;
}
settype(st, n, mktyvar(n->line));
- lappend(&st->postcheck, &st->npostcheck, n);
+ delayedcheck(st, n, curstab());
}
static void inferarray(Inferstate *st, Node *n, int *isconst)
@@ -1104,7 +1115,7 @@
/* special cases */
case Omemb: /* @a.Ident -> @b, verify type(@a.Ident)==@b later */
settype(st, n, mktyvar(n->line));
- lappend(&st->postcheck, &st->npostcheck, n);
+ delayedcheck(st, n, curstab());
break;
case Osize: /* sizeof @a -> size */
settype(st, n, mktylike(n->line, Tyuint));
@@ -1113,7 +1124,7 @@
unifycall(st, n);
break;
case Ocast: /* cast(@a, @b) -> @b */
- lappend(&st->postcheck, &st->npostcheck, n);
+ delayedcheck(st, n, curstab());
break;
case Oret: /* -> @a -> void */
if (sawret)
@@ -1471,6 +1482,7 @@
for (i = 0; i < st->npostcheck; i++) {
n = st->postcheck[i];
+ pushstab(st->postcheckscope[i]);
if (n->type == Nexpr && exprop(n) == Omemb)
infercompn(st, n);
else if (n->type == Nexpr && exprop(n) == Ocast)
@@ -1479,6 +1491,7 @@
checkstruct(st, n);
else
die("Thing we shouldn't be checking in postcheck\n");
+ popstab();
}
}
@@ -1508,6 +1521,46 @@
free(k);
}
+static void checkrange(Inferstate *st, Node *n)
+{
+ Type *t;
+ int64_t sval;
+ uint64_t uval;
+ static const int64_t svranges[][2] = {
+ /* signed ints */
+ [Tyint8] = {-128LL, 127LL},
+ [Tyint16] = {-32768LL, 32767LL},
+ [Tyint32] = {-2147483648LL, 2*2147483647LL}, /* FIXME: this has been doubled allow for uints... */
+ [Tyint] = {-2147483648LL, 2*2147483647LL},
+ [Tyint64] = {-9223372036854775808ULL, 9223372036854775807LL},
+ [Tylong] = {-9223372036854775808ULL, 9223372036854775807LL},
+ };
+
+ static const uint64_t uvranges[][2] = {
+ [Tybyte] = {0, 255ULL},
+ [Tyuint8] = {0, 255ULL},
+ [Tyuint16] = {0, 65535ULL},
+ [Tyuint32] = {0, 4294967295ULL},
+ [Tyuint64] = {0, 18446744073709551615ULL},
+ [Tyulong] = {0, 18446744073709551615ULL},
+ [Tychar] = {0, 4294967295ULL},
+ };
+
+ /* signed types */
+ t = type(st, n);
+ if (t->type >= Tyint8 && t->type <= Tylong) {
+ sval = n->lit.intval;
+ if (sval < svranges[t->type][0] || sval > svranges[t->type][1])
+ fatal(n->line, "Literal value %lld out of range for type \"%s\"", sval, tystr(t));
+ } else if ((t->type >= Tybyte && t->type <= Tyulong) || t->type == Tychar) {
+ uval = n->lit.intval;
+ if (uval < uvranges[t->type][0] || uval > uvranges[t->type][1])
+ fatal(n->line, "Literal value %llu out of range for type \"%s\"", tystr(t));
+ } else {
+ fatal(n->line, "Literal type %s has no range\n", tystr(t));
+ }
+}
+
/* After type inference, replace all types
* with the final computed type */
static void typesub(Inferstate *st, Node *n)
@@ -1576,6 +1629,8 @@
switch (n->lit.littype) {
case Lfunc:
typesub(st, n->lit.fnval); break;
+ case Lint:
+ checkrange(st, n);
default: break;
}
break;
--- a/parse/specialize.c
+++ b/parse/specialize.c
@@ -23,8 +23,6 @@
if (t->type == Typaram)
return 1;
- if (t->type == Tyname && t->isgeneric)
- return 1;
for (i = 0; i < t->nsub; i++)
if (hasparams(t->sub[i]))
return 1;
@@ -67,6 +65,7 @@
htput(tsmap, t, ret);
for (i = 0; i < t->nparam; i++)
lappend(&ret->arg, &ret->narg, tyspecialize(t->param[i], tsmap));
+ ret->isgeneric = hasparams(ret);
}
break;
case Tystruct: