diff --git a/.gitignore b/.gitignore index 848328c..a2cbc81 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,12 @@ ## IDE Stuff .ccls-cache .vscode + +## Build +build + +## Tests a.out -build \ No newline at end of file +out.asm +out.o +out \ No newline at end of file diff --git a/src/compilier/CMakeLists.txt b/CMakeLists.txt similarity index 94% rename from src/compilier/CMakeLists.txt rename to CMakeLists.txt index 5eaa688..fb7bbf2 100644 --- a/src/compilier/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ link_directories(BEFORE $ENV{LD_LIBRARY_PATH} ${CMAKE_BINARY_DIR}/ ) include(GNUInstallDirs) -set(SOURCES ./main.cxx +set(SOURCES src/main.cxx CACHE INTERNAL "") # Make this all files MINUS main.cxx dunno how tbh lmao add_executable(${PROJECT_NAME} ${SOURCES}) diff --git a/documentation/compilier/Main.md b/documentation/compilier/Main.md index 10a50ab..1e52915 100644 --- a/documentation/compilier/Main.md +++ b/documentation/compilier/Main.md @@ -1,5 +1,2 @@ # Compiler -Compiler written in C++ (sorry for it being shit, tbh i hate C++ i just need its features, will be self hosted later on? i hope anyhow.) - -Uses LLVM In the backend as it makes things... ALOT EASIER. -I'm not going to write every ounce of everything when this will just be re-written anyhow. \ No newline at end of file +Compiler written in C++ (sorry for it being shit, tbh i hate C++ i just need its features, will be self hosted later on? i hope anyhow.) \ No newline at end of file diff --git a/examples/compact.x b/examples/compact.x index f0025da..f023caf 100644 --- a/examples/compact.x +++ b/examples/compact.x @@ -1 +1 @@ -return 99; \ No newline at end of file +return 0; \ No newline at end of file diff --git a/src/compilier/main.cxx b/src/compilier/main.cxx deleted file mode 100644 index f5e0484..0000000 --- a/src/compilier/main.cxx +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int main() { - std::cout << "Cuntout" << std::endl; - return 0; -} \ No newline at end of file diff --git a/src/main.cxx b/src/main.cxx new file mode 100644 index 0000000..bbe029d --- /dev/null +++ b/src/main.cxx @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include + +enum class TokenType { + _return, + int_lit, + semi, +}; + +struct Token { + TokenType type; + std::optional value {}; +}; + +std::vector tokenize(const std::string& str) { + std::vector tokens; + + std::string buffer; + for (int i = 0; i < str.length(); i++) { + // Words + if (std::isalpha(str.at(i))) { + buffer.push_back(str.at(i)); + i++; + while (std::isalnum(str.at(i))) { + buffer.push_back(str.at(i)); + i++; + } + i--; + if (buffer == "return") { + tokens.push_back({.type = TokenType::_return}); + buffer.clear(); + continue; + } else { + std::cerr << "xlang: \033[1;31merror:\033[0m \033[1;37munknown type name \'" << buffer << "\'\033[0m" << std::endl; + exit(EXIT_FAILURE); + } + } else if (std::isdigit(str.at(i))) { // Handle Numbers + buffer.push_back(str.at(i)); + i++; + while (std::isdigit(str.at(i))) { // im going insane + buffer.push_back(str.at(i)); + i++; + } + i--; + tokens.push_back({.type = TokenType::int_lit, .value = buffer}); + buffer.clear(); + } else if (str.at(i) == ';') { + tokens.push_back({.type = TokenType::semi}); + } else if (std::isspace(str.at(i))) { // Handle White Space + continue; + } else { + std::cerr << "xlang: \033[1;31merror:\033[0m \033[1;37munknown type name \'" << buffer << "\'\033[0m" << std::endl; + exit(EXIT_FAILURE); + } + } + + // Return mah shit + return tokens; +} + +std::string tokens_to_asm(std::vector& tokens) { + std::stringstream output; + output << "global _start\n_start:\n"; + for (int i = 0; i < tokens.size(); i++) { + const Token& token = tokens.at(i); + if (token.type == TokenType::_return) { + if (i + 1 < tokens.size() && tokens.at(i + 1).type == TokenType::int_lit) { + if (i + 2 < tokens.size() && tokens.at(i + 2).type == TokenType::semi) { + output << " mov rax, 60\n"; + output << " mov rdi, " << tokens.at(i + 1).value.value() << "\n"; + output << " syscall\n"; + } + } + } + } + + return output.str(); +} + +int main(int argc, char* argv[]) { + if (argc != 2) { + std::cerr << "xlang: \033[1;31merror:\033[0m \033[1;37mno input files\033[0m" << std::endl; // yes i spent 10 minutes to emulate clangs output... + return EXIT_FAILURE; + } + + std::string contents; + { + std::stringstream contents_stream; + std::fstream input(argv[1], std::ios::in); + contents_stream << input.rdbuf(); + contents = contents_stream.str(); + } + + std::vector tokens = tokenize(contents); + + { + std::fstream output("out.asm", std::ios::out); + output << tokens_to_asm(tokens); + } + + system("nasm -felf64 out.asm"); + system("ld -o out out.o"); + + return EXIT_SUCCESS; +} \ No newline at end of file