From 013faa38b2b08e5429a730f96f9bdbd074b11b7d Mon Sep 17 00:00:00 2001 From: indiaonic Date: Wed, 14 Jun 2023 17:55:48 +0530 Subject: [PATCH] fix: build failure --- Swirl/include/pre-processor/pre-processor.h | 4 +- Swirl/src/pre-processor/pre-processor.cpp | 50 +-- Swirl/src/swirl.cpp | 106 ++++- Swirl/src/transpiler/transpiler.cpp | 439 ++++++++++---------- 4 files changed, 350 insertions(+), 249 deletions(-) diff --git a/Swirl/include/pre-processor/pre-processor.h b/Swirl/include/pre-processor/pre-processor.h index d1b103a..ec50107 100644 --- a/Swirl/include/pre-processor/pre-processor.h +++ b/Swirl/include/pre-processor/pre-processor.h @@ -2,9 +2,9 @@ #include #include -#include +//#include #ifndef PRE_PROCESSOR_H_SWIRL #define PRE_PROCESSOR_H_SWIRL -void preProcess(const std::string&, TokenStream&, std::string); +//void preProcess(const std::string&, TokenStream&, std::string); #endif \ No newline at end of file diff --git a/Swirl/src/pre-processor/pre-processor.cpp b/Swirl/src/pre-processor/pre-processor.cpp index b430e69..72675e7 100755 --- a/Swirl/src/pre-processor/pre-processor.cpp +++ b/Swirl/src/pre-processor/pre-processor.cpp @@ -1,25 +1,25 @@ -#include -#include -#include -#include -#include -#include - -#include - -#ifndef _WIN32 -#define TORNADO_PKGS_PATH "/.tornado/packages/" -#endif - -void preProcess(const std::string& _source, TokenStream& _stream, std::string _buildPath) { - std::stringstream source_strm(_source); - std::vector cimports{}; - - std::string cr_dir = "cd " + _buildPath.erase(_buildPath.size() - 16, _buildPath.size()) - + "&& mkdir __swirl_cache__"; - - if (!std::filesystem::exists(_buildPath + "__swirl_cache__")) - system(cr_dir.c_str()); - - std::ofstream cache_file(_buildPath + "__swirl_cache__" + PATH_SEP + "__main__.sw"); -} +//#include +//#include +//#include +//#include +//#include +//#include +// +//#include +// +//#ifndef _WIN32 +//#define TORNADO_PKGS_PATH "/.tornado/packages/" +//#endif +// +//void preProcess(const std::string& _source, TokenStream& _stream, std::string _buildPath) { +// std::stringstream source_strm(_source); +// std::vector cimports{}; +// +// std::string cr_dir = "cd " + _buildPath.erase(_buildPath.size() - 16, _buildPath.size()) +// + "&& mkdir __swirl_cache__"; +// +// if (!std::filesystem::exists(_buildPath + "__swirl_cache__")) +// system(cr_dir.c_str()); +// +// std::ofstream cache_file(_buildPath + "__swirl_cache__" + PATH_SEP + "__main__.sw"); +//} diff --git a/Swirl/src/swirl.cpp b/Swirl/src/swirl.cpp index 5612a1d..41f95e3 100644 --- a/Swirl/src/swirl.cpp +++ b/Swirl/src/swirl.cpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include bool SW_DEBUG = false; std::string SW_FED_FILE_PATH; @@ -42,6 +42,90 @@ std::unordered_map type_registry = { {"function","global"} }; +std::unordered_map non_assign_binary_ops = { + {"+", 0}, + {"-", 1}, + {"*", 2}, + {"/", 3}, + {"%", 4}, + {"==", 5}, + {"!=", 6}, + {">", 7}, + {"<", 8}, + {">=", 9}, + {"<=", 10}, + {"&&", 11}, + {"||", 12}, + {"&", 13}, + {"|", 14}, + {"^", 15}, + {"<<", 16}, + {">>", 17}, +}; + +std::unordered_map keywords = { + {"func", 0}, + {"return", 1}, + {"if", 2}, + {"else", 3}, + {"for", 4}, + {"while", 5}, + {"is", 6}, + {"in", 7}, + {"or", 8}, + {"and", 9}, + {"class", 10}, + {"public", 11}, + {"private", 12}, + {"const", 15}, + {"static", 16}, + {"break", 17}, + {"continue", 18}, + {"elif", 19}, + {"global", 20}, + {"importc", 21}, + {"typedef", 22}, + {"import", 23}, + {"export", 24}, + {"from", 25}, + {"var", 26} +}; + +std::unordered_map operators = { + {"+", 0}, + {"-", 1}, + {"*", 2}, + {"/", 3}, + {"%", 4}, + {"==", 5}, + {"!=", 6}, + {">", 7}, + {"<", 8}, + {">=", 9}, + {"<=", 10}, + {"&&", 11}, + {"||", 12}, + {"&", 13}, + {"|", 14}, + {"^", 15}, + {"<<", 16}, + {">>", 17}, + {"=", 18}, + {"+=", 19}, + {"-=", 20}, + {"*=", 21}, + {"/=", 22}, + {"%=", 23}, + {"===", 24}, + {"!==", 25}, + {"&=", 26}, + {"|=", 27}, + {"^=", 28}, + {"<<=", 29}, + {">>=", 30}, + {"++", 31}, + {"--", 32}, +}; std::optional> compile( std::string& _source, @@ -50,15 +134,15 @@ std::optional> compile( InputStream chrinp_stream(_source); TokenStream tk(chrinp_stream); - preProcess(_source, tk, _cacheDir); +// preProcess(_source, tk, _cacheDir); Parser parser(tk); - return Transpile( - parser.m_AST->chl, - _cacheDir, - compiled_source, - true, - symt - ); +// return Transpile( +// {}, +// _cacheDir, +// compiled_source, +// true, +// symt +// ); } int main(int argc, const char** const argv) { @@ -123,11 +207,11 @@ int main(int argc, const char** const argv) { if ( !SW_FED_FILE_SOURCE.empty() ) { InputStream is(SW_FED_FILE_SOURCE); TokenStream tk(is, _debug); - preProcess(SW_FED_FILE_SOURCE, tk, cache_dir); +// preProcess(SW_FED_FILE_SOURCE, tk, cache_dir); Parser parser(tk); parser.dispatch(); - Transpile(parser.m_AST->chl, cache_dir + SW_OUTPUT + ".cpp", compiled_source); +// Transpile(parser.m_AST->chl, cache_dir + SW_OUTPUT + ".cpp", compiled_source); } if (app.contains_flag("-r")) { diff --git a/Swirl/src/transpiler/transpiler.cpp b/Swirl/src/transpiler/transpiler.cpp index 5892535..d55f814 100644 --- a/Swirl/src/transpiler/transpiler.cpp +++ b/Swirl/src/transpiler/transpiler.cpp @@ -82,222 +82,239 @@ std::vector splitStr(const std::string& str, char delimiter) { } -std::optional> Transpile( +std::optional> +Transpile( std::list& _nodes, const std::string& _buildFile, std::string& _dest = compiled_source, bool onlyAppend = false, bool returnSymbolTable = false ) { - unsigned int prn_ind = 0; - int fn_br_ind = 0; - int rd_function = 0; - bool read_ret_type = false; - bool rd_type = false; - bool is_include = false; - std::ifstream bt_fstream{}; - std::string tmp_str_cnst{}; - TokenType last_node_type{}; - std::string last_func_ident{}; - std::string cimports{}; - std::string cr_scope{}; // %: func-local, $: global-var, @: template-arg - std::string macros{}; - - std::optional> ret = {}; - - if (_dest == compiled_source) - _dest += "int main() {\n"; - - for (Node& child : _nodes) { - if (child.type == TYPEDEF) - macros += "using " + child.ident + " = " + child.value + ";"; - - if (child.type == EXPORT) - for (Node& exp : child.body) - symbol_table[exp.value] = ""; - - if (child.type == OP) { - if (!prn_ind) - _dest.erase(_dest.size() - 1); - _dest += child.value; - if (child.value == "++" || child.value == "--") - _dest += ";"; - continue; - } - - if (child.type == STRING) { - if (is_include) { cimports += child.value + "\n"; is_include = false; continue; } - else if (child.format) { _dest += "string(" + format(child.value) + ")"; SC_IF_IN_PRNS; continue; } - _dest += "string(" + child.value + ")"; - SC_IF_IN_PRNS; - continue; - } - - if (child.type == FUNCTION) { - rd_function = true; - symbol_table[child.ident] = ""; - last_func_ident = child.ident; - - if (!child.template_args.empty()) { - compiled_funcs += "\n;template<"; - for (const Node& t : child.template_args) { - t.type == IDENT ? compiled_funcs += "typename " + t.value : compiled_source += child.value; - symbol_table[t.value] = "%" + child.ident; - } - compiled_funcs += ">\n"; - } - - if (child.template_args.empty()) compiled_funcs += ";"; - compiled_funcs += child.ctx_type + " " + child.ident; - Transpile(child.arg_nodes, _buildFile, compiled_funcs, true); - Transpile(child.body, _buildFile, compiled_funcs, true); - continue; - } - - if (child.type == FOR || child.type == WHILE) { - _dest += std::string(child.type == FOR ? "for":"while") + " (" + child.value + ")"; - continue; - } - - if (child.type == COLON) { - _dest += ":"; - read_ret_type = true; - continue; - } - - if (child.type == KEYWORD) { - if (child.value == "break" || child.value == "continue") - { _dest += child.value + ";"; continue; } - else if (child.value == "true" || child.value == "false") - { _dest += child.value; SC_IF_IN_PRNS; continue;} - else if (child.value == "importc") - { cimports += "#include "; is_include = true; continue;} - else{ _dest += child.value + " "; continue; } - } - - if (child.type == IMPORT) { - - } - - if (child.type == MACRO) { - if (child.value.starts_with("typedef")) { - std::basic_string typedefin = child.value.substr(7); - std::basic_string f_type = splitStr(typedefin, ' ')[1]; - typedefin.insert(child.value.find_first_of(f_type) + f_type.size() + 1, "="); - macros += "using " + typedefin + ";"; - continue; - } - - macros += "#" + child.value + "\n"; - continue; - } - - if (child.type == PRN_OPEN) { - _dest += "("; - prn_ind++; - continue; - } - - if (child.type == PRN_CLOSE) { - _dest += ")"; - prn_ind--; - - if (rd_function && !prn_ind) rd_function = -1; - if (!prn_ind) { - if (rd_function != -1) { _dest += ";"; continue; } - else rd_function = 0; - } - SC_IF_IN_PRNS; - continue; - } - - if (child.type == COMMA) { - _dest += ","; - continue; - } - - if (child.type == NUMBER) { - _dest += child.value; - SC_IF_IN_PRNS; - continue; - } - - if (child.type == IDENT) { -// if (read_ret_type) { -// read_ret_type = false; -// _dest.replace(_dest.find(last_func_ident) - 5, 4, child.value); +// unsigned int prn_ind = 0; +// int fn_br_ind = 0; +// int rd_function = 0; +// bool read_ret_type = false; +// bool rd_type = false; +// bool is_include = false; +// std::ifstream bt_fstream{}; +// std::string tmp_str_cnst{}; +// TokenType last_node_type{}; +// std::string last_func_ident{}; +// std::string cimports{}; +// std::string cr_scope{}; // %: func-local, $: global-var, @: template-arg +// std::string macros{}; +// +// std::optional> ret = {}; +// +// if (_dest == compiled_source) +// _dest += "int main() {\n"; +// +// for (Node& child : _nodes) { +// if (child.type == TYPEDEF) +// macros += "using " + child.ident + " = " + child.value + ";"; +// +// if (child.type == EXPORT) +// for (Node& exp : child.body) +// symbol_table[exp.value] = ""; +// +// if (child.type == OP) { +// if (!prn_ind) +// _dest.erase(_dest.size() - 1); +// _dest += child.value; +// if (child.value == "++" || child.value == "--") +// _dest += ";"; +// continue; +// } +// +// if (child.type == VAR) { +// _dest += child.ctx_type + " " + child.ident; +// if (!child.initialized) +// _dest += ';'; +// +// continue; +// } +// +// if (child.type == STRING) { +// if (is_include) { cimports += child.value + "\n"; is_include = false; continue; } +// else if (child.format) { _dest += "string(" + format(child.value) + ")"; SC_IF_IN_PRNS; continue; } +// _dest += "string(" + child.value + ")"; +// SC_IF_IN_PRNS; +// continue; +// } +// +// if (child.type == FUNCTION) { +// rd_function = true; +// symbol_table[child.ident] = ""; +// last_func_ident = child.ident; +// +// if (!child.template_args.empty()) { +// compiled_funcs += "\n;template<"; +// for (const Node& t : child.template_args) { +// t.type == IDENT ? compiled_funcs += "typename " + t.value : compiled_source += child.value; +// symbol_table[t.value] = "%" + child.ident; +// } +// compiled_funcs += ">\n"; // } - if (type_registry.contains(child.value)) {_dest += child.value + " "; rd_type = true; continue; } - if (rd_type) { - if (_dest == compiled_funcs) - symbol_table[child.value] = "%" + last_func_ident; - else - symbol_table[child.value] = "$" + std::string("__main__"); - rd_type = false; - } - _dest += child.value; - SC_IF_IN_PRNS; - continue; - } - - if (child.type == BR_OPEN) { - fn_br_ind ++; - if (_dest[_dest.size() - 2] == ')') - _dest.erase(_dest.size() - 1); - _dest += "{"; - continue; - } - - if (child.type == BR_CLOSE) { - fn_br_ind --; - _dest += "}"; - - if (!fn_br_ind) _dest += ';'; - if (!fn_br_ind ) { } - continue; - } - - if (child.type == IF || child.type == ELIF || child.type == ELSE) { - if (child.type == ELSE) - { if (_dest[_dest.size() - 1] == ';') _dest.erase(_dest.size() - 1); _dest += "else"; } - else - _dest += std::string(child.type == IF ? "if" : child.type == ELIF ? "elif":"else") + " (" + child.value + ")"; // please forgive me - continue; - } - - if (child.type == DOT) { - if (_dest.ends_with(';')) _dest.erase(_dest.size() - 1); - _dest += "."; - continue; - } - - if (child.type == VAR) { - _dest += child.ctx_type + " " + child.ident; - if (!child.initialized && !rd_function) _dest += ";"; - else _dest += "="; - continue; - } - - if (child.type == CALL) - _dest += child.ident; - last_node_type = child.type; - } - - if (returnSymbolTable) - ret = symbol_table; - - if (!onlyAppend) { - _dest.insert(bt_size, "\n" + macros + "\n"); - bt_size = bt_size + macros.size(); - - _dest.insert(bt_size, compiled_funcs); - _dest.insert(0, cimports); - - std::ofstream o_file_buf(_buildFile); - _dest += "}"; - o_file_buf << _dest; - o_file_buf.close(); - } - - return ret; +// +// if (child.template_args.empty()) compiled_funcs += ";"; +// compiled_funcs += child.ctx_type + " " + child.ident; +// Transpile(child.arg_nodes, _buildFile, compiled_funcs, true); +// Transpile(child.body, _buildFile, compiled_funcs, true); +// continue; +// } +// +// if (child.type == FOR || child.type == WHILE) { +// _dest += std::string(child.type == FOR ? "for":"while") + " (" + child.value + ")"; +// continue; +// } +// +// if (child.type == COLON) { +// _dest += ":"; +// read_ret_type = true; +// continue; +// } +// +// if (child.type == KEYWORD) { +// if (child.value == "break" || child.value == "continue") +// { _dest += child.value + ";"; continue; } +// else if (child.value == "true" || child.value == "false") +// { _dest += child.value; SC_IF_IN_PRNS; continue;} +// else if (child.value == "importc") +// { cimports += "#include "; is_include = true; continue;} +// else{ _dest += child.value + " "; continue; } +// } +// +// if (child.type == IMPORT) { +// +// } +// +// if (child.type == MACRO) { +// if (child.value.starts_with("typedef")) { +// std::basic_string typedefin = child.value.substr(7); +// std::basic_string f_type = splitStr(typedefin, ' ')[1]; +// typedefin.insert(child.value.find_first_of(f_type) + f_type.size() + 1, "="); +// macros += "using " + typedefin + ";"; +// continue; +// } +// +// macros += "#" + child.value + "\n"; +// continue; +// } +// +// if (child.type == PRN_OPEN) { +// _dest += "("; +// prn_ind++; +// continue; +// } +// +// if (child.type == PRN_CLOSE) { +// _dest += ")"; +// prn_ind--; +// +// if (rd_function && !prn_ind) rd_function = -1; +// if (!prn_ind) { +// if (rd_function != -1) { _dest += ";"; continue; } +// else rd_function = 0; +// } +// SC_IF_IN_PRNS; +// continue; +// } +// +// if (child.type == COMMA) { +// _dest += ","; +// continue; +// } +// +// if (child.type == NUMBER) { +// _dest += child.value; +// SC_IF_IN_PRNS; +// continue; +// } +// +// if (child.type == IDENT) { +//// if (read_ret_type) { +//// read_ret_type = false; +//// _dest.replace(_dest.find(last_func_ident) - 5, 4, child.value); +//// } +// if (type_registry.contains(child.value)) {_dest += child.value + " "; rd_type = true; continue; } +// if (rd_type) { +// if (_dest == compiled_funcs) +// symbol_table[child.value] = "%" + last_func_ident; +// else +// symbol_table[child.value] = "$" + std::string("__main__"); +// rd_type = false; +// } +// _dest += child.value; +// SC_IF_IN_PRNS; +// continue; +// } +// +// if (child.type == BR_OPEN) { +// fn_br_ind ++; +// if (_dest[_dest.size() - 2] == ')') +// _dest.erase(_dest.size() - 1); +// _dest += "{"; +// continue; +// } +// +// if (child.type == BR_CLOSE) { +// fn_br_ind --; +// _dest += "}"; +// +// if (!fn_br_ind) _dest += ';'; +// if (!fn_br_ind ) { } +// continue; +// } +// +// if (child.type == IF || child.type == ELIF || child.type == ELSE) { +// +// for (const auto nd : child.body) { +// } +//// if (child.type == ELSE) +//// { if (_dest[_dest.size() - 1] == ';') _dest.erase(_dest.size() - 1); _dest += "else"; } +//// else +//// _dest += std::string(child.type == IF ? "if" : child.type == ELIF ? "elif":"else") + " (" + child.value + ")"; // please forgive me +// continue; +// } +// +// if (child.type == DOT) { +// if (_dest.ends_with(';')) _dest.erase(_dest.size() - 1); +// _dest += "."; +// continue; +// } +// +// if (child.type == VAR) { +// _dest += child.ctx_type + " " + child.ident; +// if (!child.initialized && !rd_function) _dest += ";"; +// else _dest += "="; +// continue; +// } +// +// if (child.type == ASSIGNMENT) { +// _dest += '='; +// continue; +// } +// +// if (child.type == CALL) +// _dest += child.ident; +// last_node_type = child.type; +// } +// +// if (returnSymbolTable) +// ret = symbol_table; +// +// if (!onlyAppend) { +// _dest.insert(bt_size, "\n" + macros + "\n"); +// bt_size = bt_size + macros.size(); +// +// _dest.insert(bt_size, compiled_funcs); +// _dest.insert(0, cimports); +// +// std::ofstream o_file_buf(_buildFile); +// _dest += "}"; +// o_file_buf << _dest; +// o_file_buf.close(); +// } +// +// return ret; }