Permalink
Browse files

fix simple closures

  • Loading branch information...
1 parent 5566400 commit ad1eaf3bed20f1803fc2654e79b2ebf51506f581 @NotFound committed Jun 21, 2012
Showing with 55 additions and 19 deletions.
  1. +1 −0 Makefile
  2. +24 −0 t/base/10closure.t
  3. +30 −19 winxedxx.winxed
View
@@ -116,6 +116,7 @@ TESTS = t/base/00test.t \
t/base/02varcast.t \
t/base/03pmc.t t/base/03class.t \
t/base/06localfunctions.t \
+ t/base/10closure.t \
t/base/features.t
test: winxedxx.pbc $(FRONTEND) winxedxx.h $(LIB)
View
@@ -0,0 +1,24 @@
+#! winxed
+
+// winxedxx base tests
+// closures
+
+using extern Test.More is, done_testing;
+
+function getfun()
+{
+ var _TEST_n = 42;
+ var f = function () { return _TEST_n; };
+ return f;
+}
+
+function main[main](var args)
+{
+ var fun = getfun();
+ int i = fun();
+ is(i, 42, "calling a closure");
+
+ done_testing();
+}
+
+// End
View
@@ -34,6 +34,7 @@ class CastExpr;
class VarCastExpr;
class IdentifierExpr;
+class LexicalVolatileExpr;
class MemberExpr;
class MemberRefExpr;
class IndexExpr;
@@ -972,27 +973,14 @@ function emitExpr(var out, :OpAssignExpr assignexpr)
var lexpr = assignexpr.lexpr;
var rexpr = assignexpr.rexpr;
string ltype = lexpr.checkresult();
- string rtype = rexpr.checkresult();
switch {
case lexpr instanceof IdentifierExpr:
out.print(lexpr.name, " = ");
- switch {
- case ltype == REGint:
- emit_intexpr(out, rexpr);
- break;
- case ltype == REGstring:
- emit_stringexpr(out, rexpr);
- break;
- case ltype == REGfloat:
- emit_numexpr(out, rexpr);
- break;
- case ltype == REGvar && rtype == REGvar:
- emit_expr(out, rexpr); // Nothing in particular
- break;
- default:
- out.print(generatedType(ltype));
- emit_expr(out, rexpr);
- }
+ emit_typedexpr(out, rexpr, ltype);
+ break;
+ case lexpr instanceof LexicalVolatileExpr:
+ out.print(lexpr.start.getidentifier(), " = ");
+ emit_typedexpr(out, rexpr, ltype);
break;
case lexpr instanceof MemberExpr:
emit_expr(out, lexpr.left);
@@ -1442,6 +1430,23 @@ function emit_varexpr(out, expr)
emit_expr(out, expr);
}
+function emit_typedexpr(out, expr, string type)
+{
+ switch (type) {
+ case REGint:
+ emit_intexpr(out, expr);
+ break;
+ case REGstring:
+ emit_stringexpr(out, expr);
+ break;
+ case REGfloat:
+ emit_numexpr(out, expr);
+ break;
+ default:
+ emit_varexpr(out, expr);
+ }
+}
+
//**********************************************************************
// Statements in functions
//**********************************************************************
@@ -1548,8 +1553,14 @@ function emitStatement(var out, :VarStatement st)
string name = st.name;
var init = st.init;
+ int flags = 0;
+ string lexname = null;
var local = st.owner.getvar(name);
- int is_lex = local != null && (local.getflags() & VAR_is_lexical);
+ if (local != null) {
+ flags = local.getflags();
+ lexname = local.getlex();
+ }
+ int is_lex = (flags & VAR_is_lexical) || lexname != null;
// If lexical, declare a reference to the data struct
// Else, declare plain var

0 comments on commit ad1eaf3

Please sign in to comment.