Skip to content

Instantly share code, notes, and snippets.

@makoConstruct
Created March 11, 2017 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makoConstruct/da466c3fbbd5195f2f49a6d352920069 to your computer and use it in GitHub Desktop.
Save makoConstruct/da466c3fbbd5195f2f49a6d352920069 to your computer and use it in GitHub Desktop.
it's mindnumbing to go alone, take these
Term const* findTermConst(vector<Term> const& candidates, string const& key){
for(Term const& t : candidates){
if(t.isList() && t.l.l.size() > 1){
Term const& ft = t.l.l[0];
if(ft.isStr() && ft.s.s == key){
return &t;
}
}
}
return nullptr;
}
Term const* findSubTermConst(vector<Term> const& candidates, string const& key){ //returns the second element of the thing
auto ret = findTermConst(candidates, key);
if(ret){
expectList(*ret);
if(ret->l.l.size() < 2){
throw TyperError(*ret, "a subterm was saught, but this list only contains one element");
}
return &(ret->l.l[1]);
}else{
return nullptr;
}
}
Term const& seekSubTerm(string const& key, Term const& t){ //returns the second element of the matching thing
expectList(t);
auto ret = findTermConst(t.l.l, key);
if(ret){
expectList(*ret);
if(ret->l.l.size() < 2){
throw TyperError(*ret, "a subterm was saught, but this list only contains one element");
}
return ret->l.l[1];
}else{
throw TyperError(t, strc("item with key ", key, " not found"));
}
}
string subTermString(Term const& v){ //returns the second element of the thing
expectList(v);
if(v.l.l.size() != 2){
throw TyperError(v, "a subterm was saught, but the host list contains more or less than two elements");
}
Term const& resultStr = v.l.l[1];
if(resultStr.isStr()){
return resultStr.s.s;
}else{
throw TyperError(resultStr, "expected a str term as a part of a pair");
}
}
Term const& expectTagTerm(string const& tag, Term const& v){
expectList(v);
if(v.l.l.size() == 2 && v.l.l[0].isStr()){
if(v.l.l[0].s.s == tag){
return v.l.l[1];
}else{
throw TyperError(v.l.l[0], strc("expected key ", tag));
}
}else{
throw TyperError(v, "does not have the form of a tag term");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment