-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwinxedxx.winxed
2641 lines (2341 loc) · 65.3 KB
/
winxedxx.winxed
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! winxed
// winxedxx.winxed
// C++ backend for winxed
// (C) 2011-2012 Julián Albo "NotFound"
// This code uses and abuses winxed compiler internals.
// Not a good example on how to write winxed compiler extensions,
// but there is no better way right now.
//**************************************************************
// Declare used classes
namespace Winxed.Compiler
{
class Token;
class TokenString;
class TokenQuoted;
class TokenSingleQuoted;
class IntegerLiteral;
class FloatLiteral;
class StringLiteral;
class Builtin;
class ClassSpecifierStr;
class ClassSpecifierId;
class NullExpr;
class CastExpr;
class VarCastExpr;
class IdentifierExpr;
class LexicalVolatileExpr;
class MemberExpr;
class MemberRefExpr;
class IndexExpr;
class StringIndexExpr;
class OpExistsExpr;
class NewExpr;
class NewIndexedExpr;
class NewQualifiedExpr;
class ArrayExpr;
class HashExpr;
class FunctionExpr;
class FunctionId;
class FunctionRef;
class NullCheckerExpr;
class ConcatString;
class CallExpr;
class CallSubid;
class CallMemberExpr;
class CallBuiltinExpr;
class OpPreIncExpr;
class OpPreDecExpr;
class OpPostIncExpr;
class OpPostDecExpr;
class OpUnaryMinusExpr;
class OpAssignExpr;
class OpAssignToExpr;
class OpAddExpr;
class OpAddToExpr;
class OpSubToExpr;
class OpMulToExpr;
class OpSubExpr;
class RepeatString;
class OpMulExpr;
class OpDivExpr;
class OpModExpr;
class OpCModExpr;
class OpNotExpr;
class OpBinNotExpr;
class ZeroCheckerExpr;
class OpEqualExpr;
class OpSameExpr;
class OpLessEqualExpr;
class OpGreaterEqualExpr;
class OpLessExpr;
class OpGreaterExpr;
class OpBoolAndExpr;
class OpBoolOrExpr;
class OpBinOrExpr;
class OpBinAndExpr;
class OpBinXorExpr;
class OpShiftrightExpr;
class OpShiftleftExpr;
class OpInstanceOfExpr;
class OpConditionalExpr;
class OpNamespaceExpr;
class EmptyStatement;
class UsingNamespaceStatement;
class UsingStatement;
class StaticStatement;
class ExternStatement;
class ExprStatement;
class MultiStatement;
class CompoundStatement;
class VarStatement;
class IntStatement;
class FloatStatement;
class StringStatement;
class IntArrayStatement;
class FloatArrayStatement;
class StringArrayStatement;
class ConstStatement;
class MultiAssignStatement;
class ReturnStatement;
class LabelStatement;
class BreakStatement;
class ContinueStatement;
class IfStatement;
class GotoStatement;
class SwitchStatement;
class SwitchCaseStatement;
class ForStatement;
class ForeachStatement;
class DoStatement;
class WhileStatement;
class TryStatement;
class ThrowStatement;
class FunctionExtern;
class FunctionStatement;
class ClassStatement;
class NamespaceStatement;
class RootNamespace;
// Constants
const int
SEARCH_NAMESPACE = 1,
SEARCH_CLASS = 2,
VAR_is_lexical = 2;
} // namespace Winxed.Compiler
//**************************************************************
namespace WinxedXC.WinxedXX
{
using namespace Winxed.Compiler;
//**************************************************************
// Errors
function WxxError(string message)
{
return Error(message, 2, __WINXED_ERROR__);
}
function WxxUnsupported(thing, string msg[optional])
{
if (msg == null) {
if (thing instanceof "String")
msg = thing;
else
msg = typeof(thing);
}
string filename = "";
int line = 0;
string desc;
try {
var start;
if (thing instanceof Token)
start = thing;
else
start = thing.start;
filename = start.file;
line = start.line;
desc = start.viewable();
}
catch () { }
return WxxError(filename + ":" + string(line) + ": " +
msg + " unsupported near " + desc);
}
//**************************************************************
// Algorithms and binders
function for_each(params, func)
{
if (params != null) {
for (var param in params)
func(param);
}
}
function find_if(params, func)
{
for (var item in params)
if (func(item))
return item;
return null;
}
function bindfirst(func, argfirst[slurpy])
{
return function (arg2 [slurpy]) { func(argfirst:[flat], arg2:[flat]); };
}
function bindlast(func, arglast[slurpy])
{
return function (arg2 [slurpy]) { func(arg2:[flat], arglast:[flat]); };
}
function bindmethod(string name)
{
return function(var obj, var args[slurpy])
{
return obj.*name(args:[flat]);
};
}
// Types
const string DATA = "__data";
const string
// Parrot types
REGint = "I",
REGfloat = "N",
REGstring = "S",
REGvar = "P",
// Generated types, for own use and for NCI
Type_Void = "void",
Type_Short = "short",
Type_Int = "int",
Type_Long = "long",
Type_Num = "double",
Type_Float = "float",
Type_VoidPtr = "void*",
Type_String = "std::string",
Type_PMC = "WxxObjectPtr",
Type_Hash = "WxxHash",
Type_PMCArray = "WxxObjectArray",
Type_Instance = "WxxInstance";
function generatedType(string ptype)
{
switch (ptype) {
case REGint: return Type_Int;
case REGfloat: return Type_Num;
case REGstring: return Type_String;
case REGvar: return Type_PMC;
default:
throw "Internal error: wrong call: generatedType " + ptype;
}
}
// Helper C++ functions
const string ARGSEP = ", ";
const string
INTCAST = "wxx_int_cast",
NUMCAST = "wxx_num_cast",
STRINGCAST = "wxx_string_cast",
INDEXOF = "wxx_indexof",
ORD = "wxx_ord",
SUBSTR = "wxx_substr",
ERROR = "wxx_error",
OPEN = "wxx_open",
PRINT = "wxx_print",
EPRINT = "wxx_eprint";
//**************************************************************
function strtoxx(string str)
{
string result = "";
for (string c in str) {
switch (c) {
case "\"":
case "\\":
result += "\\" + c; break;
case "\t":
result += "\\t"; break;
case "\r":
result += "\\r"; break;
case "\n":
result += "\\n"; break;
default:
int code = ord(c);
if (code < 32)
result += "\\x" + substr(("0" + string(code.get_as_base(16))), -2);
else if (code > 65535)
result += "\\U" + substr(("0000000" + string(code.get_as_base(16))), -8);
else if (code > 127)
result += "\\u" + substr(("000" + string(code.get_as_base(16))), -4);
else
result += c;
}
}
return result;
}
function tokstringtoxx(strval)
{
__ASSERT__(strval instanceof TokenString);
return strtoxx(unescape(strval.getasquoted()));
}
function stringtoxx(literal)
{
if (!(literal instanceof StringLiteral)) die("UUU");
__ASSERT__(literal instanceof StringLiteral);
return tokstringtoxx(literal.strval);
}
function nciitemtoc(string item)
{
switch (item)
{
case "v":
return Type_Void;
case "i": // int
case "I": // INTVAL
return Type_Int;
case "s":
return Type_Short;
case "l":
return Type_Long;
case "d":
case "N":
return Type_Num;
case "f":
return Type_Float;
case "p":
return Type_VoidPtr;
default:
throw Error("signature unsupported: " + item);
}
}
function ncisigtoc(string sig)
{
string csig[];
for (string item in sig)
push(csig, nciitemtoc(item));
return csig;
}
//**********************************************************************
// Emit helpers
//**********************************************************************
function emit_namespacefromroot(var out, var path)
{
out.print("getRootNamespace()");
for (string item in path)
out.print(".childNamespace(\"", item, "\")");
}
function emit_plainfun1(var out, string name, var arg)
{
out.print(name, "(");
emit_expr(out, arg);
out.print(")");
}
function emit_plainfun1string(var out, string name, var arg)
{
out.print(name, "(");
if (arg.checkresult() != REGstring)
out.print(STRINGCAST);
emit_expr(out, arg);
out.print(")");
}
function emit_plainfun2(var out, string name, var arg1, var arg2)
{
out.print(name, "(");
emit_expr(out, arg1);
out.print(ARGSEP);
emit_expr(out, arg2);
out.print(")");
}
//**********************************************************************
// Builtins
//**********************************************************************
function emit_callbuiltin_void(out, string name)
{
out.print(name, "()");
}
function emit_builtin_print(out, args)
{
for (var arg in args) {
var argexp = arg.arg;
out.print(PRINT);
emit_expr(out, argexp);
out.print(ARGSEP);
}
out.print("0");
}
function emit_builtin_say(out, args)
{
for (var arg in args) {
var argexp = arg.arg;
out.print(PRINT);
emit_expr(out, argexp);
out.print(ARGSEP);
}
out.print(PRINT + "(\"\\n\"), 0");
}
function emit_builtin_cry(out, args)
{
for (var arg in args) {
var argexp = arg.arg;
out.print(EPRINT);
emit_expr(out, argexp);
out.print(ARGSEP);
}
out.print(EPRINT + "(\"\\n\"), 0");
}
function emit_builtin_open(out, args)
{
int nargs = elements(args);
out.print(OPEN, "(");
emit_stringexpr(out, args[0].arg);
if (nargs > 1) {
out.print(ARGSEP);
emit_stringexpr(out, args[1].arg);
}
out.print(")");
}
function emit_builtin_Error(out, args)
{
int nargs = elements(args);
out.print(ERROR, "(");
emit_stringexpr(out, args[0].arg);
if (nargs > 1) {
out.print(ARGSEP);
emit_intexpr(out, args[1].arg);
if (nargs > 2) {
out.print(ARGSEP);
emit_intexpr(out, args[2].arg);
}
}
out.print(")");
}
function emit_builtin_dlfunc(out, args)
{
var sig = args[2].arg;
if (! sig.isstringliteral())
throw WxxError("dlfunc siganture must be compile time constant");
var csig = ncisigtoc(sig.strval.rawstring());
int n = elements(csig);
if (n < 1)
throw WxxError("invalid signature");
out.print("wxx_dlfunc<");
out.print(csig[0]);
for (int i = 1; i < n; ++i) {
string item = csig[i];
if (item == Type_Void)
throw WxxError("invalid signature");
out.print(ARGSEP, item);
}
out.print(">(");
emit_expr(out, args[0].arg);
out.print(ARGSEP);
emit_stringexpr(out, args[1].arg);
out.print(")");
}
function emit_builtin_ord(out, args)
{
int nargs = elements(args);
out.print(ORD, "(");
emit_stringexpr(out, args[0].arg);
if (nargs > 1) {
out.print(ARGSEP);
emit_intexpr(out, args[1].arg);
}
out.print(")");
}
function emit_builtin_substr(out, args)
{
int nargs = elements(args);
out.print(SUBSTR, "(");
emit_stringexpr(out, args[0].arg);
out.print(ARGSEP);
emit_intexpr(out, args[1].arg);
if (nargs > 2) {
out.print(ARGSEP);
emit_intexpr(out, args[2].arg);
}
out.print(")");
}
function emit_builtin_indexof(out, args)
{
int nargs = elements(args);
out.print(INDEXOF, "(");
emit_stringexpr(out, args[0].arg);
out.print(ARGSEP);
emit_stringexpr(out, args[1].arg);
if (nargs > 2) {
out.print(ARGSEP);
emit_intexpr(out, args[2].arg);
}
out.print(")");
}
//**********************************************
function emitExpr(var out, :CallBuiltinExpr expr)
{
var builtin = expr.builtin;
string name = builtin.name;
var args = expr.args;
int nargs = elements(args);
var arg0 = nargs > 0 ? args[0].arg : null;
var arg1 = nargs > 1 ? args[1].arg : null;
string type0;
int i;
switch (name) {
case "__ASSERT__":
// No --debug option yet, emit something parseable as an expression
// that does nothing.
out.print("(void)0");
break;
case "string":
emit_plainfun1(out, STRINGCAST, arg0);
break;
case "int":
emit_plainfun1(out, INTCAST, arg0);
break;
case "float":
emit_plainfun1(out, NUMCAST, arg0);
break;
case "typeof":
out.print("wxx_typeof");
emit_expr(out, arg0);
break;
case "getstdin":
case "getstdout":
case "getstderr":
out.print("wxx_" + name + "()");
break;
case "print":
emit_builtin_print(out, args);
break;
case "say":
emit_builtin_say(out, args);
break;
case "cry":
emit_builtin_cry(out, args);
break;
case "elements":
emit_expr(out, arg0);
out.print(".elements()");
break;
case "length":
case "bytelength":
emit_plainfun1(out, "wxx_length", arg0);
break;
case "ord":
emit_builtin_ord(out, args);
break;
case "chr":
emit_plainfun1(out, "wxx_chr", arg0);
break;
case "indexof":
emit_builtin_indexof(out, args);
break;
case "substr":
emit_builtin_substr(out, args);
break;
case "chomp":
emit_plainfun1(out, "wxx_chomp", arg0);
break;
case "join":
emit_plainfun2(out, "wxx_join", arg0, arg1);
break;
case "downcase":
// Fake implementation, just cast to string the argument
emit_stringexpr(out, arg0);
break;
case "push":
emit_expr(out, arg0);
out.print(".push");
emit_expr(out, arg1);
break;
case "split":
emit_plainfun2(out, "wxx_split", arg0, arg1);
break;
case "die":
out.print("wxx_die(");
emit_expr(out, arg0);
out.print(")");
break;
case "Error":
emit_builtin_Error(out, args);
break;
case "open":
emit_builtin_open(out, args);
break;
case "loadlib":
emit_plainfun1(out, "wxx_loadlib", arg0);
break;
case "dlfunc":
emit_builtin_dlfunc(out, args);
break;
case "spawnw":
emit_plainfun1(out, "wxx_spawnw", arg0);
break;
case "exit":
emit_plainfun1(out, "exit", arg0);
break;
case "sin":
case "cos":
case "tan":
case "asin":
case "acos":
case "atan":
case "exp":
case "sqrt":
emit_plainfun1(out, name, arg0);
break;
case "ln":
emit_plainfun1(out, "log", arg0);
break;
case "time":
emit_callbuiltin_void(out, "wxx_time");
break;
case "floattime":
emit_callbuiltin_void(out, "wxx_floattime");
break;
case "sleep":
emit_plainfun1(out, "wxx_sleep", arg0);
break;
// Unsupported, always return null
case "load_bytecode":
case "load_language":
case "compreg":
case "getinterp":
out.print("winxedxxnull");
break;
// Unimplemented, just to allow some testing
case "clone":
emit_expr(out, arg0);
break;
// Unimplemented, just to allow some testing
case "get_class":
out.print("winxedxxnull");
break;
case "escape":
emit_plainfun1string(out, "wxx_escape", arg0);
break;
case "unescape":
emit_plainfun1string(out, "wxx_unescape", arg0);
break;
default:
throw WxxUnsupported(expr, "builtin " + name);
}
}
//**********************************************************************
function emitExpr(var out, :IntegerLiteral expr)
{
out.print(expr.getIntegerValue());
}
function emitExpr(var out, :FloatLiteral expr)
{
out.print(expr.numval);
}
function emitExpr(var out, :StringLiteral expr)
{
out.print(Type_String + "(\"");
out.print(stringtoxx(expr));
out.print("\")");
}
function emitExpr(var out, :IdentifierExpr expr)
{
string name = expr.name;
if (name == "null")
out.print("winxedxxnull");
else
out.print(name);
}
function emitExpr(var out, :LexicalVolatileExpr expr)
{
string name = expr.start.getidentifier();
out.print(name);
}
function emitExpr(var out, :NullExpr expr)
{
out.print("winxedxxnull");
}
function emitExpr(var out, :CastExpr expr)
{
var arg = expr.arg;
string type = expr.type;
switch (type) {
case REGint:
emit_intexpr(out, arg);
break;
case REGfloat:
emit_numexpr(out, arg);
break;
case REGstring:
emit_stringexpr(out, arg);
break;
default:
throw WxxError("unexpected cast");
}
}
function emitExpr(var out, :VarCastExpr expr)
{
var arg = expr.arg;
switch {
case arg instanceof MemberExpr:
string key[];
arg.buildkey(key);
string name = key.pop();
emit_namespacefromroot(out, key);
out.print(".get(\"", name, "\")");
break;
default:
throw WxxUnsupported(expr);
}
}
function emitExpr(var out, :OpPreIncExpr expr)
{
emit_plainfun1(out, "wxx_preinc", expr.subexpr);
}
function emitExpr(var out, :OpPreDecExpr expr)
{
emit_plainfun1(out, "wxx_predec", expr.subexpr);
}
function emitExpr(var out, :OpPostIncExpr expr)
{
emit_plainfun1(out, "wxx_postinc", expr.subexpr);
}
function emitExpr(var out, :OpPostDecExpr expr)
{
emit_plainfun1(out, "wxx_postdec", expr.subexpr);
}
//**********************************************************************
function emit_callargs(out, args)
{
out.print("WinxedXX::", Type_PMCArray, "()");
int nargs = args == null ? 0 : args.numargs();
for (int i = 0; i < nargs; ++i) {
out.print(".push(");
var arg = args.getfreearg(i);
emit_expr(out, arg);
out.print(")");
}
}
function emitExpr(var out, :CallSubid callexpr)
{
out.print(callexpr.subid, "(");
emit_callargs(out, callexpr.args);
out.print(")");
}
function emitExpr(var out, :CallMemberExpr callexpr)
{
var funref = callexpr.funref;
var args = callexpr.args;
int nargs = args == null ? 0 : args.numargs();
var left = funref.left;
int is_var = left.checkresult() == REGvar;
if (!is_var)
out.print(Type_PMC, "(");
emit_expr(out, funref.left);
if (!is_var)
out.print(")");
out.print(".call_method(\"", funref.right.getidentifier(), "\"");
if (nargs > 0) {
out.print(ARGSEP);
emit_callargs(out, args);
}
out.print(")");
}
function emitExpr(var out, :CallExpr callexpr)
{
var funref = callexpr.funref;
var args = callexpr.args;
switch {
case funref instanceof FunctionExpr:
out.print(Type_PMC);
emit_expr(out, funref);
break;
case funref instanceof FunctionId:
out.print(funref.subid);
break;
case funref instanceof FunctionRef:
var path = funref.sym.owner.getpath().path;
var name = funref.getstart();
emit_namespacefromroot(out, path);
out.print(".get(\"", name.getidentifier(), "\")");
break;
case funref.isidentifier():
if ((callexpr instanceof CallMemberExpr) && callexpr.subid != null)
out.print(callexpr.subid);
else {
string callname = funref.getName();
var sym = callexpr.owner.getvar(callname);
if (sym != null)
out.print(callname);
else
out.print("wxxGetCurrentNamespace().find_symbol(\"", callname, "\")");
}
break;
default:
emit_expr(out, funref);
}
out.print("(");
emit_callargs(out, args);
out.print(")");
}
function emitExpr(var out, :ArrayExpr expr)
{
var values = expr.values;
int nvalues = values == null ? 0 : elements(values);
out.print(Type_PMC, "(&((* new ", Type_PMCArray, "())");
int i;
for (i = 0; i < nvalues; ++i) {
out.print(".push");
emit_expr(out, values[i]);
}
out.print("))");
}
function emitExpr(var out, :HashExpr expr)
{
var keys = expr.keys;
var values = expr.values;
int n = elements(keys);
out.print(Type_PMC, "(&((* new ", Type_Hash, "())");
int i;
for (i = 0; i < n; ++i) {
out.print(".set(");
emit_expr(out, keys[i]);
out.print(ARGSEP);
emit_expr(out, values[i]);
out.print(")");
}
out.print("))");
}
function emitExpr(var out, :FunctionExpr expr)
{
var funst = expr.fn;
out.print("new ", funst.name, "(* ", DATA, ")");
}
function emitExpr(var out, :FunctionId funid)
{
out.print(funid.subid);
}
function emitExpr(var out, :NewExpr expr)
{
var initializer = expr.initializer;
int numinits = initializer == null ? -1 : initializer.numargs();
var value = expr.value;
if (value.isidentifier()) {
var id = expr.owner.getvar(value);
if (id != null) {
out.print("wxx_new(");
emit_expr("test");
}
else {
var cl = expr.owner.checkclass(value);
if (cl != null) {
var key = cl.getpath().path;
out.print("wxx_new_obj(getRootNamespace()");
int n = elements(key);
for (int i = 0; i < n - 1; ++i)
out.print(".childNamespace(\"", key[i], "\")");
string clname = "\"" + string(key[n - 1]) + "\"";
out.print(".get(", clname, ")");
if (numinits >= 0)
out.print(ARGSEP, clname);
}
else
out.print("wxx_new(\"", value, "\"");
}
if (numinits > 0) {
out.print(ARGSEP);
emit_callargs(out, initializer);
}
out.print(")");
}
else if (value.isstring()) {
out.print("wxx_new_string(\"", value.getasquoted(), "\")");
}
else {
if (__DEBUG__) cry("In new expression:");
throw WxxUnsupported(expr);
}
}
function emitExpr(var out, :NewIndexedExpr expr)
{
var key = expr.nskey.key;
if (elements(key) != 1)
throw WxxUnsupported(expr, "supported only with keys length == 1");
// TESTING
string name = key[0];
out.print("wxx_new_string(\"", name, "\"");
var initializer = expr.initializer;
int numinits = initializer == null ? -1 : initializer.numargs();
switch (numinits) {
case -1:
break;
case 1:
out.print(ARGSEP);
emit_expr(out, initializer.getfreearg(0));
break;
default:
throw WxxError("Invalid initializer");
}
out.print(")");
}
function emitExpr(var out, :NewQualifiedExpr expr)
{
var newkey = expr.nskey;
var nskey = newkey.checknskey(expr);
var initializer = expr.initializer;
int numinits = initializer == null ? -1 : initializer.numargs();
// TESTING
var key = newkey.key;
out.print("wxx_new_obj(getRootNamespace()");
int n = elements(key);
for (int i = 0; i < n - 1; ++i)
out.print(".childNamespace(\"", key[i], "\")");
string clname = "\"" + string(key[n - 1]) + "\"";
out.print(".get(", clname, ")");
if (numinits >= 0) {
out.print(ARGSEP, clname);
if (numinits > 0) {
out.print(ARGSEP);
emit_callargs(out, initializer);
}
}
out.print(")");
}
function emitExpr(var out, :OpAssignExpr assignexpr)
{
var lexpr = assignexpr.lexpr;