did something

This commit is contained in:
Aaro Koivukangas
2026-03-12 16:04:38 +02:00
parent a43373cdd3
commit 1229c35370
10 changed files with 198 additions and 3 deletions
+7
View File
@@ -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
-1
View File
@@ -2,7 +2,6 @@
#define LABEL_H
#include <stddef.h>
#include "expr.h"
typedef enum {
LABEL_CONST,
+2
View File
@@ -0,0 +1,2 @@
build
+22
View File
@@ -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)
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <string>
#include <vector>
#include <Token.hpp>
using std::string;
using std::vector;
class Lexer {
public:
std::string input;
vector<Token> tokens;
Lexer(string);
void get_tokens();
void parse_tokens(string);
};
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include <string>
#include <vector>
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<string> operators = {
"load",
"store",
"add",
"sub",
"mul",
"div"
};
static Token tokenize(string);
};
+55
View File
@@ -0,0 +1,55 @@
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <Lexer.hpp>
#include <Token.hpp>
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) {}
+24
View File
@@ -0,0 +1,24 @@
#include <algorithm>
#include <string>
#include <Token.hpp>
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;
}
+28
View File
@@ -0,0 +1,28 @@
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <Lexer.hpp>
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;
}
+3 -2
View File
@@ -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: