1 : /*
2 : Copyright (C) 2006, Bruce Ediger
3 :
4 : This file is part of cl.
5 :
6 : cl is free software; you can redistribute it and/or modify
7 : it under the terms of the GNU General Public License as published by
8 : the Free Software Foundation; either version 2 of the License, or
9 : (at your option) any later version.
10 :
11 : cl is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : GNU General Public License for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with cl; if not, write to the Free Software
18 : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 :
20 : */
21 :
22 : #ifndef _TCC_
23 : #ident "$Id: abbreviations.c,v 1.4 2007/04/30 03:17:06 bediger Exp $"
24 : #endif
25 :
26 : #include <stdio.h>
27 : #include <stdlib.h> /* malloc(), free() */
28 :
29 : #include <node.h>
30 : #include <hashtable.h>
31 : #include <abbreviations.h>
32 :
33 : struct hashtable *abbr_table = NULL;
34 :
35 : void
36 : setup_abbreviation_table(struct hashtable *h)
37 71 : {
38 71 : abbr_table = h;
39 71 : }
40 :
41 : struct node *
42 : abbreviation_lookup(const char *id)
43 1153 : {
44 1153 : struct node *r = NULL;
45 1153 : void *p = data_lookup(abbr_table, id);
46 : /* make a "permanent" copy of the parse tree, so that
47 : * reduce_graph() can destructively reduce the resulting
48 : * parse tree. */
49 1153 : if (p) r = arena_copy_graph((struct node *)p);
50 1153 : return r;
51 : }
52 :
53 : void
54 : abbreviation_add(const char *id, struct node *expr)
55 5145 : {
56 5145 : struct hashnode *n = NULL;
57 : unsigned int hv;
58 :
59 : /* By the time flow-of-control arrives here,
60 : * the string (variable id) has already gotten
61 : * in the hashtable. Only 1 struct hashtable exists,
62 : * used for both unique-string-saving, and storage
63 : * of "abbreviations". The lexer code guarantess
64 : * it by calling Atom_string() on all input strings.
65 : * Therefore, this code *always* finds string id
66 : * in the abbr_table.
67 : */
68 5145 : n = node_lookup(abbr_table, id, &hv);
69 :
70 5145 : free_graph((struct node *)n->data);
71 :
72 : /* make a "permanent" copy of the parse tree, so that
73 : * reduce_graph() can destructively reduce the resulting
74 : * parse tree. */
75 5145 : n->data = (void *)copy_graph(expr);
76 5145 : }
77 :
78 : struct node *
79 : copy_graph(struct node *p)
80 726295 : {
81 726295 : struct node *r = NULL;
82 :
83 726295 : if (!p)
84 0 : return r;
85 :
86 726295 : r = malloc(sizeof(*r));
87 726295 : r->typ = p->typ;
88 726295 : r->sn = -666;
89 726295 : r->cn = COMB_NONE;
90 :
91 726295 : switch (p->typ)
92 : {
93 : case APPLICATION:
94 360575 : r->name = NULL;
95 360575 : r->left = copy_graph(p->left);
96 360575 : r->right = copy_graph(p->right);
97 360575 : r->examined = 0;
98 360575 : break;
99 : case COMBINATOR:
100 365720 : r->name = p->name;
101 365720 : r->cn = p->cn;
102 365720 : r->left = r->right = NULL;
103 365720 : break;
104 : case UNTYPED:
105 0 : printf("Copying an UNTYPED node\n");
106 0 : r->name = NULL;
107 0 : r->left = r->right = NULL;
108 0 : break;
109 : default:
110 0 : printf("Copying n node of unknown (%d) type\n", p->typ);
111 0 : r->name = NULL;
112 0 : r->left = copy_graph(p->left);
113 0 : r->right = copy_graph(p->right);
114 : break;
115 : }
116 726295 : return r;
117 : }
118 :
119 : void
120 : free_graph(struct node *p)
121 1467969 : {
122 1467969 : if (!p) return;
123 :
124 726295 : free_graph(p->left);
125 726295 : free_graph(p->right);
126 :
127 726295 : p->name = NULL;
128 726295 : p->left = p->right = NULL;
129 726295 : p->typ = -1;
130 :
131 726295 : free(p);
132 : }
|