Last active
August 29, 2015 14:05
-
-
Save marionette-of-u/4f51e969f83cc31910cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdlib> | |
| #include <string> | |
| #include <vector> | |
| struct ast{ | |
| std::vector<ast*> sub; | |
| std::string token; | |
| std::string type; | |
| }; | |
| void interpreter(const std::string code){ | |
| std::vector<std::vector<ast*>> stack; | |
| ast *ptr = nullptr; | |
| for(std::size_t i = 0; i < code.size(); ++i){ | |
| char c = code[i]; | |
| if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'){ | |
| if(!ptr){ stack.back().push_back(ptr = new ast); ptr->type = "token"; } | |
| ptr->token.push_back(c); | |
| }else if(c >= '0' && c <= '9'){ | |
| if(!ptr){ stack.back().push_back(ptr = new ast); ptr->type = "value"; } | |
| ptr->token.push_back(c); | |
| }else if(c == '\''){ | |
| stack.resize(stack.size() + 1); | |
| stack.back().push_back(ptr = new ast); | |
| ptr->type = "ast"; | |
| ptr = nullptr; | |
| }else if(c == '('){ | |
| stack.resize(stack.size() + 1); | |
| stack.back().push_back(ptr = new ast); | |
| ptr->type = "function"; | |
| }else if(c == ')'){ | |
| if(stack.size() > 1 && (stack.back().front()->type != "function" && stack.back().front()->type != "ast")){ | |
| throw; | |
| } | |
| if(stack.back().front()->type == "function"){ | |
| stack.back().front()->sub.push_back(stack.back().back()); | |
| stack.back().pop_back(); | |
| }else if(stack.back().front()->type == "ast"){ | |
| // void... | |
| } | |
| stack[stack.size() - 2].front()->sub.push_back(stack.back().back()); | |
| stack.pop_back(); | |
| ptr = nullptr; | |
| }else if(c == '['){ | |
| stack.resize(stack.size() + 1); | |
| stack.back().push_back(ptr = new ast); | |
| ptr->type = "list"; | |
| ptr = nullptr; | |
| }else if(c == ']'){ | |
| if(stack.size() > 1 && stack.back().front()->type != "list"){ | |
| throw; | |
| } | |
| stack.back().front()->sub.push_back(stack.back().back()); | |
| stack.back().pop_back(); | |
| if(stack.size() > 1){ | |
| stack[stack.size() - 2].front()->sub.push_back(stack.back().back()); | |
| stack.pop_back(); | |
| } | |
| ptr = nullptr; | |
| }else if(ptr && (c == ' ' || c == '\n' || c == '\r')){ | |
| if(ptr->type == "function"){ | |
| // void... | |
| }else if(ptr->type == "list"){ | |
| // void... | |
| }else{ | |
| stack.back().front()->sub.push_back(stack.back().back()); | |
| stack.back().pop_back(); | |
| } | |
| ptr = nullptr; | |
| }else if(ptr){ | |
| if(!ptr){ stack.back().push_back(ptr = new ast); ptr->type = "token"; } | |
| ptr->token.push_back(c); | |
| } | |
| } | |
| } | |
| int main(){ | |
| interpreter("(main 1 (hoge 2 3 4) [aaa bb (c e [n] [t] o r) 1 22 333] '(+ 6 (fuga 5 4) 3))"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment