-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheme_eval.c
107 lines (94 loc) · 2.8 KB
/
scheme_eval.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
/*
libscheme
Copyright (c) 1994 Brent Benson
All rights reserved.
Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this
software and its documentation for any purpose, provided that the
above copyright notice and the following two paragraphs appear in
all copies of this software.
IN NO EVENT SHALL BRENT BENSON BE LIABLE TO ANY PARTY FOR DIRECT,
INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BRENT
BENSON HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
BRENT BENSON SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER
IS ON AN "AS IS" BASIS, AND BRENT BENSON HAS NO OBLIGATION TO
PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
MODIFICATIONS.
*/
#include "scheme_private.h"
/* locals */
static Scheme_Value scheme_eval_combination (Scheme_Value comb, Scheme_Env *env);
static Scheme_Value eval (int argc, Scheme_Value argv[]);
void
scheme_init_eval (Scheme_Env *env)
{
scheme_add_prim ("eval", eval, env);
}
Scheme_Value
scheme_eval (Scheme_Value obj, Scheme_Env *env)
{
Scheme_Value type;
type = SCHEME_TYPE (obj);
if (type == scheme_symbol_type)
{
Scheme_Value val;
val = scheme_lookup_value (obj, env);
if (! val)
{
scheme_signal_error ("reference to unbound symbol: %s", SCHEME_STR_VAL(obj));
}
return (val);
}
else if (type == scheme_pair_type)
{
return (scheme_eval_combination (obj, env));
}
else
{
return (obj);
}
}
/* local functions */
static Scheme_Value
scheme_eval_combination (Scheme_Value comb, Scheme_Env *env)
{
Scheme_Value rator, type, rands;
Scheme_Value evaled_rands[SCHEME_MAX_ARGS];
Scheme_Value fun, form;
int num_rands, i;
rator = scheme_eval (SCHEME_CAR (comb), env);
type = SCHEME_TYPE (rator);
if (type == scheme_syntax_type)
{
return (SCHEME_SYNTAX(rator)(comb, env));
}
else if (type == scheme_macro_type)
{
fun = (Scheme_Value) SCHEME_PTR_VAL (rator);
rands = SCHEME_CDR (comb);
form = scheme_apply_to_list (fun, rands);
return (scheme_eval (form, env));
}
else
{
rands = SCHEME_CDR (comb);
num_rands = scheme_list_length (rands);
i = 0;
while (rands != scheme_null)
{
evaled_rands[i] = scheme_eval (SCHEME_CAR (rands), env);
i++;
rands = SCHEME_CDR (rands);
}
return (scheme_apply (rator, num_rands, evaled_rands));
}
}
static Scheme_Value
eval (int argc, Scheme_Value argv[])
{
SCHEME_ASSERT ((argc == 1), "eval: wrong number of args");
return (scheme_eval (argv[0], scheme_env));
}