This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMain.hx
155 lines (119 loc) · 4.84 KB
/
Main.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package;
// http://haxe.org/manual/macro.html
// http://haxe.ru/makrosy-haxe-vvodnaya-statya
import sys.io.File;
import haxe.macro.Expr;
import haxe.macro.Context;
class Main {
static function main () {
//------------------------------------------------------------------
trace("--- Call macros ---");
trace("getBuildDate1: " + getBuildDate1()); // Call macros with result type as String
trace("getBuildDate2: " + getBuildDate2()); // Call macros with casting result type String to Date and parsing of string contained some Haxe source code
trace("test: " + test(Date.fromString("2013-01-01"))); // Call macro to transfer arbitrary expressions and view contents
trace("getBuildDate3: " + getBuildDate3()); // Call macros with casting result type String to Date and without parsing of string contained some Haxe source code
//------------------------------------------------------------------
trace("--- Call macros (macro reification) ---");
trace("getBuildDate4: " + getBuildDate4());
trace("call method hello: " + hello());
var a = 0;
repeat(a < 10, a++);
trace("value of a after macros called: " + a);
var b = 0;
var c = add(b++); // c = (b++) + 0 = 1 + 0 = 1, b = (b++) + (b++) = 1 + 1 = 2
trace("value of b after macros called: " + b);
trace("value of c after macros called: " + c);
trace("getFileContent: " + getFileContent("../../../../readme.txt"));
trace("getFileContent2: " + getFileContent("../../../../readme.txt"));
trace("getFileContentUpperCase1: " + getFileContentTripleUpperCase1("readme.txt"));
trace("getFileContentUpperCase2: " + getFileContentTripleUpperCase2("../../../../readme.txt"));
}
macro static public function getBuildDate1 () : Expr { // returns String as result
var d = Date.now();
return Context.makeExpr(d.toString(), Context.currentPos());
}
macro static public function getBuildDate2 () : Expr { // returns Date as result
var d = Date.now();
return Context.parse("Date.fromString('" + d.toString() + "')", Context.currentPos()); // parse the string contained some Haxe source code
}
macro static function test (e: Expr) {
trace("--- Show data of strcture Expr ---");
trace(e);
return e;
}
macro static public function getBuildDate3 () : Expr { // returns Date as result without parsing of string contained some Haxe source code
var d = Date.now();
var p = Context.currentPos();
return {
expr: ECall( {
expr: EField( {
expr: EConst(CIdent("Date")), pos: p
}, "fromString"), pos:p
},
[ { expr: EConst(CString(d.toString())), pos: p } ]), pos: p
};
}
//------------------------------------------------------------------
// macro reification expression
// http://old.haxe.org/manual/macros#macro-reification
// http://haxe.org/manual/macro-reification-expression.html
// http://ncannasse.fr/blog/reification_for_macros
macro static public function getBuildDate4 () : Expr { // упрощённый вариант макроса с приведением результирующего типа String в Date без парсинга строки с Haxe кодом
var d = Date.now();
var e = Context.makeExpr(d.toString(), Context.currentPos());
return macro Date.fromString($e);
}
macro static public function hello () {
return macro "Hello World";
}
macro public static function repeat (cond : Expr, e : Expr) : Expr {
return macro while ($cond) trace ($e);
}
macro static function add (e: Expr) {
return macro $e + $e;
}
macro public static function getFileContent2 (fname : String) : Expr {
return macro $(File.getContent(fname));
}
macro public static function getFileContent (fname : Expr) : Expr {
return macro File.getContent($fname);
}
//------------------------------------------------------------------
macro public static function getFileContentTripleUpperCase1 (fname : String) : Expr { // macro reification expression
var fContent = File.getContent(fname);
fContent = StringTools.replace(fContent, '\n', '').toUpperCase();
var tripleContent = "";
for (i in 0...3) {
tripleContent += fContent;
}
var e = Context.makeExpr(tripleContent, Context.currentPos());
return macro $e;
}
macro public static function getFileContentTripleUpperCase2 (fname : String) : Expr { // ExprDef
var p = Context.currentPos();
return {
expr: ECall({
expr: EField({
expr: ECall(
{ expr: EField({ expr: EConst(CIdent("StringTools")), pos: p }, "replace"), pos: p },
[
{
expr: ECall(
{ expr: EField( { expr: EConst(CIdent("File")), pos: p }, "getContent"), pos: p },
[{ expr: EConst(CString(fname)), pos: p }]
),
pos: p
},
{ expr: EConst(CString('\n')), pos: p },
{ expr: EConst(CString('')), pos: p }
]
),
pos: p
}, "toUpperCase"),
pos: p
},
[]),
pos: p
};
}
}