#include <base_evaluator.hpp>
Definition at line 22 of file base_evaluator.hpp.
| base_evaluator_rep | ( | ) | [inline] |
Definition at line 59 of file base_evaluator.hpp.
: env (global_environment ()) {}
| base_evaluator_rep | ( | const evaluator & | ev ) | [inline] |
Definition at line 61 of file base_evaluator.hpp.
: env (local_environment (get_environment (ev))) {}
| generic apply | ( | const generic & | fun ) | const [virtual] |
Definition at line 302 of file base_evaluator.cpp.
References base_evaluator_rep::env.
Referenced by base_evaluator_rep::apply(), base_evaluator_rep::construct(), and base_evaluator_rep::eval().
| generic apply | ( | const generic & | fun, |
| const generic & | x1 | ||
| ) | const [virtual] |
Definition at line 309 of file base_evaluator.cpp.
References base_evaluator_rep::apply(), base_evaluator_rep::env, mmx::equalize(), and mmx::is_grouped().
{
generic the_fun;
if (!env->get (fun, the_fun)) {
if (is_grouped (x1))
return equalize (fun, vec<generic> (x1));
return gen (fun, x1);
}
/*
static bool busy_flag= false;
if (!busy_flag) {
busy_flag= true;
mmerr << "Apply " << flatten_as_lisp (the_fun) << ", ";
mmerr << flatten_as_lisp (x1) << ": ";
mmerr << flatten_as_lisp (type_name (x1)) << "\n";
busy_flag= false;
}
else
mmerr << "[" << flatten_as_lisp (the_fun) << "]" << flush_now;
*/
return as<routine> (the_fun)->apply (x1);
}
| generic apply | ( | const generic & | fun, |
| const generic & | x1, | ||
| const generic & | x2 | ||
| ) | const [virtual] |
Definition at line 332 of file base_evaluator.cpp.
References base_evaluator_rep::apply(), base_evaluator_rep::env, mmx::equalize(), base_evaluator_rep::eval(), and mmx::is_grouped().
{
if (fun == GEN_TRANSTYPE) return eval (gen (fun, x1, x2));
generic the_fun;
if (!env->get (fun, the_fun)) {
if (is_grouped (x1) || is_grouped (x2))
return equalize (fun, vec<generic> (x1, x2));
return gen (fun, x1, x2);
}
return as<routine> (the_fun)->apply (x1, x2);
}
| generic apply | ( | const generic & | fun, |
| const vector< generic > & | v | ||
| ) | const [virtual] |
Definition at line 346 of file base_evaluator.cpp.
References base_evaluator_rep::apply(), base_evaluator_rep::env, mmx::equalize(), base_evaluator_rep::eval(), and mmx::type().
| generic construct | ( | const generic & | x ) | const [virtual] |
Definition at line 294 of file base_evaluator.cpp.
References base_evaluator_rep::apply(), base_evaluator_rep::env, and mmx::type().
Referenced by base_evaluator_rep::eval().
| bool contains | ( | const generic & | var ) | const [virtual] |
Definition at line 156 of file base_evaluator.cpp.
References base_evaluator_rep::env.
{
return env->contains (var);
}
| generic eval | ( | const generic & | x ) | const [virtual] |
Definition at line 175 of file base_evaluator.cpp.
References base_evaluator_rep::apply(), base_evaluator_rep::construct(), base_evaluator_rep::env, and base_evaluator_rep::eval_cdr().
Referenced by base_evaluator_rep::apply(), and base_evaluator_rep::eval_cdr().
{
#ifdef BASIX_ENABLE_EXCEPTIONS
try {
#endif
if (is<literal> (x)) {
generic r;
if (env->get (x, r)) return r;
if (env->contains (gen (GEN_METHOD, x))) {
generic sym ("." * literal_to_string (x));
return eval (gen (sym, generic (GEN_THIS)));
}
if (x == GEN_THIS) ERROR ("not inside method");
return x;
}
else if (is<compound> (x)) {
const vector<generic> v= compound_to_vector (x);
nat n= N(v);
//mmerr << "Evaluate ";
//if (n>0) mmerr << as_lisp (x);
//mmerr << " at " << ((void*) inside (x)) << "\n";
//if (n>0 && is<literal> (v[0]))
// mmerr << "Evaluate " << n << ", " << as_lisp (x) << "\n";
switch (n) {
case 0:
return x;
case 1:
{
generic fun= eval (v[0]);
if (is<primitive> (fun))
return as<primitive> (fun) -> apply (x);
else if (is<routine> (fun))
return as<routine> (fun) -> apply ();
else if (is<alias<routine> > (fun))
return get_alias (as<alias<routine> > (fun)) -> apply ();
else return apply (GEN_APPLY, fun);
}
case 2:
{
generic fun= eval (v[0]);
if (is<primitive> (fun))
return as<primitive> (fun) -> apply (x);
else {
generic a1= eval (v[1]);
//mmerr << "Argument 1 at " << ((void*) inside (a1)) << "\n";
if (is<exception> (a1)) return a1;
if (is<routine> (fun))
return as<routine> (fun) -> apply (a1);
else if (is<alias<routine> > (fun))
return get_alias (as<alias<routine> > (fun)) -> apply (a1);
else return apply (GEN_APPLY, fun, a1);
}
}
case 3:
{
generic fun= eval (v[0]);
if (is<primitive> (fun))
return as<primitive> (fun) -> apply (x);
else {
generic a1= eval (v[1]);
if (is<exception> (a1)) return a1;
generic a2= eval (v[2]);
if (is<exception> (a2)) return a2;
if (is<routine> (fun))
return as<routine> (fun) -> apply (a1, a2);
else if (is<alias<routine> > (fun))
return get_alias (as<alias<routine> > (fun)) -> apply (a1, a2);
else return apply (GEN_APPLY, vec (fun, a1, a2));
}
}
default:
{
generic fun= eval (v[0]);
if (is<primitive> (fun))
return as<primitive> (fun) -> apply (x);
else {
vector<generic> args= eval_cdr (v);
if (N(args)>0 && is<exception> (args[N(args)-1]))
return args[N(args)-1];
if (is<routine> (fun))
return as<routine> (fun) -> apply (args);
else if (is<alias<routine> > (fun))
return get_alias (as<alias<routine> > (fun)) -> apply (args);
else return apply (GEN_APPLY, cons (fun, args));
}
}
}
}
else return construct (x);
#ifdef BASIX_ENABLE_EXCEPTIONS
}
catch (const exception& err) {
generic msg= *err;
generic ret= append (range (msg, 0, N(msg)-1), gen (x));
return as<generic> (exception (ret));
}
#endif
}
| vector< generic > eval_cdr | ( | const vector< generic > & | x ) | const [virtual] |
Definition at line 274 of file base_evaluator.cpp.
References base_evaluator_rep::eval().
Referenced by base_evaluator_rep::eval().
{
nat n= N(v)-1;
vector<generic> w= fill<generic> (n);
for (nat i=0; i<n; i++) {
w[i]= eval (v[i+1]);
if (is<exception> (w[i])) return range (w, 0, i+1);
}
return w;
}
| generic get | ( | const generic & | var ) | const [virtual] |
Definition at line 161 of file base_evaluator.cpp.
References base_evaluator_rep::env.
{
return env [var];
}
| bool get | ( | const generic & | var, |
| generic & | val | ||
| ) | const [virtual] |
Definition at line 166 of file base_evaluator.cpp.
References base_evaluator_rep::env.
{
return env->get (var, val);
}
| void * get_internal_data | ( | ) | const [virtual] |
Definition at line 40 of file base_evaluator.cpp.
References base_evaluator_rep::env.
{
return (void*) &env;
}
| void overload | ( | const generic & | var, |
| const generic & | val, | ||
| nat | p | ||
| ) | const [virtual] |
Definition at line 77 of file base_evaluator.cpp.
References base_evaluator_rep::env, mmx::overloaded_routine(), and mmx::read().
{
/*
mmout << "Overload ";
mmout << as_lisp (var);
if (is<routine> (val)) {
routine f= as<routine> (val);
vector<nat> ids= f->signature ();
extern generic type_name (nat id);
mmout << ": ";
for (nat i=1; i<N(ids); i++) {
if (i!=1) mmout << ", ";
mmout << as_lisp (type_name (ids[i]));
}
mmout << " -> " << as_lisp (type_name (ids[0]));
}
mmout << "\n";
*/
base_evaluator_rep* me= const_cast<base_evaluator_rep*> (this);
if (is<routine> (val)) {
routine f= as<routine> (val);
if (f->is_overloaded ()) {
vector<routine> rs= f->meanings ();
for (nat i=0; i<N(rs); i++)
overload (var, as<generic> (rs[i]), pen);
return;
}
if (exact_eq (var, GEN_NEW)) {
const vector<nat> ids= f->signature ();
ASSERT (N(ids) == 2, "constructor should take one argument");
me->env [as<generic> (ids[1])]= val;
}
else if (exact_eq (var, GEN_CONVERT) || exact_eq (var, GEN_UPGRADE) ||
exact_eq (var, GEN_DOWNGRADE) || exact_eq (var, GEN_REWRITE))
{
const vector<nat> ids= f->signature ();
ASSERT (N(ids) == 2, "converter should take one argument");
nat src = ids[1], dest= ids[0];
if (src == dest) return;
nat trv= 0;
if (exact_eq (var, GEN_DOWNGRADE) || exact_eq (var, GEN_REWRITE))
trv += 2;
if (exact_eq (var, GEN_UPGRADE) || exact_eq (var, GEN_REWRITE))
trv += 1;
inside (env) -> set_converter (src, dest, val, trv, pen);
//mmout << "Converter: " << src << ", " << dest << ", " << val << "\n";
//mmout << "Environment: " << env << "\n";
}
/*
else if (N(f->signature ()) == 0)
me->env [var]= val;
*/
else {
if (!env->contains (var) ||
!is<routine> (env[var]) ||
N(f->signature ()) == 0)
me->env [var]= make_abstract (overloaded_routine (var, env));
routine r= as<routine> (read (me->env, var));
if (r->ref_count != 2) {
/*
mmout << "Cloning " << as_lisp (var) << " = ";
mmout << as_lisp (r -> function_body ()) << "\n";
mmout << "Done\n";
*/
r= r->clone ();
me->env [var]= as<generic> (r);
//mmout << "ref_count= " << r->ref_count << "\n";
}
//mmout << "Overloading\n";
r->overload (f);
//mmout << "Overloaded\n";
}
}
else me->env [var]= val;
}
| void reset | ( | const generic & | var ) | const [virtual] |
Definition at line 71 of file base_evaluator.cpp.
References base_evaluator_rep::env.
{
base_evaluator_rep* me= const_cast<base_evaluator_rep*> (this);
mmx::reset (me->env, var);
}
| void set | ( | const generic & | var, |
| const generic & | val | ||
| ) | const [virtual] |
Definition at line 65 of file base_evaluator.cpp.
References base_evaluator_rep::env.
{
base_evaluator_rep* me= const_cast<base_evaluator_rep*> (this);
me->env [var]= val;
}
environment env [protected] |
Definition at line 24 of file base_evaluator.hpp.
Referenced by base_evaluator_rep::apply(), base_evaluator_rep::construct(), base_evaluator_rep::contains(), base_evaluator_rep::eval(), base_evaluator_rep::get(), base_evaluator_rep::get_internal_data(), base_evaluator_rep::overload(), base_evaluator_rep::reset(), and base_evaluator_rep::set().
1.7.2