-
Notifications
You must be signed in to change notification settings - Fork 0
/
abbreviations.c
148 lines (123 loc) · 3.73 KB
/
abbreviations.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright (C) 2010-2011, Bruce Ediger
This file is part of acl.
acl is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
acl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with acl; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Id: abbreviations.c,v 1.8 2011/06/12 18:22:00 bediger Exp $ */
/*
* Abbreviations: an identifier can reference an entire parse tree
* if the user has issued the "define" or "def" interpreter commands.
*
* During parse of user input, all identifiers get passed through
* abbreviation_lookup(). Previously issued "def" or "define" commands
* cause the single struct hashtable to keep a copy of a parse tree
* with the identifier as the key.
*
* Some pieces of this code assume that the definitions and the "atom"
* strings used in the interpreter share a single struct hashtable for
* storage.
*/
#include <stdio.h>
#include <stdlib.h> /* malloc(), free() */
#include <node.h>
#include <hashtable.h>
#include <abbreviations.h>
struct hashtable *abbr_table = NULL;
void
setup_abbreviation_table(struct hashtable *h)
{
abbr_table = h;
}
struct node *
abbreviation_lookup(const char *id)
{
struct node *r = NULL;
void *p = data_lookup(abbr_table, id);
/* make an arena-allocated copy of the abbreviation, so that
* reduce_graph() can destructively reduce the resulting
* parse tree. */
if (p)
{
preallocate_nodes(((struct node *)p)->tree_size);
r = arena_copy_graph((struct node *)p);
}
return r;
}
void
abbreviation_add(const char *id, struct node *expr)
{
struct hashnode *n = NULL;
unsigned int hv;
/* By the time flow-of-control arrives here,
* the string (variable id) has already gotten
* in the hashtable. Only 1 struct hashtable exists,
* used for both unique-string-saving, and storage
* of "abbreviations". The lexer code guarantess
* it by calling Atom_string() on all input strings.
* Therefore, this code *always* finds string id
* in the abbr_table.
*/
n = node_lookup(abbr_table, id, &hv);
free_graph((struct node *)n->data);
/* Make a "permanent" copy of the parse tree, not arena.
* allocated. At the end of a read-eval-print loop, the
* interpreter resets all arenas to "nothing allocated",
* so an arena-allocated copy would eventually get overwritten.
*/
n->data = (void *)copy_graph(expr);
}
/*
* malloc() based parse tree ("graph") copy.
* If a "reduce" interpreter command causes the input graph
* to have multiply-referenced structs node, this function
* ignores that. Sub-trees that are "shared" get un-shared.
*/
struct node *
copy_graph(struct node *p)
{
struct node *r = NULL;
r = malloc(sizeof(*r));
r->typ = p->typ;
r->name = p->name;
r->sn = -666;
switch (p->typ)
{
case APPLICATION:
r->left = copy_graph(p->left);
r->right = copy_graph(p->right);
r->tree_size = r->left->tree_size + r->right->tree_size + 1;
break;
case ATOM:
r->rule = p->rule;
r->left = r->right = NULL;
r->tree_size = 1;
break;
}
return r;
}
/*
* free() based parse tree ("graph") deallocation.
* Won't work on arena-based graphs generated from
* user input.
*/
void
free_graph(struct node *p)
{
if (!p) return;
free_graph(p->left);
free_graph(p->right);
p->name = NULL;
p->left = p->right = NULL;
p->typ = -1;
free(p);
}