summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-09-29 01:33:27 -0700
committerOri Bernstein <ori@eigenstate.org>2015-09-29 01:33:27 -0700
commit6a4040c99ee0127808d77ad2dc38220f1a8a1c46 (patch)
tree13bcf7821021b988dd0e7ad546dd92ce8253017e
parent501c03fca3a77243ae2e7415a199b08e806c96b2 (diff)
downloadmc-6a4040c99ee0127808d77ad2dc38220f1a8a1c46.tar.gz
Implement casting functions to pointers.
Useful for interfacing with C ABIs.
-rw-r--r--6/simp.c10
-rw-r--r--test/fncast.myr16
-rw-r--r--test/tests1
3 files changed, 24 insertions, 3 deletions
diff --git a/6/simp.c b/6/simp.c
index d4dd168..c6db9b7 100644
--- a/6/simp.c
+++ b/6/simp.c
@@ -933,11 +933,15 @@ static Node *simpcast(Simp *s, Node *val, Type *to)
switch (t->type) {
/* ptr -> slice conversion is disallowed */
case Tyslice:
- if (t->type == Typtr)
- fatal(val, "bad cast from %s to %s",
- tystr(exprtype(val)), tystr(to));
+ if (to->type != Typtr)
+ fatal(val, "bad cast from %s to %s", tystr(exprtype(val)), tystr(to));
r = slicebase(s, val, NULL);
break;
+ case Tyfunc:
+ if (to->type != Typtr)
+ fatal(val, "bad cast from %s to %s", tystr(exprtype(val)), tystr(to));
+ r = getcode(s, val);
+ break;
/* signed conversions */
case Tyint8: case Tyint16: case Tyint32: case Tyint64:
case Tyint:
diff --git a/test/fncast.myr b/test/fncast.myr
new file mode 100644
index 0000000..08b8d1e
--- /dev/null
+++ b/test/fncast.myr
@@ -0,0 +1,16 @@
+use std
+
+const main = {
+ var fn, envfn
+ var ptr
+
+ fn = {
+ }
+ envfn = {
+ ptr = ptr
+ }
+ ptr = fn castto(byte#)
+ ptr = envfn castto(byte#)
+ std.put("ok\n", ptr)
+}
+
diff --git a/test/tests b/test/tests
index e24a6c5..9fb9246 100644
--- a/test/tests
+++ b/test/tests
@@ -59,6 +59,7 @@ B callbig E 42
B nestfn E 42
B foldidx P 123,456
B closure P 111555333666
+B fncast P ok
B loop P 0123401236789
B subrangefor P 12
B patiter P 23512