Annotation of parser3/src/main/compile.y, revision 1.85

1.24      paf         1: /*
1.85    ! paf         2:   $Id: compile.y,v 1.84 2001/03/10 15:17:46 paf Exp $
1.24      paf         3: */
                      4: 
1.1       paf         5: %{
1.9       paf         6: #define YYSTYPE  Array/*<op>*/ *
                      7: #define YYPARSE_PARAM  pc
                      8: #define YYLEX_PARAM  pc
                      9: #define YYDEBUG  1
1.1       paf        10: #define YYERROR_VERBOSE
1.9       paf        11: #define yyerror(msg)  real_yyerror((parse_control *)pc, msg)
                     12: #define YYPRINT(file, type, value)  yyprint(file, type, value)
1.1       paf        13: 
                     14: #include <stdio.h>
                     15: #include <string.h>
                     16: #include <stdlib.h>
                     17: 
                     18: #include "compile_tools.h"
1.8       paf        19: #include "pa_value.h"
1.12      paf        20: #include "pa_request.h"
1.39      paf        21: #include "pa_vobject.h"
1.52      paf        22: #include "pa_vdouble.h"
1.85    ! paf        23: #include "core.h"
1.39      paf        24: 
1.85    ! paf        25: #define SELF_ELEMENT_NAME "self"
        !            26: #define USE_CONTROL_METHOD_NAME "USE"
1.1       paf        27: 
1.9       paf        28: int real_yyerror(parse_control *pc, char *s);
                     29: static void yyprint(FILE *file, int type, YYSTYPE value);
1.1       paf        30: int yylex(YYSTYPE *lvalp, void *pc);
                     31: 
                     32: 
1.8       paf        33: // local convinient inplace typecast & var
1.9       paf        34: #define PC  ((parse_control *)pc)
1.25      paf        35: #define POOL  *PC->pool
                     36: #undef NEW
                     37: #define NEW new(POOL)
1.1       paf        38: %}
                     39: 
                     40: %pure_parser
                     41: 
1.13      paf        42: %token EON
1.4       paf        43: %token STRING
1.1       paf        44: %token BOGUS
1.55      paf        45: 
1.58      paf        46: %token BAD_STRING_COMPARISON_OPERATOR
                     47: 
1.59      paf        48: %token LAND "&&"
1.58      paf        49: %token LOR "||"
1.59      paf        50: %token LXOR "##"
1.58      paf        51: 
                     52: %token NLE "<="
                     53: %token NGE ">="
                     54: %token NEQ "=="
                     55: %token NNE "!="
                     56: 
                     57: %token SLT "lt"
                     58: %token SGT "gt"
                     59: %token SLE "le"
                     60: %token SGE "ge"
                     61: %token SEQ "eq"
                     62: %token SNE "ne"
                     63: 
1.67      paf        64: %token DEF "def"
                     65: %token IN "in"
                     66: %token FEXISTS "-f"
                     67: 
1.57      paf        68: /* logical */
1.61      paf        69: %left "lt" "gt" "le" "ge"
                     70: %left "eq" "ne"
1.59      paf        71: %left '<' '>' "<=" ">=" "##"
1.57      paf        72: %left "==" "!="
                     73: %left "||"
                     74: %left "&&"
1.67      paf        75: %left "def" "in" "-f"
1.68      paf        76: %left '!'
1.57      paf        77: 
                     78: /* bitwise */
1.59      paf        79: %left '#'
1.57      paf        80: %left '&' '|'
1.68      paf        81: %left '~'
1.57      paf        82: 
1.56      paf        83: /* numerical */
1.55      paf        84: %left '-' '+'
1.57      paf        85: %left '*' '/' '%'
1.56      paf        86: %left NEG     /* negation: unary - */
1.1       paf        87: 
                     88: %%
                     89: 
1.74      paf        90: all: /* TODO: у ^execute непременно задать какой-то name, см. 'RUN' */
1.10      paf        91:        one_big_piece {
1.75      paf        92:        Method& method=*NEW Method(POOL, 
1.85    ! paf        93:                *main_method_name, 
1.81      paf        94:                0, 0, /*min, max numbered_params_count*/
1.75      paf        95:                0/*param_names*/, 0/*local_names*/, 
                     96:                $1/*parser_code*/, 0/*native_code*/);
1.85    ! paf        97:        PC->vclass->add_method(*main_method_name, method);
1.10      paf        98: }
                     99: |      methods;
                    100: 
                    101: methods: method | methods method;
                    102: one_big_piece: maybe_codes;
                    103: 
1.34      paf       104: method: control_method | code_method;
                    105: 
                    106: control_method: '@' STRING '\n' 
                    107:                                control_strings {
1.82      paf       108:        const String& command=*SLA2S($2);
1.34      paf       109:        YYSTYPE strings_code=$4;
1.37      paf       110:        if(strings_code->size()<1*2) {
                    111:                strcpy(PC->error, "@");
1.74      paf       112:                strcat(PC->error, command.cstr());
1.37      paf       113:                strcat(PC->error, " is empty");
                    114:                YYERROR;
                    115:        }
1.74      paf       116:        if(command==CLASS_NAME) {
1.76      paf       117:                if(PC->vclass!=&PC->request->root_class) { // already changed from default?
1.73      paf       118:                        strcpy(PC->error, "class already have a name '");
                    119:                        strncat(PC->error, PC->vclass->name().cstr(), 100);
                    120:                        strcat(PC->error, "'");
                    121:                        YYERROR;
                    122:                }
                    123:                if(strings_code->size()==1*2) {
                    124:                        // new class' name
1.82      paf       125:                        const String *name=SLA2S(strings_code);
1.73      paf       126:                        // creating the class
                    127:                        PC->vclass=NEW VClass(POOL);
                    128:                        PC->vclass->set_name(*name);
                    129:                        // defaulting base. may change with @BASE
1.76      paf       130:                        PC->vclass->set_base(PC->request->root_class);
1.73      paf       131:                        // append to request's classes
                    132:                        PC->request->classes_array()+=PC->vclass;
                    133:                        PC->request->classes().put(*name, PC->vclass);
                    134:                } else {
1.34      paf       135:                        strcpy(PC->error, "@"CLASS_NAME" must contain sole name");
                    136:                        YYERROR;
                    137:                }
                    138:        } else {
1.85    ! paf       139:                if(command==USE_CONTROL_METHOD_NAME) {
1.34      paf       140:                        for(int i=0; i<strings_code->size(); i+=2) {
1.82      paf       141:                                String file(*SLA2S(strings_code, i));
                    142:                                file.APPEND_CONST(".p");
                    143:                                PC->request->use(file.cstr(), 0);
1.34      paf       144:                        }
1.74      paf       145:                } else if(command==BASE_NAME) {
1.76      paf       146:                        if(PC->vclass->base()!=&PC->request->root_class) { // already changed from default?
1.73      paf       147:                                strcpy(PC->error, "there must be only one @"BASE_NAME);
                    148:                                YYERROR;
                    149:                        }
1.45      paf       150:                        if(strings_code->size()==1*2) {
1.73      paf       151:                                // TODO: преодолеть self и циклические base
1.82      paf       152:                                const String& base_name=*SLA2S(strings_code);
1.45      paf       153:                                VClass *base=static_cast<VClass *>(
                    154:                                        PC->request->classes().get(base_name));
                    155:                                if(!base) {
                    156:                                        strcpy(PC->error, base_name.cstr());
                    157:                                        strcat(PC->error, ": undefined class in @"BASE_NAME);
1.34      paf       158:                                        YYERROR;
                    159:                                }
1.45      paf       160:                                PC->vclass->set_base(*base);
                    161:                        } else {
                    162:                                strcpy(PC->error, "@"BASE_NAME" must contain sole name");
                    163:                                YYERROR;
1.34      paf       164:                        }
                    165:                } else {
1.74      paf       166:                        strcpy(PC->error, command.cstr());
1.34      paf       167:                        strcat(PC->error, ": invalid special name. valid names are "
1.85    ! paf       168:                                CLASS_NAME", "USE_CONTROL_METHOD_NAME" and "BASE_NAME);
1.34      paf       169:                        YYERROR;
                    170:                }
                    171:        }
                    172: };
1.37      paf       173: control_strings: control_string | control_strings control_string { $$=$1; P($$, $2) };
                    174: control_string: maybe_string '\n';
                    175: maybe_string: empty | STRING;
1.34      paf       176: 
                    177: code_method: '@' STRING bracketed_maybe_strings maybe_bracketed_strings maybe_comment '\n' 
1.10      paf       178:                        maybe_codes {
1.38      paf       179:        const String *name=SLA2S($2);
1.10      paf       180: 
                    181:        YYSTYPE params_names_code=$3;
1.75      paf       182:        Array *params_names=0;
                    183:        if(int size=params_names_code->size()) {
                    184:                params_names=NEW Array(POOL);
                    185:                for(int i=0; i<size; i+=2)
                    186:                        *params_names+=SLA2S(params_names_code, i);
                    187:        }
1.10      paf       188: 
                    189:        YYSTYPE locals_names_code=$4;
1.75      paf       190:        Array *locals_names=0;
                    191:        if(int size=locals_names_code->size()) {
                    192:                locals_names=NEW Array(POOL);
                    193:                for(int i=0; i<size; i+=2)
                    194:                        *locals_names+=SLA2S(locals_names_code, i);
                    195:        }
1.10      paf       196: 
1.76      paf       197:        Method& method=*NEW Method(POOL, 
                    198:                *name, 
1.81      paf       199:                0, 0/*min,max numbered_params_count*/, 
1.76      paf       200:                params_names, locals_names, 
                    201:                $7, 0);
1.34      paf       202:        PC->vclass->add_method(*name, method);
1.8       paf       203: };
1.10      paf       204: 
                    205: maybe_bracketed_strings: empty | bracketed_maybe_strings;
                    206: bracketed_maybe_strings: '[' maybe_strings ']' {$$=$2};
                    207: maybe_strings: empty | strings;
                    208: strings: STRING | strings ';' STRING { $$=$1; P($$, $3) };
                    209: 
                    210: maybe_comment: empty | STRING;
1.1       paf       211: 
                    212: /* codes */
                    213: 
1.10      paf       214: maybe_codes: empty | codes;
                    215: 
1.1       paf       216: codes: code | codes code { 
                    217:        $$=$1; 
1.9       paf       218:        P($$, $2);
1.1       paf       219: };
1.81      paf       220: code: write_string | action;
1.1       paf       221: action: get | put | with | call;
                    222: 
                    223: /* get */
                    224: 
1.64      paf       225: get: get_value {
                    226:        $$=$1; /* stack: resulting value */
1.66      paf       227:        O($$, OP_WRITE); /* value=pop; wcontext.write(value) */
1.1       paf       228: };
1.64      paf       229: get_value: '$' get_name_value { $$=$2 }
                    230: get_name_value: name_without_curly_rdive EON | name_in_curly_rdive;
1.1       paf       231: name_in_curly_rdive: '{' name_without_curly_rdive '}' { $$=$2 };
1.44      paf       232: name_without_curly_rdive: 
                    233:        name_without_curly_rdive_read 
                    234: |      name_without_curly_rdive_root
                    235: |      name_without_curly_rdive_class;
1.19      paf       236: name_without_curly_rdive_read: name_without_curly_rdive_code {
1.25      paf       237:        $$=N(POOL); 
1.22      paf       238:        Array *diving_code=$1;
1.82      paf       239:        const String *first_name=SLA2S(diving_code);
1.85    ! paf       240:        if(first_name && *first_name==SELF_ELEMENT_NAME) {
1.54      paf       241:                O($$, OP_WITH_SELF); /* stack: starting context */
1.22      paf       242:                P($$, diving_code, 
                    243:                        /* skip over... */
                    244:                        diving_code->size()>2?3/*OP_+string+get_element*/:2/*OP_+string*/);
                    245:        } else {
1.54      paf       246:                O($$, OP_WITH_READ); /* stack: starting context */
1.22      paf       247:                P($$, diving_code);
                    248:        }
                    249:        /* diving code; stack: current context */
1.1       paf       250: };
1.19      paf       251: name_without_curly_rdive_root: ':' name_without_curly_rdive_code {
1.25      paf       252:        $$=N(POOL); 
1.54      paf       253:        O($$, OP_WITH_ROOT); /* stack: starting context */
1.19      paf       254:        P($$, $2); /* diving code; stack: current context */
                    255: };
1.44      paf       256: name_without_curly_rdive_class: class_prefix name_without_curly_rdive_code { $$=$1; P($$, $2) };
1.19      paf       257: name_without_curly_rdive_code: name_advance2 | name_path name_advance2 { $$=$1; P($$, $2) };
1.1       paf       258: 
                    259: /* put */
                    260: 
1.81      paf       261: put: '$' name_expr_wdive construct {
1.20      paf       262:        $$=$2; /* stack: context,name */
1.52      paf       263:        P($$, $3); /* stack: context,name,constructor_value */
1.20      paf       264: };
1.44      paf       265: name_expr_wdive: 
                    266:        name_expr_wdive_write
                    267: |      name_expr_wdive_root
                    268: |      name_expr_wdive_class;
1.28      paf       269: name_expr_wdive_write: name_expr_dive_code {
1.44      paf       270:        $$=N(POOL);
1.23      paf       271:        Array *diving_code=$1;
1.82      paf       272:        const String *first_name=SLA2S(diving_code);
1.85    ! paf       273:        if(first_name && *first_name==SELF_ELEMENT_NAME) {
1.54      paf       274:                O($$, OP_WITH_SELF); /* stack: starting context */
1.23      paf       275:                P($$, diving_code, 
                    276:                        /* skip over... */
                    277:                        diving_code->size()>2?3/*OP_+string+get_element*/:2/*OP_+string*/);
                    278:        } else {
1.54      paf       279:                O($$, OP_WITH_WRITE); /* stack: starting context */
1.23      paf       280:                P($$, diving_code);
                    281:        }
                    282:        /* diving code; stack: current context */
1.20      paf       283: };
1.28      paf       284: name_expr_wdive_root: ':' name_expr_dive_code {
1.25      paf       285:        $$=N(POOL); 
1.54      paf       286:        O($$, OP_WITH_ROOT); /* stack: starting context */
1.9       paf       287:        P($$, $2); /* diving code; stack: context,name */
1.1       paf       288: };
1.44      paf       289: name_expr_wdive_class: class_prefix name_expr_dive_code { $$=$1; P($$, $2) };
1.20      paf       290: 
1.81      paf       291: construct: construct_by_code | construct_by_expr;
                    292: construct_by_code: '[' any_constructor_code_value ']' {
                    293:        $$=$2; /* stack: context, name, value */
                    294:        O($$, OP_CONSTRUCT_VALUE); /* value=pop; name=pop; context=pop; construct(context,name,value) */
                    295: }
                    296: ;
                    297: construct_by_expr: '(' any_expr ')' { 
                    298:        $$=$2; /* stack: context, name, value */
                    299:        O($$, OP_CONSTRUCT_EXPR); /* value=pop; name=pop; context=pop; construct(context,name,value) */
                    300: }
1.52      paf       301: ;
1.55      paf       302: any_constructor_code_value: 
                    303:        empty_string_value /* optimized $var[] case */
1.52      paf       304: |      STRING /* optimized $var[STRING] case */
1.55      paf       305: |      constructor_code_value /* $var[something complex] */
1.1       paf       306: ;
1.55      paf       307: constructor_code_value: constructor_code {
1.25      paf       308:        $$=N(POOL); 
1.54      paf       309:        O($$, OP_CREATE_EWPOOL); /* stack: empty write context */
1.69      paf       310:        P($$, $1); /* some code that writes to that context */
1.54      paf       311:        O($$, OP_REDUCE_EWPOOL); /* context=pop; stack: context.value() */
1.1       paf       312: };
1.55      paf       313: constructor_code: codes__excluding_sole_str_literal;
1.27      paf       314: codes__excluding_sole_str_literal: action | code codes { $$=$1; P($$, $2) };
                    315: 
1.1       paf       316: /* call */
                    317: 
1.66      paf       318: call: call_value {
                    319:        $$=$1; /* stack: value */
                    320:        O($$, OP_WRITE); /* value=pop; wcontext.write(value) */
                    321: };
                    322: call_value: '^' call_name store_params EON { /* ^field.$method{vasya} */
1.42      paf       323:        $$=$2; /* with_xxx,diving code; stack: context,method_junction */
1.54      paf       324:        O($$, OP_GET_METHOD_FRAME); /* stack: context,method_frame */
1.9       paf       325:        P($$, $3); /* filling method_frame.store_params */
1.66      paf       326:        O($$, OP_CALL); /* method_frame=pop; ncontext=pop; call(ncontext,method_frame) stack: value */
1.1       paf       327: };
                    328: 
1.43      paf       329: call_name: name_without_curly_rdive;
1.38      paf       330: 
1.9       paf       331: store_params: store_param | store_params store_param { $$=$1; P($$, $2) };
1.69      paf       332: store_param: 
                    333:        store_square_param
                    334: |      store_round_param
                    335: |      store_curly_param
1.31      paf       336: ;
1.69      paf       337: store_square_param: '[' store_code_param_parts ']' {$$=$2};
                    338: store_round_param: '(' store_expr_param_parts ')' {$$=$2};
1.10      paf       339: store_curly_param: '{' maybe_codes '}' {
1.25      paf       340:        $$=N(POOL); 
1.29      paf       341:        PCA($$, $2);
1.1       paf       342: };
1.69      paf       343: store_code_param_parts:
                    344:        store_code_param_part
                    345: |      store_code_param_parts ';' store_code_param_part { $$=$1; P($$, $3) }
                    346: ;
                    347: store_expr_param_parts:
                    348:        store_expr_param_part
                    349: |      store_expr_param_parts ';' store_expr_param_part { $$=$1; P($$, $3) }
                    350: ;
                    351: store_code_param_part: 
1.78      paf       352:        empty /* optimized [] case */
                    353: |      STRING { /* optimized [STRING] case */
1.32      paf       354:        $$=$1;
1.54      paf       355:        O($$, OP_STORE_PARAM);
1.32      paf       356: }
1.78      paf       357: |      constructor_code_value { /* [something complex] */
1.32      paf       358:        $$=$1;
1.77      paf       359:        O($$, OP_STORE_PARAM);
1.32      paf       360: }
                    361: ;
1.69      paf       362: store_expr_param_part: write_expr_value {
                    363:        $$=N(POOL); 
                    364:        PCA($$, $1);
                    365: };
                    366: write_expr_value: any_expr {
                    367:        $$=$1;
                    368:        O($$, OP_WRITE);
                    369: };
1.1       paf       370: 
                    371: /* name */
                    372: 
1.20      paf       373: name_expr_dive_code: name_expr_value | name_path name_expr_value { $$=$1; P($$, $2) };
1.1       paf       374: 
1.9       paf       375: name_path: name_step | name_path name_step { $$=$1; P($$, $2) };
1.1       paf       376: name_step: name_advance1 '.';
                    377: name_advance1: name_expr_value {
                    378:        /* stack: context */
                    379:        $$=$1; /* stack: context,name */
1.54      paf       380:        O($$, OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */
1.1       paf       381: };
                    382: name_advance2: name_expr_value {
                    383:        /* stack: context */
                    384:        $$=$1; /* stack: context,name */
1.54      paf       385:        O($$, OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */
1.1       paf       386: }
1.4       paf       387: |      STRING BOGUS
1.1       paf       388: ;
                    389: name_expr_value: 
1.4       paf       390:        STRING /* subname_is_const */
1.1       paf       391: |      name_expr_subvar_value /* $subname_is_var_value */
                    392: |      name_expr_with_subvar_value /* xxx$part_of_subname_is_var_value[$...] */
                    393: ;
                    394: name_expr_subvar_value: '$' subvar_ref_name_rdive {
                    395:        $$=$2;
1.54      paf       396:        O($$, OP_GET_ELEMENT);
1.1       paf       397: };
1.4       paf       398: name_expr_with_subvar_value: STRING subvar_get_writes {
1.25      paf       399:        $$=N(POOL); 
1.54      paf       400:        O($$, OP_CREATE_EWPOOL);
1.9       paf       401:        P($$, $1);
1.54      paf       402:        O($$, OP_WRITE);
1.9       paf       403:        P($$, $2);
1.54      paf       404:        O($$, OP_REDUCE_EWPOOL);
1.1       paf       405: };
1.18      paf       406: subvar_ref_name_rdive: subvar_ref_name_rdive_read | subvar_ref_name_rdive_root;
                    407: subvar_ref_name_rdive_read: STRING {
1.25      paf       408:        $$=N(POOL); 
1.54      paf       409:        O($$, OP_WITH_READ);
1.9       paf       410:        P($$, $1);
1.1       paf       411: };
1.18      paf       412: subvar_ref_name_rdive_root: ':' STRING {
1.25      paf       413:        $$=N(POOL); 
1.54      paf       414:        O($$, OP_WITH_ROOT);
1.18      paf       415:        P($$, $2);
                    416: };
1.9       paf       417: subvar_get_writes: subvar__get_write | subvar_get_writes subvar__get_write { $$=$1; P($$, $2) };
1.1       paf       418: subvar__get_write: '$' subvar_ref_name_rdive {
                    419:        $$=$2;
1.54      paf       420:        O($$, OP_GET_ELEMENT__WRITE);
1.42      paf       421: };
                    422: 
1.44      paf       423: class_prefix: STRING ':' {
1.72      paf       424:        $$=$1; // stack: class name string
                    425:        O($$, OP_GET_CLASS);
1.1       paf       426: };
                    427: 
                    428: 
                    429: /* with */
                    430: 
                    431: with: '$' name_without_curly_rdive '{' codes '}' {
                    432:        $$=$2;
1.54      paf       433:        O($$, OP_CREATE_RWPOOL);
1.9       paf       434:        P($$, $4);
1.54      paf       435:        O($$, OP_REDUCE_RWPOOL);
                    436:        O($$, OP_WRITE);
1.1       paf       437: };
1.53      paf       438: 
1.56      paf       439: /* expr */
1.53      paf       440: 
1.56      paf       441: any_expr:
1.55      paf       442:        empty_double_value /* optimized $var() case */
1.81      paf       443: |      expr_value /* $var(something) */
1.53      paf       444: ;
1.81      paf       445: expr_value: expr {
                    446:        if(($$=$1)->size()==2) // only one string literal in there?
1.62      paf       447:                change_string_literal_to_double_literal($$); // make that string literal Double
                    448: };
1.56      paf       449: expr: 
1.62      paf       450:        STRING
1.64      paf       451: |      get_value
1.66      paf       452: |      call_value
1.64      paf       453: |      '"' string_inside_quotes_value '"' { $$ = $2; }
1.60      paf       454: |      '(' expr ')' { $$ = $2; }
                    455: /* stack: operand // stack: @operand */
                    456: |      '-' expr %prec NEG { $$=$2;  O($$, OP_NEG) }
1.68      paf       457: |      '~' expr { $$=$2;        O($$, OP_INV) }
                    458: |      '!' expr { $$=$2;  O($$, OP_NOT) }
                    459: |      "def" expr { $$=$2;  O($$, OP_DEF) }
                    460: |      "in" expr { $$=$2;  O($$, OP_IN) }
                    461: |      "-f" expr { $$=$2;  O($$, OP_FEXISTS) }
1.60      paf       462: /* stack: a,b // stack: a@b */
                    463: |      expr '-' expr { $$=$1;  P($$, $3);  O($$, OP_SUB) }
                    464: |      expr '+' expr { $$=$1;  P($$, $3);  O($$, OP_ADD) }
                    465: |      expr '*' expr { $$=$1;  P($$, $3);  O($$, OP_MUL) }
                    466: |      expr '/' expr { $$=$1;  P($$, $3);  O($$, OP_DIV) }
                    467: |      expr '%' expr { $$=$1;  P($$, $3);  O($$, OP_MOD) }
                    468: |      expr '&' expr { $$=$1;  P($$, $3);  O($$, OP_BIN_AND) }
                    469: |      expr '|' expr { $$=$1;  P($$, $3);  O($$, OP_BIN_OR) }
                    470: |      expr '#' expr { $$=$1;  P($$, $3);  O($$, OP_BIN_XOR) }
                    471: |      expr "&&" expr { $$=$1;  P($$, $3);  O($$, OP_LOG_AND) }
                    472: |      expr "||" expr { $$=$1;  P($$, $3);  O($$, OP_LOG_OR) }
                    473: |      expr "##" expr { $$=$1;  P($$, $3);  O($$, OP_LOG_XOR) }
                    474: |      expr '<' expr { $$=$1;  P($$, $3);  O($$, OP_NUM_LT) }
                    475: |      expr '>' expr { $$=$1;  P($$, $3);  O($$, OP_NUM_GT) }
                    476: |      expr "<=" expr { $$=$1;  P($$, $3);  O($$, OP_NUM_LE) }
                    477: |      expr ">=" expr { $$=$1;  P($$, $3);  O($$, OP_NUM_GE) }
                    478: |      expr "==" expr { $$=$1;  P($$, $3);  O($$, OP_NUM_EQ) }
                    479: |      expr "!=" expr { $$=$1;  P($$, $3);  O($$, OP_NUM_NE) }
1.61      paf       480: |      expr "lt" expr { $$=$1;  P($$, $3);  O($$, OP_STR_LT) }
                    481: |      expr "gt" expr { $$=$1;  P($$, $3);  O($$, OP_STR_GT) }
                    482: |      expr "le" expr { $$=$1;  P($$, $3);  O($$, OP_STR_LE) }
                    483: |      expr "ge" expr { $$=$1;  P($$, $3);  O($$, OP_STR_GE) }
                    484: |      expr "eq" expr { $$=$1;  P($$, $3);  O($$, OP_STR_EQ) }
                    485: |      expr "ne" expr { $$=$1;  P($$, $3);  O($$, OP_STR_NE) }
1.56      paf       486: ;
1.55      paf       487: 
1.65      paf       488: string_inside_quotes_value: maybe_codes {
1.64      paf       489:        $$=N(POOL);
                    490:        O($$, OP_CREATE_SWPOOL); /* stack: empty write context */
1.69      paf       491:        P($$, $1); /* some code that writes to that context */
1.64      paf       492:        O($$, OP_REDUCE_SWPOOL); /* context=pop; stack: context.get_string() */
1.53      paf       493: };
1.1       paf       494: 
1.27      paf       495: /* basics */
1.1       paf       496: 
1.81      paf       497: write_string: STRING {
1.84      paf       498:        // optimized from OP_STRING+OP_WRITE to OP_STRING__WRITE
                    499:        change_string_literal_to_write_string_literal($$=$1)
1.54      paf       500: };
                    501: 
1.79      paf       502: empty_double_value: /* empty */ { $$=VL(NEW VDouble(POOL, 0)) };
1.81      paf       503: empty_string_value: /* empty */ { $$=VL(NEW VString(POOL)) };
1.25      paf       504: empty: /* empty */ { $$=N(POOL) };
1.1       paf       505: 
                    506: %%
                    507: 
                    508: /*
                    509:        000$111(2222)00 
                    510:                000$111{3333}00
1.9       paf       511:        $,^: push,=0
1.1       paf       512:        1:( { break=pop
                    513:        2:( )  pop
                    514:        3:{ }  pop
                    515: 
                    516:        000^111(2222)4444{33333}4000
1.9       paf       517:        $,^: push,=0
1.1       paf       518:        1:( { break=pop
                    519:        2:( )=4
                    520:        3:{ }=4
                    521:                4:[^({]=pop
                    522: */
                    523: 
                    524: int yylex(YYSTYPE *lvalp, void *pc) {
                    525:        #define lexical_brackets_nestage PC->brackets_nestages[PC->sp]
1.48      paf       526:        #define RC {result=c; goto break2; }
1.1       paf       527: 
                    528:     register int c;
                    529:     int result;
                    530:        
                    531:        if(PC->pending_state) {
                    532:                result=PC->pending_state;
                    533:                PC->pending_state=0;
                    534:                return result;
                    535:        }
                    536:        
1.9       paf       537:        char *begin=PC->source;
                    538:        char *end;
                    539:        int begin_line=PC->line;
1.67      paf       540:        int skip_analized=0;
1.50      paf       541:        while(true) {
1.9       paf       542:                c=*(end=(PC->source++));
1.1       paf       543: 
1.4       paf       544:                if(c=='\n') {
1.1       paf       545:                        PC->line++;
1.8       paf       546:                        PC->col=0;
1.10      paf       547:                } else
1.4       paf       548:                        PC->col++;
1.73      paf       549: 
                    550:                // todo: # in 0+1 column comment
1.1       paf       551: 
1.48      paf       552:                // escaping: ^^ ^$ ^; ^) ^} ^( ^{ ^"
                    553:                if(c=='^') 
                    554:                        switch(*PC->source) {
                    555:                        case '^': case '$': case ';':
                    556:                        case '[': case ']':
                    557:                        case '{': case '}':
                    558:                        case '"':
1.40      paf       559:                                if(end!=begin) {
                    560:                                        // append piece till ^
                    561:                                        PC->string->APPEND(begin, end-begin, PC->file, begin_line);
                    562:                                }
1.63      paf       563:                                // reset piece 'begin' position & line
1.40      paf       564:                                begin=PC->source; // ^
1.9       paf       565:                                begin_line=PC->line;
1.40      paf       566:                                // skip over ^ and _
1.50      paf       567:                                PC->source++;  PC->col++;
1.40      paf       568:                                // skip analysis = forced literal
1.1       paf       569:                                continue;
                    570:                        }
                    571:                switch(PC->ls) {
1.10      paf       572: 
                    573:                // USER'S = NOT OURS
1.1       paf       574:                case LS_USER:
1.48      paf       575:                        switch(c) {
                    576:                        case '$':
1.10      paf       577:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       578:                                RC;
                    579:                        case '^':
                    580:                                push_LS(PC, LS_METHOD_NAME);
                    581:                                RC;
                    582:                        case '@':
                    583:                                if(PC->col==0+1) {
                    584:                                        push_LS(PC, LS_DEF_NAME);
                    585:                                        RC;
                    586:                                }
                    587:                                break;
1.1       paf       588:                        }
1.48      paf       589:                        break;
                    590:                        
                    591:                // STRING IN EXPRESSION
                    592:                case LS_EXPRESSION_STRING:
                    593:                        switch(c) {
                    594:                        case '"':
                    595:                                pop_LS(PC); //"abc".
                    596:                                RC;
                    597:                        case '$':
                    598:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
                    599:                                RC;
                    600:                        case '^':
1.10      paf       601:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       602:                                RC;
1.10      paf       603:                        }
                    604:                        break;
                    605: 
                    606:                // METHOD DEFINITION
                    607:                case LS_DEF_NAME:
1.48      paf       608:                        switch(c) {
                    609:                        case '[':
1.10      paf       610:                                PC->ls=LS_DEF_PARAMS;
1.48      paf       611:                                RC;
                    612:                        case '\n':
                    613:                                PC->ls=LS_DEF_SPECIAL_BODY;
                    614:                                RC;
1.10      paf       615:                        }
                    616:                        break;
1.48      paf       617: 
1.10      paf       618:                case LS_DEF_PARAMS:
1.48      paf       619:                        switch(c) {
                    620:                        case ';':
                    621:                                RC;
                    622:                        case ']':
1.10      paf       623:                                PC->ls=*PC->source=='['?LS_DEF_LOCALS:LS_DEF_COMMENT;
1.48      paf       624:                                RC;
1.49      paf       625:                        case '\n': // wrong. bailing out
1.10      paf       626:                                pop_LS(PC);
1.48      paf       627:                                RC;
1.10      paf       628:                        }
                    629:                        break;
1.48      paf       630: 
1.10      paf       631:                case LS_DEF_LOCALS:
1.48      paf       632:                        switch(c) {
                    633:                        case '[':
                    634:                        case ';':
                    635:                                RC;
                    636:                        case ']':
1.10      paf       637:                                PC->ls=LS_DEF_COMMENT;
1.48      paf       638:                                RC;
                    639:                        case '\n': // wrong. bailing out
1.10      paf       640:                                pop_LS(PC);
1.48      paf       641:                                RC;
1.10      paf       642:                        }
                    643:                        break;
1.48      paf       644: 
1.10      paf       645:                case LS_DEF_COMMENT:
                    646:                        if(c=='\n') {
                    647:                                pop_LS(PC);
1.48      paf       648:                                RC;
1.37      paf       649:                        }
                    650:                        break;
                    651: 
1.48      paf       652:                case LS_DEF_SPECIAL_BODY:
1.37      paf       653:                        if(c=='\n') {
1.48      paf       654:                                switch(*PC->source) {
                    655:                                case '@': case 0: // end of special_code
1.37      paf       656:                                        pop_LS(PC);
1.48      paf       657:                                        break;
                    658:                                }
                    659:                                RC;
                    660:                        }
                    661:                        break;
                    662: 
                    663:                // (EXPRESSION)
1.69      paf       664:                case LS_VAR_ROUND:
                    665:                case LS_METHOD_ROUND:
1.48      paf       666:                        switch(c) {
                    667:                        case ')':
                    668:                                if(--lexical_brackets_nestage==0)
1.69      paf       669:                                        if(PC->ls==LS_METHOD_ROUND) // method round param ended
                    670:                                                PC->ls=LS_METHOD_AFTER; // look for method end
                    671:                                        else // PC->ls==LS_VAR_ROUND // variable constructor ended
                    672:                                                pop_LS(PC); // return to normal life
1.48      paf       673:                                RC;
                    674:                        case '$':
1.69      paf       675:                                push_LS(PC, LS_EXPRESSION_VAR_NAME);                            
1.48      paf       676:                                RC;
                    677:                        case '^':
                    678:                                push_LS(PC, LS_METHOD_NAME);
                    679:                                RC;
                    680:                        case '(':
                    681:                                lexical_brackets_nestage++;
                    682:                                RC;
1.67      paf       683:                        case '-':
                    684:                                if(*PC->source=='f') { // -f
                    685:                                        skip_analized=1;
                    686:                                        result=FEXISTS;
                    687:                                } else
                    688:                                        result=c;
                    689:                                goto break2;
                    690:                        case '+': case '*': case '/': case '%': 
1.58      paf       691:                        case '~':
1.48      paf       692:                        case ';':
                    693:                                RC;
1.59      paf       694:                        case '&': case '|':  case '#':
1.58      paf       695:                                if(*PC->source==c) { // && ||
1.59      paf       696:                                        result=c=='#'?LXOR:c=='&'?LAND:LOR;
1.67      paf       697:                                        skip_analized=1;
1.58      paf       698:                                } else
                    699:                                        result=c;
                    700:                                goto break2;
                    701:                        case '<': case '>': case '=': case '!': 
                    702:                                if(*PC->source=='=') { // <= >= == !=
1.67      paf       703:                                        skip_analized=1;
1.58      paf       704:                                        switch(c) {
                    705:                                        case '<': result=NLE; break;
                    706:                                        case '>': result=NGE; break;
                    707:                                        case '=': result=NEQ; break;
                    708:                                        case '!': result=NNE; break;
                    709:                                        }
                    710:                                } else
                    711:                                        result=c;
                    712:                                goto break2;
1.48      paf       713:                        case '"':
                    714:                                push_LS(PC, LS_EXPRESSION_STRING);
                    715:                                RC;
1.50      paf       716:                        case 'l': case 'g': case 'e': case 'n':
1.51      paf       717:                                if(end==begin) // right after whitespace
1.58      paf       718:                                        switch(*PC->source) {
1.51      paf       719: //                                     case '?': // ok [and bad cases, yacc would bark at them]
                    720:                                        case 't': // lt gt [et nt]
1.62      paf       721:                                                result=c=='l'?SLT:c=='g'?SGT:BAD_STRING_COMPARISON_OPERATOR;
1.67      paf       722:                                                skip_analized=1;
1.58      paf       723:                                                goto break2;
1.51      paf       724:                                        case 'e': // le ge ne [ee]
1.58      paf       725:                                                result=c=='l'?SLE:c=='g'?SGE:c=='n'?SNE:BAD_STRING_COMPARISON_OPERATOR;
1.67      paf       726:                                                skip_analized=1;
1.58      paf       727:                                                goto break2;
1.51      paf       728:                                        case 'q': // eq [lq gq nq]
1.58      paf       729:                                                result=c=='e'?SEQ:BAD_STRING_COMPARISON_OPERATOR;
1.67      paf       730:                                                skip_analized=1;
                    731:                                                goto break2;
                    732:                                        }
                    733:                                break;
                    734:                        case 'i':
                    735:                                if(end==begin) // right after whitespace
                    736:                                        if(PC->source[0]=='n') { // in
                    737:                                                skip_analized=1;
                    738:                                                result=IN;
                    739:                                                goto break2;
                    740:                                        }
                    741:                                break;
                    742:                        case 'd':
                    743:                                if(end==begin) // right after whitespace
                    744:                                        if(PC->source[0]=='e' && PC->source[1]=='f') { // def
                    745:                                                skip_analized=2;
                    746:                                                result=DEF;
1.58      paf       747:                                                goto break2;
1.50      paf       748:                                        }
1.48      paf       749:                                break;
                    750:                        case ' ': case '\t': case '\n':
1.63      paf       751:                                if(end!=begin) { // there were a string after previous operator?
                    752:                                        result=0; // return that string
                    753:                                        goto break2;
1.48      paf       754:                                }
1.63      paf       755:                                // that's a leading|traling space or after-operator-space
                    756:                                // ignoring it
                    757:                                // reset piece 'begin' position & line
1.58      paf       758:                                begin=PC->source; // after whitespace char
1.48      paf       759:                                begin_line=PC->line;
                    760:                                continue;
1.1       paf       761:                        }
                    762:                        break;
                    763: 
1.10      paf       764:                // VARIABLE GET/PUT/WITH
1.1       paf       765:                case LS_VAR_NAME_SIMPLE:
1.69      paf       766:                case LS_EXPRESSION_VAR_NAME:
                    767:                        if(PC->ls==LS_EXPRESSION_VAR_NAME) {
1.56      paf       768:                                // name in expr ends also before binary operators 
1.48      paf       769:                                switch(c) {
                    770:                                case '+': case '-': case '*': case '/': case '%': 
                    771:                                case '&': case '|': 
1.83      paf       772:                                case '=': case '!':
1.48      paf       773:                                        pop_LS(PC);
                    774:                                        PC->source--;  if(--PC->col<0) { PC->line--;  PC->col=-1; }
                    775:                                        result=EON;
                    776:                                        goto break2;
                    777:                                }
                    778:                        }
                    779:                        switch(c) {
                    780:                        case 0:
                    781:                        case ' ': case '\t': case '\n':
                    782:                        case ';':
1.64      paf       783:                        case ']': case '}': case ')': case '"':
1.83      paf       784:                        case '<': case '>':  // these stand for HTML brackets and expression binary ops
1.1       paf       785:                                pop_LS(PC);
1.32      paf       786:                                PC->source--;  if(--PC->col<0) { PC->line--;  PC->col=-1; }
1.13      paf       787:                                result=EON;
1.1       paf       788:                                goto break2;
1.48      paf       789:                        case '[':
                    790:                                PC->ls=LS_VAR_SQUARE;
1.1       paf       791:                                lexical_brackets_nestage=1;
1.48      paf       792:                                RC;
                    793:                        case '{':
                    794:                                if(begin==end) { // ${name}, no need of EON, switching LS
                    795:                                        PC->ls=LS_VAR_NAME_CURLY; 
                    796:                                } else {
                    797:                                        PC->ls=LS_VAR_CURLY;
                    798:                                        lexical_brackets_nestage=1;
                    799:                                }
1.69      paf       800: 
1.48      paf       801:                                RC;
                    802:                        case '(':
1.69      paf       803:                                PC->ls=LS_VAR_ROUND;
1.1       paf       804:                                lexical_brackets_nestage=1;
1.48      paf       805:                                RC;
                    806:                        case '.': // name part delim
                    807:                        case '$': // name part subvar
                    808:                        case ':': // ':name' or 'class:name'
                    809:                                RC;
1.1       paf       810:                        }
                    811:                        break;
1.48      paf       812: 
1.1       paf       813:                case LS_VAR_NAME_CURLY:
1.48      paf       814:                        switch(c) {
                    815:                        case '}': // ${name} finished, restoring LS
1.1       paf       816:                                pop_LS(PC);
1.48      paf       817:                                RC;
                    818:                        case '.': // name part delim
                    819:                        case '$': // name part subvar
                    820:                        case ':': // ':name' or 'class:name'
                    821:                                RC;
1.1       paf       822:                        }
                    823:                        break;
1.48      paf       824: 
                    825:                case LS_VAR_SQUARE:
                    826:                        switch(c) {
                    827:                        case '$':
1.10      paf       828:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       829:                                RC;
                    830:                        case '^':
1.10      paf       831:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       832:                                RC;
                    833:                        case ']':
1.1       paf       834:                                if(--lexical_brackets_nestage==0) {
                    835:                                        pop_LS(PC);
1.48      paf       836:                                        RC;
1.1       paf       837:                                }
1.48      paf       838:                                break;
                    839:                        case ';': // operator_or_fmt;value delim
                    840:                                RC;
                    841:                        case '[':
                    842:                                lexical_brackets_nestage++;
                    843:                                break;
1.1       paf       844:                        }
                    845:                        break;
1.48      paf       846: 
1.1       paf       847:                case LS_VAR_CURLY:
1.48      paf       848:                        switch(c) {
                    849:                        case '$':
1.10      paf       850:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       851:                                RC;
                    852:                        case '^':
1.10      paf       853:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       854:                                RC;
                    855:                        case '}':
1.1       paf       856:                                if(--lexical_brackets_nestage==0) {
                    857:                                        pop_LS(PC);
1.48      paf       858:                                        RC;
1.1       paf       859:                                }
1.48      paf       860:                                break;
                    861:                        case '{':
1.1       paf       862:                                lexical_brackets_nestage++;
1.48      paf       863:                                break;
                    864:                        }
1.1       paf       865:                        break;
                    866: 
1.10      paf       867:                // METHOD CALL
1.1       paf       868:                case LS_METHOD_NAME:
1.48      paf       869:                        switch(c) {
                    870:                        case '[':
                    871:                                PC->ls=LS_METHOD_SQUARE;
1.1       paf       872:                                lexical_brackets_nestage=1;
1.48      paf       873:                                RC;
                    874:                        case '{':
1.1       paf       875:                                PC->ls=LS_METHOD_CURLY;
                    876:                                lexical_brackets_nestage=1;
1.48      paf       877:                                RC;
1.69      paf       878:                        case '(':
                    879:                                PC->ls=LS_METHOD_ROUND;
                    880:                                lexical_brackets_nestage=1;
                    881:                                RC;
1.48      paf       882:                        case '.': // name part delim 
                    883:                        case '$': // name part subvar
                    884:                        case ':': // ':name' or 'class:name'
                    885:                                RC;
1.1       paf       886:                        }
                    887:                        break;
1.48      paf       888: 
                    889:                case LS_METHOD_SQUARE:
                    890:                        switch(c) {
                    891:                        case '$':
1.10      paf       892:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       893:                                RC;
                    894:                        case '^':
1.10      paf       895:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       896:                                RC;
                    897:                        case ';': // param delim
                    898:                                RC;
                    899:                        case ']':
1.1       paf       900:                                if(--lexical_brackets_nestage==0) {
                    901:                                        PC->ls=LS_METHOD_AFTER;
1.48      paf       902:                                        RC;
1.1       paf       903:                                }
1.48      paf       904:                                break;
                    905:                        case '[':
1.1       paf       906:                                lexical_brackets_nestage++;
1.48      paf       907:                                break;
                    908:                        }
1.1       paf       909:                        break;
1.48      paf       910: 
1.1       paf       911:                case LS_METHOD_CURLY:
1.48      paf       912:                        switch(c) {
                    913:                        case '$':
1.10      paf       914:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       915:                                RC;
                    916:                        case '^':
1.10      paf       917:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       918:                                RC;
                    919:                        case '}':
1.1       paf       920:                                if(--lexical_brackets_nestage==0) {
                    921:                                        PC->ls=LS_METHOD_AFTER;
1.48      paf       922:                                        RC;
1.1       paf       923:                                }
1.48      paf       924:                                break;
                    925:                        case '{':
1.1       paf       926:                                lexical_brackets_nestage++;
1.48      paf       927:                                break;
                    928:                        }
1.1       paf       929:                        break;
1.48      paf       930: 
1.1       paf       931:                case LS_METHOD_AFTER:
1.69      paf       932:                        if(c=='[') {/* ][ }[ )[ */
1.48      paf       933:                                PC->ls=LS_METHOD_SQUARE;
1.1       paf       934:                                lexical_brackets_nestage=1;
1.48      paf       935:                                RC;
1.1       paf       936:                        }                                          
1.69      paf       937:                        if(c=='{') {/* ]{ }{ ){ */
1.1       paf       938:                                PC->ls=LS_METHOD_CURLY;
1.69      paf       939:                                lexical_brackets_nestage=1;
                    940:                                RC;
                    941:                        }                                          
                    942:                        if(c=='(') {/* ]( }( )( */
                    943:                                PC->ls=LS_METHOD_ROUND;
1.1       paf       944:                                lexical_brackets_nestage=1;
1.48      paf       945:                                RC;
1.1       paf       946:                        }                                          
                    947:                        pop_LS(PC);
1.32      paf       948:                        PC->source--;  if(--PC->col<0) { PC->line--;  PC->col=-1; }
1.13      paf       949:                        result=EON;
1.1       paf       950:                        goto break2;
                    951:                }
1.9       paf       952:                if(c==0) {
1.1       paf       953:                        result=-1;
                    954:                        break;
                    955:                }
                    956:        }
                    957: 
                    958: break2:
1.59      paf       959:        if(end!=begin) { // there is last piece?
                    960:                if((c=='@' || c==0) && end[-1]=='\n') { // we are before LS_DEF_NAME or EOF?
                    961:                        // strip last \n
1.10      paf       962:                        end--;
1.59      paf       963:                }
                    964:                if(end!=begin) { // last piece still alive?
                    965:                        // append it
1.30      paf       966:                        PC->string->APPEND(begin, end-begin, PC->file, begin_line/*, start_col*/);
                    967:                }
1.59      paf       968:        }
                    969:        if(PC->string->size()) { // something accumulated?
1.17      paf       970:                // create STRING value: array of OP_VALUE+vstring
1.55      paf       971:                *lvalp=VL(NEW VString(*PC->string));
1.10      paf       972:                // new pieces storage
1.25      paf       973:                PC->string=NEW String(POOL);
1.58      paf       974:                // make current result be pending for next call, return STRING for now
                    975:                PC->pending_state=result;  result=STRING;
                    976:        }
1.67      paf       977:        if(skip_analized) {
                    978:                PC->source+=skip_analized;  PC->col+=skip_analized;
1.1       paf       979:        }
1.58      paf       980:        return result;
1.1       paf       981: }
                    982: 
1.9       paf       983: int real_yyerror(parse_control *pc, char *s)  /* Called by yyparse on error */
1.1       paf       984:      {
1.16      paf       985:        //fprintf(stderr, "[%s]\n", s);
1.6       paf       986: 
1.46      paf       987:           strncpy(pc->error, s, MAX_STRING); // TODO: перепроверить с треклятым последним байтом
1.1       paf       988:           return 1;
                    989:      }
                    990: 
                    991: static void
1.9       paf       992:      yyprint(
1.1       paf       993:           FILE *file,
                    994:           int type,
                    995:           YYSTYPE value)
                    996:      {
1.9       paf       997:        if(type==STRING)
1.38      paf       998:          fprintf(file, " \"%s\"", SLA2S(value)->cstr());
1.1       paf       999:      }
                   1000: 

E-mail: