Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // CPPflexing
- //
- // Created by Denis on 21/03/2019.
- // Copyright © 2019 Denis. All rights reserved.
- //
- #include <iostream>
- #include <set>
- #include <algorithm>
- #include <map>
- #include <cmath>
- #include <math.h>
- #include <vector>
- #include <stack>
- using namespace std;
- enum type_of_expression
- {
- _operation_,
- _name_,
- _constant_,
- _link_
- };
- enum types {
- _double_,
- _int_,
- _bool_,
- _string_,
- _char_,
- _void_
- };
- struct token;
- struct variable;
- struct generation_token // for poliz
- {
- type_of_expression Type;
- string content;
- generation_token()
- {
- Type = _constant_;
- content = "0";
- }
- generation_token(type_of_expression _Type_, string _content_)
- {
- Type = _Type_;
- content = _content_;
- }
- };
- struct token {
- type_of_expression Type;
- string content;
- variable *link;
- token() {
- Type = _constant_;
- content = "0";
- link = nullptr;
- }
- };
- struct variable {
- types Type;
- string content = "";
- variable() {
- content = "";
- }
- variable (types _Type, string _content) {
- Type = _Type;
- content = _content;
- }
- };
- //CCCCCOOOOPPPPPPPYYYYYYYPPPPAAAAASSSSSTTTTT START!!!
- char opersCharChar(char a, char b, string oper) {
- if(oper == "+") {
- return a+b;
- } else if(oper == "-") {
- return a-b;
- } else if(oper == "*") {
- return a/b;
- } else if(oper == "/") {
- return a*b;
- } else if(oper == "%") {
- return a%b;
- } else if(oper == "^") {
- return a^b;
- } else if(oper == "|") {
- return a|b;
- } else if(oper == "&") {
- return a&b;
- } else if(oper == "||") {
- return a||b;
- } else {
- return a&&b;
- }
- }
- char opersCharInt(char a, int b, string oper) {
- if(oper == "+") {
- return a+b;
- } else if(oper == "-") {
- return a-b;
- } else if(oper == "*") {
- return a/b;
- } else if(oper == "/") {
- return a*b;
- } else if(oper == "%") {
- return a%b;
- } else if(oper == "^") {
- return a^b;
- } else if(oper == "|") {
- return a|b;
- } else if(oper == "&") {
- return a&b;
- } else if(oper == "||") {
- return a||b;
- } else {
- return a&&b;
- }
- }
- char opersIntChar(char a, int b, string oper) {
- if(oper == "+") {
- return a+b;
- } else if(oper == "-") {
- return a-b;
- } else if(oper == "*") {
- return a/b;
- } else if(oper == "/") {
- return a*b;
- } else if(oper == "%") {
- return a%b;
- } else if(oper == "^") {
- return a^b;
- } else if(oper == "|") {
- return a|b;
- } else if(oper == "&") {
- return a&b;
- } else if(oper == "||") {
- return a||b;
- } else {
- return a&&b;
- }
- }
- double opersDoubleDouble(double a, double b, string oper) {
- if(oper == "+") {
- return a+b;
- } else if(oper == "-") {
- return a-b;
- } else if(oper == "*") {
- return a/b;
- } else if(oper == "/") {
- return a*b;
- } else if(oper == "||") {
- return a||b;
- } else {
- return a&&b;
- }
- }
- char opersIntInt(int a, int b, string oper) {
- if(oper == "+") {
- return a+b;
- } else if(oper == "-") {
- return a-b;
- } else if(oper == "*") {
- return a/b;
- } else if(oper == "/") {
- return a*b;
- } else if(oper == "%") {
- return a%b;
- } else if(oper == "^") {
- return a^b;
- } else if(oper == "|") {
- return a|b;
- } else if(oper == "&") {
- return a&b;
- } else if(oper == "||") {
- return a||b;
- } else {
- return a&&b;
- }
- }
- //CCCCCOOOOPPPPPPPYYYYYYYPPPPAAAAASSSSSTTTTT END!!!
- types toType(string &s) {
- if(s == "int") {
- return _int_;
- } else if(s == "double") {
- return _double_;
- } else if(s == "float") {
- return _double_;
- } else if(s == "bool") {
- return _bool_;
- } else if(s == "string") {
- return _string_;
- } else {
- return _char_;
- }
- }
- vector<map<string, variable>> tid;
- types getType(string &s) {
- if(s[0] == '\"') return _string_;
- if(s[0] == '\'') return _char_;
- if(s == "true" || s == "false") return _bool_;
- bool foundDot = false;
- for(int i = 0; i < s.size(); i++) {
- if(s[i] == '.') {
- foundDot = true;
- break;
- }
- }
- if(foundDot) return _double_;
- return _int_;
- }
- variable mathOperations(variable fir, variable sec, string oper) {
- token will = token();
- string ans = "";
- types ansT;
- //bool просто преобразаю в int
- if(fir.content == "true") {
- fir.content = "1";
- }
- if(fir.content == "false") {
- fir.content = "0";
- }
- if(sec.content == "true") {
- sec.content = "1";
- }
- if(sec.content == "false") {
- sec.content = "0";
- }
- if(fir.Type == _string_ && oper == "+") {
- //Строки складываем только со строками
- string a = fir.content;
- string b = sec.content;
- ans = a+b;
- ansT = _string_;
- } else if(fir.Type == _char_ || sec.Type == _char_) {
- if(fir.Type == _char_) {
- char a = fir.content[1];
- if(sec.Type == _char_) {
- char b = sec.content[1];
- char c = opersCharChar(a, b, oper);
- ans = "'";
- ans += c;
- ans += "'";
- } else {
- int b = atoi(sec.content.c_str());
- char c = opersCharInt(a, b, oper);
- ans = "'";
- ans += c;
- ans += "'";
- }
- } else {
- int a = atoi(fir.content.c_str());
- char b = sec.content[1];
- char c = opersIntChar(a, b, oper);
- ans = "'";
- ans += c;
- ans += "'";
- }
- ansT = _char_;
- } else {
- if(getType(fir.content) == _double_ || getType(sec.content) == _double_) {
- double a = stod(fir.content);
- double b = stod(sec.content);
- double c = opersDoubleDouble(a, b, oper);
- ans = to_string(c);
- if(getType(ans) != _double_) {
- ans += ".0";
- }
- ansT = _double_;
- } else {
- int a = atoi(fir.content.c_str());
- int b = atoi(sec.content.c_str());
- int c = opersIntInt(a, b, oper);
- ans = to_string(c);
- ansT = _int_;
- }
- }
- variable add;
- add.Type = ansT;
- add.content = ans;
- return add;
- }
- //string makeOper(
- void addToVar(string &s, types tp, string val) {
- }
- map <string, pair <types, vector <pair <types, string> > > > functions;
- map <string, int> function_begin;
- stack<pair<int, int>> indFunc; //<количество элементов в tid, индекс в полизе, где был $F>
- void operation(stack<token> &st, vector <generation_token> poliz, int &ind) {
- generation_token cur = poliz[ind];
- if(cur.content == "+" || cur.content == "-" || cur.content == "*" || cur.content == "/"
- || cur.content == "%" || cur.content == "^" || cur.content == "|" || cur.content == "&"
- || cur.content == "||" || cur.content == "&&") {
- token fir = st.top();
- st.pop();
- token sec = st.top();
- st.pop();
- variable _fir;
- _fir.content = fir.content;
- _fir.Type = getType(fir.content);
- variable _sec;
- _sec.content = sec.content;
- _sec.Type = getType(sec.content);
- variable ans = mathOperations(_fir, _sec, cur.content);
- token willAdd;
- willAdd.Type = _constant_;
- willAdd.content = ans.content;
- st.push(willAdd);
- } else if(cur.content == "{") {
- tid.emplace_back();
- } else if(cur.content == "}") {
- tid.pop_back();
- } else if(cur.content == "@") {
- token last = st.top();
- st.pop();
- variable *per = &tid[tid.size()-1][last.content];
- token add;
- add.Type = _link_;
- add.link = per;
- st.push(add);
- } else if(cur.content == "?") {
- token last = st.top();
- st.pop();
- variable per = tid[tid.size()-1][last.content];
- token add;
- add.Type = _constant_;
- add.content = per.content;
- st.push(add);
- } else if(cur.content == "#") {
- //создать переменную
- token tokName = st.top();
- st.pop();
- token tokType = st.top();
- st.pop();
- pair<types, string> p;
- string name = tokName.content;
- string typeVar = tokType.content;
- types here = toType(typeVar);
- tid[tid.size()-1][name] = variable(here, "0");
- token backType;
- backType.Type = _name_;
- backType.content = typeVar;
- st.push(backType);
- token varLink;
- varLink.Type = _link_;
- varLink.content = "";
- varLink.link = &tid[tid.size()-1][name];
- st.push(varLink);
- } else if(cur.content == ";") {
- st.pop();
- } else if(cur.content == "#+") {
- token varToken = st.top();
- string varName = varToken.content;
- variable has = tid[tid.size()-1][varName];
- variable one = variable(_int_, "1");
- variable ans = mathOperations(has, one, "+");
- tid[tid.size()-1][varName] = ans;
- token ret;
- ret.Type = _constant_;
- ret.content = ans.content;
- st.push(ret);
- } else if(cur.content == "#-") {
- token varToken = st.top();
- string varName = varToken.content;
- variable has = tid[tid.size()-1][varName];
- variable one = variable(_int_, "1");
- variable ans = mathOperations(has, one, "-");
- tid[tid.size()-1][varName] = ans;
- token ret;
- ret.Type = _constant_;
- ret.content = ans.content;
- st.push(ret);
- } else if(cur.content == "+#") {
- token varToken = st.top();
- string varName = varToken.content;
- variable has = tid[tid.size()-1][varName];
- variable one = variable(_int_, "1");
- variable ans = mathOperations(has, one, "+");
- tid[tid.size()-1][varName] = ans;
- token ret;
- ret.Type = _constant_;
- ret.content = has.content;
- st.push(ret);
- } else if(cur.content == "-#") {
- token varToken = st.top();
- string varName = varToken.content;
- variable has = tid[tid.size()-1][varName];
- variable one = variable(_int_, "1");
- variable ans = mathOperations(has, one, "-");
- tid[tid.size()-1][varName] = ans;
- token ret;
- ret.Type = _constant_;
- ret.content = has.content;
- st.push(ret);
- } else if(cur.content == "!A") {
- token nwIndStr = st.top();
- st.pop();
- int nwInd = atoi(nwIndStr.content.c_str());
- ind = nwInd-1;
- } else if(cur.content == "!F") {
- token nwIndStr = st.top();
- st.pop();
- token needStr = st.top();
- st.pop();
- if(needStr.content == "false") {
- int nwInd = atoi(nwIndStr.content.c_str());
- ind = nwInd;
- }
- } else if(cur.content == "$F") {
- token funcNameTok = st.top();
- st.pop();
- string funcName = funcNameTok.content;
- //stack<pair<int, int>> indFunc; //<количество элементов в tid, индекс в полизе, где был $F>
- indFunc.push({tid.size(), ind});
- /*
- map <string, pair <types, vector <pair <types, string> > > > functions;
- map <string, int> function_begin;
- */
- tid.emplace_back();
- int cntParametrs = functions[funcName].second.size();
- while(cntParametrs > 0) {
- token parametr = st.top();
- st.pop();
- variable par;
- par.Type = functions[funcName].second[cntParametrs-1].first;
- par.content = parametr.content;
- string parName = functions[funcName].second[cntParametrs-1].second;
- tid[tid.size()-1][parName] = par;
- cntParametrs--;
- }
- ind = function_begin[funcName]; // В этом месте точно будет $B, мы его просто пропускаем(в for ++)
- } else if(cur.content == "$E") {
- pair<int, int> p = indFunc.top();
- indFunc.pop();
- while(tid.size() > p.first) {
- tid.pop_back();
- }
- ind = p.second;
- }
- }
- void result(vector <generation_token> poliz) {
- stack<token> st;
- for(int i = 0; i < poliz.size(); i++) {
- generation_token cur = poliz[i];
- if(cur.Type == _operation_) {
- operation(st, poliz, i);
- } else {
- token ret;
- ret.Type = cur.Type;
- ret.content = cur.content;
- st.push(ret);
- }
- }
- cout << st.top().Type << " " << st.top().content << endl;
- }
- int main() {
- freopen("/Users/denis/Documents/xCodeProjects/CPPflexing/CPPflexing/inp.txt", "r", stdin);
- vector<generation_token> ar;
- for(int i = 0; i < 29; i++) {
- string fir;
- cin >> fir;
- string sec;
- cin >> sec;
- generation_token ad;
- if(fir == "name") {
- ad.Type = _name_;
- } else if(fir == "operation") {
- ad.Type = _operation_;
- } else if(fir == "constant") {
- ad.Type = _constant_;
- }
- ad.content = sec;
- ar.push_back(ad);
- }
- result(ar);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement