-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathList.cpp
More file actions
216 lines (144 loc) · 4.59 KB
/
Copy pathList.cpp
File metadata and controls
216 lines (144 loc) · 4.59 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
////////////////////////////////////////////////////////////////////////////
//
// ccli : The Computation and Control Language Tools
// -------------------------------------------------
//
// Copyright (c) 2003 Eric Klavins, California Institute of Technology
// For more information, email klavins@caltech.edu
//
// This program 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.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//
#include <stdio.h>
#include "SymbolTable.h"
#include "Expr.h"
#define LISTVAL data.list_val.vals
void Value::set ( std::list<Value*> * L ) {
if ( type != LIST ) {
fprintf ( stderr, "Value::set ( list ): attempted to get list type of non-LIST symbol\n" );
exit ( -1 );
} else {
delete LISTVAL;
LISTVAL = L;
}
}
std::list<Value*> * Value::list_value ( void ) {
if ( type != LIST ) {
fprintf ( stderr, "Value::list_value ( void ): attempted to access LIST value of non-LIST symbol '" );
print ( stderr ); fprintf ( stderr, "'\n" );
exit ( -1 );
}
return LISTVAL;
}
Value * Expr::list_head ( Scope * s ) {
Value * temp = e1->eval ( s ), * h;
if ( temp->get_type() != Value::LIST ) {
fprintf ( stderr, "Expr::list_head: attempted to take head of non-list\n" );
exit ( -1 );
}
if ( temp->list_value()->size() == 0 ) {
RUNTIME_ERROR(file,line,"Attempted to take head of an empty list.")
}
h = temp->list_value()->front()->copy();
delete temp;
return h;
}
Value * Expr::list_tail ( Scope * s ) {
Value * temp = e1->eval ( s );
if ( temp->get_type() != Value::LIST ) {
fprintf ( stderr, "Expr::list_head: attempted to take head of non-list\n" );
exit ( -1 );
}
if ( temp->list_value()->size() == 0 ) {
RUNTIME_ERROR(file,line,"Attempted to take tail of an empty list.")
}
delete temp->list_value()->front();
temp->list_value()->pop_front();
return temp;
}
Value * Expr::list_cons ( Scope * s ) {
Value * temp1 = e1->eval ( s ),
* temp2 = e2->eval ( s );
if ( temp2->get_type() != Value::LIST ) {
fprintf ( stderr, "Expr::list_cons: attempted to cons to a non-list '" );
temp1->print ( stderr ); printf ( "' and\n'" );
temp2->print ( stderr ); printf ( "'\n" );
exit ( -1 );
}
temp2->list_value()->push_front ( temp1 );
return temp2;
}
Value * Expr::list_join ( Scope * s ) {
Value * temp1 = e1->eval ( s ),
* temp2 = e2->eval ( s );
std::list<Value *>::iterator i;
if ( temp1->get_type() != Value::LIST || temp2->get_type() != Value::LIST ) {
fprintf ( stderr, "Expr::list_join: attempted to join non-lists\n" );
exit ( -1 );
}
for ( i = temp2->list_value()->begin(); i != temp2->list_value()->end(); i++ )
temp1->list_value()->push_back(*i);
while ( temp2->list_value()->size() > 0 )
temp2->list_value()->pop_front();
delete temp2;
return temp1;
}
Value * Expr::list_equal ( Value * v1, Value * v2, Scope * s ) {
bool rval = true;
std::list<Value *>::iterator i, j;
if ( v1->list_value()->size() != v2->list_value()->size() )
rval = false;
else for ( i=v1->list_value()->begin(), j=v2->list_value()->begin();
i != v1->list_value()->end() && j != v2->list_value()->end();
i++, j++ ) {
rval = Value::equal ( *i, *j, s );
if ( !rval )
break;
}
return new Value ( rval );
}
bool Value::equal ( Value * v1, Value * v2, Scope * s ) {
bool answer;
if ( v1->type != v2->type )
answer = false;
else {
switch ( v1->type ) {
case UNIT:
answer = true;
break;
case BOOLEAN:
answer = ( v1->bool_value() == v2->bool_value() );
break;
case INTEGER:
answer = ( v1->int_value() == v2->int_value() );
break;
case REAL:
answer = ( v1->real_value() == v2->real_value() );
break;
case STRING:
answer = ( v1->string_value() == v2->string_value() );
break;
case LIST:
answer = Expr::list_equal ( v1, v2, s );
break;
case RECORD:
case FUNCTION:
case EX_FUNCTION:
ASSERT ( false );
break;
}
}
return answer;
}