aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/src/LuaSL_compile.c
blob: a410152f10ffe65018f12a21f82689479edc3dae (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
#include "LuaSL.h"


static LSL_Leaf *evaluateFloatToken(LSL_Leaf  *content, LSL_Leaf *left, LSL_Leaf *right);
static LSL_Leaf *evaluateIntegerToken(LSL_Leaf  *content, LSL_Leaf *left, LSL_Leaf *right);
static LSL_Leaf *evaluateNoToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
static LSL_Leaf *evaluateOperationToken(LSL_Leaf  *content, LSL_Leaf *left, LSL_Leaf *right);
static LSL_Leaf *eveluateParenthesisToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
static LSL_Leaf *evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
static void outputBlockToken(FILE *file, outputMode mode, LSL_Leaf *content);
static void outputFloatToken(FILE *file, outputMode mode, LSL_Leaf *content);
static void outputFunctionToken(FILE *file, outputMode mode, LSL_Leaf *content);
static void outputIntegerToken(FILE *file, outputMode mode, LSL_Leaf *content);
static void outputIdentifierToken(FILE *file, outputMode mode, LSL_Leaf *content);
static void outputParameterListToken(FILE *file, outputMode mode, LSL_Leaf *content);
static void outputParenthesisToken(FILE *file, outputMode mode, LSL_Leaf *content);
static void outputStateToken(FILE *file, outputMode mode, LSL_Leaf *content);
static void outputStatementToken(FILE *file, outputMode mode, LSL_Leaf *content);

LSL_Token LSL_Tokens[] =
{
    // Various forms of "space".
    {LSL_COMMENT,		ST_NONE,		"/*",	LSL_NONE,				NULL, NULL},
    {LSL_COMMENT_LINE,		ST_NONE,		"//",	LSL_NONE,				NULL, NULL},
    {LSL_SPACE,			ST_NONE,		" ",	LSL_NONE,				NULL, NULL},

    // Operators, in order of precedence, low to high
    // Left to right, unless otherwise stated.
    // According to http://wiki.secondlife.com/wiki/Category:LSL_Operators
    {LSL_BOOL_AND,		ST_BOOLEAN,		"&&",	LSL_RIGHT2LEFT,				NULL, evaluateOperationToken},
// QUIRK - Seems to be some disagreement about BOOL_AND/BOOL_OR precedence.  Either they are equal, or OR is higher.
// QUIRK - No boolean short circuiting.
// QUIRK - Booleans and conditionals are executed right to left.  Or maybe not, depending on who you believe.
// LUA   - Short circiuts boolean operations, and goes left to right.
// LUA   - "and" returns its first argument if it is false, otherwise, it returns its second argument. "or" returns its first argument if it is not false, otherwise it returns its second argument.
//           Note that the above means that "and/or" can return any type.
    {LSL_BOOL_OR,		ST_BOOLEAN,		"||",	LSL_RIGHT2LEFT,				NULL, evaluateOperationToken},
    {LSL_BIT_OR,		ST_BITWISE,		"|",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},
    {LSL_BIT_XOR,		ST_BITWISE,		"^",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},
    {LSL_BIT_AND,		ST_BITWISE,		"&",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},
// QUIRK - Booleans and conditionals are executed right to left.  Or maybe not, depending on who you believe.
    {LSL_NOT_EQUAL,		ST_EQUALITY,		"!=",	LSL_RIGHT2LEFT,				NULL, evaluateOperationToken},
    {LSL_EQUAL,			ST_EQUALITY,		"==",	LSL_RIGHT2LEFT,				NULL, evaluateOperationToken},
    {LSL_GREATER_EQUAL,		ST_COMPARISON,		">=",	LSL_RIGHT2LEFT,				NULL, evaluateOperationToken},
    {LSL_LESS_EQUAL,		ST_COMPARISON,		"<=",	LSL_RIGHT2LEFT,				NULL, evaluateOperationToken},
    {LSL_GREATER_THAN,		ST_COMPARISON,		">",	LSL_RIGHT2LEFT,				NULL, evaluateOperationToken},
    {LSL_LESS_THAN,		ST_COMPARISON,		"<",	LSL_RIGHT2LEFT,				NULL, evaluateOperationToken},
// LUA   - comparisons are always false if they are different types.  Tables, userdata, and functions are compared by reference.  Strings campare in alphabetical order, depending on current locale.
// LUA   - really only has three conditionals, as it translates a ~= b to not (a == b), a > b to b < a, and a >= b to b <= a.
    {LSL_RIGHT_SHIFT,		ST_BITWISE,		">>",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},
    {LSL_LEFT_SHIFT,		ST_BITWISE,		"<<",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},
    {LSL_CONCATENATE,		ST_ADD,			"+",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},
    {LSL_ADD,			ST_ADD,			"+",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},
    {LSL_SUBTRACT,		ST_SUBTRACT,		"-",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},
    {LSL_CROSS_PRODUCT,		ST_NONE,		"%",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},
    {LSL_DOT_PRODUCT,		ST_NONE,		"*",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},
    {LSL_MULTIPLY,		ST_MULTIPLY,		"*",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},
    {LSL_MODULO,		ST_MODULO,		"%",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},
    {LSL_DIVIDE,		ST_MULTIPLY,		"/",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},
    {LSL_NEGATION,		ST_NEGATE,		"-",	LSL_RIGHT2LEFT | LSL_UNARY,		NULL, evaluateOperationToken},
    {LSL_BOOL_NOT,		ST_BOOL_NOT,		"!",	LSL_RIGHT2LEFT | LSL_UNARY,		NULL, evaluateOperationToken},
    {LSL_BIT_NOT,		ST_BIT_NOT,		"~",	LSL_RIGHT2LEFT | LSL_UNARY,		NULL, evaluateOperationToken},

// LUA precedence - (it has no bit operators, at least not until 5.2, but LuaJIT has them.)
// or
// and
// < > <= >= ~= ==
// ..
// + -
// * /
// not negate
// exponentiation (^)

    {LSL_TYPECAST_CLOSE,	ST_NONE,		")",	LSL_RIGHT2LEFT | LSL_UNARY,		NULL, evaluateNoToken},
    {LSL_TYPECAST_OPEN,		ST_NONE,		"(",	LSL_RIGHT2LEFT | LSL_UNARY,		outputParenthesisToken, evaluateOperationToken},
    {LSL_ANGLE_CLOSE,		ST_NONE,		">",	LSL_LEFT2RIGHT | LSL_CREATION,		NULL, evaluateNoToken},
    {LSL_ANGLE_OPEN,		ST_NONE,		"<",	LSL_LEFT2RIGHT | LSL_CREATION,		NULL, evaluateOperationToken},
    {LSL_BRACKET_CLOSE,		ST_NONE,		"]",	LSL_INNER2OUTER | LSL_CREATION,		NULL, evaluateNoToken},
    {LSL_BRACKET_OPEN,		ST_NONE,		"[",	LSL_INNER2OUTER | LSL_CREATION,		NULL, evaluateOperationToken},
    {LSL_PARENTHESIS_CLOSE,	ST_NONE,		")",	LSL_INNER2OUTER,			NULL, evaluateNoToken},
    {LSL_PARENTHESIS_OPEN,	ST_NONE,		"(",	LSL_INNER2OUTER,			outputParenthesisToken, eveluateParenthesisToken},
    {LSL_ASSIGNMENT_CONCATENATE,ST_CONCATENATION,	"+=",	LSL_RIGHT2LEFT | LSL_ASSIGNMENT,	NULL, evaluateOperationToken},
    {LSL_ASSIGNMENT_ADD,	ST_CONCATENATION,	"+=",	LSL_RIGHT2LEFT | LSL_ASSIGNMENT,	NULL, evaluateOperationToken},
    {LSL_ASSIGNMENT_SUBTRACT,	ST_ASSIGNMENT,		"-=",	LSL_RIGHT2LEFT | LSL_ASSIGNMENT,	NULL, evaluateOperationToken},
    {LSL_ASSIGNMENT_MULTIPLY,	ST_ASSIGNMENT,		"*=",	LSL_RIGHT2LEFT | LSL_ASSIGNMENT,	NULL, evaluateOperationToken},
    {LSL_ASSIGNMENT_MODULO,	ST_MODULO,		"%=",	LSL_RIGHT2LEFT | LSL_ASSIGNMENT,	NULL, evaluateOperationToken},
    {LSL_ASSIGNMENT_DIVIDE,	ST_ASSIGNMENT,		"/=",	LSL_RIGHT2LEFT | LSL_ASSIGNMENT,	NULL, evaluateOperationToken},
    {LSL_ASSIGNMENT_PLAIN,	ST_CONCATENATION,	"=",	LSL_RIGHT2LEFT | LSL_ASSIGNMENT,	NULL, evaluateOperationToken},
    {LSL_DOT,			ST_NONE,		".",	LSL_RIGHT2LEFT,				NULL, evaluateOperationToken},
    {LSL_DECREMENT_POST,	ST_NONE,		"--",	LSL_RIGHT2LEFT | LSL_UNARY,		NULL, evaluateOperationToken},
    {LSL_DECREMENT_PRE,		ST_NONE,		"--",	LSL_RIGHT2LEFT | LSL_UNARY,		NULL, evaluateOperationToken},
    {LSL_INCREMENT_POST,	ST_NONE,		"++",	LSL_RIGHT2LEFT | LSL_UNARY,		NULL, evaluateOperationToken},
    {LSL_INCREMENT_PRE,		ST_NONE,		"++",	LSL_RIGHT2LEFT | LSL_UNARY,		NULL, evaluateOperationToken},
    {LSL_COMMA,			ST_NONE,		",",	LSL_LEFT2RIGHT,				NULL, evaluateOperationToken},

    {LSL_EXPRESSION,		ST_NONE,	"expression",	LSL_NONE	,			NULL, NULL},

    // Types.
    {LSL_FLOAT,			ST_NONE,	"float",	LSL_NONE,				outputFloatToken, evaluateFloatToken},
    {LSL_INTEGER,		ST_NONE,	"integer",	LSL_NONE,				outputIntegerToken, evaluateIntegerToken},
    {LSL_KEY,			ST_NONE,	"key",		LSL_NONE,				NULL, NULL},
    {LSL_LIST,			ST_NONE,	"list",		LSL_NONE,				NULL, NULL},
    {LSL_ROTATION,		ST_NONE,	"rotation",	LSL_NONE,				NULL, NULL},
    {LSL_STRING,		ST_NONE,	"string",	LSL_NONE,				NULL, NULL},
    {LSL_VECTOR,		ST_NONE,	"vector",	LSL_NONE,				NULL, NULL},

    // Types names.
    {LSL_TYPE_FLOAT,		ST_NONE,	"float",	LSL_NONE,				NULL, NULL},
    {LSL_TYPE_INTEGER,		ST_NONE,	"integer",	LSL_NONE,				NULL, NULL},
    {LSL_TYPE_KEY,		ST_NONE,	"key",		LSL_NONE,				NULL, NULL},
    {LSL_TYPE_LIST,		ST_NONE,	"list",		LSL_NONE,				NULL, NULL},
    {LSL_TYPE_ROTATION,		ST_NONE,	"rotation",	LSL_NONE,				NULL, NULL},
    {LSL_TYPE_STRING,		ST_NONE,	"string",	LSL_NONE,				NULL, NULL},
    {LSL_TYPE_VECTOR,		ST_NONE,	"vector",	LSL_NONE,				NULL, NULL},

    // Then the rest of the syntax tokens.
    {LSL_FUNCTION_CALL,		ST_NONE,	"funccall",	LSL_NONE,				NULL, NULL},
    {LSL_IDENTIFIER,		ST_NONE,	"identifier",	LSL_NONE,				outputIdentifierToken, NULL},

    {LSL_LABEL,			ST_NONE,	"@",		LSL_NONE,				NULL, NULL},

    {LSL_DO,			ST_NONE,	"do",		LSL_NONE,				NULL, NULL},
    {LSL_FOR,			ST_NONE,	"for",		LSL_NONE,				NULL, NULL},
    {LSL_ELSE_IF,		ST_NONE,	"else if",	LSL_NONE,				NULL, NULL},
    {LSL_ELSE,			ST_NONE,	"else",		LSL_NONE,				NULL, NULL},
    {LSL_IF,			ST_NONE,	"if",		LSL_NONE,				NULL, NULL},
    {LSL_JUMP,			ST_NONE,	"jump",		LSL_NONE,				NULL, NULL},
    {LSL_RETURN,		ST_NONE,	"return",	LSL_NONE,				NULL, NULL},
    {LSL_STATE_CHANGE,		ST_NONE,	"state",	LSL_NONE,				NULL, NULL},
    {LSL_WHILE,			ST_NONE,	"while",	LSL_NONE,				NULL, NULL},
    {LSL_STATEMENT,		ST_NONE,	";",		LSL_NOIGNORE,				outputStatementToken, evaluateStatementToken},

    {LSL_BLOCK_CLOSE,		ST_NONE,	"}",		LSL_NONE,				NULL, NULL},
    {LSL_BLOCK_OPEN,		ST_NONE,	"{",		LSL_NONE,				outputBlockToken, NULL},
    {LSL_PARAMETER,		ST_NONE,	"parameter",	LSL_NONE,				outputIdentifierToken, NULL},
    {LSL_PARAMETER_LIST,	ST_NONE,	"plist",	LSL_NONE,				outputParameterListToken, NULL},
    {LSL_FUNCTION,		ST_NONE,	"function",	LSL_NONE,				outputFunctionToken, NULL},
    {LSL_DEFAULT,		ST_NONE,	"default",	LSL_NONE,				outputStateToken, NULL},
    {LSL_STATE,			ST_NONE,	"state",	LSL_NONE,				outputStateToken, NULL},
    {LSL_SCRIPT,		ST_NONE,	"",		LSL_NONE,				NULL,  NULL},

    {LSL_UNKNOWN,		ST_NONE,	"unknown",	LSL_NONE,				NULL, NULL},

    // A sentinal.
    {999999, ST_NONE, NULL, LSL_NONE, NULL, NULL}
};

allowedTypes allowed[] = 
{
    {OT_nothing,	"nothing",	(ST_NONE)},																// 

    {OT_bool,		"boolean",	(ST_BOOL_NOT)},																// bool				!
    {OT_integer,	"integer",	(ST_BOOL_NOT | ST_BIT_NOT | ST_NEGATE)},												// int				! - ~
    {OT_float,		"float",	(ST_BOOL_NOT)},																// float			! -
    {OT_key,		"key",		(ST_NONE)},																// 
    {OT_list,		"list",		(ST_NONE)},																// 
    {OT_rotation,	"rotation",	(ST_NONE)},																// 
    {OT_string,		"string",	(ST_NONE)},																// 
    {OT_vector,		"vector",	(ST_NONE)},																// 
    {OT_other,		"other",	(ST_NONE)},																// 

    {OT_bool,		"boolean",	(ST_BOOLEAN | ST_EQUALITY)},														// bool		bool		          == !=           =                            && ||

    {OT_integer,	"integer",	(ST_MULTIPLY | ST_ADD | ST_SUBTRACT | ST_EQUALITY | ST_COMPARISON | ST_CONCATENATION | ST_ASSIGNMENT | ST_MODULO | ST_BITWISE)},	// int		int		* / + - % == != < > <= >= = += -= *= /= %= & | ^ << >>
    {OT_float,		"float",	(ST_MULTIPLY | ST_ADD | ST_SUBTRACT | ST_EQUALITY | ST_COMPARISON | ST_CONCATENATION | ST_ASSIGNMENT)},					// int		float		cast to float float
    {OT_float,		"float",	(ST_MULTIPLY | ST_ADD | ST_SUBTRACT | ST_EQUALITY | ST_COMPARISON | ST_CONCATENATION | ST_ASSIGNMENT)},					// float	int		cast to float float
    {OT_float,		"float",	(ST_MULTIPLY | ST_ADD | ST_SUBTRACT | ST_EQUALITY | ST_COMPARISON | ST_CONCATENATION | ST_ASSIGNMENT)},					// float	float		* / + -   == != < > <= >= = += -= *= /= 

    {OT_string,		"string",	(ST_ADD | ST_EQUALITY | ST_CONCATENATION)},												// key		key		cast to string string
    {OT_string,		"string",	(ST_ADD | ST_EQUALITY | ST_CONCATENATION)},												// key		string		cast to string string
    {OT_string,		"string",	(ST_ADD | ST_EQUALITY | ST_CONCATENATION)},												// string	key		cast to string string
    {OT_string,		"string",	(ST_ADD | ST_EQUALITY | ST_CONCATENATION)},												// string	string		    +     == !=           = +=

    {OT_list,		"list",		(ST_ADD | ST_EQUALITY | ST_CONCATENATION)},												// list		list		    +     == !=           = +=
    {OT_list,		"list",		(ST_ADD | ST_COMPARISON | ST_CONCATENATION)},												// list		integer		    +           < > <= >= = +=
    {OT_list,		"list",		(ST_ADD | ST_COMPARISON | ST_CONCATENATION)},												// list		float		    +           < > <= >= = +=
    {OT_integer,	"integer",	(ST_ADD | ST_COMPARISON)},														// integer	list		    +           < > <= >=
    {OT_float,		"float",	(ST_ADD | ST_COMPARISON)},														// float	list		    +           < > <= >=
    {OT_list,		"list",		(ST_ADD | ST_CONCATENATION)},														// list		other		    +                     = +=

    {OT_vector,		"vector",	(ST_MULTIPLY | ST_ADD | ST_SUBTRACT | ST_EQUALITY | ST_CONCATENATION | ST_ASSIGNMENT | ST_MODULO)},					// vector	vector		* / + - % == !=           = += -= *= /= %=
    {OT_vector,		"vector",	(ST_MULTIPLY)},																// vector	float		* /
    {OT_vector,		"vector",	(ST_MULTIPLY)},																// vector	rotation	* /

    {OT_rotation,	"rotation",	(ST_MULTIPLY | ST_ADD | ST_SUBTRACT | ST_EQUALITY | ST_CONCATENATION | ST_ASSIGNMENT)},							// rotation	rotation	* / + -   == !=           = += -= *= /= 

    {OT_other,		"other",	(ST_NONE)},																// 
    {OT_undeclared,	"undeclared",	(ST_NONE)},																// 
    {OT_invalid,	"invalid",	(ST_NONE)}																// 
};

opType opExpr[][10] =
{
    {OT_nothing,  OT_bool,     OT_integer,  OT_float,       OT_key,       OT_list,      OT_rotation,         OT_string,       OT_vector,       OT_other},
    {OT_bool,     OT_boolBool, OT_invalid,  OT_invalid,     OT_invalid,   OT_invalid,   OT_invalid,          OT_invalid,      OT_invalid,      OT_invalid},
    {OT_integer,  OT_invalid,  OT_intInt,   OT_intFloat,    OT_invalid,   OT_intList,   OT_invalid,          OT_invalid,      OT_invalid,      OT_invalid},
    {OT_float,    OT_invalid,  OT_floatInt, OT_floatFloat,  OT_invalid,   OT_floatList, OT_invalid,          OT_invalid,      OT_invalid,      OT_invalid},
    {OT_key,      OT_invalid,  OT_invalid,  OT_invalid,     OT_keyKey,    OT_invalid,   OT_invalid,          OT_keyString,    OT_invalid,      OT_invalid},
    {OT_list,     OT_invalid,  OT_listInt,  OT_listFloat,   OT_invalid,   OT_listList,  OT_invalid,          OT_invalid,      OT_invalid,      OT_listOther},
    {OT_rotation, OT_invalid,  OT_invalid,  OT_invalid,     OT_invalid,   OT_invalid,   OT_rotationRotation, OT_invalid,      OT_invalid,      OT_invalid},
    {OT_string,   OT_invalid,  OT_invalid,  OT_invalid,     OT_stringKey, OT_invalid,   OT_invalid,          OT_stringString, OT_invalid,      OT_invalid},
    {OT_vector,   OT_invalid,  OT_invalid,  OT_vectorFloat, OT_invalid,   OT_invalid,   OT_vectorRotation,   OT_invalid,      OT_vectorVector, OT_invalid},
    {OT_other,    OT_invalid,  OT_invalid,  OT_invalid,     OT_invalid,   OT_invalid,   OT_invalid,          OT_invalid,      OT_invalid,      OT_otherOther}
};


LSL_Token **tokens = NULL;
LSL_Script constants;
int lowestToken = 999999;


static LSL_Leaf *newLeaf(LSL_Type type, LSL_Leaf *left, LSL_Leaf *right)
{
    LSL_Leaf *leaf = calloc(1, sizeof(LSL_Leaf));

    if (leaf)
    {
	leaf->left = left;
	leaf->right = right;
	leaf->token = tokens[type - lowestToken];
    }

    return leaf;
}

void burnLeaf(void *data)
{
    LSL_Leaf *leaf = data;

    if (leaf)
    {
// TODO - the problem here is that lemon wants to free these after a reduce, but we might want to keep them around.  Should ref count them or something.
//	burnLeaf(leaf->left);
//	burnLeaf(leaf->right);
	// TODO - Should free up the value to.
#ifdef LUASL_DIFF_CHECK
//	eina_strbuf_free(leaf->ignorableText);
#endif
//	free(leaf);
    }
}

static LSL_Leaf *findFunction(LuaSL_compiler *compiler, const char *name)
{
    LSL_Leaf  *func = NULL;

    if (name)
    {
	if (NULL == func)
	    func = eina_hash_find(constants.functions, name);
	if (NULL == func)
	    func = eina_hash_find(compiler->script.functions, name);
    }

    return func;
}

static LSL_Leaf *findVariable(LuaSL_compiler *compiler, const char *name)
{
    LSL_Leaf  *var = NULL;

    if (name)
    {
	LSL_Block *block = compiler->currentBlock;

	while ((block) && (NULL == var))
	{
	    if (block->function)
	    {
		LSL_Leaf *param = NULL;
		EINA_INARRAY_FOREACH((&(block->function->vars)), param)
		{
		    if ((param) && (LSL_PARAMETER == param->token->type))
		    {
//			if (name == param->value.identifierValue->name)		// Assuming they are stringshares.
			if (0 == strcmp(name, param->value.identifierValue->name))	// Not assuming they are stringeshares.
			    var = param;
		    }
		}
	    }
	    if ((NULL == var) && block->variables)
		var = eina_hash_find(block->variables, name);
	    block = block->outerBlock;
	}

	if (NULL == var)
	    var = eina_hash_find(constants.variables, name);
	if (NULL == var)
	    var = eina_hash_find(compiler->script.variables, name);
    }

    return var;
}

LSL_Leaf *checkVariable(LuaSL_compiler *compiler, LSL_Leaf *identifier)
{
    gameGlobals *game = compiler->game;
    LSL_Leaf *var = findVariable(compiler, identifier->value.stringValue);

    if (NULL == var)
	PE("NOT found %s @ line %d, column %d!", identifier->value.stringValue, identifier->line, identifier->column);
#ifdef LUASL_DEBUG
    else
	PI("Found %s!", identifier->value.stringValue);
#endif

    return var;
}

LSL_Leaf *addOperation(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right)
{
    gameGlobals *game = compiler->game;

    if (lval)
    {
	opType lType, rType;

	lval->left = left;
	lval->right = right;

	// Convert subtract to negate if needed.
	if ((NULL == left) && (LSL_SUBTRACT == lval->token->type))
	    lval->token = tokens[LSL_NEGATION - lowestToken];

	// Try to figure out what type of operation this is.
	if (NULL == left)
	    lType = OT_nothing;
	else
	{
	    if ((left->token) && (LSL_IDENTIFIER == left->token->type) && (left->value.identifierValue))
	    {
		LSL_Leaf *var = findVariable(compiler, left->value.identifierValue->name);

		if (var)
		    lType = var->basicType;
	    }
	    else
		lType = left->basicType;
	    if (OT_undeclared == lType)
	    {
		lval->basicType = OT_undeclared;
		return lval;
	    }
	    if (OT_vector < lType)
		lType = allowed[lType].result;
	}
	if (NULL == right)
	    rType = OT_nothing;
	else
	{
	    if ((right->token) && (LSL_IDENTIFIER == right->token->type) && (right->value.identifierValue))
	    {
		LSL_Leaf *var = findVariable(compiler, right->value.identifierValue->name);

		if (var)
		    rType = var->basicType;
	    }
	    else
		rType = right->basicType;
	    if (OT_undeclared == rType)
	    {
		lval->basicType = OT_undeclared;
		return lval;
	    }
	    if (OT_vector < rType)
		rType = allowed[rType].result;
	}

	// The basic lookup.
	lval->basicType = opExpr[lType][rType];
	if (OT_invalid != lval->basicType)
	{
	    // Check if it's an allowed operation.
	    if (0 == (lval->token->subType & allowed[lval->basicType].subTypes))
	    {
		lval->basicType = OT_invalid;
	    }
	    else
	    {
		// Double check the corner cases.
		switch (lval->token->subType)
		{
		    case ST_BOOLEAN :
		    case ST_COMPARISON :
		    case ST_EQUALITY :
			lval->basicType = OT_bool;
			break;
		    case ST_MULTIPLY :
			if (OT_vectorVector == lval->basicType)
			{
			    if (LSL_MULTIPLY == lval->token->type)
			    {
				lval->basicType = OT_float;
			    	lval->token = tokens[LSL_DOT_PRODUCT - lowestToken];
			    }
			    else
				lval->basicType = OT_vector;
			}
			break;
		    default :
			break;
		}
	    }
	}
	if (OT_invalid == lval->basicType)
	{
	    const char *leftType = "", *rightType = "", *leftToken = "", *rightToken = "";

	    if (left)
	    {
		if (left->token)
		    leftToken = left->token->token;
		else
		    PE("BROKEN LEFT TOKEN!!!!!!!!!!!!!!!!!!");
		leftType = allowed[left->basicType].name;
	    }
	    if (right)
	    {
		if (right->token)
		    rightToken = right->token->token;
		else
		    PE("BROKEN RIGHT TOKEN!!!!!!!!!!!!!!!!!!");
		rightType = allowed[right->basicType].name;
	    }

	    PE("Invalid operation [%s(%s) %s %s(%s)] @ line %d, column %d!", leftType, leftToken, lval->token->token, rightType, rightToken, lval->line, lval->column);
	}
    }

    return lval;
}

LSL_Leaf *addParameter(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identifier)
{
    LSL_Identifier *result = calloc(1, sizeof(LSL_Identifier));

    if ( (identifier) && (result))
    {
	result->name = identifier->value.stringValue;
	result->value.token = tokens[LSL_UNKNOWN - lowestToken];
	identifier->value.identifierValue = result;
	identifier->token = tokens[LSL_PARAMETER - lowestToken];
	identifier->left = type;
	if (type)
	{
	    identifier->basicType = type->basicType;
	    result->value.basicType = type->basicType;
	    result->value.token = type->token;	// This is the LSL_TYPE_* token instead of the LSL_* token.  Not sure if that's a problem.
	}
    }
    return identifier;
}

LSL_Leaf *collectParameters(LuaSL_compiler *compiler, LSL_Leaf *list, LSL_Leaf *comma, LSL_Leaf *newParam)
{
    LSL_Function *func = NULL;

    if (NULL == list)
	list = newLeaf(LSL_FUNCTION, NULL, NULL);

    if (list)
    {
	func = list->value.functionValue;
	if (NULL == func)
	{
	    func = calloc(1, sizeof(LSL_Function));
	    if (func)
	    {
		list->value.functionValue = func;
		eina_inarray_setup(&(func->vars), sizeof(LSL_Leaf), 3);
	    }
	}

	if (func)
	{
	    if (newParam)
	    {
#ifdef LUASL_DIFF_CHECK
		// Stash the comma for diff later.
		if (comma)
		    eina_inarray_append(&(func->vars), comma);
#endif
		eina_inarray_append(&(func->vars), newParam);
		// At this point, pointers to newParams are not pointing to the one in func->vars, AND newParam is no longer needed.
	    }
	}
    }
    return list;
}

LSL_Leaf *addFunction(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *open, LSL_Leaf *params, LSL_Leaf *close)
{
    LSL_Function *func = NULL;

    if (params)
    {
	func = params->value.functionValue;
	// At this point, params is no longer needed, except if we are doing diff.
	// open and close are not needed either if we are not doing diff.
	if (func)
	{
	    if (identifier)
	    {
		func->name = identifier->value.stringValue;
		identifier->token = tokens[LSL_FUNCTION - lowestToken];
		identifier->value.functionValue = func;
		func->type = type;
		if (type)
		    identifier->basicType = type->basicType;
		else
		    identifier->basicType = OT_nothing;
		eina_hash_add(compiler->script.functions, func->name, identifier);
#ifdef LUASL_DIFF_CHECK
		func->params = addParenthesis(open, params, LSL_PARAMETER_LIST, close);
#endif
	    }
	    if (compiler->currentBlock)
		compiler->currentBlock->function = func;
	}
    }

    return identifier;
}

LSL_Leaf *addFunctionBody(LuaSL_compiler *compiler, LSL_Leaf *function, LSL_Leaf *block)
{
    if (function)
	function->value.functionValue->block = block;

    return function;
}

LSL_Leaf *addFunctionCall(LuaSL_compiler *compiler, LSL_Leaf *identifier, LSL_Leaf *open, LSL_Leaf *params, LSL_Leaf *close)
{
    LSL_Leaf *func = findFunction(compiler, identifier->value.stringValue);
    LSL_FunctionCall *call = calloc(1, sizeof(LSL_FunctionCall));

    identifier->token = tokens[LSL_UNKNOWN - lowestToken];

    if (func)
    {
	if (call)
	{
	    call->function = func->value.functionValue;
	    eina_clist_element_init(&(call->functionCall));
	    call->call = identifier;
	}
	identifier->value.functionCallValue = call;
	// TODO - Put the params in call.
	identifier->token = tokens[LSL_FUNCTION_CALL - lowestToken];
	identifier->basicType = func->basicType;
    }
    else
    {
	// It may be declared later, so store it and check later.
	if (call)
	{
	    eina_clist_add_tail(&(compiler->danglingCalls), &(call->functionCall));
	    call->call = identifier;
	}
	identifier->basicType = OT_undeclared;
	compiler->undeclared = TRUE;
    }

    return identifier;
}

LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Type type, LSL_Leaf *rval)
{
    LSL_Parenthesis *parens = calloc(1, sizeof(LSL_Parenthesis));

    if (parens)
    {
	parens->contents = expr;
	parens->type = type;
#ifdef LUASL_DIFF_CHECK
	parens->rightIgnorableText = rval->ignorableText;
	// Actualy, at this point, rval is no longer needed.
//	rval->ignorableText = eina_strbuf_new();
#endif
	if (lval)
	{
	    lval->value.parenthesis = parens;
	    if (expr)
		lval->basicType = expr->basicType;
	}
    }
    return lval;
}

LSL_Leaf *addState(LuaSL_compiler *compiler, LSL_Leaf *identifier, LSL_Leaf *block)
{
    LSL_State *result = calloc(1, sizeof(LSL_State));

    if ((identifier) && (result))
    {
	result->name = identifier->value.stringValue;
	result->block = block;
	identifier->value.stateValue = result;
	eina_hash_add(compiler->script.states, result->name, identifier);
    }

    return identifier;
}

LSL_Leaf *addStatement(LSL_Leaf *lval, LSL_Type type, LSL_Leaf *expr)
{
    LSL_Statement *stat = calloc(1, sizeof(LSL_Statement));

    if (stat)
    {
	stat->type = type;
	stat->expressions = expr;
	eina_clist_element_init(&(stat->statement));
	if (lval)
	    lval->value.statementValue = stat;
    }

    return lval;
}

LSL_Leaf *collectStatements(LuaSL_compiler *compiler, LSL_Leaf *list, LSL_Leaf *statement)
{
    if (NULL == list)
    {
	list = newLeaf(LSL_BLOCK_OPEN, NULL, NULL);
	if (list)
	{
	    list->value.blockValue = compiler->currentBlock;	// Maybe NULL.
	}
    }

    if (list)
    {
	if (statement)
	{
	    if (list->value.blockValue)
	    {
		eina_clist_add_tail(&(list->value.blockValue->statements), &(statement->value.statementValue->statement));
	    }
	}
    }

    return list;
}

LSL_Leaf *addTypecast(LSL_Leaf *lval, LSL_Leaf *type, LSL_Leaf *rval, LSL_Leaf *expr)
{
    addParenthesis(lval, expr, LSL_TYPECAST_OPEN, rval);
    if (lval)
    {
	if (type)
	    lval->basicType = type->basicType;
	// Actualy, at this point, type is no longer needed.
	lval->token = tokens[LSL_TYPECAST_OPEN - lowestToken];
    }
//    if (rval)
//	rval->token = tokens[LSL_TYPECAST_CLOSE - lowestToken];

    return lval;
}

LSL_Leaf *addVariable(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *assignment, LSL_Leaf *expr)
{
    LSL_Identifier *result = calloc(1, sizeof(LSL_Identifier));

    if ( (identifier) && (result))
    {
	result->name = identifier->value.stringValue;
	result->value.token = tokens[LSL_UNKNOWN - lowestToken];
	identifier->value.identifierValue = result;
	identifier->left = type;
	identifier->right = assignment;
	if (assignment)
	    assignment->right = expr;
	if (type)
	{
	    identifier->basicType = type->basicType;
	    result->value.basicType = type->basicType;
	    result->value.token = type->token;	// This is the LSL_TYPE_* token instead of the LSL_* token.  Not sure if that's a problem.
	}
	if (compiler->currentBlock)
	    eina_hash_add(compiler->currentBlock->variables, result->name, identifier);
	else
	    eina_hash_add(compiler->script.variables, result->name, identifier);
    }

    return identifier;
}

void beginBlock(LuaSL_compiler *compiler, LSL_Leaf *block)
{
    LSL_Block *blok = calloc(1, sizeof(LSL_Block));

    if (blok)
    {
	eina_clist_init(&(blok->statements));
	blok->variables = eina_hash_stringshared_new(burnLeaf);
	block->value.blockValue = blok;
	blok->outerBlock = compiler->currentBlock;
	compiler->currentBlock = blok;
    }
}

void endBlock(LuaSL_compiler *compiler, LSL_Leaf *block)
{
    compiler->currentBlock = compiler->currentBlock->outerBlock;
}

static LSL_Leaf *evaluateLeaf(LSL_Leaf *leaf, LSL_Leaf *left, LSL_Leaf *right)
{
    LSL_Leaf *result = NULL;

    if (leaf)
    {
	LSL_Leaf *lresult = NULL;
	LSL_Leaf *rresult = NULL;

	if (LSL_RIGHT2LEFT & leaf->token->flags)
	{
	    rresult = evaluateLeaf(leaf->right, left, right);
	    if (!(LSL_UNARY & leaf->token->flags))
		lresult = evaluateLeaf(leaf->left, left, right);
	}
	else // Assume left to right.
	{
	    lresult = evaluateLeaf(leaf->left, left, right);
	    if (!(LSL_UNARY & leaf->token->flags))
		rresult = evaluateLeaf(leaf->right, left, right);
	}

	if (leaf->token->evaluate)
	    result = leaf->token->evaluate(leaf, lresult, rresult);
	else
	{
	    result = newLeaf(LSL_UNKNOWN, NULL, NULL);
	    if (rresult && result)
		memcpy(result, rresult, sizeof(LSL_Leaf));
	}

	if (lresult)
	    free(lresult);
	if (rresult)
	    free(rresult);
    }

    return result;
}

static LSL_Leaf *evaluateFloatToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right)
{
    LSL_Leaf *result = newLeaf(LSL_FLOAT, NULL, NULL);

    if (content && result)
    {
#ifdef LUASL_DEBUG
	printf(" <%g> ", content->value.floatValue);
#endif
	memcpy(result, content, sizeof(LSL_Leaf));
	result->basicType = OT_float;
    }
    return result;
}

static LSL_Leaf *evaluateIntegerToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right)
{
    LSL_Leaf *result = newLeaf(LSL_INTEGER, NULL, NULL);

    if (content && result)
    {
#ifdef LUASL_DEBUG
	printf(" <%d> ", content->value.integerValue);
#endif
	memcpy(result, content, sizeof(LSL_Leaf));
	result->basicType = OT_integer;
    }
    return result;
}

static LSL_Leaf *evaluateNoToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right)
{
    // Do nothing, that's the point.

    return content;
}

/*  Typecasting

LSL is statically typed, so stored values are not converted, only the values used in expressions are.
Lua is dynamically typed, so stored values are changed (sometimes I think).

LSL implicitly typecasts - There is a shitload of QUIRKs about this.  Apparently some don't work anyway.
			integer -> float  (Says in lslwiki that precision is never lost, which is bullshit, since they are both 32 bit.  Would be true if the float is 64 bit.  Lua suggest to use 64 bit floats to emulate 32 bit integers.)
			string  -> key
			    Some functions need help with this or the other way around.
			string  -> vector (Maybe, should test that.)
			vector  -> string (Maybe, should test that.)
	Also happens when getting stuff from lists.

Explicit type casting -
			string -> integer
			    Leading spaces are ignored, as are any characters after the run of digits.
			    All other strings convert to 0.
			    Which means "" and " " convert to 0.
			    Strings in hexadecimal format will work.
			keys <-> string
			    No other typecasting can be done with keys.
			float -> string
			    You get a bunch of trailing 0s.

QUIRK - I have seen cases where a double explicit typecast was needed in SL, but was considered to be invalid syntax in OS.

Any binary operation involving a float and an integer implicitly casts the integer to float.

A boolean operation deals with TRUE (1) and FALSE (0).  Any non zero value is a TRUE (generally sigh).
On the other hand, in Lua, only false and nil are false, everything else is true.
Bitwise operations only apply to integers.  The shifts are arithmatic, not logical.  Right shifted bits are dropped, left shifts the sign bit.

integer  = integer0   % integer1;  // Apparently only applies to integers, but works fine on floats in OS.
string   = string0    + string1;   // Concatenation.
list     = list0      + list1;     // Concatenation.   Also works if either is not a list, it's promoted to a list first.
list     = (list=[])  + list + ["new_item"];  // Voodoo needed for old LSL, works in Mono but not needed, does not work in OS.  Works for strings to.
bool     = list == != int          // Only compares the lengths, probably applies to the other conditionals to.
vector   = vector0    + vector1;   // Add elements together.
vector   = vector0    - vector1;   // Subtract elements of vector1 from elements of vector0.
float    = vector0    * vector1;   // A dot product of the vectors.
vector   = vector0    % vector1;   // A cross product of the vectors.
vector   = vector     * float;     // Scale the vector, works the other way around I think.  Works for integer to, but it will end up being cast to float.
vector   = vector     / float;     // Scale the vector, works the other way around I think.  Works for integer to, but it will end up being cast to float.
vector   = vector     * rotation;  // Rotate the vector by the rotation.  Other way around wont compile.
vector   = vector     / rotation;  // Rotate the vector by the rotation, in the opposite direction.  Other way around wont compile.
rotation = llGetRot() * rotation;  // Rotate an object around the global axis.
rotation = rotation   * llGetLocalRot();  // Rotate an object around the local axis.
rotation = rotation0  * rotation1; // Add two rotations, so the result is as if you applied each rotation one after the other.
				   // Division rotates in the opposite direction.
rotation = rotation0  + rotation1; // Similar to vector, but it's a meaningless thing as far as rotations go.
rotation = rotation0  - rotation1; // Similar to vector, but it's a meaningless thing as far as rotations go.

A boolean     operator results in a boolean value.	(any types)
A comparison  operator results in a boolean value.	(any types)
A bitwise     operator results in an integer value.	(intInt or int)
A dot product operator results in a float value.  	(vector * vector)
A vectorFloat          results in a vector value.

*/

static LSL_Leaf *evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right)
{
    LSL_Leaf *result = newLeaf(LSL_UNKNOWN, NULL, NULL);

    if (content && result)
    {
#ifdef LUASL_DEBUG
	printf(" [%s] ", content->token->token);
#endif

	memcpy(result, content, sizeof(LSL_Leaf));

	// Figure out the type of the operation.
	if (OT_vector < result->basicType)
	    result->basicType = allowed[result->basicType].result;

	switch (result->basicType)
	{
	    case OT_float   :
	    {
		float fleft  = left->value.floatValue;
		float fright = right->value.floatValue;

		// Do the casting.
		if (OT_floatInt == content->basicType)
		    fright = right->value.integerValue;
		if (OT_intFloat == content->basicType)
		    fleft = left->value.integerValue;
		switch (result->token->type)
		{
		    case LSL_COMMA			:
		    case LSL_INCREMENT_PRE		:
		    case LSL_INCREMENT_POST		:
		    case LSL_DECREMENT_PRE		:
		    case LSL_DECREMENT_POST		:
		    case LSL_ASSIGNMENT_PLAIN		:
		    case LSL_ASSIGNMENT_DIVIDE		:
		    case LSL_ASSIGNMENT_MULTIPLY	:
		    case LSL_ASSIGNMENT_SUBTRACT	:
		    case LSL_ASSIGNMENT_ADD		:
		    case LSL_BRACKET_OPEN		:
		    case LSL_BRACKET_CLOSE		:
		    case LSL_ANGLE_OPEN			:
		    case LSL_ANGLE_CLOSE		:
		    case LSL_TYPECAST_OPEN		:
		    case LSL_TYPECAST_CLOSE		:
		    case LSL_DOT_PRODUCT		:
			break;
		    case LSL_NEGATION		:  result->value.floatValue = 0 - fright;	break;
		    case LSL_DIVIDE		:  result->value.floatValue = fleft /  fright;	break;
		    case LSL_MULTIPLY		:  result->value.floatValue = fleft *  fright;	break;
		    case LSL_SUBTRACT		:  result->value.floatValue = fleft -  fright;	break;
		    case LSL_ADD		:  result->value.floatValue = fleft +  fright;	break;
		    case LSL_LESS_THAN		:  result->value.floatValue = fleft <  fright;	break;
		    case LSL_GREATER_THAN	:  result->value.floatValue = fleft >  fright;	break;
		    case LSL_LESS_EQUAL		:  result->value.floatValue = fleft <= fright;	break;
		    case LSL_GREATER_EQUAL	:  result->value.floatValue = fleft >= fright;	break;
		    case LSL_EQUAL		:  result->value.floatValue = fleft == fright;	break;
		    case LSL_NOT_EQUAL		:  result->value.floatValue = fleft != fright;	break;
		}
#ifdef LUASL_DEBUG
		printf(" (=%g) ", result->value.floatValue);
#endif
		break;
	    }

	    case OT_integer :
	    {
		switch (result->token->type)
		{
		    case LSL_COMMA			:
		    case LSL_INCREMENT_PRE		:
		    case LSL_INCREMENT_POST		:
		    case LSL_DECREMENT_PRE		:
		    case LSL_DECREMENT_POST		:
		    case LSL_DOT			:
		    case LSL_ASSIGNMENT_PLAIN		:
		    case LSL_ASSIGNMENT_DIVIDE		:
		    case LSL_ASSIGNMENT_MODULO		:
		    case LSL_ASSIGNMENT_MULTIPLY	:
		    case LSL_ASSIGNMENT_SUBTRACT	:
		    case LSL_ASSIGNMENT_ADD		:
		    case LSL_BRACKET_OPEN		:
		    case LSL_BRACKET_CLOSE		:
		    case LSL_ANGLE_OPEN			:
		    case LSL_ANGLE_CLOSE		:
		    case LSL_TYPECAST_OPEN		:
		    case LSL_TYPECAST_CLOSE		:
			break;
		    case LSL_BIT_NOT		:  result->value.integerValue = ~ right->value.integerValue;				break;
		    case LSL_BOOL_NOT		:  result->value.integerValue = ! right->value.integerValue;				break;
		    case LSL_NEGATION		:  result->value.integerValue = 0 - right->value.integerValue;				break;
		    case LSL_DIVIDE		:  result->value.integerValue = left->value.integerValue /  right->value.integerValue;	break;
		    case LSL_MODULO		:  result->value.integerValue = left->value.integerValue %  right->value.integerValue;	break;
		    case LSL_MULTIPLY		:  result->value.integerValue = left->value.integerValue *  right->value.integerValue;	break;
		    case LSL_SUBTRACT		:  result->value.integerValue = left->value.integerValue -  right->value.integerValue;	break;
		    case LSL_ADD		:  result->value.integerValue = left->value.integerValue +  right->value.integerValue;	break;
		    case LSL_LEFT_SHIFT		:  result->value.integerValue = left->value.integerValue << right->value.integerValue;	break;
		    case LSL_RIGHT_SHIFT	:  result->value.integerValue = left->value.integerValue >> right->value.integerValue;	break;
		    case LSL_LESS_THAN		:  result->value.integerValue = left->value.integerValue <  right->value.integerValue;	break;
		    case LSL_GREATER_THAN	:  result->value.integerValue = left->value.integerValue >  right->value.integerValue;	break;
		    case LSL_LESS_EQUAL		:  result->value.integerValue = left->value.integerValue <= right->value.integerValue;	break;
		    case LSL_GREATER_EQUAL	:  result->value.integerValue = left->value.integerValue >= right->value.integerValue;	break;
		    case LSL_EQUAL		:  result->value.integerValue = left->value.integerValue == right->value.integerValue;	break;
		    case LSL_NOT_EQUAL		:  result->value.integerValue = left->value.integerValue != right->value.integerValue;	break;
		    case LSL_BIT_AND		:  result->value.integerValue = left->value.integerValue &  right->value.integerValue;	break;
		    case LSL_BIT_XOR		:  result->value.integerValue = left->value.integerValue ^  right->value.integerValue;	break;
		    case LSL_BIT_OR		:  result->value.integerValue = left->value.integerValue |  right->value.integerValue;	break;
		    case LSL_BOOL_OR		:  result->value.integerValue = left->value.integerValue || right->value.integerValue;	break;
		    case LSL_BOOL_AND		:  result->value.integerValue = left->value.integerValue && right->value.integerValue;	break;
		}
#ifdef LUASL_DEBUG
		printf(" (=%d) ", result->value.integerValue);
#endif
		break;
	    }

	    default :
		break;
	}
    }
    return result;
}

static LSL_Leaf *eveluateParenthesisToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right)
{
    LSL_Leaf *result = NULL;

    if (content)
    {
	if (LSL_PARAMETER_LIST != content->value.parenthesis->type)
	    result = evaluateLeaf(content->value.parenthesis->contents, left, right);
    }
    return result;
}


static LSL_Leaf *evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right)
{
    LSL_Leaf *result = NULL;

    if (content)
    {
	result = evaluateLeaf(content->value.statementValue->expressions, left, right);
	if (result)
	{
	    switch (result->basicType)
	    {
		case OT_float   :  printf("\nResult is the float %g.\n", result->value.floatValue);  break;
		case OT_integer :  printf("\nResult is the integer %d.\n", result->value.integerValue);  break;
		default         :  printf("\nResult of an unknown type [%d] %d!\n", result->basicType, result->value.integerValue);  break;
	    }
	    free(result);
	    result = NULL;
	}
	if (left)
	    left->value.integerValue = 0;
	if (right)
	    right->value.integerValue = 0;
    }
    return result;
}

static void outputLeaf(FILE *file, outputMode mode, LSL_Leaf *leaf)
{
    if (leaf)
    {
	outputLeaf(file, mode, leaf->left);
#ifdef LUASL_DIFF_CHECK
	if ((!(LSL_NOIGNORE & leaf->token->flags)) && (leaf->ignorableText))
	    fwrite(eina_strbuf_string_get(leaf->ignorableText), 1, eina_strbuf_length_get(leaf->ignorableText), file);
#endif
	if (leaf->token->output)
	    leaf->token->output(file, mode, leaf);
	else
	    fprintf(file, "%s", leaf->token->token);
	outputLeaf(file, mode, leaf->right);
    }
}

static void outputBlockToken(FILE *file, outputMode mode, LSL_Leaf *content)
{
    if (content)
    {
	fprintf(file, "\n{\n");
	if (content->value.blockValue)
	{
	    LSL_Statement *statement = NULL;

	    EINA_CLIST_FOR_EACH_ENTRY(statement, &(content->value.blockValue->statements), LSL_Statement, statement)
	    {
		outputLeaf(file, mode, statement->expressions);
		fprintf(file, ";\n");
	    }
	}
	fprintf(file, "}\n");
    }
}

static void outputFloatToken(FILE *file, outputMode mode, LSL_Leaf *content)
{
    if (content)
	fprintf(file, "%g", content->value.floatValue);
}

static void outputFunctionToken(FILE *file, outputMode mode, LSL_Leaf *content)
{
    if (content)
    {
	LSL_Function *func = content->value.functionValue;
	LSL_Leaf *param = NULL;
	int first = TRUE;

	outputLeaf(file, mode, func->type);
// TODO - should print comma and parenthesis ignorables.
	fprintf(file, "%s(", func->name);
	EINA_INARRAY_FOREACH((&(func->vars)), param)
	{
	    if (!first)
		fprintf(file, ", ");
	    outputLeaf(file, mode, param);
	    first = FALSE;
	}
	fprintf(file, ")");
	outputLeaf(file, mode, func->block);
#ifndef LUASL_DIFF_CHECK
	fprintf(file, "\n");
#endif
    }
}

static void outputIntegerToken(FILE *file, outputMode mode, LSL_Leaf *content)
{
    if (content)
	fprintf(file, "%d", content->value.integerValue);
}

static void outputIdentifierToken(FILE *file, outputMode mode, LSL_Leaf *content)
{
    if (content)
#ifdef LUASL_DIFF_CHECK
	fprintf(file, "%s", content->value.identifierValue->name);
#else
	fprintf(file, " %s", content->value.identifierValue->name);
#endif
}

static void outputParameterListToken(FILE *file, outputMode mode, LSL_Leaf *content)
{
    if (content)
	outputLeaf(file, mode, content->value.listValue);
}

static void outputParenthesisToken(FILE *file, outputMode mode, LSL_Leaf *content)
{
    if (content)
    {
	fprintf(file, "(");
	if (LSL_TYPECAST_OPEN == content->value.parenthesis->type)
	    fprintf(file, "%s", allowed[content->basicType].name);	// TODO - We are missing the type ignorable text here.
	else
	    outputLeaf(file, mode, content->value.parenthesis->contents);
#ifdef LUASL_DIFF_CHECK
	fprintf(file, "%s)", eina_strbuf_string_get(content->value.parenthesis->rightIgnorableText));
#else
	fprintf(file, ")");
#endif
	if (LSL_TYPECAST_OPEN == content->value.parenthesis->type)
	    outputLeaf(file, mode, content->value.parenthesis->contents);
    }
}

static void outputStateToken(FILE *file, outputMode mode, LSL_Leaf *content)
{
    if (content)
    {
	LSL_State *state = content->value.stateValue;

	if (state)
	{
	    if (0 == strcmp(state->name, "default"))
		fprintf(file, "%s\n", state->name);
	    else
		fprintf(file, "state %s\n", state->name);
	    outputLeaf(file, mode, state->block);
	    fprintf(file, "\n");
	}
    }
}

static void outputStatementToken(FILE *file, outputMode mode, LSL_Leaf *content)
{
    if (content)
    {
	outputLeaf(file, mode, content->value.statementValue->expressions);
#ifdef LUASL_DIFF_CHECK
	if (content->ignorableText)
	    fwrite(eina_strbuf_string_get(content->ignorableText), 1, eina_strbuf_length_get(content->ignorableText), file);
#endif
	fprintf(file, "%s", content->token->token);
#ifndef LUASL_DIFF_CHECK
	fprintf(file, "\n");
#endif
    }
}

static void doneParsing(LuaSL_compiler *compiler)
{
    gameGlobals *game = compiler->game;

    if (compiler->ast)
    {
	FILE *out;
	char buffer[PATH_MAX];
	char outName[PATH_MAX];
	char luaName[PATH_MAX];

	outputLeaf(stdout, OM_LSL, compiler->ast);
	printf("\n");
	evaluateLeaf(compiler->ast, NULL, NULL);
	printf("\n");

	strcpy(outName, compiler->fileName);
	strcat(outName, "2");
	strcpy(luaName, compiler->fileName);
	strcat(luaName, ".lua");
	out = fopen(outName, "w");
	if (out)
	{
#ifdef LUASL_DIFF_CHECK
	    int count;
#endif
	    outputLeaf(out, OM_LSL, compiler->ast);
	    fclose(out);
	    sprintf(buffer, "diff %s %s", compiler->fileName, outName);
#ifdef LUASL_DIFF_CHECK
	    count = system(buffer);
	    PI("Return value of %s is %d", buffer, count);
	    if (0 != count)
	        PE("%s says they are different!", buffer);
#endif
	}
	else
	    PC("Unable to open file %s for writing!", outName);
	out = fopen(luaName, "w");
	if (out)
	{
	    outputLeaf(out, OM_LUA, compiler->ast);
	    fclose(out);
	}
	else
	    PC("Unable to open file %s for writing!", luaName);
    }
}

Eina_Bool compilerSetup(gameGlobals *game)
{
    int i;

    // Figure out what numbers lemon gave to our tokens.
    for (i = 0; LSL_Tokens[i].token != NULL; i++)
    {
	if (lowestToken > LSL_Tokens[i].type)
	    lowestToken = LSL_Tokens[i].type;
    }
    tokens = calloc(i + 1, sizeof(LSL_Token *));
    if (tokens)
    {
	char buf[PATH_MAX];

	// Sort the token table.
	for (i = 0; LSL_Tokens[i].token != NULL; i++)
	{
	    int j = LSL_Tokens[i].type - lowestToken;

	    tokens[j] = &(LSL_Tokens[i]);
	}

	// Compile the constants.
	snprintf(buf, sizeof(buf), "%s/src/constants.lsl", PACKAGE_DATA_DIR);
	compileLSL(game, buf, TRUE);

	return EINA_TRUE;
    }
    else
	PC("No memory for tokens!");

    return EINA_FALSE;
}

Eina_Bool compileLSL(gameGlobals *game, char *script, boolean doConstants)
{
    Eina_Bool result = EINA_FALSE;
    LuaSL_compiler compiler;
    void *pParser = ParseAlloc(malloc);
    int yv;

// Parse the  LSL script, validating it and reporting errors.
//   Just pass all constants and function names through to Lua, assume they are globals there.

    memset(&compiler, 0, sizeof(LuaSL_compiler));
    compiler.game = game;
    compiler.script.functions = eina_hash_stringshared_new(burnLeaf);
    compiler.script.states = eina_hash_stringshared_new(burnLeaf);
    compiler.script.variables = eina_hash_stringshared_new(burnLeaf);
    eina_clist_init(&(compiler.danglingCalls));
#ifdef LUASL_DIFF_CHECK
    compiler.ignorableText = eina_strbuf_new();
#endif

    strncpy(compiler.fileName, script, PATH_MAX - 1);
    compiler.fileName[PATH_MAX - 1] = '\0';
    compiler.file = fopen(compiler.fileName, "r");
    if (NULL == compiler.file)
    {
	PE("Error opening file %s.", compiler.fileName);
	return FALSE;
    }
    PI("Opened %s.", compiler.fileName);
    compiler.ast = NULL;
    compiler.lval = newLeaf(LSL_UNKNOWN, NULL, NULL);
    // Text editors usually start counting at 1, even programmers editors.
    compiler.column = 1;
    compiler.line = 1;

    if (yylex_init_extra(&compiler, &(compiler.scanner)))
	return result;
#ifdef LUASL_DEBUG
    yyset_debug(1, compiler.scanner);
    ParseTrace(stdout, "LSL_lemon ");
#endif
    yyset_in(compiler.file, compiler.scanner);
    // on EOF yylex will return 0
    while((yv = yylex(compiler.lval, compiler.scanner)) != 0)
    {
	Parse(pParser, yv, compiler.lval, &compiler);
	if (LSL_SCRIPT == yv)
	    break;
	compiler.lval = newLeaf(LSL_UNKNOWN, NULL, NULL);
    }

    yylex_destroy(compiler.scanner);
    Parse (pParser, 0, compiler.lval, &compiler);
    ParseFree(pParser, free);

    if (compiler.undeclared)
    {
	PI("A second pass will be needed to check if functions where declared after they where used.  To avoid this second pass, don't do that.");
	if (eina_clist_count(&(compiler.danglingCalls)))
	{
	    LSL_FunctionCall *call = NULL;

	    EINA_CLIST_FOR_EACH_ENTRY(call, &(compiler.danglingCalls), LSL_FunctionCall, functionCall)
	    {
		LSL_Leaf *func = findFunction(&(compiler), call->call->value.stringValue);

		if (func)
		{
		    call->function = func->value.functionValue;
		    call->call->value.functionCallValue = call;
		    call->call->token = tokens[LSL_FUNCTION_CALL - lowestToken];
		    call->call->basicType = func->basicType;
		}
		else
		    PE("Undeclared function %s called @ line %d, column %d!", call->call->value.stringValue, call->call->line, call->call->column);
	    }
	}
// TODO - Run through the expressions, cleaning up the function calls.
	PI("Second pass completed.");
    }

    if (doConstants)
    {
	memcpy(&constants, &(compiler.script), sizeof(LSL_Script));
    }
    else
    {
	doneParsing(&compiler);

// Take the result of the parse, and convert it into Lua source.
//   Each LSL script becomes a Lua state.
//   LSL states are handled as Lua tables, with each LSL state function being a table function in a common metatable.
//   LL and OS functions are likely to be C functions. 

// Compile the Lua source by the Lua compiler.

    }

    if (NULL != compiler.file)
    {
	fclose(compiler.file);
	compiler.file = NULL;
    }

    if (!doConstants)
	burnLeaf(compiler.ast);

    return result;
}


// Code for running it stand alone, and with files input on the command line.
#if 0
static int nextFile(LuaSL_yyparseParam *param)
{
    if (NULL != param->file)
    {
	fclose(param->file);
	param->file = NULL;
    }
    if (--(param->argc) > 0 && *++(param->argv) != '\0')
    {
	strncpy(param->fileName, *(param->argv), PATH_MAX - 1);
	param->fileName[PATH_MAX - 1] = '\0';
	param->file = fopen(param->fileName, "r");
	if (NULL == param->file)
	{
	    PE(stderr, "Error opening file %s.", param->fileName);
	    return FALSE;
	}
	PE("Opened %s.", param->fileName);
	burnLeaf(param->ast);
	param->ast = NULL;
	param->lval = newLeaf(LSL_UNKNOWN, NULL, NULL);
	// Text editors usually start counting at 1, even programmers editors.
	param->column = 1;
	param->line = 1;
	return TRUE;
    }
/*
	if ('\0' == fileName[0])
	{
//strcpy(fileName, "test.lsl");

	    count = read(STDIN_FILENO, fileName, PATH_MAX - 1);
	    if (0 > count)
	    {
		printf("Error in stdin!\n");
		return 1;
	    }
	    else if (0 == count)
	    {
		printf("No bytes in stdin!\n");
		return 1;
	    }
	    else
	    {
		fileName[count] = '\0';
		printf("Filename %s in stdin.\n", fileName);
	    }

	}
*/

    return FALSE;
}

char *test[] = {"test2.lsl", "test2.lsl"};

int main(int argc, char **argv)
{
//    char *programName = argv[0];
    int i;

    // Figure out what numbers yacc gave to our tokens.
    for (i = 0; LSL_Tokens[i].token != NULL; i++)
    {
	if (lowestToken > LSL_Tokens[i].type)
	    lowestToken = LSL_Tokens[i].type;
    }
    tokens = calloc(i + 1, sizeof(LSL_Token *));
    if (tokens)
    {
	LuaSL_yyparseParam param;

	// Sort the token table.
	for (i = 0; LSL_Tokens[i].token != NULL; i++)
	{
	    int j = LSL_Tokens[i].type - lowestToken;

	    tokens[j] = &(LSL_Tokens[i]);
	}

	// First time setup.
	if (1 == argc)
	{
	    // Fake a test file if there is none.  Mostly for ddd.
	    argc++;
	    argv = test;
	}
	memset(&param, 0, sizeof(param));
	param.argc = argc;
	param.argv = argv;

	// Loop through the files.
	while (nextFile(&param))
	{
	    void *pParser = ParseAlloc(malloc);
	    int yv;

	    if (yylex_init_extra(&param, &(param.scanner)))
		return 1;
#ifdef LUASL_DEBUG
	    yyset_debug(1, param.scanner);
	    ParseTrace(stdout, "LSL_lemon ");
#endif
	    yyset_in(param.file, param.scanner);
	    // on EOF yylex will return 0
	    while((yv = yylex(param.lval, param.scanner)) != 0)
	    {
		Parse(pParser, yv, param.lval, &param);
		if (LSL_SCRIPT == yv)
		    break;
		param.lval = newLeaf(LSL_UNKNOWN, NULL, NULL);
	    }

	    yylex_destroy(param.scanner);
	    Parse (pParser, 0, param.lval, &param);
	    ParseFree(pParser, free);
	    doneParsing(&param);
	}
    }
    else
    {
	fprintf(stderr, "No memory for tokens!");
	return 1;
    }

    return 0;
}
#endif