HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.18k stars 655 forks source link

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

Closed issuesbot closed 11 years ago

issuesbot commented 11 years ago

[Google Issue #562 : https://code.google.com/p/haxe/issues/detail?id=562] by stephane...@gmail.com, at 01/11/2011, 21:53:14 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.

issuesbot commented 11 years ago

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

issuesbot commented 11 years ago

[comment from stephane...@gmail.com, published at 11/11/2011, 20:32:46] 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() };
}
issuesbot commented 11 years ago

[comment from shedok...@gmail.com, published at 26/11/2011, 15:11:35] Or use % instead of $.

issuesbot commented 11 years ago

[comment from ncanna...@gmail.com, published at 24/01/2012, 10:18:47]

issuesbot commented 11 years ago

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

issuesbot commented 11 years ago

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

issuesbot commented 11 years ago

[comment from stephane...@gmail.com, published at 15/05/2012, 13:25:43] @ nicolas macro would be very welcome.

issuesbot commented 11 years ago

[comment from ncanna...@gmail.com, published at 17/06/2012, 13:40:36] 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 :)

issuesbot commented 11 years ago

[comment from ncanna...@gmail.com, published at 14/07/2012, 11:51:10]