diff --git a/c/.clangd b/c/.clangd new file mode 100644 index 0000000..be82cf3 --- /dev/null +++ b/c/.clangd @@ -0,0 +1,7 @@ +# Source - https://stackoverflow.com/a +# Posted by Aincvy, modified by community. See post 'Timeline' for change history +# Retrieved 2026-01-11, License - CC BY-SA 4.0 + +CompileFlags: # Tweak the parse settings, example directory given to show format + Add: + - "-I./include" # e.g `-I/usr/include/SDL2` suggested from @Palmkeep diff --git a/c/include/label.h b/c/include/label.h index 831e847..79835e6 100644 --- a/c/include/label.h +++ b/c/include/label.h @@ -2,7 +2,6 @@ #define LABEL_H #include -#include "expr.h" typedef enum { LABEL_CONST, diff --git a/cpp/.gitignore b/cpp/.gitignore new file mode 100644 index 0000000..9ef9604 --- /dev/null +++ b/cpp/.gitignore @@ -0,0 +1,2 @@ +build + diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt new file mode 100644 index 0000000..dc07e83 --- /dev/null +++ b/cpp/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 4.0) +project(ttk91_compiler C CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_C_STANDARD 99) + +# creates compile_commands.json +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + + +set(SOURCES + src/main.cpp + src/Lexer.cpp + src/Token.cpp +) + +add_library(ttk91_compiler_lib ${SOURCES}) +add_executable(ttk91_compiler src/main.cpp) + +target_include_directories(ttk91_compiler PRIVATE include) +target_include_directories(ttk91_compiler_lib PRIVATE include) +target_link_libraries(ttk91_compiler ttk91_compiler_lib) diff --git a/cpp/include/Lexer.hpp b/cpp/include/Lexer.hpp new file mode 100644 index 0000000..eccc159 --- /dev/null +++ b/cpp/include/Lexer.hpp @@ -0,0 +1,18 @@ +#pragma once +#include +#include +#include + +using std::string; +using std::vector; + +class Lexer { +public: + std::string input; + vector tokens; + + Lexer(string); + void get_tokens(); + void parse_tokens(string); +}; + diff --git a/cpp/include/Token.hpp b/cpp/include/Token.hpp new file mode 100644 index 0000000..36cba87 --- /dev/null +++ b/cpp/include/Token.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include +#include + +using std::string; +using std::vector; + +enum TokenType { + KEYWORD, + OPERATOR, + REGISTER, + LABEL, + ADDR_MODE, + NUMBER, + COMMA, + COMMENT, + ERROR +}; + +class Token { +public: + string value; + TokenType type; + + Token(string, TokenType); + + + static inline vector operators = { + "load", + "store", + "add", + "sub", + "mul", + "div" + }; + + static Token tokenize(string); +}; diff --git a/cpp/src/Lexer.cpp b/cpp/src/Lexer.cpp new file mode 100644 index 0000000..8d3689c --- /dev/null +++ b/cpp/src/Lexer.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +#include +#include + +using std::string; +using std::stringstream; +using std::vector; + +Lexer::Lexer(string input) { + this->input = input; + this->tokens = {}; +} + +void Lexer::get_tokens() { + stringstream ss(this->input); + string line; + string word; + char delim = ' '; + + std::cout << "Starting to get tokens :) " << ss.str() << std::endl; + + // Lines in input file + while (getline(ss, line)) { + stringstream ss_line(line); + + bool comment = false; + + // Words (tokens separated by whitespace) in line + while (getline(ss_line, word, delim)) { + // Skip comments + if (word == ";") { + comment = true; + } + + if (comment) { + continue; + } + + std::cout << word << std::endl; + this->tokens.push_back(Token::tokenize(word)); + } + + comment = false; + } + + for (Token token : tokens) { + std::cout << token.value << std::endl; + } +} + +void Lexer::parse_tokens(string token) {} diff --git a/cpp/src/Token.cpp b/cpp/src/Token.cpp new file mode 100644 index 0000000..01bc48b --- /dev/null +++ b/cpp/src/Token.cpp @@ -0,0 +1,24 @@ +#include +#include + +#include + +using std::string; + +Token::Token(string value, TokenType type) { + this->value = value; + this->type = type; +} + +Token Token::tokenize(string token) { + if (std::find(operators.begin(), operators.end(), token) != operators.end()) { + return Token(token, OPERATOR); + } else { + return Token(token, ERROR); + } +} + +std::ostream& operator << ( std::ostream& outs, const Token & token ) +{ + return outs << token.value; +} diff --git a/cpp/src/main.cpp b/cpp/src/main.cpp new file mode 100644 index 0000000..948c201 --- /dev/null +++ b/cpp/src/main.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#include + +int main() { + std::cout << "Hello world!" << std::endl; + std::ifstream source_file ("../../docs/example-file-simple.k91"); + + std::string line; + std::stringstream input; + + // Open file and read the contents + if (source_file.is_open()) { + while (getline(source_file, line)) { + input << line << "\n"; + } + } + + source_file.close(); + + Lexer lex = Lexer(input.str()); + + lex.get_tokens(); + return 0; +} diff --git a/haskell/ttk91-compiler.cabal b/haskell/ttk91-compiler.cabal index 3a929b3..1ef5de0 100644 --- a/haskell/ttk91-compiler.cabal +++ b/haskell/ttk91-compiler.cabal @@ -1,6 +1,6 @@ -cabal-version: 2.2 +cabal-version: 2.2 --- This file has been generated from package.yaml by hpack version 0.36.0. +-- This file has been generated from package.yaml by hpack version 0.38.1. -- -- see: https://github.com/sol/hpack @@ -26,6 +26,7 @@ source-repository head library exposed-modules: Lib + Parser other-modules: Paths_ttk91_compiler autogen-modules: