comparison lwcc/cpp.c @ 296:83fcc1ed6ad6 ccdev

Checkpoint lwcc development Initial untested version of the preprocessor with macro expansion but without file inclusion.
author William Astle <lost@l-w.ca>
date Sat, 14 Sep 2013 20:04:38 -0600
parents 4b17780f2777
children d85d173ba120
comparison
equal deleted inserted replaced
295:4b17780f2777 296:83fcc1ed6ad6
50 pp = lw_alloc(sizeof(struct preproc_info)); 50 pp = lw_alloc(sizeof(struct preproc_info));
51 memset(pp, 0, sizeof(struct preproc_info)); 51 memset(pp, 0, sizeof(struct preproc_info));
52 pp -> fn = lw_strdup(fn); 52 pp -> fn = lw_strdup(fn);
53 pp -> fp = fp; 53 pp -> fp = fp;
54 pp -> ra = CPP_NOUNG; 54 pp -> ra = CPP_NOUNG;
55 pp -> ppeolseen = 1;
55 return pp; 56 return pp;
56 } 57 }
57 58
58 struct token *preproc_next_token(struct preproc_info *pp) 59 struct token *preproc_next_token(struct preproc_info *pp)
59 { 60 {
60 struct token *t; 61 struct token *t;
61 62
63 if (pp -> curtok)
64 token_free(pp -> curtok);
65
66 /*
67 If there is a list of tokens to process, move it to the "unget" queue
68 with an EOF marker at the end of it.
69 */
70 if (pp -> sourcelist)
71 {
72 for (t = pp -> sourcelist; t -> next; t = t -> next)
73 /* do nothing */ ;
74 t -> next = token_create(TOK_EOF, NULL, -1, -1, "");
75 t -> next -> next = pp -> tokqueue;
76 pp -> tokqueue = pp -> sourcelist;
77 pp -> sourcelist = NULL;
78 }
79 again:
62 if (pp -> tokqueue) 80 if (pp -> tokqueue)
63 { 81 {
64 t = pp -> tokqueue; 82 t = pp -> tokqueue;
65 pp -> tokqueue = t -> next; 83 pp -> tokqueue = t -> next;
66 if (pp -> tokqueue) 84 if (pp -> tokqueue)
67 pp -> tokqueue -> prev = NULL; 85 pp -> tokqueue -> prev = NULL;
68 t -> next = NULL; 86 t -> next = NULL;
69 t -> prev = NULL; 87 t -> prev = NULL;
70 return t; 88 pp -> curtok = t;
89 goto ret;
71 } 90 }
72 return(preproc_lex_next_token(pp)); 91 pp -> curtok = preproc_lex_next_token(pp);
92 t = pp -> curtok;
93 ret:
94 if (t -> ttype == TOK_ENDEXPAND)
95 {
96 struct expand_e *e;
97 e = pp -> expand_list;
98 pp -> expand_list = e -> next;
99 lw_free(e);
100 goto again;
101 }
102 return t;
103 }
104
105 void preproc_unget_token(struct preproc_info *pp, struct token *t)
106 {
107 t -> next = pp -> tokqueue;
108 pp -> tokqueue = t;
109 if (pp -> curtok == t)
110 pp -> curtok = NULL;
73 } 111 }
74 112
75 void preproc_finish(struct preproc_info *pp) 113 void preproc_finish(struct preproc_info *pp)
76 { 114 {
77 lw_free((void *)(pp -> fn)); 115 lw_free((void *)(pp -> fn));
78 fclose(pp -> fp); 116 fclose(pp -> fp);
117 if (pp -> curtok)
118 token_free(pp -> curtok);
119 while (pp -> tokqueue)
120 {
121 preproc_next_token(pp);
122 token_free(pp -> curtok);
123 }
79 lw_free(pp); 124 lw_free(pp);
80 } 125 }
81 126
82 void preproc_register_error_callback(struct preproc_info *pp, void (*cb)(const char *)) 127 void preproc_register_error_callback(struct preproc_info *pp, void (*cb)(const char *))
83 { 128 {