From e6ae86f673e797916f88ef0388f966fe67f42530 Mon Sep 17 00:00:00 2001 From: bigsketti Date: Sun, 22 Sep 2024 11:42:41 -0400 Subject: [PATCH] Initial commit, only the lexer as of now. --- headers/lexer.h | 63 +++++++++++++++++++++++ profaneCompiler | Bin 0 -> 43120 bytes src/lexer.cpp | 110 ++++++++++++++++++++++++++++++++++++++++ src/profaneCompiler.cpp | 48 ++++++++++++++++++ testInput.txt | 3 ++ 5 files changed, 224 insertions(+) create mode 100644 headers/lexer.h create mode 100755 profaneCompiler create mode 100644 src/lexer.cpp create mode 100644 src/profaneCompiler.cpp create mode 100644 testInput.txt diff --git a/headers/lexer.h b/headers/lexer.h new file mode 100644 index 0000000..09ac0b7 --- /dev/null +++ b/headers/lexer.h @@ -0,0 +1,63 @@ +#ifndef LEXER_H +#define LEXER_H + +#include + +//list of keywords +enum class Keywords { + identifier, + integer, + floatpnt, + string, + character, + boolean, + plus, + minus, + multiply, + divide, + l_Paren, + r_Paren, + l_Brace, + r_Brace, + l_Brack, + r_Brack, + semicolon, + end_of_file, + unknown +}; + +struct Token { + Keywords type; + std::string value; + + + Token(Keywords type, std::string value) : type(type), value(value) {} +}; + +class Lexer { + private: + + std::string stringInput; + int position; + char currentChar; + + //advances position in the input string + void nextPosition(); + + void skipWhiteSpace(); + + //handles integer literals + Token number(); + + //handles identifiers + Token identifier(); + + public: + + Lexer(const std::string &input); + + Token getNextToken(); + +}; + +#endif \ No newline at end of file diff --git a/profaneCompiler b/profaneCompiler new file mode 100755 index 0000000000000000000000000000000000000000..0cb9e7c5c62a6f325f732b794760d3299a508aff GIT binary patch literal 43120 zcmeHwdwf*Ywg1UuAUq@iK`B}t5h)Z6dGOSt;W0RoSDFw?ReBsI6Ed1)VrCM8^$`tt z8DlK=iskxfW3RPbTPvSl<%ibVh+bl?-lFwd^!AZ!)y@d5@o{am*4*!RKhB&vnL{DH zzyALCag?mH*WT-~*WP>WwfC8G_6@$3%d@kx41MJo7aC0UO_i83m9eK=-2jvs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I0L^==0zQ8XHAcv+lY(5!=uWYUcVotp?)7o zmL6*VtC7KXZ#2Dr-$uhmMX2@HQ6nj{t9h$;f6NV^!j}w z4ZW;1E-hcv>-v8Kd8A*$Lb&w%NgC?+nMh9>C;ffEQH17ic#+`c6@U3NoV3f?{`Vl) zPVe=~;u`koiYYGm*`>HN{4P><`o5_$U&ABk$h7{e>2&%b+}P>$`)3;BXSQjVQ~!Te z^p=_xs zIU0V8#<5+Fr12*5_**G8><#_C-mV5o{j#z%6=R_ytG{14@RIutdpkScY`Qq?2AZPB!Tr;(`z`!L9gEjOep#$hru+Rh7%q1 z`gfe`=~!^-aVk?O{dJn}J&>2yzx_s;)1>GZIQmbSCRC8`9pQh&Sx-%`euigJu`Dk; zbchi7yOsQY^#eHktjyq8()HH$V)xX#3qtj0*NyV#FbQp!ma9?Yag&L@<6E-C3 +#include + +void Lexer::nextPosition() { + position++; + + if(position < stringInput.size()) { + currentChar = stringInput[position]; + std::cout << "Advancing to: " << currentChar << " | at position: " << position << std::endl; + } else { + currentChar = '\0'; + std::cout << "end of input\n"; + } +} + +void Lexer::skipWhiteSpace() { + if (currentChar != '\0' && std::isspace(currentChar)) { + Lexer::nextPosition(); + } +} + +Token Lexer::number() { + std::string value; + + while (currentChar != '\0' && isdigit(currentChar)) { + value += currentChar; + nextPosition(); + } + return Token(Keywords::integer, value); +} + +Token Lexer::identifier() { + std::string value; + + while (currentChar != '\0' && std::isalnum(currentChar)) { + value += currentChar; + nextPosition(); + } + return Token(Keywords::identifier, value); +} + +Lexer::Lexer(const std::string &input) { + this->position = 0; + this->stringInput = input; + this->currentChar = stringInput[0]; +} + +Token Lexer::getNextToken() { + while (currentChar != '\0') { + std::cout << currentChar << std::endl; + + if (std::isspace(currentChar)) { + skipWhiteSpace(); + continue; + } + + if (std::isdigit(currentChar)) { + return number(); + } + + if (std::isalpha(currentChar)) { + return identifier(); + } + + //this sure is ugly + switch (currentChar) { + case '+': + nextPosition(); + return Token(Keywords::plus, "+"); + case '-': + nextPosition(); + return Token(Keywords::minus, "-"); + case '*': + nextPosition(); + return Token(Keywords::multiply, "*"); + case '/': + nextPosition(); + return Token(Keywords::divide, "/"); + case '(': + nextPosition(); + return Token(Keywords::l_Paren, "("); + case ')': + nextPosition(); + return Token(Keywords::r_Paren, ")"); + case '{': + nextPosition(); + return Token(Keywords::l_Brace, "{"); + case '}': + nextPosition(); + return Token(Keywords::r_Brace, "}"); + case '[': + nextPosition(); + return Token(Keywords::l_Brack, "["); + case ']': + nextPosition(); + return Token(Keywords::r_Brack, "]"); + case ';': + nextPosition(); + return Token(Keywords::semicolon, ";"); + default: + nextPosition(); + return Token(Keywords::unknown, ""); + } + } + std::cout << "EOF" << std::endl; + return Token(Keywords::end_of_file, ""); + +} \ No newline at end of file diff --git a/src/profaneCompiler.cpp b/src/profaneCompiler.cpp new file mode 100644 index 0000000..26be998 --- /dev/null +++ b/src/profaneCompiler.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +#include "/home/mason/code-shit/ProfaneC/headers/lexer.h" + +std::ostream& operator<<(std::ostream& os, const Token& token) { + os << "Type: " << static_cast(token.type) << ", Value: " << token.value; + return os; +} + +int main() { + + std::vector tokenVec; + std::ifstream file_in("/home/mason/code-shit/ProfaneC/testInput.txt"); + + if (file_in.is_open()) { + std::cout << "file open\n"; + std::string line; + + while (getline(file_in, line)) { + std::cout << "Reading line: " << line << std::endl; + + Lexer Lexer(line); + Token token = Lexer.getNextToken(); + + while (token.type != Keywords::end_of_file) { + std::cout << "Token " << static_cast(token.type) << " | Value: " << token.value << std::endl; + tokenVec.push_back(token); + token = Lexer.getNextToken(); + } + } + + } else if (file_in.fail()) { + std::cerr << "File read error" << std::endl; + } + + file_in.close(); + + std::cout << "printing all tokens : \n"; + + for (int i = 0; i < tokenVec.size(); i++) { + std::cout << tokenVec.at(i) << std::endl; + } + + return 0; +} \ No newline at end of file diff --git a/testInput.txt b/testInput.txt new file mode 100644 index 0000000..e9cdae1 --- /dev/null +++ b/testInput.txt @@ -0,0 +1,3 @@ +int main() { + return 0; +} \ No newline at end of file