it lives!

This commit is contained in:
Xircon 2026-07-30 04:47:46 -04:00
parent d1ab554b52
commit 834783a279
6 changed files with 119 additions and 13 deletions

9
.gitignore vendored
View File

@ -1,5 +1,12 @@
## IDE Stuff
.ccls-cache
.vscode
a.out
## Build
build
## Tests
a.out
out.asm
out.o
out

View File

@ -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})

View File

@ -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.

View File

@ -1 +1 @@
return 99;
return 0;

View File

@ -1,6 +0,0 @@
#include <iostream>
int main() {
std::cout << "Cuntout" << std::endl;
return 0;
}

108
src/main.cxx Normal file
View File

@ -0,0 +1,108 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <optional>
#include <vector>
enum class TokenType {
_return,
int_lit,
semi,
};
struct Token {
TokenType type;
std::optional<std::string> value {};
};
std::vector<Token> tokenize(const std::string& str) {
std::vector<Token> 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<Token>& 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<Token> 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;
}