-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.cpp
More file actions
152 lines (134 loc) · 3.67 KB
/
func.cpp
File metadata and controls
152 lines (134 loc) · 3.67 KB
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
149
150
151
152
////////////////////////////////////////////////////////////
// func.c -- class implementation for S-Expression functions
//
// written by Larry I. Gritz, 1993
// The George Washington University, Dept. of EE&CS
// Computer Graphics and Animation Lab
////////////////////////////////////////////////////////////
#include <iostream>
#include <malloc.h>
#include <string>
#include <stdio.h>
#include "gp.h"
// Constructor
FunctionSet::FunctionSet (void)
{
n = 0; // Currently no functions in the list
maxn = 16; // Allocate room for 16 terminals to start
functions = (SFunction *) malloc (maxn*sizeof(SFunction));
nencapsulated = 0; // No encapsulated functions
}
// Destructor
FunctionSet::~FunctionSet (void)
{
if (functions)
{
//for (int i = 0; i < n; ++i)
//delete functions[i].name;
//free (functions[i].name);
//delete functions;
//free (functions);
}
}
// Add a new function to the set
//
void FunctionSet::add (const char *name, int nargs, impfunc implementation, editfunc edit_function, int has_sides)
{
// First, check to see if the name is already in the list
for (int i = 0; i < n; ++i)
if (! strcmp (functions[i].name, name))
{
cout << "FunctionSet Error: attempt to add existing function: " << functions[i].name << '\n';
return;
}
// Check to see if nargs is in range
if (nargs > MAX_SEXP_ARGS) {
cout << "Error! Function " << name << " declared with " << nargs << "arguments.\nYou have to change MAX_SEXP_ARGS and recompile.\n";
}
// If we need more space, allocate it
if (n == maxn - 1)
{
maxn *= 2;
functions = (SFunction *) realloc (functions,
maxn * sizeof (SFunction));
}
// Okay, now add it
functions[n].name = strdup (name);
functions[n].nargs = nargs;
functions[n].func = implementation;
functions[n].edit = edit_function;
functions[n].active = 1;
functions[n].side_effects = has_sides;
functions[n++].s = NULL;
}
// Add a new function to the set
int FunctionSet::encapsulate (S_Expression *s, int nargs)
{
// Construct a name
char buffer[16];
sprintf (buffer, "(E%d)", nencapsulated);
++nencapsulated;
// If we need more space, allocate it
if (n == maxn - 1)
{
maxn *= 2;
functions = (SFunction *)
realloc (functions, maxn * sizeof (SFunction));
}
for (int i = 0; i < n; ++i)
{
if (functions[i].s && equiv (s, functions[i].s))
return i;
}
// Okay, it's not a duplicate, so we add it
functions[n].name = strdup (buffer);
functions[n].nargs = nargs;
functions[n].func = NULL;
functions[n].edit = NULL;
functions[n].active = 1;
functions[n].side_effects = s->side_effects();
functions[n].s = s->copy();
cout << "\nEncapsulating " << buffer << " = " << functions[n].s << '\n';
cout.flush();
n++;
return (n-1);
}
// Return the index number for the named function
int FunctionSet::index (const char *name)
{
for (int i = 0; i < n; ++i)
if (! strcmp (functions[i].name, name))
return i;
// Oops! We didn't find the function
cout << "FunctionSet Error: could not index function: " << name << '\n';
return -1;
}
// Look up the name of a function based on the index number
char * FunctionSet::getname (int ind)
{
if (ind < 0 || ind >= n)
{
cout << "FunctionSet Error: attempted to getname with bad index (" << ind << ")\n";
return NULL;
}
else
return functions[ind].name;
}
// Print entire table
void FunctionSet::print (void)
{
cout << "\nFunction set listing:\n"
<< "---------------------\n";
for (int i = 0; i < n; ++i) {
cout << '\"' << functions[i].name << "\": "
<< functions[i].nargs << " parameters ";
if (! functions[i].active)
cout << " (deleted) ";
if (functions[i].s)
cout << functions[i].s;
cout << '\n';
}
cout << '\n';
}
// Declaration for the globally visible function set
FunctionSet Fset;