func.cc revision 2e6cbfc3e72abc242b87f338c540afb9708a1df5
1#include "func.h" 2 3#include <stdio.h> 4 5#include <unordered_map> 6 7#include "eval.h" 8#include "log.h" 9#include "strutil.h" 10 11namespace { 12 13void PatsubstFunc(const vector<Value*>& args, Evaluator* ev, string* s) { 14 shared_ptr<string> pat = args[0]->Eval(ev); 15 shared_ptr<string> repl = args[1]->Eval(ev); 16 shared_ptr<string> str = args[2]->Eval(ev); 17 bool needs_space = false; 18 for (StringPiece tok : WordScanner(*str)) { 19 if (needs_space) 20 s->push_back(' '); 21 else 22 needs_space = true; 23 AppendSubstPattern(tok, *pat, *repl, s); 24 } 25} 26 27void StripFunc(const vector<Value*>&, Evaluator*, string*) { 28 printf("TODO(strip)"); 29} 30 31void SubstFunc(const vector<Value*>&, Evaluator*, string*) { 32 printf("TODO(subst)"); 33} 34 35void FindstringFunc(const vector<Value*>&, Evaluator*, string*) { 36 printf("TODO(findstring)"); 37} 38 39void FilterFunc(const vector<Value*>&, Evaluator*, string*) { 40 printf("TODO(filter)"); 41} 42 43void FilterOutFunc(const vector<Value*>&, Evaluator*, string*) { 44 printf("TODO(filter-out)"); 45} 46 47void SortFunc(const vector<Value*>&, Evaluator*, string*) { 48 printf("TODO(sort)"); 49} 50 51void WordFunc(const vector<Value*>&, Evaluator*, string*) { 52 printf("TODO(word)"); 53} 54 55void WordlistFunc(const vector<Value*>&, Evaluator*, string*) { 56 printf("TODO(wordlist)"); 57} 58 59void WordsFunc(const vector<Value*>&, Evaluator*, string*) { 60 printf("TODO(words)"); 61} 62 63void FirstwordFunc(const vector<Value*>&, Evaluator*, string*) { 64 printf("TODO(firstword)"); 65} 66 67void LastwordFunc(const vector<Value*>&, Evaluator*, string*) { 68 printf("TODO(lastword)"); 69} 70 71void JoinFunc(const vector<Value*>&, Evaluator*, string*) { 72 printf("TODO(join)"); 73} 74 75void WildcardFunc(const vector<Value*>&, Evaluator*, string*) { 76 printf("TODO(wildcard)"); 77} 78 79void DirFunc(const vector<Value*>&, Evaluator*, string*) { 80 printf("TODO(dir)"); 81} 82 83void NotdirFunc(const vector<Value*>&, Evaluator*, string*) { 84 printf("TODO(notdir)"); 85} 86 87void SuffixFunc(const vector<Value*>&, Evaluator*, string*) { 88 printf("TODO(suffix)"); 89} 90 91void BasenameFunc(const vector<Value*>&, Evaluator*, string*) { 92 printf("TODO(basename)"); 93} 94 95void AddsuffixFunc(const vector<Value*>&, Evaluator*, string*) { 96 printf("TODO(addsuffix)"); 97} 98 99void AddprefixFunc(const vector<Value*>&, Evaluator*, string*) { 100 printf("TODO(addprefix)"); 101} 102 103void RealpathFunc(const vector<Value*>&, Evaluator*, string*) { 104 printf("TODO(realpath)"); 105} 106 107void AbspathFunc(const vector<Value*>&, Evaluator*, string*) { 108 printf("TODO(abspath)"); 109} 110 111void IfFunc(const vector<Value*>&, Evaluator*, string*) { 112 printf("TODO(if)"); 113} 114 115void AndFunc(const vector<Value*>&, Evaluator*, string*) { 116 printf("TODO(and)"); 117} 118 119void OrFunc(const vector<Value*>&, Evaluator*, string*) { 120 printf("TODO(or)"); 121} 122 123void ValueFunc(const vector<Value*>&, Evaluator*, string*) { 124 printf("TODO(value)"); 125} 126 127void EvalFunc(const vector<Value*>&, Evaluator*, string*) { 128 printf("TODO(eval)"); 129} 130 131void ShellFunc(const vector<Value*>&, Evaluator*, string*) { 132 printf("TODO(shell)"); 133} 134 135void CallFunc(const vector<Value*>&, Evaluator*, string*) { 136 printf("TODO(call)"); 137} 138 139void ForeachFunc(const vector<Value*>&, Evaluator*, string*) { 140 printf("TODO(foreach)"); 141} 142 143void OriginFunc(const vector<Value*>&, Evaluator*, string*) { 144 printf("TODO(origin)"); 145} 146 147void FlavorFunc(const vector<Value*>&, Evaluator*, string*) { 148 printf("TODO(flavor)"); 149} 150 151void InfoFunc(const vector<Value*>& args, Evaluator* ev, string*) { 152 shared_ptr<string> a = args[0]->Eval(ev); 153 printf("%s\n", a->c_str()); 154 fflush(stdout); 155} 156 157void WarningFunc(const vector<Value*>& args, Evaluator* ev, string*) { 158 shared_ptr<string> a = args[0]->Eval(ev); 159 printf("%s:%d: %s\n", LOCF(ev->loc()), a->c_str()); 160 fflush(stdout); 161} 162 163void ErrorFunc(const vector<Value*>& args, Evaluator* ev, string*) { 164 shared_ptr<string> a = args[0]->Eval(ev); 165 ev->Error(StringPrintf("*** %s.", a->c_str())); 166} 167 168FuncInfo g_func_infos[] = { 169 { "patsubst", &PatsubstFunc, 3 }, 170 { "strip", &StripFunc, 1 }, 171 { "subst", &SubstFunc, 3 }, 172 { "findstring", &FindstringFunc, 2 }, 173 { "filter", &FilterFunc, 2 }, 174 { "filter-out", &FilterOutFunc, 2 }, 175 { "sort", &SortFunc, 1 }, 176 { "word", &WordFunc, 2 }, 177 { "wordlist", &WordlistFunc, 3 }, 178 { "words", &WordsFunc, 1 }, 179 { "firstword", &FirstwordFunc, 1 }, 180 { "lastword", &LastwordFunc, 1 }, 181 { "join", &JoinFunc, 2 }, 182 183 { "wildcard", &WildcardFunc, 1 }, 184 { "dir", &DirFunc, 1 }, 185 { "notdir", &NotdirFunc, 1 }, 186 { "suffix", &SuffixFunc, 1 }, 187 { "basename", &BasenameFunc, 1 }, 188 { "addsuffix", &AddsuffixFunc, 1 }, 189 { "addprefix", &AddprefixFunc, 1 }, 190 { "realpath", &RealpathFunc, 1 }, 191 { "abspath", &AbspathFunc, 1 }, 192 { "if", &IfFunc, 1 }, 193 { "and", &AndFunc, 1 }, 194 { "or", &OrFunc, 1 }, 195 { "value", &ValueFunc, 1 }, 196 { "eval", &EvalFunc, 1 }, 197 { "shell", &ShellFunc, 1 }, 198 { "call", &CallFunc, 1 }, 199 { "foreach", &ForeachFunc, 1 }, 200 { "origin", &OriginFunc, 1 }, 201 { "flavor", &FlavorFunc, 1 }, 202 { "info", &InfoFunc, 1 }, 203 { "warning", &WarningFunc, 1 }, 204 { "error", &ErrorFunc, 1 }, 205}; 206 207unordered_map<StringPiece, FuncInfo*>* g_func_info_map; 208 209} // namespace 210 211void InitFuncTable() { 212 g_func_info_map = new unordered_map<StringPiece, FuncInfo*>; 213 for (size_t i = 0; i < sizeof(g_func_infos) / sizeof(g_func_infos[0]); i++) { 214 FuncInfo* fi = &g_func_infos[i]; 215 bool ok = g_func_info_map->insert(make_pair(Intern(fi->name), fi)).second; 216 CHECK(ok); 217 } 218} 219 220void QuitFuncTable() { 221 delete g_func_info_map; 222} 223 224FuncInfo* GetFuncInfo(StringPiece name) { 225 auto found = g_func_info_map->find(name); 226 if (found == g_func_info_map->end()) 227 return NULL; 228 return found->second; 229} 230