| ︙ | | |
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
|
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
|
-
+
+
-
+
+
+
|
* Copyright (c) 1997 Sun Microsystems, Inc.
* Copyright (c) 1998-2000 by Scriptics Corporation.
* Contributions from Don Porter, NIST, 2006. (not subject to US copyright)
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclCompExpr.c,v 1.53.2.7 2007/07/12 14:29:54 dgp Exp $
* RCS: @(#) $Id: tclCompExpr.c,v 1.53.2.8 2007/07/19 22:52:57 dgp Exp $
*/
#include "tclInt.h"
#include "tclCompile.h" /* CompileEnv */
/*
* Expression parsing takes place in the routine ParseExpr(). It takes a
* string as input, parses that string, and generates a representation of
* the expression in the form of a tree of operators, a list of literals,
* a list of function names, and an array of Tcl_Token's within a Tcl_Parse
* struct. The tree is composed of OpNodes.
*/
typedef struct OpNode {
int left; /* "Pointer" to the left operand. */
int right; /* "Pointer" to the right operand. */
union {
int parent; /* "Pointer" to the parent operand. */
int parent; /* "Pointer" to the parent operand. */
int prev; /* "Pointer" joining incomplete tree stack */
} p;
unsigned char lexeme; /* Code that identifies the operator. */
unsigned char precedence; /* Precedence of the operator */
} OpNode;
/*
* The storage for the tree is dynamically allocated array of OpNodes. The
* array is grown as parsing needs dictate according to a scheme similar to
|
| ︙ | | |
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
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
|
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
|
/*
* Note that it is sufficient to store in the tree just the type of leaf
* operand, without any explicit pointer to which leaf. This is true because
* the inorder traversals of the completed tree we perform are known to visit
* the leaves in the same order as the original parse.
*
* Those OpNodes that are themselves (roots of subexpression trees that are)
* operands of some operator store in their parent field a "pointer" to the
* OpNode of that operator. The parent field permits a destructive inorder
* traversal of the tree within a non-recursive routine (ConvertTreeToTokens()
* and CompileExprTree()). This means that even expression trees of great
* depth pose no risk of blowing the C stack.
* In a completed parse tree, those OpNodes that are themselves (roots of
* subexpression trees that are) operands of some operator store in their
* p.parent field a "pointer" to the OpNode of that operator. The p.parent
* field permits a destructive inorder traversal of the tree within a
* non-recursive routine (ConvertTreeToTokens() and CompileExprTree()). This
* means that even expression trees of great depth pose no risk of blowing
* the C stack.
*
* While the parse tree is being constructed, the same memory space is used
* to hold the p.prev field which chains together a stack of incomplete
* trees awaiting their right operands.
*
* The lexeme field is filled in with the lexeme of the operator that is
* returned by the ParseLexeme() routine. Only lexemes for unary and
* binary operators get stored in an OpNode. Other lexmes get different
* treatement.
*
* Each lexeme belongs to one of four categories, which determine
|
| ︙ | | |
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
-
|
PREC_UNARY, /* UNARY_PLUS */
PREC_UNARY, /* UNARY_MINUS */
PREC_UNARY, /* FUNCTION */
PREC_START, /* START */
PREC_OPEN_PAREN, /* OPEN_PAREN */
PREC_UNARY, /* NOT*/
PREC_UNARY, /* BIT_NOT*/
0, 0, 0, 0, 0, 0, 0, 0,
};
/*
* The JumpList struct is used to create a stack of data needed for the
* TclEmitForwardJump() and TclFixupForwardJump() calls that are performed
* when compiling the short-circuiting operators QUESTION/COLON, AND, and OR.
* Keeping a stack permits the CompileExprTree() routine to be non-recursive.
|
| ︙ | | |
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
-
+
-
-
+
-
-
+
|
* Declarations for local functions to this file:
*/
static void CompileExprTree(Tcl_Interp *interp, OpNode *nodes,
Tcl_Obj *const litObjv[], Tcl_Obj *funcList,
Tcl_Token *tokenPtr, int *convertPtr,
CompileEnv *envPtr);
static void ConvertTreeToTokens(Tcl_Interp *interp,
static void ConvertTreeToTokens(const char *start, int numBytes,
const char *start, int numBytes, OpNode *nodes,
Tcl_Obj *litList, Tcl_Token *tokenPtr,
OpNode *nodes, Tcl_Token *tokenPtr,
Tcl_Parse *parsePtr);
static int CopyTokens(Tcl_Token *sourcePtr, Tcl_Parse *parsePtr);
static int GenerateTokensForLiteral(const char *script,
int numBytes, Tcl_Obj *litList, int nextLiteral,
Tcl_Parse *parsePtr);
int numBytes, Tcl_Parse *parsePtr);
static int ParseExpr(Tcl_Interp *interp, const char *start,
int numBytes, OpNode **opTreePtr,
Tcl_Obj *litList, Tcl_Obj *funcList,
Tcl_Parse *parsePtr, int parseOnly);
static int ParseLexeme(const char *start, int numBytes,
unsigned char *lexemePtr, Tcl_Obj **literalPtr);
|
| ︙ | | |
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
-
+
-
-
|
*----------------------------------------------------------------------
*/
static int
ParseExpr(
Tcl_Interp *interp, /* Used for error reporting. */
const char *start, /* Start of source string to parse. */
int numBytes, /* Number of bytes in string. If < 0, the
int numBytes, /* Number of bytes in string. */
* string consists of all bytes up to the
* first null character. */
OpNode **opTreePtr, /* Points to space where a pointer to the
* allocated OpNode tree should go. */
Tcl_Obj *litList, /* List to append literals to. */
Tcl_Obj *funcList, /* List to append function names to. */
Tcl_Parse *parsePtr, /* Structure to fill with tokens representing
* those operands that require run time
* substitutions. */
|
| ︙ | | |
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
|
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
|
-
-
-
-
-
+
-
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
|
* we build the parse tree. */
int nodesAvailable = 64; /* Initial size of the storage array. This
* value establishes a minimum tree memory cost
* of only about 1 kibyte, and is large enough
* for most expressions to parse with no need
* for array growth and reallocation. */
int nodesUsed = 0; /* Number of OpNodes filled. */
int code = TCL_OK; /* Return code */
int scanned = 0; /* Capture number of byte scanned by
* parsing routines. */
unsigned char lexeme = START; /* Most recent lexeme parsed. */
int lastOpen = 0; /* Index of the OpNode of the OPEN_PAREN
* operator we most recently matched. */
int lastParsed = 0; /* Stores info about what the lexeme parsed
int lastParsed; /* Stores info about what the lexeme parsed
* the previous pass through the parsing loop
* was. If it was an operator, lastParsed is
* the index of the OpNode for that operator.
* If it was not and operator, lastParsed holds
* If it was not an operator, lastParsed holds
* an OperandTypes value encoding what we
* need to know about it. The initial value
* is 0 indicating that as we start the "last
* thing we parsed" was the START lexeme stored
* in node 0. */
* need to know about it. */
int incomplete; /* Index of the most recent incomplete tree
* in the OpNode array. Heads a stack of
* incomplete trees linked by p.prev. */
int complete = OT_NONE; /* "Index" of the complete tree (that is, a
* complete subexpression) determined at the
* moment. OT_NONE is a nonsense value
* used only to silence compiler warnings.
* During a parse, complete will always hold
* an index or an OperandTypes value pointing
* to an actual leaf at the time the complete
* tree is needed. */
/* These variables control generation of the error message. */
Tcl_Obj *msg = NULL; /* The error message. */
Tcl_Obj *post = NULL; /* In a few cases, an additional postscript
* for the error message, supplying more
* information after the error msg and
* location have been reported. */
|
| ︙ | | |
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
|
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
|
-
-
-
-
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
|
* should be inserted. */
const int limit = 25; /* Portions of the error message are
* constructed out of substrings of the
* original expression. In order to keep the
* error message readable, we impose this limit
* on the substring size we extract. */
if (numBytes < 0) {
numBytes = (start ? strlen(start) : 0);
}
TclParseInit(interp, start, numBytes, parsePtr);
nodes = (OpNode *) attemptckalloc(nodesAvailable * sizeof(OpNode));
if (nodes == NULL) {
TclNewLiteralStringObj(msg, "not enough memory to parse expression");
code = TCL_ERROR;
} else {
/*
* Initialize the parse tree with the special "START" node.
goto error;
}
/* Initialize the parse tree with the special "START" node. */
*/
nodes->lexeme = lexeme;
nodes->precedence = prec[lexeme];
nodes->left = OT_NONE;
nodes->right = OT_NONE;
nodes->parent = -1;
nodesUsed++;
}
nodes->lexeme = START;
nodes->precedence = prec[START];
nodes->left = OT_NONE;
nodes->right = OT_NONE;
incomplete = lastParsed = nodesUsed;
nodesUsed++;
/*
* Main parsing loop parses one lexeme per iteration. We exit the
* loop only when there's a syntax error with a "goto error" which
* takes us to the error handling code following the loop, or when
* we've successfully completed the parse and we return to the caller.
*/
while ((code == TCL_OK) && (lexeme != END)) {
while (1) {
OpNode *nodePtr; /* Points to the OpNode we may fill this
* pass through the loop. */
unsigned char lexeme; /* The lexeme we parse this iteration. */
Tcl_Obj *literal; /* Filled by the ParseLexeme() call when
* a literal is parsed that has a Tcl_Obj
* rep worth preserving. */
const char *lastStart = start - scanned;
/* Compute where the lexeme parsed the
* previous pass through the loop began.
* This is helpful for detecting invalid
|
| ︙ | | |
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
|
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
|
-
-
+
-
-
+
-
-
-
-
+
-
-
-
-
+
-
-
+
+
+
+
|
newPtr = (OpNode *) attemptckrealloc((char *) nodes,
(unsigned int) size * sizeof(OpNode));
} while ((newPtr == NULL)
&& ((size -= (size - nodesUsed) / 2) > nodesUsed));
if (newPtr == NULL) {
TclNewLiteralStringObj(msg,
"not enough memory to parse expression");
code = TCL_ERROR;
continue;
goto error;
}
nodesAvailable = size;
nodes = newPtr;
}
nodePtr = nodes + nodesUsed;
/*
* Skip white space between lexemes.
/* Skip white space between lexemes. */
*/
scanned = TclParseAllWhiteSpace(start, numBytes);
start += scanned;
numBytes -= scanned;
scanned = ParseLexeme(start, numBytes, &lexeme, &literal);
/*
* Use context to categorize the lexemes that are ambiguous.
/* Use context to categorize the lexemes that are ambiguous. */
*/
if ((NODE_TYPE & lexeme) == 0) {
switch (lexeme) {
case INVALID:
msg = Tcl_ObjPrintf(
"invalid character \"%.*s\"", scanned, start);
code = TCL_ERROR;
continue;
goto error;
case INCOMPLETE:
msg = Tcl_ObjPrintf(
"incomplete operator \"%.*s\"", scanned, start);
code = TCL_ERROR;
continue;
goto error;
case BAREWORD:
/*
* Most barewords in an expression are a syntax error.
* The exceptions are that when a bareword is followed by
* an open paren, it might be a function call, and when the
* bareword is a legal literal boolean value, we accept that
* as well.
*/
if (start[scanned+TclParseAllWhiteSpace(
start+scanned, numBytes-scanned)] == '(') {
lexeme = FUNCTION;
/*
* When we compile the expression we'll need the function
* name, and there's no place in the parse tree to store
* it, so we keep a separate list of all the function
* names we've parsed in the order we found them.
*/
Tcl_ListObjAppendElement(NULL, funcList, literal);
} else {
int b;
if (Tcl_GetBooleanFromObj(NULL, literal, &b) == TCL_OK) {
lexeme = BOOLEAN;
} else {
Tcl_DecrRefCount(literal);
|
| ︙ | | |
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
|
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
|
-
-
+
+
+
-
+
-
-
+
-
-
-
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
+
|
start, (scanned < limit) ? "" : "...",
(scanned < limit) ? scanned : limit - 3,
start, (scanned < limit) ? "" : "...");
Tcl_AppendPrintfToObj(post,
" or \"%.*s%s(...)\" or ...",
(scanned < limit) ? scanned : limit - 3,
start, (scanned < limit) ? "" : "...");
code = TCL_ERROR;
continue;
goto error;
}
}
break;
case PLUS:
case MINUS:
if (IsOperator(lastParsed)) {
/*
* A "+" or "-" coming just after another operator
* must be interpreted as a unary operator.
*/
lexeme |= UNARY;
} else {
lexeme |= BINARY;
}
}
}
} /* Uncategorized lexemes */
/*
* Handle lexeme based on its category.
/* Handle lexeme based on its category. */
*/
switch (NODE_TYPE & lexeme) {
/*
* Each LEAF results in either a literal getting appended to the
* litList, or a sequence of Tcl_Tokens representing a Tcl word
* getting appended to the parsePtr->tokens. No OpNode is filled
* for this lexeme.
*/
case LEAF: {
Tcl_Token *tokenPtr;
const char *end;
const char *end = start;
int wordIndex;
int code = TCL_OK;
/*
* Store away any literals on the list now, so they'll
* be available for our caller to free if we error out
* A leaf operand appearing just after something that's not an
* operator is a syntax error.
* of this routine. [Bug 1705778, leak K23]
*/
switch (lexeme) {
case NUMBER:
case BOOLEAN:
Tcl_ListObjAppendElement(NULL, litList, literal);
break;
default:
break;
}
if (NotOperator(lastParsed)) {
msg = Tcl_ObjPrintf("missing operator at %s", mark);
if (lastStart[0] == '0') {
Tcl_Obj *copy = Tcl_NewStringObj(lastStart,
start + scanned - lastStart);
if (TclCheckBadOctal(NULL, Tcl_GetString(copy))) {
TclNewLiteralStringObj(post,
"looks like invalid octal number");
}
Tcl_DecrRefCount(copy);
}
scanned = 0;
insertMark = 1;
parsePtr->errorType = TCL_PARSE_BAD_NUMBER;
code = TCL_ERROR;
continue;
/* Free any literal to avoid a memleak. */
if ((lexeme == NUMBER) || (lexeme == BOOLEAN)) {
Tcl_DecrRefCount(literal);
}
goto error;
}
switch (lexeme) {
case NUMBER:
case BOOLEAN:
Tcl_ListObjAppendElement(NULL, litList, literal);
lastParsed = OT_LITERAL;
complete = lastParsed = OT_LITERAL;
start += scanned;
numBytes -= scanned;
continue;
default:
break;
}
|
| ︙ | | |
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
|
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
|
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
+
+
-
-
-
+
+
-
+
-
-
+
|
tokenPtr->start = start;
parsePtr->numTokens++;
switch (lexeme) {
case QUOTED:
code = Tcl_ParseQuotedString(interp, start, numBytes,
parsePtr, 1, &end);
if (code != TCL_OK) {
/* TODO: This adjustment of scanned is untested and
* and uncommented. Correct that. Its only possible
* purpose is to influence the error message. */
scanned = parsePtr->term - start;
scanned += (scanned < numBytes);
continue;
}
scanned = end - start;
break;
case BRACED:
code = Tcl_ParseBraces(interp, start, numBytes,
parsePtr, 1, &end);
if (code != TCL_OK) {
continue;
}
scanned = end - start;
break;
case VARIABLE:
code = Tcl_ParseVarName(interp, start, numBytes, parsePtr, 1);
if (code != TCL_OK) {
/* TODO: This adjustment of scanned is untested and
/*
* and uncommented. Correct that. Its only possible
* purpose is to influence the error message. */
scanned = parsePtr->term - start;
* Handle the quirk that Tcl_ParseVarName reports a successful
* parse even when it gets only a "$" with no variable name.
scanned += (scanned < numBytes);
continue;
}
*/
tokenPtr = parsePtr->tokenPtr + wordIndex + 1;
if (tokenPtr->type != TCL_TOKEN_VARIABLE) {
if (code == TCL_OK && tokenPtr->type != TCL_TOKEN_VARIABLE) {
TclNewLiteralStringObj(msg, "invalid character \"$\"");
code = TCL_ERROR;
continue;
goto error;
}
scanned = tokenPtr->size;
break;
case SCRIPT: {
Tcl_Parse *nestedPtr =
(Tcl_Parse *) TclStackAlloc(interp, sizeof(Tcl_Parse));
|
| ︙ | | |
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
|
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
|
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
code = TCL_ERROR;
break;
}
}
TclStackFree(interp, nestedPtr);
end = start;
start = tokenPtr->start;
if (code != TCL_OK) {
/* TODO: This adjustment of scanned is untested and
* and uncommented. Correct that. Its only possible
* purpose is to influence the error message. */
scanned = parsePtr->term - start;
scanned += (scanned < numBytes);
continue;
}
scanned = end - start;
tokenPtr->size = scanned;
parsePtr->numTokens++;
break;
}
}
if (code != TCL_OK) {
/*
* Here we handle all the syntax errors generated by
* the Tcl_Token generating parsing routines called in the
* switch just above. If the value of parsePtr->incomplete
* is 1, then the error was an unbalanced '[', '(', '{',
* or '"' and parsePtr->term is pointing to that unbalanced
* character. If the value of parsePtr->incomplete is 0,
* then the error is one of lacking whitespace following a
* quoted word, for example: expr {[an error {foo}bar]},
* and parsePtr->term points to where the whitespace is
* missing. We reset our values of start and scanned so that
* when our error message is constructed, the location of
* the syntax error is sure to appear in it, even if the
* quoted expression is truncated.
*/
start = parsePtr->term;
scanned = parsePtr->incomplete;
goto error;
}
tokenPtr = parsePtr->tokenPtr + wordIndex;
tokenPtr->size = scanned;
tokenPtr->numComponents = parsePtr->numTokens - wordIndex - 1;
if (!parseOnly && ((lexeme == QUOTED) || (lexeme == BRACED))) {
|
| ︙ | | |
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
|
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
|
-
+
-
+
+
-
+
+
+
+
+
+
+
-
+
-
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
-
-
+
|
* wasteful to convert to a literal only to convert back again
* later.
*/
literal = Tcl_NewObj();
if (TclWordKnownAtCompileTime(tokenPtr, literal)) {
Tcl_ListObjAppendElement(NULL, litList, literal);
lastParsed = OT_LITERAL;
complete = lastParsed = OT_LITERAL;
parsePtr->numTokens = wordIndex;
break;
}
Tcl_DecrRefCount(literal);
}
lastParsed = OT_TOKENS;
complete = lastParsed = OT_TOKENS;
break;
} /* case LEAF */
}
case UNARY:
/*
* A unary operator appearing just after something that's not an
* operator is a syntax error -- something trying to be the left
* operand of an operator that doesn't take one.
*/
case UNARY:
if (NotOperator(lastParsed)) {
msg = Tcl_ObjPrintf("missing operator at %s", mark);
scanned = 0;
insertMark = 1;
code = TCL_ERROR;
continue;
goto error;
}
lastParsed = nodesUsed;
nodePtr->lexeme = lexeme;
nodePtr->precedence = prec[lexeme];
nodePtr->left = OT_NONE;
nodePtr->right = OT_NONE;
nodePtr->parent = nodePtr - nodes - 1;
/* Create an OpNode for the unary operator */
nodePtr->lexeme = lexeme; /* Remember the operator... */
nodePtr->precedence = prec[lexeme]; /* ... and its precedence. */
nodePtr->left = OT_NONE; /* No left operand */
nodePtr->right = OT_NONE; /* Right operand not
* yet known. */
/*
* This unary operator is a new incomplete tree, so push it
* onto our stack of incomplete trees. Also remember it as
* the last lexeme we parsed.
*/
nodePtr->p.prev = incomplete;
incomplete = lastParsed = nodesUsed;
nodesUsed++;
break;
case BINARY: {
OpNode *otherPtr = NULL;
OpNode *incompletePtr;
unsigned char precedence = prec[lexeme];
/*
* A binary operator appearing just after another operator is a
* syntax error -- one of the two operators is missing an operand.
*/
if (IsOperator(lastParsed)) {
if ((lexeme == CLOSE_PAREN)
&& (nodePtr[-1].lexeme == OPEN_PAREN)) {
if (nodePtr[-2].lexeme == FUNCTION) {
/*
* Normally, "()" is a syntax error, but as a special
* case accept it as an argument list for a function.
* Treat this as a special LEAF lexeme, and restart
* the parsing loop with zero characters scanned.
* We'll parse the ")" again the next time through,
* but with the OT_EMPTY leaf as the subexpression
* between the parens.
*/
scanned = 0;
lastParsed = OT_EMPTY;
complete = lastParsed = OT_EMPTY;
/* TODO: explain */
nodePtr[-1].left--;
break;
}
msg = Tcl_ObjPrintf("empty subexpression at %s", mark);
scanned = 0;
insertMark = 1;
code = TCL_ERROR;
continue;
goto error;
}
if (nodePtr[-1].precedence > precedence) {
if (nodePtr[-1].lexeme == OPEN_PAREN) {
TclNewLiteralStringObj(msg, "unbalanced open paren");
parsePtr->errorType = TCL_PARSE_MISSING_PAREN;
} else if (nodePtr[-1].lexeme == COMMA) {
|
| ︙ | | |
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
|
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
|
-
-
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
-
-
-
+
-
-
+
-
-
-
+
+
+
-
-
-
+
-
-
-
+
+
+
+
-
+
-
-
-
+
-
+
-
+
-
-
+
+
+
-
-
-
+
+
+
-
-
+
+
+
-
-
-
+
+
+
-
+
-
-
+
+
-
+
+
-
-
-
+
+
+
-
-
+
+
-
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
-
+
-
-
+
+
-
+
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
+
+
-
+
-
-
-
-
+
+
+
+
-
-
-
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
-
+
+
+
-
-
-
+
-
-
-
-
-
-
-
-
+
-
-
-
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
|
}
}
if (msg == NULL) {
msg = Tcl_ObjPrintf("missing operand at %s", mark);
scanned = 0;
insertMark = 1;
}
code = TCL_ERROR;
continue;
goto error;
}
/*
* Here is where the tree comes together. At this point, we
* have a stack of incomplete trees corresponding to
* substrings that are incomplete expressions, followed by
* a complete tree corresponding to a substring that is itself
* a complete expression, followed by the binary operator we have
if (lastParsed == OT_NONE) {
otherPtr = nodes + lastOpen - 1;
lastParsed = lastOpen;
* just parsed. The incomplete trees can each be completed by
* adding a right operand.
*
* To illustrate with an example, when we parse the expression
* "1+2*3-4" and we reach this point having just parsed the "-"
* operator, we have these incomplete trees: START, "1+", and
* "2*". Next we have the complete subexpression "3". Last is
* the "-" we've just parsed.
*
* The next step is to join our complete tree to an operator.
* The choice is governed by the precedence and associativity
* of the competing operators. If we connect it as the right
* operand of our most recent incomplete tree, we get a new
* complete tree, and we can repeat the process. The while
* loop following repeats this until precedence indicates it
* is time to join the complete tree as the left operand of
* the just parsed binary operator.
} else {
otherPtr = nodePtr - 1;
}
*
* Continuing the example, the first pass through the loop
* will join "3" to "2*"; the next pass will join "2*3" to
* "1+". Then we'll exit the loop and join "1+2*3" to "-".
* When we return to parse another lexeme, our stack of
* incomplete trees is START and "1+2*3-".
*/
while (1) {
/*
* lastParsed is "index" of item to be linked.
* otherPtr points to competing operator.
incompletePtr = nodes + incomplete;
*/
if (otherPtr->precedence < precedence) {
if (incompletePtr->precedence < precedence) {
break;
}
if (otherPtr->precedence == precedence) {
/*
* Right association rules for exponentiation.
if (incompletePtr->precedence == precedence) {
/* Right association rules for exponentiation. */
*/
if (lexeme == EXPON) {
break;
}
/*
* Special association rules for the ternary operators.
* Special association rules for the conditional operators.
* The "?" and ":" operators have equal precedence, but
* must be linked up in sensible pairs.
*/
if ((otherPtr->lexeme == QUESTION)
&& (NotOperator(lastParsed)
|| (nodes[lastParsed].lexeme != COLON))) {
if ((incompletePtr->lexeme == QUESTION)
&& (NotOperator(complete)
|| (nodes[complete].lexeme != COLON))) {
break;
}
if ((incompletePtr->lexeme == COLON)
if ((otherPtr->lexeme == COLON) && (lexeme == QUESTION)) {
&& (lexeme == QUESTION)) {
break;
}
}
/*
* We should link the lastParsed item to the otherPtr as its
* right operand. First make some syntax checks.
/* Some special syntax checks... */
*/
/* Parens must balance */
if ((otherPtr->lexeme == OPEN_PAREN)
if ((incompletePtr->lexeme == OPEN_PAREN)
&& (lexeme != CLOSE_PAREN)) {
TclNewLiteralStringObj(msg, "unbalanced open paren");
parsePtr->errorType = TCL_PARSE_MISSING_PAREN;
code = TCL_ERROR;
break;
goto error;
}
/* Right operand of "?" must be ":" */
if ((otherPtr->lexeme == QUESTION)
&& (NotOperator(lastParsed)
|| (nodes[lastParsed].lexeme != COLON))) {
if ((incompletePtr->lexeme == QUESTION)
&& (NotOperator(complete)
|| (nodes[complete].lexeme != COLON))) {
msg = Tcl_ObjPrintf(
"missing operator \":\" at %s", mark);
scanned = 0;
insertMark = 1;
code = TCL_ERROR;
break;
goto error;
}
/* Operator ":" may only be right operand of "?" */
if (IsOperator(lastParsed)
&& (nodes[lastParsed].lexeme == COLON)
&& (otherPtr->lexeme != QUESTION)) {
if (IsOperator(complete)
&& (nodes[complete].lexeme == COLON)
&& (incompletePtr->lexeme != QUESTION)) {
TclNewLiteralStringObj(msg,
"unexpected operator \":\" without preceding \"?\"");
"unexpected operator \":\" "
code = TCL_ERROR;
break;
"without preceding \"?\"");
goto error;
}
/*
* Link orphan as right operand of otherPtr.
* Attach complete tree as right operand of most recent
* incomplete tree.
*/
otherPtr->right = lastParsed;
if (lastParsed >= 0) {
nodes[lastParsed].parent = otherPtr - nodes;
incompletePtr->right = complete;
if (IsOperator(complete)) {
nodes[complete].p.parent = incomplete;
}
lastParsed = otherPtr - nodes;
if (otherPtr->lexeme == OPEN_PAREN) {
if (incompletePtr->lexeme == START) {
/*
* CLOSE_PAREN can only close one OPEN_PAREN.
* Completing the START tree indicates we're done.
* Transfer the parse tree to the caller and return.
*/
*opTreePtr = nodes;
return TCL_OK;
}
break;
}
if (otherPtr->lexeme == START) {
/*
* Don't backtrack beyond the start.
*/
/*
* With a right operand attached, last incomplete tree has
* become the complete tree. Pop it from the incomplete
* tree stack.
*/
complete = incomplete;
incomplete = incompletePtr->p.prev;
/* CLOSE_PAREN can only close one OPEN_PAREN. */
if (incompletePtr->lexeme == OPEN_PAREN) {
break;
}
otherPtr = nodes + otherPtr->parent;
}
if (code != TCL_OK) {
continue;
}
/* More syntax checks... */
/* Parens must balance. */
if (lexeme == CLOSE_PAREN) {
if (otherPtr->lexeme == START) {
if (incompletePtr->lexeme != OPEN_PAREN) {
TclNewLiteralStringObj(msg, "unbalanced close paren");
code = TCL_ERROR;
continue;
goto error;
}
}
lastParsed = OT_NONE;
lastOpen = otherPtr - nodes;
otherPtr->left++;
/* Commas must appear only in function argument lists. */
if (lexeme == COMMA) {
if ((incompletePtr->lexeme != OPEN_PAREN)
|| (incompletePtr[-1].lexeme != FUNCTION)) {
TclNewLiteralStringObj(msg,
"unexpected \",\" outside function argument list");
goto error;
}
/*
* Create no node for a CLOSE_PAREN lexeme.
*/
break;
/* TODO: explain */
incompletePtr->left++;
}
if (lexeme == COMMA) {
if ((otherPtr->lexeme != OPEN_PAREN)
|| (otherPtr[-1].lexeme != FUNCTION)) {
TclNewLiteralStringObj(msg,
"unexpected \",\" outside function argument list");
/* Operator ":" may only be right operand of "?" */
if (IsOperator(complete) && (nodes[complete].lexeme == COLON)) {
TclNewLiteralStringObj(msg,
"unexpected operator \":\" without preceding \"?\"");
code = TCL_ERROR;
continue;
}
goto error;
}
otherPtr->left++;
}
if (IsOperator(lastParsed) && (nodes[lastParsed].lexeme == COLON)) {
TclNewLiteralStringObj(msg,
"unexpected operator \":\" without preceding \"?\"");
code = TCL_ERROR;
continue;
/* Create no node for a CLOSE_PAREN lexeme. */
if (lexeme == CLOSE_PAREN) {
/* TODO: explain */
incompletePtr->left++;
break;
}
/* Link complete tree as left operand of new node. */
if (lexeme == END) {
continue;
nodePtr->lexeme = lexeme;
nodePtr->precedence = precedence;
nodePtr->right = OT_NONE;
nodePtr->left = complete;
if (IsOperator(complete)) {
nodes[complete].p.parent = nodesUsed;
}
/*
* Link orphan as left operand of new node.
* With a left operand attached and a right operand missing,
* the just-parsed binary operator is root of a new incomplete
* tree. Push it onto the stack of incomplete trees.
*/
nodePtr->lexeme = lexeme;
nodePtr->precedence = precedence;
nodePtr->right = -1;
nodePtr->p.prev = incomplete;
nodePtr->left = lastParsed;
if (lastParsed < 0) {
nodePtr->parent = nodePtr - nodes - 1;
} else {
nodePtr->parent = nodes[lastParsed].parent;
nodes[lastParsed].parent = nodePtr - nodes;
}
lastParsed = nodesUsed;
incomplete = lastParsed = nodesUsed;
nodesUsed++;
break;
}
}
} /* case BINARY */
} /* lexeme handler */
/* Advance past the just-parsed lexeme */
start += scanned;
numBytes -= scanned;
} /* main parsing loop */
}
error:
/*
* We only get here if there's been an error.
* Any errors that didn't get a suitable parsePtr->errorType,
* get recorded as syntax errors.
*/
if (parsePtr->errorType == TCL_PARSE_SUCCESS) {
parsePtr->errorType = TCL_PARSE_SYNTAX;
}
/* Free any partial parse tree we've built. */
if (code != TCL_OK && nodes != NULL) {
if (nodes != NULL) {
ckfree((char*) nodes);
}
if (code == TCL_OK) {
*opTreePtr = nodes;
} else if (interp == NULL) {
if (interp == NULL) {
/* Nowhere to report an error message, so just free it */
if (msg) {
Tcl_DecrRefCount(msg);
}
} else {
/*
* Construct the complete error message. Start with the simple
* error message, pulled from the interp result if necessary...
*/
if (msg == NULL) {
msg = Tcl_GetObjResult(interp);
}
/*
* Add a detailed quote from the bad expression, displaying and
* sometimes marking the precise location of the syntax error.
*/
Tcl_AppendPrintfToObj(msg, "\nin expression \"%s%.*s%.*s%s%s%.*s%s\"",
((start - limit) < parsePtr->string) ? "" : "...",
((start - limit) < parsePtr->string)
? (start - parsePtr->string) : limit - 3,
((start - limit) < parsePtr->string)
? parsePtr->string : start - limit + 3,
(scanned < limit) ? scanned : limit - 3, start,
(scanned < limit) ? "" : "...", insertMark ? mark : "",
(start + scanned + limit > parsePtr->end)
? parsePtr->end - (start + scanned) : limit-3,
start + scanned,
(start + scanned + limit > parsePtr->end) ? "" : "...");
/* Next, append any postscript message. */
if (post != NULL) {
Tcl_AppendToObj(msg, ";\n", -1);
Tcl_AppendObjToObj(msg, post);
Tcl_DecrRefCount(post);
}
Tcl_SetObjResult(interp, msg);
/* Finally, place context information in the errorInfo. */
numBytes = parsePtr->end - parsePtr->string;
Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
"\n (parsing expression \"%.*s%s\")",
(numBytes < limit) ? numBytes : limit - 3,
parsePtr->string, (numBytes < limit) ? "" : "..."));
}
if (code != TCL_OK && parsePtr->errorType == TCL_PARSE_SUCCESS) {
parsePtr->errorType = TCL_PARSE_SYNTAX;
}
return code;
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* GenerateTokensForLiteral --
*
|
| ︙ | | |
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
|
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
|
-
-
-
-
+
-
-
|
*----------------------------------------------------------------------
*/
static int
GenerateTokensForLiteral(
const char *script,
int numBytes,
Tcl_Obj *litList,
int nextLiteral,
Tcl_Parse *parsePtr)
{
int scanned;
const char *start = script;
Tcl_Token *destPtr;
unsigned char lexeme;
/*
* Have to reparse to get pointers into source string.
/* Have to reparse to get pointers into source string. */
*/
scanned = TclParseAllWhiteSpace(start, numBytes);
start +=scanned;
scanned = ParseLexeme(start, numBytes-scanned, &lexeme, NULL);
if (parsePtr->numTokens + 1 >= parsePtr->tokensAvailable) {
TclExpandTokenArray(parsePtr);
}
|
| ︙ | | |
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
|
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
|
-
-
-
|
* parsed expression.
*
*----------------------------------------------------------------------
*/
static void
ConvertTreeToTokens(
Tcl_Interp *interp,
const char *start,
int numBytes,
OpNode *nodes,
Tcl_Obj *litList,
Tcl_Token *tokenPtr,
Tcl_Parse *parsePtr)
{
OpNode *nodePtr = nodes;
int nextLiteral = 0;
int scanned, copied, tokenIdx;
unsigned char lexeme;
Tcl_Token *destPtr;
while (1) {
switch (NODE_TYPE & nodePtr->lexeme) {
case UNARY:
|
| ︙ | | |
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
|
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
|
-
+
-
+
|
destPtr++;
destPtr->type = TCL_TOKEN_OPERATOR;
destPtr->start = start;
destPtr->size = scanned;
destPtr->numComponents = 0;
parsePtr->numTokens += 2;
}
start +=scanned;
start += scanned;
numBytes -= scanned;
}
switch (right) {
case OT_EMPTY:
break;
case OT_LITERAL:
scanned = GenerateTokensForLiteral(start, numBytes,
litList, nextLiteral++, parsePtr);
parsePtr);
start +=scanned;
numBytes -= scanned;
break;
case OT_TOKENS:
copied = CopyTokens(tokenPtr, parsePtr);
scanned = tokenPtr->start + tokenPtr->size - start;
start +=scanned;
|
| ︙ | | |
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
|
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
|
-
+
|
} else {
tokenIdx = OT_NONE - nodePtr->right;
nodePtr->right = OT_NONE;
destPtr = parsePtr->tokenPtr + tokenIdx;
destPtr->size = start - destPtr->start;
destPtr->numComponents = parsePtr->numTokens - tokenIdx - 1;
}
nodePtr = nodes + nodePtr->parent;
nodePtr = nodes + nodePtr->p.parent;
}
break;
case BINARY:
if (nodePtr->left > OT_NONE) {
int left = nodePtr->left;
nodePtr->left = OT_NONE;
|
| ︙ | | |
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
|
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
|
-
+
|
destPtr++;
destPtr->type = TCL_TOKEN_OPERATOR;
parsePtr->numTokens += 2;
}
switch (left) {
case OT_LITERAL:
scanned = GenerateTokensForLiteral(start, numBytes,
litList, nextLiteral++, parsePtr);
parsePtr);
start +=scanned;
numBytes -= scanned;
break;
case OT_TOKENS:
copied = CopyTokens(tokenPtr, parsePtr);
scanned = tokenPtr->start + tokenPtr->size - start;
start +=scanned;
|
| ︙ | | |
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
|
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
|
-
+
|
destPtr->numComponents = 0;
}
start +=scanned;
numBytes -= scanned;
switch (right) {
case OT_LITERAL:
scanned = GenerateTokensForLiteral(start, numBytes,
litList, nextLiteral++, parsePtr);
parsePtr);
start +=scanned;
numBytes -= scanned;
break;
case OT_TOKENS:
copied = CopyTokens(tokenPtr, parsePtr);
scanned = tokenPtr->start + tokenPtr->size - start;
start +=scanned;
|
| ︙ | | |
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
|
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
|
-
+
|
if ((nodePtr->lexeme != COMMA) && (nodePtr->lexeme != COLON)) {
tokenIdx = OT_NONE - nodePtr->left;
nodePtr->left = OT_NONE;
destPtr = parsePtr->tokenPtr + tokenIdx;
destPtr->size = start - destPtr->start;
destPtr->numComponents = parsePtr->numTokens-tokenIdx-1;
}
nodePtr = nodes + nodePtr->parent;
nodePtr = nodes + nodePtr->p.parent;
}
break;
}
}
}
/*
|
| ︙ | | |
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
|
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
|
+
-
-
-
-
+
+
+
+
+
-
-
+
+
-
-
+
+
-
-
|
int numBytes, /* Number of bytes in string. If < 0, the
* string consists of all bytes up to the
* first null character. */
Tcl_Parse *parsePtr) /* Structure to fill with information about
* the parsed expression; any previous
* information in the structure is ignored. */
{
int code;
OpNode *opTree = NULL; /* Will point to the tree of operators */
Tcl_Obj *litList = Tcl_NewObj(); /* List to hold the literals */
Tcl_Obj *funcList = Tcl_NewObj(); /* List to hold the functon names*/
Tcl_Parse *exprParsePtr =
(Tcl_Parse *) TclStackAlloc(interp, sizeof(Tcl_Parse));
/* Holds the Tcl_Tokens of substitutions */
int code = ParseExpr(interp, start, numBytes, &opTree, litList,
funcList, exprParsePtr, 1 /* parseOnly */);
int errorType = exprParsePtr->errorType;
const char* term = exprParsePtr->term;
if (numBytes < 0) {
numBytes = (start ? strlen(start) : 0);
}
code = ParseExpr(interp, start, numBytes, &opTree, litList,
funcList, exprParsePtr, 1 /* parseOnly */);
Tcl_DecrRefCount(funcList);
Tcl_DecrRefCount(litList);
TclParseInit(interp, start, numBytes, parsePtr);
if (code == TCL_OK) {
ConvertTreeToTokens(interp, start, numBytes, opTree, litList,
exprParsePtr->tokenPtr, parsePtr);
ConvertTreeToTokens(start, numBytes,
opTree, exprParsePtr->tokenPtr, parsePtr);
} else {
parsePtr->term = term;
parsePtr->errorType = errorType;
parsePtr->term = exprParsePtr->term;
parsePtr->errorType = exprParsePtr->errorType;
}
Tcl_FreeParse(exprParsePtr);
TclStackFree(interp, exprParsePtr);
Tcl_DecrRefCount(funcList);
Tcl_DecrRefCount(litList);
ckfree((char *) opTree);
return code;
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | |
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
|
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
|
-
+
-
-
|
*----------------------------------------------------------------------
*/
int
TclCompileExpr(
Tcl_Interp *interp, /* Used for error reporting. */
const char *script, /* The source script to compile. */
int numBytes, /* Number of bytes in script. If < 0, the
int numBytes, /* Number of bytes in script. */
* string consists of all bytes up to the
* first null character. */
CompileEnv *envPtr) /* Holds resulting instructions. */
{
OpNode *opTree = NULL; /* Will point to the tree of operators */
Tcl_Obj *litList = Tcl_NewObj(); /* List to hold the literals */
Tcl_Obj *funcList = Tcl_NewObj(); /* List to hold the functon names*/
Tcl_Parse *parsePtr =
(Tcl_Parse *) TclStackAlloc(interp, sizeof(Tcl_Parse));
|
| ︙ | | |
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
|
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
|
-
+
|
TclEmitInstInt4(INST_INVOKE_STK4, numWords, envPtr);
}
*convertPtr = 1;
} else {
TclEmitOpcode(instruction[nodePtr->lexeme], envPtr);
*convertPtr = 0;
}
nodePtr = nodes + nodePtr->parent;
nodePtr = nodes + nodePtr->p.parent;
}
break;
case BINARY:
if (nodePtr->left > OT_NONE) {
int left = nodePtr->left;
nodePtr->left = OT_NONE;
/* TODO: reduce constant expressions */
|
| ︙ | | |
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
|
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
|
-
+
|
freePtr = jumpPtr;
jumpPtr = jumpPtr->next;
TclStackFree(interp, freePtr);
freePtr = jumpPtr;
jumpPtr = jumpPtr->next;
TclStackFree(interp, freePtr);
}
nodePtr = nodes + nodePtr->parent;
nodePtr = nodes + nodePtr->p.parent;
}
break;
}
}
}
static int
|
| ︙ | | |
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
|
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
|
-
+
|
ParseLexeme(occdPtr->operator, strlen(occdPtr->operator), &lexeme, NULL);
nodes[0].lexeme = START;
nodes[0].right = 1;
nodes[1].lexeme = lexeme;
nodes[1].left = OT_LITERAL;
nodes[1].right = OT_LITERAL;
nodes[1].parent = 0;
nodes[1].p.parent = 0;
return OpCmd(interp, nodes, objv+1);
}
int
TclSortingOpCmd(
ClientData clientData,
|
| ︙ | | |
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
|
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
|
-
+
-
+
-
+
|
nodes[2*(i-1)-1].lexeme = lexeme;
nodes[2*(i-1)-1].left = OT_LITERAL;
nodes[2*(i-1)-1].right = OT_LITERAL;
litObjv[2*(i-1)] = objv[i];
nodes[2*(i-1)].lexeme = AND;
nodes[2*(i-1)].left = lastAnd;
nodes[lastAnd].parent = 2*(i-1);
nodes[lastAnd].p.parent = 2*(i-1);
nodes[2*(i-1)].right = 2*(i-1)+1;
nodes[2*(i-1)+1].parent= 2*(i-1);
nodes[2*(i-1)+1].p.parent= 2*(i-1);
lastAnd = 2*(i-1);
}
litObjv[2*(objc-2)-1] = objv[objc-1];
nodes[2*(objc-2)-1].lexeme = lexeme;
nodes[2*(objc-2)-1].left = OT_LITERAL;
nodes[2*(objc-2)-1].right = OT_LITERAL;
nodes[0].right = lastAnd;
nodes[lastAnd].parent = 0;
nodes[lastAnd].p.parent = 0;
code = OpCmd(interp, nodes, litObjv);
TclStackFree(interp, nodes);
TclStackFree(interp, litObjv);
}
return code;
|
| ︙ | | |
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
|
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
|
-
+
-
+
-
+
-
+
-
+
|
decrMe = 1;
litObjv[0] = objv[1];
nodes[0].lexeme = START;
nodes[0].right = 1;
nodes[1].lexeme = lexeme;
nodes[1].left = OT_LITERAL;
nodes[1].right = OT_LITERAL;
nodes[1].parent = 0;
nodes[1].p.parent = 0;
} else {
if (lexeme == DIVIDE) {
litObjv[0] = Tcl_NewDoubleObj(1.0);
} else {
litObjv[0] = Tcl_NewIntObj(occdPtr->numArgs);
}
Tcl_IncrRefCount(litObjv[0]);
litObjv[1] = objv[1];
nodes[0].lexeme = START;
nodes[0].right = 1;
nodes[1].lexeme = lexeme;
nodes[1].left = OT_LITERAL;
nodes[1].right = OT_LITERAL;
nodes[1].parent = 0;
nodes[1].p.parent = 0;
}
code = OpCmd(interp, nodes, litObjv);
Tcl_DecrRefCount(litObjv[decrMe]);
return code;
} else {
OpNode *nodes = (OpNode *) TclStackAlloc(interp,
(objc-1)*sizeof(OpNode));
int i, lastOp = OT_LITERAL;
nodes[0].lexeme = START;
if (lexeme == EXPON) {
for (i=objc-2; i>0; i-- ) {
nodes[i].lexeme = lexeme;
nodes[i].left = OT_LITERAL;
nodes[i].right = lastOp;
if (lastOp >= 0) {
nodes[lastOp].parent = i;
nodes[lastOp].p.parent = i;
}
lastOp = i;
}
} else {
for (i=1; i<objc-1; i++ ) {
nodes[i].lexeme = lexeme;
nodes[i].left = lastOp;
if (lastOp >= 0) {
nodes[lastOp].parent = i;
nodes[lastOp].p.parent = i;
}
nodes[i].right = OT_LITERAL;
lastOp = i;
}
}
nodes[0].right = lastOp;
nodes[lastOp].parent = 0;
nodes[lastOp].p.parent = 0;
code = OpCmd(interp, nodes, objv+1);
TclStackFree(interp, nodes);
return code;
}
|
| ︙ | | |