Annotation of parser3/src/targets/parser/parser.C, revision 1.8
1.1 paf 1: /*
1.8 ! paf 2: $Id: parser.C,v 1.7 2001/02/20 18:45:53 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.1 paf 15:
16: char *itoa(int n, char *buf){
17: snprintf(buf,MAX_STRING,"%d",n);
18: return buf;
19: }
20:
21: int main(int argc, char *argv[]) {
1.2 paf 22: Exception fatal_exception;
23: if(EXCEPTION_TRY(fatal_exception)) {
1.4 paf 24: Pool pool(fatal_exception);
1.2 paf 25:
26: char *file="file1";
1.5 paf 27: String& string=*new(pool) String(pool);
1.7 paf 28: string.APPEND("Hello, ", 0,file, 1);
29: string.APPEND("w", 0,file, 2);
30: string.APPEND("o", 0,file, 3);
31: string.APPEND("r", 0,file, 4);
32: string.APPEND("l", 0,file, 5);
33: string.APPEND("d", 0,file, 6);
34: string.APPEND("!\n ", 0,file, 7);
1.2 paf 35: printf(string.cstr());
36:
37: char *key1_file="key1_file";
1.5 paf 38: Hash& hash=*new(pool) Hash(pool, false);
1.2 paf 39: String key1=string;
1.7 paf 40: key1.APPEND("1", 0,key1_file, 1);
1.5 paf 41: String& value1=*new(pool) String(pool);
1.7 paf 42: value1.APPEND("i'm value1\n", 0,file, 1);
1.5 paf 43: String& value2=*new(pool) String(pool);
1.7 paf 44: value2.APPEND("i'm value2\n", 0,file, 1);
1.2 paf 45: hash.put(key1, &value1);
46: char *key2_file="key2_file";
47: String key2=string;
1.7 paf 48: key2.APPEND("2", 0,key2_file, 1);
1.2 paf 49: hash.put(key2, &value2);
50: String *found_value=(String*)hash.get(key2);
51: printf(found_value?found_value->cstr():"not found\n");
52:
1.7 paf 53: String& a=*new(pool) String(pool); a.APPEND("fi", 0,file, 1); a.APPEND("rst", 0,file, 2);
54: String& b=*new(pool) String(pool); b.APPEND("fir", 0,file, 1); b.APPEND("st", 0,file, 2);
1.2 paf 55: printf(a==b?"eq\n":"ne\n");
56:
57:
1.5 paf 58: Array& array=*new(pool) Array(pool, 2);
1.2 paf 59: array+="first";
60: array+="second";
61: array+="third";
62: printf("%s-%s-%s\n",
1.1 paf 63: array.get_cstr(0),
64: array.get_cstr(1),
65: array.get_cstr(2));
1.2 paf 66:
1.5 paf 67: Array& a1=*new(pool) Array(pool);
1.2 paf 68: a1+="first";
1.5 paf 69: Array& a2=*new(pool) Array(pool);
1.2 paf 70: a2+="second";
1.5 paf 71: Array& asum=*new(pool) Array(pool);
1.2 paf 72: asum.append_array(a1);
73: asum.append_array(a2);
74: printf("%s-%s\n",
1.1 paf 75: asum.get_cstr(0),
76: asum.get_cstr(1));
77:
1.4 paf 78: Pool request_pool(fatal_exception);
1.2 paf 79: Request request(request_pool);
80:
81: Exception operator_exception;
82: Local_request_exception subst(request, operator_exception);
83: if(EXCEPTION_TRY(request.exception())) {
1.3 paf 84: /*
1.2 paf 85: Array acolumns(request.pool());
1.1 paf 86: acolumns+="id";
87: acolumns+="name";
88: acolumns+="age";
1.3 paf 89: Table table(request, "_file.cfg", 1, &acolumns);
90: */
91: Table table(request, "_file.cfg", 1, 0);
1.1 paf 92: for(int n=1; n<=5; n++) {
1.5 paf 93: Array& row=*new(request.pool()) Array(request.pool(), 3/*table.columns()->size()*/);
1.2 paf 94: char *buf=static_cast<char *>(request.pool().malloc(MAX_STRING));
1.1 paf 95: row+=itoa(n, buf);
96: row+="paf";
97: row+="99";
98:
1.3 paf 99: table+=&row;
1.1 paf 100: }
1.3 paf 101: /*
102: for(int i=0; i<table.columns()->size(); i++)
103: printf("%s\t", table.columns()->get_cstr(i));
1.1 paf 104: printf("\n");
1.3 paf 105: */
106: for(table.set_current(0); table.get_current()<table.size(); table.inc_current()) {
1.2 paf 107: String line(request.pool());
1.3 paf 108: for(int i=0; i<5/*table.columns()->size()*/; i++) {
109: /**/
110: String name(request.pool());
111: char *buf=static_cast<char *>(request.pool().malloc(MAX_STRING));
1.7 paf 112: name.APPEND(itoa(i, buf), 0,"names file", 0);
1.1 paf 113: //name.APPEND("id", "names file", 0);
1.3 paf 114: table.read_item(line, name);
115: /*
116: const char *cstr_name=table.columns()->get_cstr(i);
1.2 paf 117: String name(request.pool());
1.1 paf 118: name.APPEND(cstr_name, 0, 0);
1.3 paf 119: table.read_item(line, name);
120: /**/
1.7 paf 121: line.APPEND("\t", 0,0, 0);
1.1 paf 122: }
123: printf("%s\n", line.cstr());
124: }
1.6 paf 125:
1.7 paf 126: /*
1.6 paf 127: String it(request.pool());
128: it.APPEND("ab.cd[zzz]", 0,0);
129: String_iterator si(it);
1.7 paf 130: / *si++;
1.6 paf 131: si++;
132: si++;
133: si++;
134: si++;
135: si++;
1.7 paf 136: * /
137: / *bool found=si.skip_to('.');
1.6 paf 138: si++;
1.7 paf 139: * /
1.6 paf 140: si++;
141: Char_types types;
142: types.set(' ', 1);
143: types.set('[', 2);
144: types.set(']', 3);
145: int type=si.skip_to(types);
146: si++;
1.7 paf 147: */
148: // compile
1.8 ! paf 149: char *file="c:\\temp\\test.p";
1.7 paf 150: char *source=file_read(pool, file);
151: Array *ops=COMPILE(&pool, source, file);
152: execute(&pool, ops);
1.6 paf 153:
1.1 paf 154: } else {
1.2 paf 155: Exception& e=request.exception();
1.3 paf 156: printf("operator_error occured: %s\n", e.comment());
1.2 paf 157: const String *type=e.type();
158: if(type) {
159: printf(" type: %s", type->cstr());
160: const String *code=e.code();
161: if(code)
162: printf(", code: %s", code->cstr());
163: printf("\n");
164: }
165: const String *problem_source=e.problem_source();
1.1 paf 166: if(problem_source) {
167: const Origin& origin=problem_source->origin();
1.3 paf 168: printf(" '%s' [%s:%d]\n",
1.1 paf 169: problem_source->cstr(),
170: origin.file, origin.line);
171: }
172: }
173: } else {
1.2 paf 174: printf("fatal exception occured: %s\n", fatal_exception.comment());
1.1 paf 175: }
176:
177: return 0;
178: }
E-mail: