#ifndef __EVALUATORS_H #define __EVALUATORS_H #include //#include #include <5D/Values> #include <5D/Operations> #include <5D/Evaluators> #include "Values/Values" namespace Evaluators { class EvaluationException : public std::exception { char* message; public: EvaluationException(const char* s) throw(); virtual const char* what() const throw(); }; static inline Values::NodeT throwFallback(const char* text) { throw Evaluators::EvaluationException(text); return(NULL); } #define FALLBACK Evaluators::throwFallback("could not reduce") /* (Values::NodeT) reduce(makeOperation(Symbols::Sspace, fallback, argument))*/ void getFreeVariables(Values::NodeT root, Values::Hashtable& freeNames); Values::NodeT annotate(Values::NodeT root); extern int fGeneration; static inline bool application_result_P(Values::NodeT app) { return(((Values::Application*) app)->resultGeneration == fGeneration); } bool quote_P(Values::NodeT root); extern int recursionLevel; Values::NodeT reduce1(Values::NodeT term); static inline Values::NodeT reduce(Values::NodeT term) { Values::NodeT result; //std::string tv = Evaluators::str(term); //fprintf(stderr, "DEBUG: (%d) reducing %s\n", recursionLevel, tv.c_str()); if(!Values::application_P(term)) result = term; else { if(application_result_P(term)) result = (((Values::Application*) term)->result); else result = (reduce1(term)); } //std::string rv = Evaluators::str(result); //fprintf(stderr, "=> result %s\n", rv.c_str()); return(result); } Values::NodeT close(Values::NodeT /* symbol */ parameter, Values::NodeT argument, Values::NodeT body); Values::NodeT makeError(const char* reason); bool define_P(Values::NodeT input); Values::NodeT programFromSExpression(Values::NodeT root); Values::NodeT getResult(Values::NodeT input); Values::NodeT quote(Values::NodeT node); int increaseGeneration(void); Values::NodeT replace(Values::NodeT needle /* not Symbol */, Values::NodeT replacement, Values::NodeT haystack);/* intended for builtins only */ typedef Values::NodeT (replace_predicate_t)(void* userData, Values::NodeT node); Values::NodeT mapTree(void* userData, replace_predicate_t* replacer, Values::NodeT term); DECLARE_SIMPLE_OPERATION(Reducer) DECLARE_SIMPLE_OPERATION(Quoter) DECLARE_SIMPLE_OPERATION(FreeVariablesGetter) /* BEGIN Curried Operations */ struct CurriedOperation : Values::Node { Values::NodeT fOperation; /* either CurriedOperation* or CProcedure* */ Values::NodeT fArgument; //CurriedOperation(Values::NodeT operation, Values::NodeT argument) : fOperation(operation), fArgument(argument) {} }; static inline Values::NodeT get_curried_operation_operation(Values::NodeT operation) { return(((CurriedOperation*)operation)->fOperation); } static inline Values::NodeT get_curried_operation_argument(Values::NodeT operation) { return(((CurriedOperation*)operation)->fArgument); } CurriedOperation* makeCurriedOperation(Values::NodeT operation, Values::NodeT argument); static inline bool curried_operation_P(Values::NodeT root) { return(dynamic_cast(root) != NULL); } /* END Curried Operations */ static inline bool cons_P(Values::NodeT node) { return(pair_P(node)); //if(pair_P(node)) { // Values::NodeT snd = Values::get_pair_second(node); // snd = evaluate(snd); // return(nil_P(snd) || pair_P(snd)); //} else // return(false); } static inline Values::NodeT uncurried(Values::NodeT actualOperation, Values::NodeT fallback) { return(Values::makeApplication(&Evaluators::Reducer, Values::makeApplication(actualOperation, fallback))); } }; #endif /*ndef __EVALUATORS_H*/