#ifndef __REPL_REPLENVIRONMENT_H #define __REPL_REPLENVIRONMENT_H #include #include "REPL/ExtREPL" namespace GUI { }; namespace REPLX { struct REPL; typedef Values::Node EnvEntry; /* actually Application, but we don't want the client to assume silly things */ using namespace GUI; /* [ ->!] [ ->!] [ ->!] ^ ^ ^ fTailEnvironment fTailUserEnvironment fTailUserEnvironmentFrontier entry = ((\x B) a) ((\+ (\* B (M +)) (\x nil) is used as a tail marker for where the user code should go. ) P) env-entry = application(abstraction(\+, xxx), P) xxx = application(abstraction(...), ...) or nil or junk there is a dummy entry at the beginnning, called fTailEnvironment. fTailUserEnvironment is the last entry before the user environment, it can be used to smuggle in new builtins. fTailUserEnvironmentFrontier is the last entry of the user environment. Its tail will be the code to execute. */ static bool envEntry_P(Values::NodeT node) { return(application_P(node) && abstraction_P(get_application_operator(node))); } /* FIXME delete this! */ static void envEntrySetTail(Values::NodeT node, Values::NodeT value) { // continue at the abstraction body embedded in the application (all other slots are used up) assert(envEntry_P(node)); Values::NodeT op = get_application_operator(node); assert(abstraction_P(op)); ((Values::Abstraction*)op)->body = value; // shoot me now } static Values::NodeT envEntryGetTail(Values::NodeT node) { // continue at the abstraction body embedded in the application (all other slots are used up) assert(envEntry_P(node)); Values::NodeT op = get_application_operator(node); assert(abstraction_P(op)); return(get_abstraction_body(op)); } static Values::NodeT makeEnvEntry(Values::NodeT /* symbol */ name, Values::NodeT body, Values::NodeT next) { return(makeApplication(makeAbstraction(name, next), body)); } static void getEnvEntry(Values::NodeT entry, Values::NodeT& name, Values::NodeT& body, Values::NodeT& next) { assert(application_P(entry)); Values::NodeT abstraction = get_application_operator(entry); assert(abstraction_P(abstraction)); name = get_abstraction_parameter(abstraction); next = get_abstraction_body(abstraction); body = get_application_operand(entry); } Values::NodeT REPL_get_definition_backwards(struct REPL* self, Values::NodeT /*Symbol*/ name, size_t backOffset) { // envEntrySetTail(self->fTailUserEnvironmentFrontier, NULL); Values::NodeT x_name; Values::NodeT x_body; Values::NodeT x_next; std::deque matches; for(Values::NodeT entry = envEntryGetTail(self->fTailEnvironment); getEnvEntry(entry, x_name, x_body, x_next), true; entry = x_next) { if(x_name == name) { matches.push_front(x_body); } if(entry == self->fTailUserEnvironmentFrontier) break; } if(backOffset >= 0 && backOffset < matches.size()) return(matches[backOffset]); else return(NULL); } Values::NodeT REPL_get_definition(struct REPL* self, size_t offset) { // envEntrySetTail(self->fTailUserEnvironmentFrontier, NULL); Values::NodeT x_name; Values::NodeT x_body; Values::NodeT x_next; std::deque matches; for(Values::NodeT entry = envEntryGetTail(self->fTailEnvironment); getEnvEntry(entry, x_name, x_body, x_next), true; entry = x_next, --offset) { if(offset == 0) return(x_body); if(entry == self->fTailUserEnvironmentFrontier) break; } return(NULL); } bool exit_P(const char* text) { return(strncmp(text, "#exit", strlen("#exit")) == 0); } Values::NodeT REPL_prepare(struct REPL* self, Values::NodeT input); Scanners::OperatorPrecedenceList* REPL_ensure_operator_precedence_list(struct REPL* self) { Scanners::OperatorPrecedenceList* result; result = new Scanners::OperatorPrecedenceList; // FIXME add the operators, reading the environment return(result); } template Values::NodeT REPL_parse(struct REPL* self, const char* command, int commandLen, T destination/*for errors*/) { // TODO just call the builtin! RStrMathParser /* is not allowed to both print stuff AND return non-null, except when it updates the destination iter */ Scanners::MathParser parser; FILE* input_file = fmemopen((void*) command, commandLen, "r"); if(input_file) { try { parser.push(input_file, 0, ""); Values::NodeT result = NULL; result = parser.parse(REPL_ensure_operator_precedence_list(self), Symbols::SlessEOFgreater); fclose(input_file); return(result); } catch(...) { fclose(input_file); throw; } } // REMOVE REPL_queue_scroll_down(self); return(NULL); } template bool REPL_execute(struct REPL* self, Values::NodeT input, T destination) { bool B_ok = false; try { Values::NodeT result = input; Evaluators::resetWorld(); if(self->fBRunIO) result = Values::makeApplication(&Evaluators::IORunner, result); result = REPL_prepare(self, result); result = Evaluators::reduce(result); /*std::string v = result ? result->str() : "OK"; v = " => " + v + "\n"; REPL_insert_into_output_buffer(self, destination, v.c_str());*/ REPL_enqueue_LATEX(self, result, destination); B_ok = true; } catch(Evaluators::EvaluationException e) { std::string v = e.what() ? e.what() : "error"; v = " => " + v + "\n"; REPL_insert_into_output_buffer(self, destination, v.c_str()); } REPL_set_file_modified(self, true); return(B_ok); } #define DECLARE_REPL_OPERATION(P) \ DECLARE_FULL_OPERATION(P) #define DEFINE_REPL_OPERATION(P, B) \ DEFINE_FULL_OPERATION(P, B) static Values::NodeT REPL_import(Values::NodeT options, Values::NodeT argument); DEFINE_REPL_OPERATION(RImporter, { return(REPL_import(fn, argument)); }) static Values::HashTable setFromList(Values::NodeT listNode) { Values::HashTable result; for(Values::Cons* node = Evaluators::evaluateToCons(listNode); node; node = Evaluators::evaluateToCons(node->tail)) { const char* name = Values::get_symbol1_name(Values::get_cons_head(node)); // TODO error handling if(name) result[GCx_strdup(name)] = NULL; } return(result); } static Values::NodeT REPL_import(Values::NodeT options, Values::NodeT argument) { Evaluators::CXXArguments arguments = Evaluators::CXXfromArguments(options, argument); Evaluators::CXXArguments::const_iterator iter = arguments.begin(); struct REPL* self = (struct REPL*)(Evaluators::get_pointer(iter->second)); assert(self); ++iter; assert(iter != arguments.end()); Values::NodeT filename = iter->second; FETCH_WORLD(iter); if(!str_P(filename)) throw Evaluators::EvaluationException("import: expected Str filename"); Values::NodeT body = Evaluators::import_module(options/*FIXME*/, filename); //body = Evaluators::annotate(body); //REPL_prepare(self, body); //REPL_add_to_environment(self, name, body); // TODO make it possible to limit imports (use whitelist and blacklist) Values::NodeT whitelistN = Evaluators::CXXgetKeywordArgumentValue(arguments, Values::keywordFromStr("whitelist:")); Values::HashTable whitelist = setFromList(whitelistN); Values::NodeT blacklistN = Evaluators::CXXgetKeywordArgumentValue(arguments, Values::keywordFromStr("blacklist:")); Values::HashTable blacklist = setFromList(blacklistN); Values::NodeT prefixN = Evaluators::CXXgetKeywordArgumentValue(arguments, Values::keywordFromStr("prefix:")); char* prefix = prefixN ? Evaluators::get_string(prefixN) : NULL; Values::NodeT exports = Evaluators::reduce(Values::makeApplication(body, Symbols::Sexports)); Values::NodeT usedExports = NULL; blacklist["exports"] = NULL; // Sexports for(Values::Cons* node = Evaluators::evaluateToCons(exports); node; node = Evaluators::evaluateToCons(node->tail)) { Values::NodeT key = get_cons_head(node); const char* xname = Values::get_symbol1_name(key); if(xname && (whitelist.empty() || whitelist.find(xname) != whitelist.end()) && blacklist.find(xname) == blacklist.end()) { Values::NodeT xbody = Values::makeApplication(body, Evaluators::quote(key)); //REPL_prepare(self, xbody); if(prefix && prefix[0]) { std::stringstream sst; sst << prefix << xname; std::string v = sst.str(); key = Values::symbolFromStr(v.c_str()); } REPL_add_to_environment(self, key, xbody); usedExports = Values::makeCons(key, usedExports); } } return(CHANGED_WORLD(usedExports)); } static Values::NodeT REPL_define(Values::NodeT options, Values::NodeT argument) { Evaluators::CXXArguments arguments = Evaluators::CXXfromArguments(options, argument); Evaluators::CXXArguments::const_iterator iter = arguments.begin(); assert(iter != arguments.end()); struct REPL* self = (struct REPL*)(Evaluators::get_pointer(iter->second)); assert(self); ++iter; assert(iter != arguments.end()); Values::NodeT name = iter->second; ++iter; assert(iter != arguments.end()); Values::NodeT body = iter->second; FETCH_WORLD(iter); /* make sure it would actually work... (otherwise would throw exception) */ REPL_prepare(self, body); REPL_add_to_environment(self, name, body); return(CHANGED_WORLD(body)); } static Values::NodeT REPL_describe(Values::NodeT options, Values::NodeT argument) { Evaluators::CXXArguments arguments = Evaluators::CXXfromArguments(options, argument); struct REPL* self = (struct REPL*)(Evaluators::get_pointer(arguments.front().second)); assert(self); Evaluators::CXXArguments::const_iterator iter = arguments.begin(); assert(iter != arguments.end()); ++iter; assert(iter != arguments.end()); argument = iter->second; FETCH_WORLD(iter); Values::NodeT backOffsetNode = Evaluators::CXXgetKeywordArgumentValue(arguments, Values::keywordFromStr("@backOffset:")); int backOffset = backOffsetNode ? Evaluators::get_int(backOffsetNode) : 0; if(Values::get_symbol1_name(argument) != NULL) { Values::NodeT body = REPL_get_definition_backwards(self, argument, backOffset); return(CHANGED_WORLD(body)); } else return(CHANGED_WORLD(argument)); } static Values::NodeT REPL_purge(Values::NodeT options, Values::NodeT argument) { Evaluators::CXXArguments arguments = Evaluators::CXXfromArguments(options, argument); Evaluators::CXXArguments::const_iterator iter = arguments.begin(); assert(iter != arguments.end()); struct REPL* self = (struct REPL*)(Evaluators::get_pointer(iter->second)); assert(self); FETCH_WORLD(iter); // TODO actually purge return(CHANGED_WORLD(NULL)); } static Values::NodeT REPL_getOperatorPrecedenceList(Values::NodeT options, Values::NodeT argument) { Evaluators::CXXArguments arguments = Evaluators::CXXfromArguments(options, argument); Evaluators::CXXArguments::const_iterator iter = arguments.begin(); assert(iter != arguments.end()); struct REPL* self = (struct REPL*)(Evaluators::get_pointer(iter->second)); std::string v = Evaluators::str(iter->second); assert(self); FETCH_WORLD(iter); return(CHANGED_WORLD(Values::makeBox(REPL_ensure_operator_precedence_list(self), NULL/*TODO*/))); } using namespace GUI; static Values::NodeT REPL_execute1(Values::NodeT options, Values::NodeT argument) { Evaluators::CXXArguments arguments = Evaluators::CXXfromArguments(options, argument); Evaluators::CXXArguments::const_iterator iter = arguments.begin(); assert(iter != arguments.end()); struct REPL* self = (struct REPL*)(Evaluators::get_pointer(iter->second)); assert(self); ++iter; assert(iter != arguments.end()); Values::NodeT expression = iter->second; { FETCH_WORLD(iter); FILL_END_ITER return(CHANGED_WORLD(Evaluators::internNative(REPL_execute(self, expression, END_ITER)))); } } //fTailEnvironment //fTailUserEnvironment /* =fTailBuiltinEnvironmentFrontier */ //fTailUserEnvironmentFrontier void REPL_add_to_environment_simple(struct REPL* self, Values::NodeT name, Values::NodeT value); static void REPL_add_static_builtin_binding(struct REPL* self, Values::NodeT name, Values::NodeT value) { REPL_add_to_environment_simple(self, name, value); } using namespace Evaluators; #define DECLARE_REPL_OPERATION(P) \ DECLARE_FULL_OPERATION(P) #define DEFINE_REPL_OPERATION(P, B) \ DEFINE_FULL_OPERATION(P, B) DEFINE_REPL_OPERATION(RInformant, { return(REPL_describe(fn, argument)); }) DEFINE_REPL_OPERATION(RDefiner, { return(REPL_define(fn, argument)); }) DEFINE_REPL_OPERATION(ROperatorPrecedenceListGetter, { return(REPL_getOperatorPrecedenceList(fn, argument)); }) DEFINE_REPL_OPERATION(RPurger, { return(REPL_purge(fn, argument)); }) DEFINE_REPL_OPERATION(RExecutor, { return(REPL_execute1(fn, argument)); }) //#define REPL_add_builtin_method(s, n, v) REPL_add_to_environment_simple(s, n, uncurried((Values::NodeT) v, (Values::NodeT) Values::makeApplication(&Evaluators::Quoter, n))) #define REPL_add_builtin_method(s, n, v) REPL_add_to_environment_simple(s, n, v) static void REPL_init_builtins(struct REPL* self) { if(self->fTailUserEnvironment && envEntryGetTail(self->fTailUserEnvironment)) { fprintf(stderr, "warning: There is a user environment, so not clobbering it by builtins.\n"); return; } if(!self->fTailEnvironment) { /* first init */ self->fTailEnvironment = makeApplication(makeAbstraction(Symbols::Snil, NULL/*mutable*/), NULL); self->fTailUserEnvironment = self->fTailEnvironment; self->fTailUserEnvironmentFrontier = self->fTailUserEnvironment; } Evaluators::BuiltinSelector_init(); REPL_add_static_builtin_binding(self, Symbols::Squote, &Evaluators::Quoter); /* keep at the beginning */ REPL_add_static_builtin_binding(self, Symbols::Sdot, Values::makeAbstraction(Symbols::Sa, Values::makeAbstraction(Symbols::Sb, Values::makeApplication(Symbols::Sa, Symbols::Sb)))); // TODO close REPL_add_static_builtin_binding(self, Symbols::Slet, &Evaluators::Quoter); // dummy // ??? REPL_add_static_builtin_binding(self, Symbols::Sinline, &Evaluators::Reducer); REPL_add_static_builtin_binding(self, Values::symbolFromStr("REPL"), Values::makeAbstraction(Symbols::Sa, Values::makeApplication(Values::makeApplication( &REPLX::REPLMethodGetter, // Values::makeApplication(&Evaluators::BuiltinGetter, Values::symbolFromStr("REPLMethods")), Symbols::Sa), Values::makeBox(self, Values::symbolFromStr("REPL"))))); REPL_add_static_builtin_binding(self, Symbols::SrequireModule, &ModuleLoader); REPL_add_builtin_method(self, Symbols::SBuiltins, &Evaluators::BuiltinGetter); REPL_add_builtin_method(self, Symbols::Scolon, &Evaluators::Conser); // module dispatch list needs that anyway REPL_add_builtin_method(self, Symbols::Snil, NULL); // module dispatch list needs that anyway //REPL_add_builtin_method(self, Symbols::Sreturnexclam, get_module_entry_accessor("IO", Symbols::Sreturnexclam)); self->fTailUserEnvironment = self->fTailUserEnvironmentFrontier; /*self->fTailUserEnvironmentFrontier = self->fTailUserEnvironmentFrontier;*/ } //int REPL_add_to_environment_simple_GUI(struct REPL* self, Values::NodeT name, Values::NodeT value); /* returns: whether we just inserted something after endIter, ostensibly moving stuff */ bool REPL_add_to_environment_simple_end(struct REPL* self, Values::NodeT name, Values::NodeT value, Values::NodeT endIter) { using namespace Values; using namespace Evaluators; int index = REPL_add_to_environment_simple_GUI(self, name, value); assert(index >= 0); EnvEntry* prevNode = self->fTailEnvironment; EnvEntry* shiftedNode = NULL; EnvEntry* newNode; // there is a sentinel at the beginning. for(; index > 0 && prevNode != endIter; --index) prevNode = envEntryGetTail(prevNode); if(!prevNode) { abort(); } shiftedNode = envEntryGetTail(prevNode); newNode = makeEnvEntry(name, value, /*next*/shiftedNode); envEntrySetTail(prevNode, newNode); return(prevNode == endIter); } void REPL_add_to_environment_simple(struct REPL* self, Values::NodeT name, Values::NodeT value) { if(REPL_add_to_environment_simple_end(self, name, value, self->fTailUserEnvironmentFrontier)) self->fTailUserEnvironmentFrontier = envEntryGetTail(self->fTailUserEnvironmentFrontier); } static Values::NodeT REPL_close_environment(struct REPL* self, Values::NodeT node) { if(self->fTailUserEnvironmentFrontier) { if(increaseGeneration() == 1) { /* overflow */ mapTree(NULL, Values::uncacheNodeResult, Evaluators::evaluateToCons(self->fTailUserEnvironment)->tail); } envEntrySetTail(self->fTailUserEnvironmentFrontier, node); return(envEntryGetTail(self->fTailEnvironment)); } else return(node); } static void REPL_unfilter_environment(struct REPL* self, Values::NodeT environment) { /* filters out Builtins */ using namespace Values; Values::NodeT name; Values::NodeT body; Values::NodeT next; if(environment == Symbols::Snil) { self->fTailUserEnvironmentFrontier = self->fTailUserEnvironment; return; } for(; environment; environment = next) { getEnvEntry(environment, name, body, next); REPL_add_to_environment_simple_GUI(self, name, body); if(next == NULL || next == Symbols::Snil) { // used as a tail marker self->fTailUserEnvironmentFrontier = environment; envEntrySetTail(self->fTailUserEnvironmentFrontier, NULL); break; } } } void REPL_set_environment(struct REPL* self, EnvEntry* environment) { envEntrySetTail(self->fTailUserEnvironment, NULL); envEntrySetTail(self->fTailUserEnvironmentFrontier, NULL); // help GC self->fTailUserEnvironmentFrontier = NULL; envEntrySetTail(self->fTailUserEnvironment, environment); REPL_unfilter_environment(self, environment); assert(self->fTailUserEnvironmentFrontier != NULL); } Values::NodeT REPL_get_user_environment(struct REPL* self) { if(self->fTailUserEnvironmentFrontier) envEntrySetTail(self->fTailUserEnvironmentFrontier, NULL); /* clear the last command from the environment, nobody cares. */ return(self->fTailUserEnvironment ? envEntryGetTail(self->fTailUserEnvironment) : NULL); } Values::NodeT REPL_prepare(struct REPL* self, Values::NodeT input) { return(prepareModule(REPL_close_environment(self, input))); } /* TODO defrec! is defrec 'a 5 let a := rec\f 5 in ... */ REGISTER_BUILTIN(RImporter, (-3), 1, Values::symbolFromStr("import!")) REGISTER_BUILTIN(RInformant, (-3), 1, Values::symbolFromStr("describe!")) REGISTER_BUILTIN(RDefiner, (-4), 1, Values::symbolFromStr("define!")) REGISTER_BUILTIN(ROperatorPrecedenceListGetter, (-2), 1, Values::symbolFromStr("getOperatorPrecedenceList!")) REGISTER_BUILTIN(RPurger, (-2), 1, Values::symbolFromStr("purge!")) REGISTER_BUILTIN(RExecutor, (-3), 1, Values::symbolFromStr("execute!")) REGISTER_STR(REPL, return("REPL");) }; /* end namespace */ #endif /* __REPL_REPLENVIRONMENT_H */