Skip to content

Commit

Permalink
first draw of the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
michael.burzan committed Dec 27, 2024
1 parent 12bf451 commit 1cb3e2f
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 79 deletions.
2 changes: 1 addition & 1 deletion include/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enum class TokenKind {
SEMICOLON, // ;
ARROW, // ->
COMMA, // ,

COLON, // :
// KEYWORDS
FN, // fn
LET, // let
Expand Down
39 changes: 39 additions & 0 deletions include/Parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef PALLADIUM_PARSER_H
#define PALLADIUM_PARSER_H
#include <optional>
#include "Lexer.h"
#include <string>

using AstNode = bool;

class Parser {
public:
Parser(const std::string& code);

auto parse() -> bool;

private:
auto parse_translation_unit() -> AstNode;
auto parse_function() -> AstNode;
auto parse_statements() -> AstNode;
auto parse_statement() -> AstNode;
auto parse_variable_declaration() -> AstNode;
auto parse_constant_declaration() -> AstNode;
auto parse_loop() -> AstNode;
auto parse_return_statement() -> AstNode;
auto parse_expression() -> AstNode;
auto parse_array_initialization() -> AstNode;
auto parse_binary_expression() -> AstNode;
auto parse_condition() -> AstNode;
auto parse_operator() -> AstNode;
auto parse_type() -> AstNode;

auto accept(TokenKind tk) -> bool;

private:
Lexer _lexer;
Token _current_token;
Token _last_token;
};

#endif
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ target_sources(OBJECT_LIB PRIVATE
Lexer.cpp
LexerStream.cpp
Instruction.cpp
Parser.cpp
Util.cpp
VMType.cpp
VMStruct.cpp
)

if( CMAKE_BUILD_TYPE STREQUAL "Debug")
if( CMAKE_BUILD_TYPE STREQUAL "Debug" )
target_link_libraries(OBJECT_LIB -fsanitize=address)
endif()

Expand Down
67 changes: 24 additions & 43 deletions src/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,13 @@ auto is_digit = [](char x) { return std::isdigit(x); };
auto is_xdigit = [](char x) { return std::isxdigit(x); };

auto to_string(TokenKind tk) -> std::string {
static const char* converter[] = {"IDENTIFIER",
"TEXT",
"INTEGER",
"DOUBLE",
"OP_ADD",
"OP_SUB",
"OP_MULT",
"OP_DIV",
"OP_SET",
"OP_EQ",
"OP_NEQ",
"OP_NOT",
"OP_LS",
"OP_LS_EQ",
"OP_GT",
"OP_GT_EQ",
"EDGE_CLAMP_OPEN",
"EDGE_CLAMP_CLOSE",
"CLAMP_OPEN",
"CLAMP_CLOSE",
"CURLY_OPEN",
"CURLY_CLOSE",
"SEMICOLON",
"ARROW",
"COMMA",
"FN",
"LET",
"CONST",
"I32",
"VOID",
"RETURN",
"WHILE",
"END_OF_FILE"};
static const char* converter[] = {
"IDENTIFIER", "TEXT", "INTEGER", "DOUBLE", "OP_ADD", "OP_SUB",
"OP_MULT", "OP_DIV", "OP_SET", "OP_EQ", "OP_NEQ", "OP_NOT",
"OP_LS", "OP_LS_EQ", "OP_GT", "OP_GT_EQ", "EDGE_CLAMP_OPEN", "EDGE_CLAMP_CLOSE",
"CLAMP_OPEN", "CLAMP_CLOSE", "CURLY_OPEN", "CURLY_CLOSE", "SEMICOLON", "ARROW",
"COMMA", "COLON", "FN", "LET", "CONST", "I32",
"VOID", "RETURN", "WHILE", "END_OF_FILE"};

std::cout << "INT TK: " << static_cast<std::size_t>(tk) << " : " << std::size(converter) << std::endl;
if (static_cast<std::size_t>(tk) < std::size(converter)) {
Expand All @@ -84,16 +58,23 @@ static const std::unordered_map<std::string, Token> KEYWORDS = {
{"while", Token(TokenKind::WHILE, "while")},
};

static const std::unordered_map<char, Token> SIMPLE_CHAR_TO_TOKEN = {
{'+', Token(TokenKind::OP_ADD, "+")}, {'-', Token(TokenKind::OP_SUB, "-")},
{'*', Token(TokenKind::OP_MULT, "*")}, {'/', Token(TokenKind::OP_DIV, "/")},
{'=', Token(TokenKind::OP_SET, "=")}, {'!', Token(TokenKind::OP_NOT, "!")},
{'<', Token(TokenKind::OP_LS, "<")}, {'>', Token(TokenKind::OP_GT, ">")},
{'[', Token(TokenKind::EDGE_CLAMP_OPEN, "[")}, {']', Token(TokenKind::EDGE_CLAMP_CLOSE, "]")},
{'(', Token(TokenKind::CLAMP_OPEN, "(")}, {')', Token(TokenKind::CLAMP_CLOSE, ")")},
{'{', Token(TokenKind::CURLY_OPEN, "{")}, {'}', Token(TokenKind::CURLY_CLOSE, "}")},
{';', Token(TokenKind::SEMICOLON, ";")}, {',', Token(TokenKind::COMMA, ",")},
};
static const std::unordered_map<char, Token> SIMPLE_CHAR_TO_TOKEN = {{'+', Token(TokenKind::OP_ADD, "+")},
{'-', Token(TokenKind::OP_SUB, "-")},
{'*', Token(TokenKind::OP_MULT, "*")},
{'/', Token(TokenKind::OP_DIV, "/")},
{'=', Token(TokenKind::OP_SET, "=")},
{'!', Token(TokenKind::OP_NOT, "!")},
{'<', Token(TokenKind::OP_LS, "<")},
{'>', Token(TokenKind::OP_GT, ">")},
{'[', Token(TokenKind::EDGE_CLAMP_OPEN, "[")},
{']', Token(TokenKind::EDGE_CLAMP_CLOSE, "]")},
{'(', Token(TokenKind::CLAMP_OPEN, "(")},
{')', Token(TokenKind::CLAMP_CLOSE, ")")},
{'{', Token(TokenKind::CURLY_OPEN, "{")},
{'}', Token(TokenKind::CURLY_CLOSE, "}")},
{';', Token(TokenKind::SEMICOLON, ";")},
{',', Token(TokenKind::COMMA, ",")},
{':', Token(TokenKind::COLON, ":")}};

static const std::unordered_map<char, std::unordered_map<char, Token>> OPERATOR_MULTI_TRANSITION = {
{'=', {{'=', Token(TokenKind::OP_EQ, "==")}}}, {'!', {{'=', Token(TokenKind::OP_NEQ, "!=")}}},
Expand Down
Loading

0 comments on commit 1cb3e2f

Please sign in to comment.