Annotation of parser3/src/targets/parser/parser.C, revision 1.13

1.1       paf         1: /*
1.13    ! paf         2:   $Id: parser.C,v 1.12 2001/02/21 15:00:08 paf Exp $
1.1       paf         3: */
                      4: 
                      5: #include <stdio.h>
                      6: #include <stdlib.h>
                      7: 
                      8: #include "pa_hash.h"
                      9: #include "pa_array.h"
                     10: #include "pa_table.h"
                     11: #include "pa_common.h"
1.4       paf        12: #include "pa_pool.h"
1.7       paf        13: #include "compile.h"
                     14: #include "execute.h"
1.10      paf        15: #include "pa_vclass.h"
1.1       paf        16: 
                     17: char *itoa(int n, char *buf){
                     18:     snprintf(buf,MAX_STRING,"%d",n);
                     19:     return buf;
                     20: }
                     21: 
                     22: int main(int argc, char *argv[]) {
1.2       paf        23:        Exception fatal_exception;
                     24:        if(EXCEPTION_TRY(fatal_exception)) {
1.4       paf        25:                Pool pool(fatal_exception);
1.2       paf        26:                
                     27:                char *file="file1";
1.5       paf        28:                String& string=*new(pool) String(pool);
1.7       paf        29:                string.APPEND("Hello, ", 0,file, 1);
                     30:                string.APPEND("w", 0,file, 2);
                     31:                string.APPEND("o", 0,file, 3);
                     32:                string.APPEND("r", 0,file, 4);
                     33:                string.APPEND("l", 0,file, 5);
                     34:                string.APPEND("d", 0,file, 6);
                     35:                string.APPEND("!\n ", 0,file, 7);
1.2       paf        36:                printf(string.cstr());
                     37:                
                     38:                char *key1_file="key1_file";
1.10      paf        39:                Hash& hash=*new(pool) Hash(pool);
1.2       paf        40:                String key1=string;
1.7       paf        41:                key1.APPEND("1", 0,key1_file, 1);
1.5       paf        42:                String& value1=*new(pool) String(pool);
1.7       paf        43:                value1.APPEND("i'm value1\n", 0,file, 1);
1.5       paf        44:                String& value2=*new(pool) String(pool);
1.7       paf        45:                value2.APPEND("i'm value2\n", 0,file, 1);
1.2       paf        46:                hash.put(key1, &value1);
                     47:                char *key2_file="key2_file";
                     48:                String key2=string;
1.7       paf        49:                key2.APPEND("2", 0,key2_file, 1);
1.2       paf        50:                hash.put(key2, &value2);
                     51:                String *found_value=(String*)hash.get(key2);
                     52:                printf(found_value?found_value->cstr():"not found\n");
                     53:                
1.7       paf        54:                String& a=*new(pool) String(pool);      a.APPEND("fi", 0,file, 1); a.APPEND("rst", 0,file, 2);
                     55:                String& b=*new(pool) String(pool);      b.APPEND("fir", 0,file, 1); b.APPEND("st", 0,file, 2);
1.2       paf        56:                printf(a==b?"eq\n":"ne\n");
                     57:                
                     58:                
1.5       paf        59:                Array& array=*new(pool) Array(pool, 2);
1.2       paf        60:                array+="first";
                     61:                array+="second";
                     62:                array+="third";
                     63:                printf("%s-%s-%s\n", 
1.1       paf        64:                        array.get_cstr(0), 
                     65:                        array.get_cstr(1),
                     66:                        array.get_cstr(2));
1.2       paf        67:                
1.5       paf        68:                Array& a1=*new(pool) Array(pool);
1.2       paf        69:                a1+="first";
1.5       paf        70:                Array& a2=*new(pool) Array(pool);
1.2       paf        71:                a2+="second";
1.5       paf        72:                Array& asum=*new(pool) Array(pool);
1.2       paf        73:                asum.append_array(a1);
                     74:                asum.append_array(a2);
                     75:                printf("%s-%s\n", 
1.1       paf        76:                        asum.get_cstr(0), 
                     77:                        asum.get_cstr(1));
                     78: 
1.4       paf        79:                Pool request_pool(fatal_exception);
1.2       paf        80:                Request request(request_pool);
                     81: 
                     82:                Exception operator_exception;
                     83:                Local_request_exception subst(request, operator_exception);
                     84:                if(EXCEPTION_TRY(request.exception())) {
1.3       paf        85:                        /*
1.2       paf        86:                        Array acolumns(request.pool());
1.1       paf        87:                        acolumns+="id";
                     88:                        acolumns+="name";
                     89:                        acolumns+="age";
1.3       paf        90:                        Table table(request, "_file.cfg", 1, &acolumns);
                     91:                        */
                     92:                        Table table(request, "_file.cfg", 1, 0);
1.1       paf        93:                        for(int n=1; n<=5; n++) {
1.5       paf        94:                                Array& row=*new(request.pool()) Array(request.pool(), 3/*table.columns()->size()*/);
1.2       paf        95:                                char *buf=static_cast<char *>(request.pool().malloc(MAX_STRING));
1.1       paf        96:                                row+=itoa(n, buf);
                     97:                                row+="paf";
                     98:                                row+="99";
                     99:                                
1.3       paf       100:                                table+=&row;
1.1       paf       101:                        }
1.3       paf       102:                        /*
                    103:                        for(int i=0; i<table.columns()->size(); i++) 
                    104:                                printf("%s\t", table.columns()->get_cstr(i));
1.1       paf       105:                        printf("\n");
1.3       paf       106:                        */
                    107:                        for(table.set_current(0); table.get_current()<table.size(); table.inc_current()) {
1.2       paf       108:                                String line(request.pool());
1.3       paf       109:                                for(int i=0; i<5/*table.columns()->size()*/; i++) {
                    110:                                        /**/
                    111:                                        String name(request.pool());
                    112:                                        char *buf=static_cast<char *>(request.pool().malloc(MAX_STRING));
1.7       paf       113:                                        name.APPEND(itoa(i, buf), 0,"names file", 0);
1.1       paf       114:                                        //name.APPEND("id", "names file", 0);
1.3       paf       115:                                        table.read_item(line, name);
                    116:                                        /*
                    117:                                        const char *cstr_name=table.columns()->get_cstr(i);
1.2       paf       118:                                        String name(request.pool());
1.1       paf       119:                                        name.APPEND(cstr_name, 0, 0);
1.3       paf       120:                                        table.read_item(line, name);
                    121:                                        /**/
1.7       paf       122:                                        line.APPEND("\t", 0,0, 0);
1.1       paf       123:                                }
                    124:                                printf("%s\n", line.cstr());
                    125:                        }
1.6       paf       126: 
1.7       paf       127: /*
1.6       paf       128:                        String it(request.pool());
                    129:                        it.APPEND("ab.cd[zzz]", 0,0);
                    130:                        String_iterator si(it);
1.7       paf       131:                        / *si++;
1.6       paf       132:                        si++;
                    133:                        si++;
                    134:                        si++;
                    135:                        si++;
                    136:                        si++;
1.7       paf       137:                        * /
                    138:                        / *bool found=si.skip_to('.');
1.6       paf       139:                        si++;
1.7       paf       140:                        * /
1.6       paf       141:                        si++;
                    142:                        Char_types types;
                    143:                        types.set(' ', 1);
                    144:                        types.set('[', 2);
                    145:                        types.set(']', 3);
                    146:                        int type=si.skip_to(types);
                    147:                        si++;
1.7       paf       148: */
                    149:                        // compile
1.8       paf       150:                        char *file="c:\\temp\\test.p";
1.9       paf       151:                        char *source=file_read(request.pool(), file);
1.13    ! paf       152:                        Array& compiled_methods=COMPILE(request, source, file);
1.10      paf       153:                        
                    154:                        String name_RUN(request.pool()); name_RUN.APPEND_CONST("RUN");
                    155:                        //   create new 'name' vclass, add it to request's classes
                    156:                        {
                    157:                                Array immediate_parents(pool);
                    158:                                VClass *vclass=new(pool) VClass(pool, &name_RUN, immediate_parents);
                    159:                                request.classes().put(name_RUN, vclass);
                    160: 
1.13    ! paf       161:                                for(int i=0; i<compiled_methods.size(); i++) {
        !           162:                                        Method &method=*static_cast<Method *>(compiled_methods.quick_get(i));
        !           163:                                        vclass->add_method(method.name, method);
1.10      paf       164:                                }
                    165:                        }
                    166: 
                    167:                        {
1.11      paf       168:                                VClass *class_RUN=static_cast<VClass *>(request.classes().get(name_RUN));
1.10      paf       169:                                String name_main(pool);
1.13    ! paf       170:                                name_main.APPEND_CONST(MAIN_METHOD_NAME);
1.11      paf       171:                                Method *method_main=class_RUN->get_method(name_main);
1.10      paf       172:                                if(!method_main)
                    173:                                        request.exception().raise(0,0,
1.11      paf       174:                                                &class_RUN->name(),
1.12      paf       175:                                                "no 'main' method in class");
1.10      paf       176: 
1.11      paf       177:                                Array& code=method_main->code;
                    178:                                execute(pool, code);
1.10      paf       179:                        }
1.6       paf       180: 
1.1       paf       181:                } else {
1.2       paf       182:                        Exception& e=request.exception();
1.3       paf       183:                        printf("operator_error occured: %s\n", e.comment());
1.2       paf       184:                        const String *type=e.type();
                    185:                        if(type) {
                    186:                                printf("  type: %s", type->cstr());
                    187:                                const String *code=e.code();
                    188:                                if(code)
                    189:                                        printf(", code: %s", code->cstr());
                    190:                                printf("\n");
                    191:                        }
                    192:                        const String *problem_source=e.problem_source();
1.1       paf       193:                        if(problem_source) {
                    194:                                const Origin& origin=problem_source->origin();
1.12      paf       195:                                printf("  '%s'\n", 
                    196:                                        problem_source->cstr());
                    197:                                if(origin.file)
                    198:                                        printf(" [%s:%d]",
                    199:                                                origin.file, origin.line);
                    200:                                printf("\n");
1.1       paf       201:                        }
                    202:                }
                    203:        } else {
1.2       paf       204:                printf("fatal exception occured: %s\n", fatal_exception.comment());
1.1       paf       205:        }
                    206: 
                    207:        return 0;
                    208: }

E-mail: