Skip to content

Commit

Permalink
style: reformat all files
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Dec 1, 2023
1 parent c2b10b4 commit d7101ed
Show file tree
Hide file tree
Showing 25 changed files with 193 additions and 247 deletions.
66 changes: 66 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Generated from CLion C/C++ Code Style settings
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignOperands: Align
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: true
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 0
CompactNamespaces: false
ContinuationIndentWidth: 8
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Right
ReflowComments: false
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 0
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Never
7 changes: 5 additions & 2 deletions src/builtin_functions/Function.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#ifndef YASI_FUNCTION_H
#define YASI_FUNCTION_H

#include "../parser/SyntaxTreeNode.h"

class Function {
public:
virtual ~Function()= default;
virtual SyntaxTreeNode evaluate(const std::vector<SyntaxTreeNode>& args) = 0;
virtual ~Function() = default;
virtual SyntaxTreeNode evaluate(
const std::vector<SyntaxTreeNode> &args) = 0;
};

#endif
9 changes: 2 additions & 7 deletions src/builtin_functions/add/add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
SyntaxTreeNode Add::evaluate(const std::vector<SyntaxTreeNode> &args) {
int result = 0;

for (const auto& arg: args) {
for (const auto &arg: args) {
auto evArg = Evaluate::evaluate(arg).token;
result += std::stoi(evArg.token);
}

return SyntaxTreeNode(
Token(
Token::Integer,
std::to_string(result)
)
);
return SyntaxTreeNode(Token(Token::Integer, std::to_string(result)));
}
2 changes: 1 addition & 1 deletion src/builtin_functions/add/add.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Add : public Function {
public:
SyntaxTreeNode evaluate(const std::vector<SyntaxTreeNode>& args) override;
SyntaxTreeNode evaluate(const std::vector<SyntaxTreeNode> &args) override;
};

#endif
9 changes: 2 additions & 7 deletions src/builtin_functions/divide/Divide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@
#include "eval/eval.h"

SyntaxTreeNode Divide::evaluate(const std::vector<SyntaxTreeNode> &args) {
int result = std::stoi(Evaluate::evaluate(args[0]).token.token);
int result = std::stoi(Evaluate::evaluate(args[0]).token.token);

for (int i = 1; i < args.size(); i++) {
auto arg = Evaluate::evaluate(args[i]).token;
result /= std::stoi(arg.token);
}

return SyntaxTreeNode(
Token(
Token::Integer,
std::to_string(result)
)
);
return SyntaxTreeNode(Token(Token::Integer, std::to_string(result)));
}
3 changes: 1 addition & 2 deletions src/builtin_functions/divide/Divide.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

class Divide : public Function {
public:
SyntaxTreeNode evaluate(const std::vector<SyntaxTreeNode>& args) override;
SyntaxTreeNode evaluate(const std::vector<SyntaxTreeNode> &args) override;
};


#endif
7 changes: 1 addition & 6 deletions src/builtin_functions/multiply/multiply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,5 @@ SyntaxTreeNode Multiply::evaluate(const std::vector<SyntaxTreeNode> &args) {
result *= std::stoi(evArg.token);
}

return SyntaxTreeNode(
Token(
Token::Integer,
std::to_string(result)
)
);
return SyntaxTreeNode(Token(Token::Integer, std::to_string(result)));
}
2 changes: 1 addition & 1 deletion src/builtin_functions/multiply/multiply.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Multiply : public Function {
public:
SyntaxTreeNode evaluate(const std::vector<SyntaxTreeNode>& args) override;
SyntaxTreeNode evaluate(const std::vector<SyntaxTreeNode> &args) override;
};

#endif
7 changes: 1 addition & 6 deletions src/builtin_functions/subtract/subtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,5 @@ SyntaxTreeNode Subtract::evaluate(const std::vector<SyntaxTreeNode> &args) {
result -= std::stoi(arg.token);
}

return SyntaxTreeNode(
Token(
Token::Integer,
std::to_string(result)
)
);
return SyntaxTreeNode(Token(Token::Integer, std::to_string(result)));
}
2 changes: 1 addition & 1 deletion src/builtin_functions/subtract/subtract.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Subtract : public Function {
public:
SyntaxTreeNode evaluate(const std::vector<SyntaxTreeNode>& args) override;
SyntaxTreeNode evaluate(const std::vector<SyntaxTreeNode> &args) override;
};

#endif
12 changes: 5 additions & 7 deletions src/eval/eval.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
#include <unordered_map>
#include "eval.h"
#include "../builtin_functions/add/add.h"
#include "../builtin_functions/multiply/multiply.h"
#include "../builtin_functions/subtract/subtract.h"
#include "exceptions/SyntaxError.h"
#include "builtin_functions/divide/Divide.h"
#include "exceptions/SyntaxError.h"
#include <unordered_map>

SyntaxTreeNode Evaluate::evaluate(const SyntaxTreeNode &tree) {
static std::unordered_map<std::string, Function *> builtin = {
{"+", dynamic_cast<Function *>(new Add())},
{"-", dynamic_cast<Function *>(new Subtract())},
{"*", dynamic_cast<Function *>(new Multiply())},
{"/", dynamic_cast<Function *>(new Divide())}
};
{"/", dynamic_cast<Function *>(new Divide())}};
if (tree.children.empty()) {
return SyntaxTreeNode(tree.token);
}
if (builtin.find(tree.token.token) != builtin.end()) {
return builtin.at(tree.token.token)->evaluate(tree.children);
} else {
auto errorMessage = "Syntax error: name `"
+ tree.token.token +
"` is not defined";
auto errorMessage =
"Syntax error: name `" + tree.token.token + "` is not defined";
throw SyntaxError(errorMessage);
}
}
3 changes: 1 addition & 2 deletions src/exceptions/SyntaxError.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class SyntaxError : public std::exception {
public:
std::string message;

explicit SyntaxError(std::string message)
: message(std::move(message)) {}
explicit SyntaxError(std::string message) : message(std::move(message)) {}
};

#endif
8 changes: 3 additions & 5 deletions src/lexer/Lexer.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include <cstring>
#include "Lexer.h"
#include <cstring>

std::vector<Token> Lexer::tokenize(const std::string &line) {
std::vector<Token> result;
std::string currentToken;

for (auto c: line) {
if (c != '(' && c != ')'
&& (c != ' ' || currentToken[0] == '"')
&& (c != '"' || currentToken[0] != '"')
) {
if (c != '(' && c != ')' && (c != ' ' || currentToken[0] == '"') &&
(c != '"' || currentToken[0] != '"')) {
currentToken.append(1, c);
continue;
}
Expand Down
10 changes: 3 additions & 7 deletions src/lexer/Lexer.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef YASI_LEXER_H
#define YASI_LEXER_H

#include <utility>
#include <iostream>
#include <utility>
#include <vector>

class Token {
Expand All @@ -22,15 +22,11 @@ class Token {
Token() : type(Token::Invalid) {}

Token(TokenType type, std::string token)
: type(type),
token(std::move(token)) {}
: type(type), token(std::move(token)) {}

bool operator==(const Token &object) const {
return token == object.token;
}
bool operator==(const Token &object) const { return token == object.token; }
};


class Lexer {
public:
static std::vector<Token> tokenize(const std::string &line);
Expand Down
16 changes: 7 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <cstdlib>
#include <iostream>
#include "read.hpp"
#include "lexer/Lexer.h"
#include "eval/eval.h"
#include "parser/Parser.h"
#include "exceptions/SyntaxError.h"
#include "lexer/Lexer.h"
#include "parser/Parser.h"
#include "read.hpp"
#include <cstdlib>
#include <iostream>

int main() {
std::cout << "Yasi v0.0.0\n";
Expand All @@ -18,13 +18,11 @@ int main() {
}

try {
auto result = Evaluate::evaluate(
Parser::parse(Lexer::tokenize(userInput))
);
auto result =
Evaluate::evaluate(Parser::parse(Lexer::tokenize(userInput)));
std::cout << result.token.token << '\n';
} catch (SyntaxError &error) {
std::cout << error.message << '\n';
}

}
}
14 changes: 6 additions & 8 deletions src/parser/Parser.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stack>
#include "Parser.h"
#include <stack>

SyntaxTreeNode Parser::parse(const std::vector<Token> &tokens) {
std::stack<Token> operators_stack;
Expand All @@ -14,13 +14,11 @@ SyntaxTreeNode Parser::parse(const std::vector<Token> &tokens) {
} else if (token.type == Token::ClosedBracket) {
auto args = nodes_stack.top();
nodes_stack.pop();
if (nodes_stack.empty()) nodes_stack.emplace();
nodes_stack.top().emplace_back(
operators_stack.top(),
args
);
operators_stack.pop(); // pop the operator
operators_stack.pop(); // pop the bracket
if (nodes_stack.empty())
nodes_stack.emplace();
nodes_stack.top().emplace_back(operators_stack.top(), args);
operators_stack.pop();// pop the operator
operators_stack.pop();// pop the bracket
} else {
nodes_stack.top().emplace_back(token);
}
Expand Down
9 changes: 3 additions & 6 deletions src/parser/SyntaxTreeNode.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
#ifndef YASI_SYNTAXTREENODE_H
#define YASI_SYNTAXTREENODE_H

#include "lexer/Lexer.h"
#include <utility>
#include <vector>
#include "lexer/Lexer.h"

class SyntaxTreeNode {
public:
Token token;
std::vector<SyntaxTreeNode> children;

SyntaxTreeNode(
Token token,
const std::vector<SyntaxTreeNode> &children
) : token(std::move(token)),
children(children) {}
SyntaxTreeNode(Token token, const std::vector<SyntaxTreeNode> &children)
: token(std::move(token)), children(children) {}

explicit SyntaxTreeNode(Token token) : token(std::move(token)) {}

Expand Down
21 changes: 7 additions & 14 deletions tests/add_test.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include <gtest/gtest.h>
#include "parser/SyntaxTreeNode.h"
#include "builtin_functions/add/add.h"
#include "parser/SyntaxTreeNode.h"
#include <gtest/gtest.h>

TEST(add_test, ShouldAddTwoNumbers) {
auto expression = {
SyntaxTreeNode(Token(Token::Integer, "1")),
SyntaxTreeNode(Token(Token::Integer, "2"))
};
auto expression = {SyntaxTreeNode(Token(Token::Integer, "1")),
SyntaxTreeNode(Token(Token::Integer, "2"))};

auto expectedResult = SyntaxTreeNode(Token(Token::Integer, "3"));
auto actual = Add().evaluate(expression);
Expand All @@ -17,14 +15,9 @@ TEST(add_test, ShouldAddTwoNumbers) {
TEST(add_test, ShouldEvaluateNestedAddition) {
auto expression = {
SyntaxTreeNode(Token(Token::Integer, "1")),
SyntaxTreeNode(
Token(Token::Integer, "+"),
{
SyntaxTreeNode(Token(Token::Integer, "2")),
SyntaxTreeNode(Token(Token::Integer, "3"))
}
)
};
SyntaxTreeNode(Token(Token::Integer, "+"),
{SyntaxTreeNode(Token(Token::Integer, "2")),
SyntaxTreeNode(Token(Token::Integer, "3"))})};

auto expectedResult = SyntaxTreeNode(Token(Token::Integer, "6"));
auto actual = Add().evaluate(expression);
Expand Down
Loading

0 comments on commit d7101ed

Please sign in to comment.