Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
Language: Cpp
BasedOnStyle: Google

# Basic Formatting
IndentWidth: 4
ColumnLimit: 100
TabWidth: 4
UseTab: Never

# Braces and Spacing
BreakBeforeBraces: Attach
AllowShortBlocksOnASingleLine: Empty
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false

# Pointers and References (e.g., int* ptr instead of int *ptr)
PointerAlignment: Left

# Alignment for readability
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: false
AlignOperands: Align
AlignTrailingComments: true

# Includes
SortIncludes: true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#build directory
build/

# Config files
.vscode

# Prerequisites
*.d

Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"makefile.configureOnOpen": true
}
144 changes: 0 additions & 144 deletions Lexer/LexerClasses - Part 1.cpp

This file was deleted.

8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
#for change in header files
-include $(DEPS)

.PHONY: all clean run
.PHONY: all clean run format check-format

run: all
./$(BUILD_DIR)/$(TARGET_EXEC)

clean:
rm -rf $(BUILD_DIR)

format:
find $(SRC_DIR)/ -name '*.cpp' -o -name '*.h' | xargs clang-format -i

check-format:
find $(SRC_DIR)/ -name '*.cpp' -o -name '*.h' | xargs clang-format --dry-run --Werror
22 changes: 22 additions & 0 deletions src/common/ast/astexpr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "astexpr.hpp"

ASTExprNode::ASTExprNode(ASTExprNodeType type) : type(type) {}

BinaryOperatorNode::BinaryOperatorNode(OperatorType op, std::unique_ptr<ASTExprNode> lhs,
std::unique_ptr<ASTExprNode> rhs)
: ASTExprNode(ASTExprNodeType::BINARY), op(op), lhs(std::move(lhs)), rhs(std::move(rhs)) {}

UnaryOperatorNode::UnaryOperatorNode(OperatorType op, std::unique_ptr<ASTExprNode> rhs)
: ASTExprNode(ASTExprNodeType::UNARY), op(op), rhs(std::move(rhs)) {}

StringNode::StringNode(std::string value)
: ASTExprNode(ASTExprNodeType::STRING), value(std::move(value)) {}

NumberNode::NumberNode(double value) : ASTExprNode(ASTExprNodeType::NUMBER), value(value) {}

BooleanNode::BooleanNode(bool value) : ASTExprNode(ASTExprNodeType::BOOLEAN), value(value) {}

NoneNode::NoneNode() : ASTExprNode(ASTExprNodeType::NONE) {}

ReferenceNode::ReferenceNode(std::string name)
: ASTExprNode(ASTExprNodeType::REFERENCE), name(std::move(name)) {}
104 changes: 104 additions & 0 deletions src/common/ast/astexpr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#pragma once

#include <string>
#include <memory>

enum class ASTExprNodeType {
BINARY, //binary operators
UNARY, //unary operators
STRING, //string literals
NUMBER, //number literals
BOOLEAN, //boolean literals
NONE, //None
REFERENCE //variable(identifier) references
};

enum class OperatorType {
//logical
OR, // or
AND, // and
NOT, // not

//comparisons
GREATERTHAN, // >
GREATEREQUAL, // >=
LESSEQUAL, // <=
LESSERTHAN, // <
EQEQUAL, // ==
NOTEQUAL, // !=

//additive/unary
PLUS, // +
MINUS, // -

//multiplicative
STAR, // *
SLASH, // /
DOUBLESLASH, // //
MODULO, // %

//power
POWER, // **
};

//abstract ASTExprNode class
class ASTExprNode {
public:
ASTExprNode(ASTExprNodeType type);
virtual ~ASTNode() = default;

//type
ASTExprNodeType type;
};

class BinaryOperatorNode : public ASTExprNode {
public:
BinaryOperatorNode(OperatorType op, std::unique_ptr<ASTExprNode> lhs, std::unique_ptr<ASTExprNode> rhs);

//operator type
OperatorType op;
std::unique_ptr<ASTExprNode> lhs;
std::unique_ptr<ASTExprNode> rhs;
};

class UnaryOperatorNode : public ASTExprNode {
public:
UnaryOperatorNode(OperatorType op, std::unique_ptr<ASTExprNode> rhs);

//operator type
OperatorType op;
std::unique_ptr<ASTExprNode> rhs;
};

class StringNode : public ASTExprNode {
public:
StringNode(std::string value);

std::string value;
};

class NumberNode : public ASTExprNode {
public:
NumberNode(double value);

double value;
};

class BooleanNode : public ASTExprNode {
public:
BooleanNode(bool value);

bool value;
}

class NoneNode : public ASTExprNode {
public:
NoneNode();
}

class ReferenceNode : public ASTExprNode {
public:
ReferenceNode(std::string name);

std::string name;
};
6 changes: 6 additions & 0 deletions src/common/token/token.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "token.hpp"

#include <string>

Token::Token(TokenType type, std::string val, int l, int c)
: type(type), value(val), line(l), column(c) {}
6 changes: 5 additions & 1 deletion src/common/token/token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ enum class TokenType {
COLON, // :
INDENT,
DEDENT,
COMMENT // #
COMMENT, // #
PRINT,
AMPERSAND, // &
PIPE, // |
SPACE,
};


Expand Down
27 changes: 27 additions & 0 deletions src/lexer/keywords.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <unordered_map>
#include <string>
#include <vector>

#include "token.hpp"

inline const std::unordered_map<std::string, TokenType> keywords = {
{"def", TokenType::DEF},
{"return", TokenType::RETURN},
{"if", TokenType::IF},
{"else", TokenType::ELSE},
{"print", TokenType::PRINT},
{"=", TokenType::ASSIGN},
{"+", TokenType::PLUS},
{"-", TokenType::MINUS},
{"*", TokenType::STAR},
{"/", TokenType::SLASH},
{"(", TokenType::LPAREN},
{")", TokenType::RPAREN},
{":", TokenType::COLON},
{",", TokenType::COMMA},
{"\n", TokenType::NEWLINE},
{"\t", TokenType::INDENT},
{" ", TokenType::SPACE}
};
Loading