waneck / testrepo

0 stars 0 forks source link

Issue 562 - Context parsing can have expression parameters. - haxe #562

Closed waneck closed 11 years ago

waneck commented 11 years ago

[Google Issue #562 : http://code.google.com/haxe/issues/detail?id=562] by stephane...@gmail.com, at 2011-11-01T21:53:14.000Z What steps will reproduce the problem?

  1. It's a macro api proposal; today we cannot parse some code referencing expression passed as parameter in our macros, we have to build the whole AST by hand. Something building the AST is requiered but often it would be easier to just not to have to.. 2. 3.

What is the expected output? What do you see instead?

This is a proposal to introduce another Context method to substitue some identifiers with existing expressions while parsing. The subsitution candidates would be passed by an anonymous object.

@:macro public static function foo(a : Expr) : Expr {

return Context.parseReplace("function () { return $a; }",                                                          Context.currentPos(), {a : a} );

}

Please complete the following class with minimal code reproducing the problem :

Please provide any additional information below.

waneck commented 11 years ago

[comment from ncanna...@gmail.com, published at 2011-11-11T19:44:54.000Z] This is similar to Scala proposal for quasiquotations right ? http://scalamacros.org/sips/quasiquotations.html

waneck commented 11 years ago

[comment from stephane...@gmail.com, published at 2011-11-11T20:32:46.000Z] Yes, I think this is an instance of quasi quotation. And to be frank; I though about it and what I've proposed here is ugly (wanted to propose something I though would be easier to do).

Quasi quotation look similar on more an more languages. The last implementation of stagging I have done brings us there https://github.com/sledorze/haxeExtensionBuilder/blob/master/src/StaggedTestMacros.hx But I don't like all the computations they involve and think Quasi quotation should be part of standard haxe macros; it's elegant, intuitive and cover a lot of macros use cases.

@:macro public static function forExample2(init : Expr, cond : Expr, inc : Expr, body : Expr, nb : Int = 5) : Expr return {

var arr = [];
for (ind in 0...5) {

  var localExpr =
    "{
      trace('wow' + $ind);
    }".stagged();

  arr.push(
    "{
      trace('i ' + $ind);
      $localExpr;
      $init;

      function oneTime() {
        if ($cond) {
          $body;
          $inc;
          oneTime();
        }
      }
      oneTime();
    }".stagged()
  );
}

return { expr : EBlock(arr), pos : Context.currentPos() };

}

waneck commented 11 years ago

[comment from shedok...@gmail.com, published at 2011-11-26T15:11:35.000Z] Or use % instead of $.

waneck commented 11 years ago

[comment from ncanna...@gmail.com, published at 2012-01-24T10:18:47.000Z]

waneck commented 11 years ago

[comment from ncanna...@gmail.com, published at 2012-05-15T09:25:02.000Z] One of the thing I was thinking of would be to add "macro" as a keyword in Haxe 3.0 :

waneck commented 11 years ago

[comment from franco.p...@gmail.com, published at 2012-05-15T13:06:33.000Z] macro are already so important that they really seem to deserve a keyword.

waneck commented 11 years ago

[comment from stephane...@gmail.com, published at 2012-05-15T13:25:43.000Z] @nicolas macro would be very welcome.

waneck commented 11 years ago

[comment from ncanna...@gmail.com, published at 2012-06-17T13:40:36.000Z] I've added the following :

function foo() { var x = macro myVar; var block = macro { foo(); foo(); foo(); } var efor = macro for( $x in array ) $block; return efor; }

Some testing/feedback is required, thanks :)

waneck commented 11 years ago

[comment from ncanna...@gmail.com, published at 2012-07-14T11:51:10.000Z]