-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBRSCompiler.hx
More file actions
executable file
·172 lines (147 loc) · 5.55 KB
/
BRSCompiler.hx
File metadata and controls
executable file
·172 lines (147 loc) · 5.55 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package brscompiler;
// Make sure this code only exists at compile-time.
#if (macro || brs_runtime)
// Import relevant Haxe macro types.
import brscompiler.subcompiler.ClassCompiler;
import brscompiler.subcompiler.ExpressionCompiler;
import brscompiler.subcompiler.FieldAccessCompiler;
import brscompiler.subcompiler.FieldCompiler;
import brscompiler.subcompiler.TypeCompiler;
import haxe.macro.Expr;
import haxe.macro.Type;
import reflaxe.DirectToStringCompiler;
import reflaxe.data.ClassFuncArg;
import reflaxe.data.ClassFuncData;
import reflaxe.data.ClassVarData;
import reflaxe.data.EnumOptionData;
import reflaxe.helpers.Context;
import sys.FileSystem;
import sys.io.File;
import brscompiler.config.Define;
using StringTools;
using reflaxe.helpers.ArrayHelper;
using reflaxe.helpers.BaseTypeHelper;
using reflaxe.helpers.ClassFieldHelper;
using reflaxe.helpers.ClassTypeHelper;
using reflaxe.helpers.ModuleTypeHelper;
using reflaxe.helpers.NameMetaHelper;
using reflaxe.helpers.NullHelper;
using reflaxe.helpers.NullableMetaAccessHelper;
using reflaxe.helpers.OperatorHelper;
using reflaxe.helpers.StringBufHelper;
using reflaxe.helpers.SyntaxHelper;
using reflaxe.helpers.TypeHelper;
using reflaxe.helpers.TypedExprHelper;
class BRSCompiler extends DirectToStringCompiler {
public final selfStack:Array<{selfName:String, publicOnly:Bool}> = [];
public final compilingInConstructor:Bool = false;
public final TComp:TypeCompiler;
final EComp:ExpressionCompiler;
final CComp:ClassCompiler;
final FAComp:FieldAccessCompiler;
public final FComp:FieldCompiler;
public function new() {
super();
TComp = new TypeCompiler(this);
EComp = new ExpressionCompiler(this);
CComp = new ClassCompiler(this);
FAComp = new FieldAccessCompiler(this);
FComp = new FieldCompiler(this);
}
public static function saveFileWithDirs(filePath:String, content:String):Void {
// Find the last slash (Unix or Windows style)
final slashIndex = Math.max(filePath.lastIndexOf("/"), filePath.lastIndexOf("\\"));
// Extract the directory portion from the filePath (if any)
final directory = if (slashIndex == -1) "" else filePath.substr(0, Std.int(slashIndex));
// Create the directory structure if not empty (and doesn't already exist)
if (directory != "" && !FileSystem.exists(directory)) {
FileSystem.createDirectory(directory);
}
// Now write the file content
File.saveContent(filePath, content);
}
public function compileClassImpl(classType:ClassType, varFields:Array<ClassVarData>, funcFields:Array<ClassFuncData>):Null<String> {
return CComp.compileClassImpl(classType, varFields, funcFields);
}
public function compileFunctionArgument(arg:ClassFuncArg, pos:Position, forceObject:Bool = false) {
final result = new StringBuf();
result.add(compileVarName(arg.getName()));
if (forceObject) {
// TODO: Implement proper type-based coercion
// result.addMulti(' as ', 'Object');
} else {
final type = TComp.compileType(arg.type, pos);
if (type != null) {
// TODO: Implement proper type-based coercion
// result.addMulti(' as ', type);
}
}
return result.toString();
}
public function compileEnumImpl(enumType:EnumType, constructs:Array<EnumOptionData>):Null<String> {
return null;
}
public function fieldAccessToBrightScript(e:TypedExpr, fa:FieldAccess):String {
return FAComp.fieldAccessToBrightScript(e, fa);
}
public function fieldAccessToBrightScriptResolved(resolvedExpr:String, e:TypedExpr, fa:FieldAccess):String {
return FAComp.fieldAccessToBrightScriptResolved(resolvedExpr, e, fa);
}
public function fullCallPath(cls:ClassType) {
final className = cls.name;
final pkg = cls.pack;
final fullPath:Array<String> = pkg.copy();
fullPath.push(className);
return fullPath;
}
public function compileExpressionImpl(expr:TypedExpr, topLevel:Bool):Null<String> {
return EComp.compileExpressionImpl(expr, topLevel);
}
override public function compileVarName(name: String, expr: Null<TypedExpr> = null, field: Null<ClassField> = null): String {
if(name == "isSync"){
// trace('================>' + name, expr);
}
// return super.compileVarName(name, expr, field);
if(reservedVarNameMap != null) {
while(reservedVarNameMap.exists(name)) {
name = "_" + name;
}
}
return '$name';
}
override function onOutputComplete() {
final outputFile = Context.getDefines().get("brightscript-output");
var content = sys.io.File.getContent(outputFile);
// Generate __callFnN__ helpers for bound closure dispatch.
// These check if a function value is a bound method wrapper (AA with .call)
// and call through .call() to preserve the `m` context binding.
var helpers = new StringBuf();
for (arity in 0...6) {
helpers.add('${Define.Function} ${Define.FnCall(arity)}(${Define.Ctx} as Object, fn as Object');
for (i in 0...arity) {
helpers.add(', a$i as Object');
}
helpers.add(') as Object\n');
helpers.add('if Type(fn) = "roAssociativeArray" AND fn.__self <> invalid then return fn.call(');
for (i in 0...arity) {
if (i > 0) helpers.add(', ');
helpers.add('a$i');
}
helpers.add('${arity > 0 ? ", " : ""}fn.__self)');
helpers.add('\nif Type(fn) = "roAssociativeArray" AND fn.${Define.Ctx} <> invalid then return fn.call(fn.${Define.Ctx}');
for (i in 0...arity) {
helpers.add(', ');
helpers.add('a$i');
}
helpers.add(')');
helpers.add('\nreturn fn(');
for (i in 0...arity) {
if (i > 0) helpers.add(', ');
helpers.add('a$i');
}
helpers.add(')\n${Define.EndFunction}\n\n');
}
sys.io.File.saveContent(outputFile, helpers.toString() + content + "\n\n\nMain().main()");
}
}
#end