forked from HaxeFoundation/haxe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[macro] add Context.eval (closes HaxeFoundation#2278)
- Loading branch information
Showing
9 changed files
with
137 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import haxe.macro.Expr; | ||
import haxe.macro.Context; | ||
|
||
class Macro { | ||
macro static public function call(efun:ExprOf<Int->Int>, eval:Expr):Expr { | ||
var vfun = Context.eval(efun); | ||
var vval = Context.eval(eval); | ||
var r = vfun(vval); | ||
return macro $v{r}; | ||
} | ||
|
||
macro static public function call2(efun:Int->Int, eval:Int):Expr { | ||
var r = efun(eval); | ||
return macro $v{r}; | ||
} | ||
|
||
macro static public function call3(efun:Int->Int, eval:Expr):Expr { | ||
var r = efun(Context.eval(eval)); | ||
return macro $v{r}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Macro; | ||
|
||
class Main { | ||
static function main() { | ||
write(Macro.call(function(x) return x * 3, 3)); | ||
write(Macro.call(MyTools.double, 3)); | ||
write(MyTools.double.call(3)); | ||
write(Macro.call(MyTools.double, Macro.call(MyTools.double, 3))); | ||
|
||
write(Macro.call2(function(x) return x * 3, 3)); | ||
write(Macro.call2(MyTools.double, 3)); | ||
write(MyTools.double.call2(3)); | ||
write(Macro.call3(MyTools.double, Macro.call(MyTools.double, 3))); | ||
} | ||
|
||
static function write(i:Int) { | ||
Sys.stderr().writeString(i + "\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class MyTools { | ||
static public function double(x:Int) { | ||
return x * 2; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-main Main | ||
--interp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
9 | ||
6 | ||
6 | ||
12 | ||
9 | ||
6 | ||
6 | ||
12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters