map instead of pile if's
This commit is contained in:
parent
ecffde3635
commit
c35d00e155
202
ml.cpp
202
ml.cpp
|
|
@ -2063,7 +2063,6 @@ void load_std_lib(MlEnvironment &env) {
|
|||
|
||||
// Does this environment, or its parent environment, have a variable?
|
||||
bool MlEnvironment::has(const std::string &name) const {
|
||||
// Find the value in the map
|
||||
std::map<std::string, MlValue>::const_iterator itr = defs.find(name);
|
||||
if (itr != defs.end())
|
||||
// If it was found
|
||||
|
|
@ -2075,128 +2074,143 @@ bool MlEnvironment::has(const std::string &name) const {
|
|||
else return false;
|
||||
}
|
||||
|
||||
// Get the value associated with this name in this scope
|
||||
MlValue MlEnvironment::get(const std::string &name) const {
|
||||
|
||||
|
||||
std::map <std::string, Builtin> builtin_funcs {
|
||||
// Special forms
|
||||
if (name == "define") return MlValue("define", builtin::define);
|
||||
if (name == "set!") return MlValue("set!", builtin::setx);
|
||||
if (name == "if") return MlValue("if", builtin::if_then_else);
|
||||
if (name == "cond") return MlValue("if", builtin::cond);
|
||||
if (name == "do") return MlValue("do", builtin::do_block);
|
||||
if (name == "for") return MlValue("for", builtin::for_loop);
|
||||
if (name == "while") return MlValue("while", builtin::while_loop);
|
||||
if (name == "scope") return MlValue("scope", builtin::scope);
|
||||
if (name == "quote") return MlValue("quote", builtin::quote);
|
||||
if (name == "defun") return MlValue("defun", builtin::defun);
|
||||
if (name == "lambda") return MlValue("lambda", builtin::lambda);
|
||||
if (name == "and") return MlValue("and", builtin::do_and);
|
||||
if (name == "or") return MlValue("or", builtin::do_or);
|
||||
std::make_pair("define", builtin::define),
|
||||
std::make_pair("lambda", builtin::lambda),
|
||||
std::make_pair("if", builtin::if_then_else),
|
||||
std::make_pair("if", builtin::cond),
|
||||
std::make_pair("do", builtin::do_block),
|
||||
std::make_pair("for", builtin::for_loop),
|
||||
std::make_pair("while", builtin::while_loop),
|
||||
std::make_pair("scope", builtin::scope),
|
||||
std::make_pair("quote", builtin::quote),
|
||||
std::make_pair("defun", builtin::defun),
|
||||
std::make_pair("and", builtin::do_and),
|
||||
std::make_pair("or", builtin::do_or),
|
||||
std::make_pair("set!", builtin::setx),
|
||||
|
||||
// Comparison operations
|
||||
if (name == "=") return MlValue("=", builtin::eq);
|
||||
if (name == "!=") return MlValue("!=", builtin::neq);
|
||||
if (name == ">") return MlValue(">", builtin::greater);
|
||||
if (name == "<") return MlValue("<", builtin::less);
|
||||
if (name == ">=") return MlValue(">=", builtin::greater_eq);
|
||||
if (name == "<=") return MlValue("<=", builtin::less_eq);
|
||||
std::make_pair("=", builtin::eq),
|
||||
std::make_pair("!=", builtin::neq),
|
||||
std::make_pair(">", builtin::greater),
|
||||
std::make_pair("<", builtin::less),
|
||||
std::make_pair(">=", builtin::greater_eq),
|
||||
std::make_pair("<=", builtin::less_eq),
|
||||
|
||||
// Meta operations
|
||||
if (name == "eval") return MlValue("eval", builtin::eval);
|
||||
if (name == "type") return MlValue("type", builtin::get_type_name);
|
||||
if (name == "parse") return MlValue("parse", builtin::parse);
|
||||
std::make_pair("eval", builtin::eval),
|
||||
std::make_pair("type", builtin::get_type_name),
|
||||
std::make_pair("parse", builtin::parse),
|
||||
|
||||
// Arithmetic operations
|
||||
if (name == "+") return MlValue("+", builtin::sum);
|
||||
if (name == "-") return MlValue("-", builtin::subtract);
|
||||
if (name == "*") return MlValue("*", builtin::product);
|
||||
if (name == "/") return MlValue("/", builtin::divide);
|
||||
if (name == "%") return MlValue("%", builtin::remainder);
|
||||
std::make_pair("+", builtin::sum),
|
||||
std::make_pair("-", builtin::subtract),
|
||||
std::make_pair("*", builtin::product),
|
||||
std::make_pair("/", builtin::divide),
|
||||
std::make_pair("%", builtin::remainder),
|
||||
|
||||
// List operations
|
||||
if (name == "list") return MlValue("list", builtin::list);
|
||||
if (name == "insert") return MlValue("insert", builtin::insert);
|
||||
if (name == "index") return MlValue("index", builtin::index);
|
||||
if (name == "remove") return MlValue("remove", builtin::remove);
|
||||
if (name == "len") return MlValue("len", builtin::len);
|
||||
if (name == "push") return MlValue("push", builtin::push);
|
||||
if (name == "pop") return MlValue("pop", builtin::pop);
|
||||
if (name == "head") return MlValue("head", builtin::head);
|
||||
if (name == "tail") return MlValue("tail", builtin::tail);
|
||||
if (name == "first") return MlValue("first", builtin::head);
|
||||
if (name == "second") return MlValue("second", builtin::second);
|
||||
if (name == "last") return MlValue("last", builtin::pop);
|
||||
if (name == "range") return MlValue("range", builtin::range);
|
||||
std::make_pair("list", builtin::list),
|
||||
std::make_pair("insert", builtin::insert),
|
||||
std::make_pair("index", builtin::index),
|
||||
std::make_pair("remove", builtin::remove),
|
||||
std::make_pair("len", builtin::len),
|
||||
std::make_pair("push", builtin::push),
|
||||
std::make_pair("pop", builtin::pop),
|
||||
std::make_pair("head", builtin::head),
|
||||
std::make_pair("tail", builtin::tail),
|
||||
std::make_pair("first", builtin::head),
|
||||
std::make_pair("second", builtin::second),
|
||||
std::make_pair("last", builtin::pop),
|
||||
std::make_pair("range", builtin::range),
|
||||
|
||||
// Functional operations
|
||||
if (name == "map") return MlValue("map", builtin::map_list);
|
||||
if (name == "filter") return MlValue("filter", builtin::filter_list);
|
||||
if (name == "reduce") return MlValue("reduce", builtin::reduce_list);
|
||||
std::make_pair("map", builtin::map_list),
|
||||
std::make_pair("filter", builtin::filter_list),
|
||||
std::make_pair("reduce", builtin::reduce_list),
|
||||
|
||||
// IO operations
|
||||
if (name == "exit") return MlValue("exit", builtin::exit);
|
||||
if (name == "quit") return MlValue("quit", builtin::exit);
|
||||
if (name == "print") return MlValue("print", builtin::print);
|
||||
if (name == "input") return MlValue("input", builtin::input);
|
||||
if (name == "random") return MlValue("random", builtin::random);
|
||||
if (name == "include") return MlValue("include", builtin::include);
|
||||
if (name == "read-file") return MlValue("read-file", builtin::read_file);
|
||||
if (name == "read-file-lines") return MlValue("read-file-lines", builtin::read_file_lines);
|
||||
if (name == "write-file") return MlValue("write-file", builtin::write_file);
|
||||
if (name == "read-url") return MlValue("read-url", builtin::read_url);
|
||||
if (name == "system-cmd") return MlValue("system-cmd", builtin::system_cmd);
|
||||
if (name == "ls-dir") return MlValue("ls-dir", builtin::ls_dir);
|
||||
if (name == "is-file?") return MlValue("is-file?", builtin::is_file);
|
||||
if (name == "is-dir?") return MlValue("is-dir?", builtin::is_dir);
|
||||
if (name == "tcp-server") return MlValue("tcp-client", builtin::tcp_server);
|
||||
if (name == "tcp-client") return MlValue("tcp-client", builtin::tcp_client);
|
||||
std::make_pair("exit", builtin::exit),
|
||||
std::make_pair("quit", builtin::exit),
|
||||
std::make_pair("print", builtin::print),
|
||||
std::make_pair("input", builtin::input),
|
||||
std::make_pair("random", builtin::random),
|
||||
std::make_pair("include", builtin::include),
|
||||
std::make_pair("read-file", builtin::read_file),
|
||||
std::make_pair("read-file-lines", builtin::read_file_lines),
|
||||
std::make_pair("write-file", builtin::write_file),
|
||||
std::make_pair("read-url", builtin::read_url),
|
||||
std::make_pair("system-cmd", builtin::system_cmd),
|
||||
std::make_pair("ls-dir", builtin::ls_dir),
|
||||
std::make_pair("is-file?", builtin::is_file),
|
||||
std::make_pair("is-dir?", builtin::is_dir),
|
||||
std::make_pair("tcp-server", builtin::tcp_server),
|
||||
std::make_pair("tcp-client", builtin::tcp_client),
|
||||
|
||||
// parsing operations
|
||||
if (name == "parse-csv") return MlValue("parse-csv", builtin::parse_csv);
|
||||
if (name == "parse-json") return MlValue("parse-json", builtin::parse_json);
|
||||
std::make_pair("parse-csv", builtin::parse_csv),
|
||||
std::make_pair("parse-json", builtin::parse_json),
|
||||
|
||||
// Datetime operations
|
||||
if (name == "get-universal-time") return MlValue("get-universal-time", builtin::get_universal_time);
|
||||
if (name == "get-localtime-offset") return MlValue("get-localtime-offset", builtin::get_localtime_offset);
|
||||
if (name == "date-to-str") return MlValue("date-to-str", builtin::date_to_str);
|
||||
if (name == "str-to-date") return MlValue("str-to-date", builtin::str_to_date);
|
||||
if (name == "date-add") return MlValue("date-add", builtin::date_add);
|
||||
std::make_pair("get-universal-time", builtin::get_universal_time),
|
||||
std::make_pair("get-localtime-offset", builtin::get_localtime_offset),
|
||||
std::make_pair("date-to-str", builtin::date_to_str),
|
||||
std::make_pair("str-to-date", builtin::str_to_date),
|
||||
std::make_pair("date-add", builtin::date_add),
|
||||
|
||||
// String operations
|
||||
if (name == "debug") return MlValue("debug", builtin::debug);
|
||||
if (name == "sprintf") return MlValue("sprintf", builtin::sprintf);
|
||||
if (name == "display") return MlValue("display", builtin::display);
|
||||
if (name == "string-replace") return MlValue("string-replace", builtin::string_replace);
|
||||
if (name == "string-replace-re") return MlValue("string-replace-re", builtin::string_replace_re);
|
||||
if (name == "string-regex?") return MlValue("string-regex?", builtin::string_regex);
|
||||
if (name == "string-regex-list") return MlValue("string-regex-list", builtin::string_regex_list);
|
||||
if (name == "string-split") return MlValue("string-split", builtin::string_split);
|
||||
if (name == "string-pad") return MlValue("string-pad", builtin::string_pad);
|
||||
if (name == "string-rltrim") return MlValue("string-rltrim", builtin::string_rltrim);
|
||||
if (name == "string-case") return MlValue("string-case", builtin::string_case);
|
||||
if (name == "string-len") return MlValue("string-len", builtin::string_len);
|
||||
if (name == "string-substr") return MlValue("string-substr", builtin::string_substr);
|
||||
if (name == "string-find") return MlValue("string-find", builtin::string_find);
|
||||
std::make_pair("debug", builtin::debug),
|
||||
std::make_pair("sprintf", builtin::sprintf),
|
||||
std::make_pair("display", builtin::display),
|
||||
std::make_pair("string-replace", builtin::string_replace),
|
||||
std::make_pair("string-replace-re", builtin::string_replace_re),
|
||||
std::make_pair("string-regex?", builtin::string_regex),
|
||||
std::make_pair("string-regex-list", builtin::string_regex_list),
|
||||
std::make_pair("string-split", builtin::string_split),
|
||||
std::make_pair("string-pad", builtin::string_pad),
|
||||
std::make_pair("string-rltrim", builtin::string_rltrim),
|
||||
std::make_pair("string-case", builtin::string_case),
|
||||
std::make_pair("string-len", builtin::string_len),
|
||||
std::make_pair("string-substr", builtin::string_substr),
|
||||
std::make_pair("string-find", builtin::string_find),
|
||||
|
||||
// Casting operations
|
||||
if (name == "int") return MlValue("int", builtin::cast_to_int);
|
||||
if (name == "float") return MlValue("float", builtin::cast_to_float);
|
||||
if (name == "string") return MlValue("string", builtin::cast_to_string);
|
||||
std::make_pair("int", builtin::cast_to_int),
|
||||
std::make_pair("float", builtin::cast_to_float),
|
||||
std::make_pair("string", builtin::cast_to_string),
|
||||
|
||||
// Other special forms
|
||||
if (name == "benchmark") return MlValue("benchmark", builtin::benchmark);
|
||||
std::make_pair("benchmark", builtin::benchmark),
|
||||
|
||||
// Threading operations
|
||||
if (name == "thread-create") return MlValue("thread-create", builtin::thread_create);
|
||||
if (name == "thread-under-lock") return MlValue("thread-under-lock", builtin::thread_under_lock);
|
||||
if (name == "thread-sleep") return MlValue("thread-sleep", builtin::thread_sleep);
|
||||
if (name == "threads-join") return MlValue("threads-join", builtin::threads_join);
|
||||
std::make_pair("thread-create", builtin::thread_create),
|
||||
std::make_pair("thread-under-lock", builtin::thread_under_lock),
|
||||
std::make_pair("thread-sleep", builtin::thread_sleep),
|
||||
std::make_pair("threads-join", builtin::threads_join),
|
||||
|
||||
// Exceptions
|
||||
if (name == "try") return MlValue("try", builtin::try_block);
|
||||
if (name == "throw") return MlValue("throw", builtin::throw_exception);
|
||||
std::make_pair("try", builtin::try_block),
|
||||
std::make_pair("throw", builtin::throw_exception),
|
||||
|
||||
// Usql
|
||||
if (name == "usql") return MlValue("usql", builtin::usql);
|
||||
std::make_pair("usql", builtin::usql)
|
||||
};
|
||||
|
||||
|
||||
// Get the value associated with this name in this scope
|
||||
MlValue MlEnvironment::get(const std::string &name) const {
|
||||
|
||||
// PERF, here can be a few of for fast access
|
||||
if (name == "define") return MlValue("define", builtin::define);
|
||||
if (name == "if") return MlValue("if", builtin::if_then_else);
|
||||
if (name == "lambda") return MlValue("lambda", builtin::lambda);
|
||||
|
||||
auto it = builtin_funcs.find(name);
|
||||
if (it != builtin_funcs.end())
|
||||
return MlValue(name, it->second);
|
||||
|
||||
std::map<std::string, MlValue>::const_iterator itr = defs.find(name);
|
||||
if (itr != defs.end()) return itr->second;
|
||||
|
|
|
|||
Loading…
Reference in New Issue