-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.js
1167 lines (1111 loc) · 31.4 KB
/
expression.js
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
var sys = require('sys'),
definitions= require('./definitions/dev/dictionary'),
prio= require('./definitions/priorities'),
tokenTypes= require('./definitions/token_types'),
Token=require('./token'),
util = require('util'),
Debug = require('./debug/debug'),
nodeNames = require('./definitions/node_names'),
ExpressionError=require('./errors/expression_error');
var LPAREN = 1 << 1;
var RPAREN = 1 << 2;
var COMMA = 1 << 3;
var SIGNAL = 1 << 4;
var CALL = 1 << 5;
var NOT = 1 << 7;
var BOOLEAN = 1 << 8;
var TEXT = 1 << 9;
var ARITHMETICOP = 1 << 10;
var LOGICOP = 1 << 11;
var NUMBER = 1 << 12;
var BITWISE_NOT = 1 << 13;
var VAR = 1 << 14;
var FACT = 1 << 15;
var MATHFUNC_CALL = 1<< 16;
var PRIMARY = (NUMBER | TEXT | BOOLEAN);
var Expression = function(lng, definition, nodeType_, isArgument, outsidePos, isDebug){
this.lng=lng||"dev";
this.nodeTypes = definition;
this.isArgument = isArgument || false;
this.isDebug = isDebug || false;
this.outsidePos = outsidePos||0;
this.nodeType_=nodeType_;
try{
definitions = require('./definitions/'+lng+'/dictionary');
}
catch(e){
definitions = require('./definitions/dev/dictionary');
}
};
if (!Array.indexOf) {
Array.prototype.indexOf = function (obj, start) {
for (var i = (start || 0); i < this.length; i++) {
if (this[i] === obj) {
return i;
}
}
return -1;
};
}
Expression.prototype.setLanguage = function(lng){
this.lng=lng;
definitions = require('./definitions/'+lng+'/dictionary');
};
Expression.prototype.setExpr = function(expr){
this.expr=expr;
this.pos=0;
};
Expression.prototype.toPostfix = function(expr,nodeType_){
this.nodeType_=nodeType_||this.nodeType_;
this.expr=expr;
this.errormsg="";
this.pos=0;
this.tokenValue=0;
this.tokenSymbol=0;
this.tokenprio=0;
this.tokentype=0;
this.tmpstr="";
var expected=(PRIMARY | VAR | LPAREN | SIGNAL | NOT | BITWISE_NOT);
this.numOperands=0;
this.operStack = [];
this.postfixStack=[];
this.parameterStack=[];
//SE A EXPRESSAO É NULA (tamanho=0)
try{
if(expr.length===0){
return []; //devolve array vazio
}
}
catch(e){
if(expr===null || expr===undefined){
return [];
}
throw e;
}
while(this.pos<this.expr.length){
/*if(this.isUnaryOp()){
if((expected & UNARY) === 0){
//MUDAR PARA EXCEPÇAO
console.log("ERRO: Nao e esperado um operador UNARY");
break;
}
this.addOperator(postfixStack, operStack, tokenTypes.OPERATOR);
expected=(PRIMARY | LPAREN | FUNCTION);
}*/
if(this.isBitwiseNot()){
if((expected & BITWISE_NOT) === 0){
this.throwError("UNEXPECTED_BITWISE_NOT",this.pos);
//throw new Error("Nao e esperado um operador NOT");
}
this.addOperator(tokenTypes.UNARY_LEFT_OP);
expected=(NUMBER| LPAREN | VAR);
}
else if(this.isArithmeticOp()){
if((expected & ARITHMETICOP) === 0){
//****************************************************************
// VERIFICAR SE O OPERANDO É DO TIPO BOOLEAN. DA ERRO
//****************************************************************
if(this.getLastType()==tokenTypes.BOOLEAN){
var parameters=[this.tokenSymbol];
this.throwError("UNEXPECTED_ARITHMETICOP",this.pos,parameters);
}
//****************************************************************
//VERIFICAR SE É UM SINAL (negativo ou positivo) E AGRUPAR SINAIS
//****************************************************************
var counter=0;
var signals=[];
//verificar se há sinais positivos e negativos
while(this.isSignal()){
counter++;
if((expected & SIGNAL) === 0){
var parameters=[this.tokenSymbol];
this.throwError("UNEXPECTED_SIGNAL",this.pos,parameters);
}
signals.push(this.tokenValue); //guarda sinal numa pilha
this.pos++;
}
//****************************************************************
// NAO ENCONTROU SINAIS
//****************************************************************
if(counter===0){
var parameters=[this.tokenSymbol];
this.throwError("UNEXPECTED_BINARYOP",this.pos,parameters);
}
//****************************************************************
// ENCONTROU UM OU MAIS SINAIS
//****************************************************************
else{
var previous=signals.pop();
while(signals.length>0){
var next=signals.pop();
if(next===previous){
previous="+"; //sinais iguais -> +
}
else{
previous="-"; //sinais diferentes -> -
}
}
this.pos--; //acertar posição actual
//se o resultado for um sinal negativo cria o token
if(previous=="-"){
this.tokenValue=previous;
this.tokenSymbol=this.tokenValue;
this.tokenprio=prio.UNARY;
this.addOperator(tokenTypes.UNARY_LEFT_OP);
}
expected=(NUMBER | LPAREN |VAR | BITWISE_NOT);
}
}
//****************************************************************
// É ESPERADO UM OPERADOR ARITMÉTICO
//****************************************************************
else{
this.addOperator(this.tokentype);
expected=(NUMBER| TEXT | LPAREN | VAR | SIGNAL | BITWISE_NOT);
}
}
else if(this.isLogicOp()){
if((expected & LOGICOP)===0){
var parameters=[this.tokenSymbol];
this.throwError("UNEXPECTED_LOGICOP",this.pos,parameters);
}
this.addOperator(tokenTypes.BINARY_LOGIC_OP);
expected=( PRIMARY | LPAREN | VAR | NOT | SIGNAL);
//expected=( BOOLEAN | LPAREN | VAR | NOT);
}
else if(this.isNumber()){
if((expected & NUMBER) === 0){
var parameters=[this.tokenSymbol];
this.throwError("UNEXPECTED_NUMBER",this.pos,parameters);
//throw new Error("Nao e esperado um numero");
}
if(this.isArgument){
expected = (ARITHMETICOP| RPAREN | COMMA | FACT | LOGICOP);
}
else{
expected = (ARITHMETICOP | RPAREN | FACT | LOGICOP);
}
this.addOperand(this.tokentype);
}
else if(this.isLeftPar()){
if((expected & LPAREN) === 0){
this.throwError("UNEXPECTED_LEFTPAR",this.pos);
//throw new Error("Nao e esperado um parentesis esquerdo");
}
//****************************************************************
// SE É ESPERADA UMA FUNÇAO
//****************************************************************
if ((expected & CALL) || (expected & MATHFUNC_CALL)) {
//****************************************************************
// MODIFICA O TIPO DE TOKEN DE VARIAVEL PARA FUNCAO
//****************************************************************
if(expected & CALL){
var aux = this.postfixStack.pop();
this.tokenprio=prio.VALUE;
this.tokenValue=aux.value_;
this.tokenSymbol=this.tokenValue;
this.addOperator(tokenTypes.FUNC);
}
//****************************************************************
// AADQUIRE O ARGUMENTO DA FUNÇAO E AVALIA RECURSIVAMENTE
//****************************************************************
var leftPar=1;
var oldpos=this.pos;
var str="";
while(leftPar!==0 && this.pos<this.expr.length){
str+=this.expr.charAt(this.pos);
if(this.expr.charAt(this.pos)=="("){
leftPar++;
}
if(this.expr.charAt(this.pos)==")"){
leftPar--;
}
this.pos++;
}
//ao procurar argumento chegou ao final da expressao, logo esta mal construida
if(this.pos>this.expr.length){
var parameters=[str];
this.throwError("BAD_ARGUMENT",oldpos,parameters);
}
//guarda argumento da funçao
var argument=this.expr.substring(oldpos,this.pos-1);
//avaliar recursivamente a expressão do argumento
var outsidePos=oldpos+this.outsidePos;
this.parameterStack= new Expression(this.lng,this.nodeTypes,undefined,true,outsidePos).toPostfix(argument);
//cria o token do tipo argumento que guarda a stack com os parâmetros da funçao
this.tokenValue="argument";
this.tokenSymbol=argument;
this.tokenprio=prio.VALUE;
this.addOperand(tokenTypes.ARGUMENT);
if(this.isArgument){
//se for argumento é permitido ter uma vírgula seguir ao parentesis direito da função
//exemplo ---> func(func(1,2),3)
// |
// ---------->permitido
expected = (ARITHMETICOP | LOGICOP | RPAREN | FACT | COMMA) ;
}
else{
expected = (ARITHMETICOP | LOGICOP | RPAREN | FACT) ;
}
}
//****************************************************************
// É UM PARENTESIS ESQUERDO SIMPLES
//****************************************************************
else{
this.addOperator(tokenTypes.PARENT);
expected = (PRIMARY | LPAREN | VAR | SIGNAL | NOT | RPAREN | BITWISE_NOT);
}
}
else if(this.isRightPar()){
if((expected & RPAREN) === 0){
this.throwError("UNEXPECTED_RIGHTPAR",this.pos);
//throw new Error("Nao e esperado um parentesis direito");
}
else{
if(this.isArgument){
expected = (ARITHMETICOP | LOGICOP | RPAREN | FACT | COMMA);
}
//expectedParameter=NOPARAMETER; //já não são esperados parâmetros
else{
expected = (ARITHMETICOP | LOGICOP | RPAREN | FACT);
}
}
this.tokenValue=")";
this.tokenSymbol=this.tokenValue;
this.tokenprio=prio.PARENT;
this.addOperator(tokenTypes.PARENT);
}
else if(this.isNot()){
if((expected & NOT) === 0){
//verifica se é factorial
if((expected & FACT) !== 0){
this.tokenValue="!";
this.tokenSymbol=this.tokenValue;
this.tokenprio=prio.UNARY;
this.addOperator(tokenTypes.UNARY_RIGHT_OP);
expected=(ARITHMETICOP | RPAREN);
}
else{
this.throwError("UNEXPECTED_NOT",this.pos);
}
}
else{
this.addOperator(tokenTypes.UNARY_LEFT_OP);
expected=(BOOLEAN | LPAREN | VAR);
}
}
//Se o caracter é uma vírgula
else if (this.isComma()) {
//Se não é esperada uma vírgula dá erro
if ((expected & COMMA) === 0) {
this.throwError("UNEXPECTED_COMMA",this.pos);
//throw new Error("unexpected \",\"");
}
//this.addOperator(tokenTypes.COMMA);
//this.numOperands+=1;
this.tokensymnol=",";
this.tokenprio=prio.COMMA;
this.addOperator(tokenTypes.COMMA);
expected = (PRIMARY | VAR | SIGNAL | NOT | LPAREN | BITWISE_NOT);
}
else if(this.isChar()){
if((expected & TEXT) === 0){
var parameters=[this.tokenSymbol];
this.throwError("UNEXPECTED_CHAR",this.pos,parameters);
}
this.addOperand(tokenTypes.CHAR);
if(this.isArgument){
expected = (ARITHMETICOP | LOGICOP | RPAREN | COMMA);
//expected = (ARITHMETICOP | RPAREN | COMMA);
}
else{
expected = (ARITHMETICOP | LOGICOP | RPAREN);
//expected = (ARITHMETICOP | RPAREN);
}
}
else if(this.isString()){
if((expected & TEXT) === 0){
var parameters=[this.tokenSymbol];
this.throwError("UNEXPECTED_STRING",this.pos,parameters);
//throw new Error("Nao e esperada uma string");
}
this.addOperand(tokenTypes.STRING);
if(this.isArgument){
expected = (ARITHMETICOP | LOGICOP | RPAREN | COMMA);
//expected = (ARITHMETICOP | RPAREN | COMMA);
}
else{
expected = (ARITHMETICOP | LOGICOP | RPAREN);
//expected = (ARITHMETICOP | RPAREN | COMMA);
}
}
//****************************************************************
// SE ENCONTRAR TEXTO GUARDA-O NUMA VARIAVEL E VERIFICA O QUE É (variavel, funcao, etc..)
//****************************************************************
//o método getText() procura texto e guarda-o na string temporária tmpstr
else if(this.getText().length>0){
var i;
//Procura o símbolo nas definitions
for(i=0; i<definitions.length; i++){
//se encontrar o símbolo nas definitions
if(definitions[i].symbol==this.tmpstr){
this.tokenprio=prio.VALUE;
// se é do tipo value
if(definitions[i].type=='value'){
this.tokenSymbol=definitions[i].symbol;
this.tokenValue=definitions[i].value;
//se o subtipo é boolean
if(definitions[i].subtype=='boolean'){
//this.tokenValue=definitions[i].value;
//verifica se é esperado este subtipo.
if((expected & BOOLEAN)===0){
var parameters=[this.tmpstr];
this.throwError("UNEXPECTED_BOOLEAN",(this.pos-definitions[i].symbol.length+1),parameters);
}
this.addOperand(tokenTypes.BOOLEAN);
expected = (LOGICOP | RPAREN);
//se neste momento é um argumento é esperada também uma vírgula
if(this.isArgument){
expected = (LOGICOP | RPAREN | COMMA);
}
}
//se o subtipo é null
else if(definitions[i].subtype=='null'){
this.addOperand(tokenTypes.NULL);
expected = (ARITHMETICOP | LOGICOP | RPAREN);
}
//o subtipo não é boolean nem null
//se neste momento está a ser avaliado um argumento é esperada ainda uma vírgula
else if(this.isArgument){
this.addOperand(tokenTypes.REAL);
expected = (ARITHMETICOP | LOGICOP | RPAREN | COMMA);
}
else{
this.addOperand(tokenTypes.REAL);
expected = (ARITHMETICOP | LOGICOP | RPAREN);
}
}
//é uma definition do tipo função
else if(definitions[i].type=='mathFunction'){
this.tokenSymbol=definitions[i].symbol;
this.tokenValue=definitions[i].functionName;
var completeName=definitions[i].name;
var extraParameters={"funcName":completeName};
this.addOperator(tokenTypes.MATHFUNC,extraParameters);
expected = (LPAREN | MATHFUNC_CALL);
}
break; //quebra o for
}
}
//se percorrer a lista e não encontrar uma definition correspondente
//é uma variável (ou função)
if(i>=definitions.length){
//Se não é esperada uma variável dá erro
if ((expected & VAR) === 0) {
var parameters=[this.tokenSymbol];
this.throwError("UNEXPECTED_VAR",this.pos,parameters);
//throw new Error("Nao e esperada uma variavel");
}
this.tokenprio=prio.VALUE;
this.addOperand(tokenTypes.VAR);
//é esperado também um parentesis esquerdo porque pode ser uma função
expected = (ARITHMETICOP | LOGICOP | LPAREN | RPAREN | CALL | FACT);
}
}
else if(this.isAssign()){
if(this.isArgument){
var parameters=[this.expr];
this.throwError("ARGUMENT_ASSIGN",this.pos,parameters);
}
if(this.nodeType_!=this.nodeTypes.PROCESS){
var parameters=["NodeTypes."+nodeNames[this.nodeType_]];
//console.log("PARAMETERS");
//console.log(parameters);
this.throwError("INVALID_NODE_ASSIGN",this.pos,parameters);
}
//o item é o item que fica antes do sinal de igual
var item=this.postfixStack[this.postfixStack.length-1];
//console.log(item);
//se ainda só existe um elemento na postfixstack e esse elemento é uma Variável
//a atribuição é válida, e por isso adiciona o operador = à stack
if(this.postfixStack.length==1 && item.type_==tokenTypes.VAR){
this.addOperator(tokenTypes.ASSIGN);
}
else{
//expressão até ao sinal de igual
var badVar=this.expr.substring(0,this.pos-1);
var parameters=[badVar];
this.throwError("NOT_VAR_ASSIGN",this.pos,parameters);
}
expected = (PRIMARY | VAR | LPAREN | BITWISE_NOT | NOT | SIGNAL);
}
else if(this.isWhite()){
//salta espaço em branco
}
else {
this.throwError("INVALID_OPERATION",this.pos);
}
} //FIM DO WHILE
//descarrega os operadores restantes para a pilha pos fixa
this.popAll();
if(this.isArgument){
this.removeCommas();
}
if(this.isDebug){
console.log("numero de operandos: "+this.numOperands);
console.log("numero de tokens:"+this.postfixStack.length);
}
if((this.numOperands+1!=this.postfixStack.length) && this.postfixStack.length>0){
this.throwError("MISSING_OPERANDS",this.pos+1);
//throw new Error("Faltam operandos na expressão");
}
if(this.isDebug){
var dbg = new Debug();
console.log("POSTFIXTACK:");
dbg.printStack(this.postfixStack);
console.log("OPERATOR STACK:");
dbg.printStack(this.operStack);
}
return this.postfixStack;
};
Expression.prototype.removeCommas = function(){
var tempStack=[];
var item={};
while(this.postfixStack.length>0){
//retira o primeiro item da pilha
item=this.postfixStack.shift();
if(item.type_!=tokenTypes.COMMA){
tempStack.push(item);
}
}
this.postfixStack=tempStack;
};
Expression.prototype.isAssign = function(){
var code1 = this.expr.charAt(this.pos);
var code2 = this.expr.charAt(this.pos+1);
if(code1=="=" && code2!="="){
this.pos++;
this.tokenValue="=";
this.tokenSymbol=this.tokenValue;
this.tokenprio=prio.ASSIGN;
return true;
}
return false;
};
Expression.prototype.getLastType = function(){
if(this.postfixStack.length>0){
return this.postfixStack[this.postfixStack.length-1].type_;
}
return false;
};
Expression.prototype.popAll = function(){
var aux;
while(this.operStack.length>0){
aux=this.operStack.pop();
if(aux.value_=="("){
this.throwError("UNMATCHED_PAR",this.pos);
//throw new Error("Parentesis sem correspondencia");
}
this.postfixStack.push(aux);
}
};
Expression.prototype.throwError = function(errorCode, column, parameters){
/*if(msg===undefined && column!==undefined){
this.errormsg = "parse error: "+ column;
throw new Error(this.errormsg);
}
this.errormsg = "parse error [column " + (column) + "]: " + msg;*/
if(parameters===undefined){
parameters=[];
}
//somar colunas exteriores ao argumento de funções
column = this.outsidePos+column;
throw new ExpressionError(errorCode, column, parameters);
};
//adiciona um novo token à pilha de operadores
Expression.prototype.addOperator = function(type_,extraParameters){
if(type_===tokenTypes.BINARYOP || type_===tokenTypes.BINARY_LOGIC_OP){
this.numOperands+=2; //são esperados 2 operandos
}
if(type_===tokenTypes.UNARY_LEFT_OP || type_===tokenTypes.UNARY_RIGHT_OP){
this.numOperands++; //é esperado 1 operando
}
if(type_===tokenTypes.FUNC){
this.numOperands++; //é esperado 1 operando
}
if(type_===tokenTypes.MATHFUNC){
//console.log(this.tokenprio);
this.numOperands++; //é esperado 1 operando
}
if(type_===tokenTypes.COMMA){
this.numOperands+=1; //é esperado 1 operando (a vírgula é apenas um separador e é removida no fim)
}
if(type_===tokenTypes.ASSIGN){
this.numOperands+=2; //são esperados 2 operandos
}
var operator = new Token(type_, this.tokenValue, this.tokenSymbol, this.tokenprio, extraParameters);
var tmp;
if(this.isDebug){
console.log(this.tokenValue);
}
//se for parentesis esquerdo carrega para a pilha
if(this.tokenValue=="("){
this.operStack.push(operator);
}
//se for parentesis direito
else if(this.tokenValue==")"){
if(this.operStack.length>0){
tmp = this.operStack.pop();
while(tmp.value_ != ("(")){
if(this.operStack.length>0){
this.postfixStack.push(tmp);
tmp=this.operStack.pop();
}
else{
this.throwError("TOO_MANY_PAR",this.pos);
}
}
}
else{
this.throwError("TOO_MANY_PAR",this.pos);
}
}
else{
/*Se já existem tokens na pilha de operadores verifica se o novo operador
é prioritário. Se for prioritário é adicionado à stack, caso contrário
o operador anterior passa da stack de operadores para a stack pós fixa
e depois o novo operador é passado para a stack de operadores.*/
if(this.operStack.length>0){
while((this.operStack.length>0) && this.isntPrio(operator.prio_)){
this.postfixStack.push(this.operStack.pop());
}
this.operStack.push(operator);
/*
if(this.isntPrio(operator.prio_)){
this.postfixStack.push(this.operStack.pop());
this.operStack.push(operator);
}
else{
this.operStack.push(operator);
}*/
}
else{
this.operStack.push(operator);
}
}
};
//Compara a prioridade recebida por parâmetro com o topo da pilha de operadores
Expression.prototype.isntPrio = function(prio){
//faz peek (copia valor sem retirar da pilha)
//console.log(this.operStack.length);
var lasttoken = this.operStack[this.operStack.length-1];
//só compara prioridade se o topo da pilha de operadores não tiver um par. esq.
if(lasttoken.symbol!="("){
if(prio>=this.operStack[this.operStack.length-1].prio_){
return true;
}
}
return false;
};
/*
Expression.prototype.isReserved = function(str){
if(str.length > 0 && (str in reserved)){
this.tokenValue=str;
this.tokenprio=prio.RESERVED;
return true;
}
return false;
};
*/
Expression.prototype.addOperand = function(type_){
var operand;
var value;
if(type_==tokenTypes.INTEGER){
//conversão para inteiro
value=parseInt(this.tokenValue,10);
}
else if(type_==tokenTypes.REAL){
//conversão para real
value=parseFloat(this.tokenValue);
}
else{
value=this.tokenValue;
}
if(type_==tokenTypes.ARGUMENT){
var extraParameters={"parameterStack":this.parameterStack};
operand = new Token(type_, this.tokenValue, this.tokenSymbol, this.tokenprio, extraParameters);
}
else{
operand = new Token(type_, value, this.tokenSymbol, this.tokenprio);
}
this.postfixStack.push(operand);
};
/*
Expression.prototype.isUnaryOp = function(){
//TESTE PARA DETECTAR DINAMICAMENTE COM VARIOS CARACTERES
//var str=this.expr.charAt(this.pos);
//var found=false;
//Vai pegando nos caracteres e procura uma correspondencia na lista de operadores unary
//while(this.pos<this.expr.length){
// if(unaryOps[str]!=undefined){
// this.tokenprio=prio.UNARY;
// this.tokenValue=str;
// this.pos++;
// found=true;
// str+=this.expr.charAt(this.pos);
// }
// else{
// break;
// }
//}
//return found;
var code=this.expr.charAt(this.pos);
if(code==="!" || code ==="-"){
this.tokenValue=code;
this.tokenprio=prio.UNARY;
return true;
}
return false;
};
*/
Expression.prototype.isNot = function(){
var code = this.expr.charAt(this.pos);
var code2= this.expr.charAt(this.pos+1);
if(code=="!" && code2!="="){
this.pos++;
this.tokenValue="!";
this.tokenSymbol=this.tokenValue;
this.tokenprio=prio.UNARY;
return true;
}
return false;
};
Expression.prototype.isBitwiseNot = function(){
var code = this.expr.charAt(this.pos);
if(code=="~"){
this.pos++;
this.tokenValue="~";
this.tokenSymbol=this.tokenValue;
this.tokenprio=prio.UNARY;
return true;
}
return false;
};
Expression.prototype.isSignal = function(){
if(this.expr.charAt(this.pos-1)=="-"){
this.tokenValue="-";
this.tokenSymbol=this.tokenValue;
this.tokenprio=prio.UNARY;
return true;
}
else if(this.expr.charAt(this.pos-1)=="+"){
this.tokenValue="+";
this.tokenSymbol=this.tokenValue;
this.tokenprio=prio.UNARY;
return true;
}
return false;
};
Expression.prototype.isComma = function () {
var code = this.expr.charCodeAt(this.pos);
if (code === 44) { // ,
this.pos++;
this.tokenprio = prio.COMMA;
this.tokenValue = ",";
this.tokenSymbol=this.tokenValue;
return true;
}
return false;
};
Expression.prototype.isArithmeticOp = function(){
var code=this.expr.charAt(this.pos);
var code2=this.expr.charAt(this.pos+1);
if(code==="+"){
this.tokenValue=code;
this.tokenprio=prio.SUM;
this.tokentype=tokenTypes.BINARYOP;
}
else if(code==="-"){
this.tokenValue=code;
this.tokenprio=prio.SUB;
this.tokentype=tokenTypes.BINARYOP;
}
else if(code==="/"){
this.tokenValue=code;
this.tokenprio=prio.DIV;
this.tokentype=tokenTypes.BINARYOP;
}
else if(code==="%"){
this.tokenValue=code;
this.tokenprio=prio.MOD;
this.tokentype=tokenTypes.BINARYOP;
}
else if(code==="*"){
this.tokenValue=code;
this.tokenprio=prio.MUL;
this.tokentype=tokenTypes.BINARYOP;
if(code2==="*"){
this.pos++;
this.tokenValue="**";
this.tokenprio=prio.POW;
this.tokentype=tokenTypes.BINARYOP;
}
}
else if(code==="<"){
this.tokenprio=prio.COMP1;
this.tokenValue=code;
this.tokentype=tokenTypes.BINARY_LOGIC_OP;
if(code2==="="){
this.pos++;
this.tokenValue="<=";
this.tokentype=tokenTypes.BINARY_LOGIC_OP;
}
if(code2==="<"){
this.pos++;
this.tokenValue="<<";
this.tokenprio=prio.SHIFT;
this.tokentype=tokenTypes.BINARYOP;
}
}
else if(code===">"){
this.tokenprio=prio.COMP1;
this.tokenValue=code;
this.tokentype=tokenTypes.BINARY_LOGIC_OP;
if(code2==="="){
this.pos++;
this.tokenValue=">=";
this.tokentype=tokenTypes.BINARY_LOGIC_OP;
}
if(code2===">"){
this.pos++;
this.tokenValue=">>";
this.tokenprio=prio.SHIFT;
this.tokentype=tokenTypes.BINARYOP;
}
}
else if(code==="=" && code2==="="){
this.pos++;
this.tokenValue="==";
this.tokenprio=prio.COMP2;
this.tokentype=tokenTypes.BINARY_LOGIC_OP;
}
else if(code==="&" && code2!="&"){
this.tokenValue="&";
this.tokenprio=prio.BITAND;
this.tokentype=tokenTypes.BINARYOP;
}
else if(code==="|" && code2!="|"){
this.tokenValue="|";
this.tokenprio=prio.BITINOR;
this.tokentype=tokenTypes.BINARYOP;
}
else if(code==="^"){
this.tokenValue=code;
this.tokenprio=prio.BITEXOR;
this.tokentype=tokenTypes.BINARYOP;
}
else if(code==="~"){
this.tokenValue=code;
this.tokenprio=prio.UNARY;
this.tokentype=tokenTypes.UNARY_LEFT_OP;
}
else{
return false;
}
this.tokenSymbol=this.tokenValue;
this.pos++;
return true;
};
Expression.prototype.isLogicOp = function(){
var code=this.expr.charAt(this.pos);
var code2=this.expr.charAt(this.pos+1);
if(code=="!" && code2=="="){
this.pos++;
this.tokenValue="!=";
this.tokenprio=prio.COMP2;
}
else if(code==="&" && code2==="&"){
this.pos++;
this.tokenValue="&&";
this.tokenprio=prio.LOGICAND;
}
else if(code==="|" && code2==="|"){
this.pos++;
this.tokenValue="||";
this.tokenprio=prio.LOGICOR;
}
else{
return false;
}
this.tokenSymbol=this.tokenValue;
this.pos++;
return true;
};
Expression.prototype.isNumber = function(){
var r = false;
var str = "";
var decimalPoint=0;
while (this.pos < this.expr.length) {
var code = this.expr.charAt(this.pos);
//se é algarismo
if (code >= '0' && code <= '9') {
str += this.expr.charAt(this.pos);
this.pos++;
this.tokenValue=str;
this.tokentype=tokenTypes.INTEGER;
r = true;
}
//se é ponto decimal
else if(code=='.'){
str += this.expr.charAt(this.pos);
this.pos++;
this.tokenValue=str;
str += this.isDecimalPart();
this.tokenValue=str;
this.tokentype=tokenTypes.REAL;
}/*
if(code=='e' || code=='E'){
str+=this.expr.charAt(this.pos);
this.pos++;
str+=this.addScientificNotation();
this.tokenValue=str;
this.tokentype=tokenTypes.INTEGER;
}*/
else {
break; //quebra o ciclo quando o caracter não for um algarismo nem ponto decimal
}
}
this.tokenSymbol=this.tokenValue;
this.tokenprio=prio.VALUE;
return r;
};
Expression.prototype.isDecimalPart = function(){
var str = "";
//percorre os caracteres da expressão desde a posição actual ao final
while (this.pos < this.expr.length) {
//guarda o caracter actual
var code = this.expr.charAt(this.pos);
//se o caracter é alfanumérico (0-9)
if (code >= '0' && code <= '9') {
str += this.expr.charAt(this.pos);
this.pos++;
this.tokenValue = parseFloat(str);
this.tokenSymbol = this.tokenValue;
}
//se não é número
else{
//se é um ponto decimal
if(code=='.'){
this.throwError("TOO_MANY_DECIMAL_POINT",this.pos);
}
else{
break;
}
}
}
return str;
};
Expression.prototype.addScientificNotation= function(){
var str = "";
var ch = this.expr.charAt(this.pos);
if(ch=="+" || ch=="-"){
str+=ch;
}
//percorre os caracteres da expressão desde a posição actual ao final
while (this.pos < this.expr.length) {
ch=this.expr.charAt(this.pos);
if(ch >= '0' && ch <= '9'){
str+=ch;
}
else{
this.throwError("BAD_SCIENTIFIC_NOTATION",this.pos);
}
}
return str;
};
Expression.prototype.getText = function(){
var str = "";
for (var i = this.pos; i < this.expr.length; i++) {
var c = this.expr.charAt(i);
//se o caracter não for uma letra alfabética (uppercase=lowercase)
if (c.toUpperCase() === c.toLowerCase()) {
//se a string não começar com uma letra alfabética OU o caracter não
//for _ nem algarismo termina o ciclo for
if (i === this.pos || (c != '_' && (c < '0' || c > '9'))) {
break;
}
}
str += c; //constrói a string a partir dos caracteres
}
//incrementar posições(numero de caracteres da string)
this.pos+=str.length;
this.tmpstr=str;
this.tokenValue=str;
this.tokenSymbol=this.tokenValue;
return str;
};
Expression.prototype.isString = function(){
var str='';
var startpos=this.pos;
var aux;
if (this.expr.charAt(this.pos) == '"') {
this.pos++;
aux=this.pos;
while (this.pos < this.expr.length) {