Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into mv/optree-intf
Browse files Browse the repository at this point in the history
  • Loading branch information
vla5924 committed Apr 1, 2024
2 parents 4647622 + 82dd37b commit 9973680
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
7 changes: 6 additions & 1 deletion compiler/include/compiler/ast/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "compiler/utils/source_ref.hpp"

#include "compiler/ast/node_type.hpp"
#include "compiler/ast/types.hpp"
#include "compiler/ast/variables_table.hpp"

namespace ast {
Expand All @@ -26,6 +27,7 @@ struct Node {
* value type : node type
* long int : IntegerLiteralValue
* double : FloatingPointLiteralValue
* bool : BooleanLiteralValue
* std::string : StringLiteralValue, FunctionName, VariableName
* TypeId : TypeName
* BinaryOperation : BinaryOperation
Expand All @@ -34,14 +36,17 @@ struct Node {
* nothing : other types
*/
NodeType type;
std::variant<long int, double, std::string, TypeId, BinaryOperation, UnaryOperation, VariablesTable> value;
std::variant<long int, double, bool, std::string, TypeId, BinaryOperation, UnaryOperation, VariablesTable> value;

const long int &intNum() const {
return std::get<long int>(value);
}
const double &fpNum() const {
return std::get<double>(value);
}
const bool &boolean() const {
return std::get<bool>(value);
}
const std::string &str() const {
return std::get<std::string>(value);
}
Expand Down
1 change: 1 addition & 0 deletions compiler/include/compiler/ast/node_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum class UnaryOperation {

enum class NodeType {
BinaryOperation,
BooleanLiteralValue,
BranchRoot,
ElifStatement,
ElseStatement,
Expand Down
5 changes: 5 additions & 0 deletions compiler/lib/ast/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <iostream>
#include <sstream>

#include "node_type.hpp"

using namespace ast;

namespace {
Expand Down Expand Up @@ -103,6 +105,9 @@ void Node::dump(std::ostream &stream, int depth) const {
case NodeType::BinaryOperation:
stream << "BinaryOperation: " << binaryOperationToString(binOp()) << "\n";
break;
case NodeType::BooleanLiteralValue:
stream << "BooleanLiteralValue: " << (boolean() ? "True" : "False") << "\n";
break;
case NodeType::BranchRoot:
stream << "BranchRoot";
if (std::holds_alternative<VariablesTable>(value)) {
Expand Down
17 changes: 16 additions & 1 deletion compiler/lib/frontend/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <functional>
#include <unordered_map>

#include "compiler/ast/node.hpp"
#include "compiler/ast/node_type.hpp"

#include "lexer/token_types.hpp"
#include "parser/parser_context.hpp"
#include "parser/parser_error.hpp"
#include "parser/type_registry.hpp"
Expand Down Expand Up @@ -88,7 +92,8 @@ OperationType getOperationType(const ast::Node &node) {

ExpressionTokenType getExpressionTokenType(const Token &token) {
if (token.type == TokenType::Identifier || token.type == TokenType::IntegerLiteral ||
token.type == TokenType::FloatingPointLiteral || token.type == TokenType::StringLiteral)
token.type == TokenType::FloatingPointLiteral || token.type == TokenType::StringLiteral ||
token.is(Keyword::True) || token.is(Keyword::False))
return ExpressionTokenType::Operand;
if (token.is(Operator::LeftBrace))
return ExpressionTokenType::OpeningBrace;
Expand Down Expand Up @@ -163,6 +168,9 @@ size_t getOperationPriority(const Token &token) {
case BinaryOperation::Greater:
case BinaryOperation::GreaterEqual:
return 30;
case BinaryOperation::Equal:
case BinaryOperation::NotEqual:
return 35;
case BinaryOperation::And:
return 40;
case BinaryOperation::Or:
Expand Down Expand Up @@ -222,6 +230,13 @@ void buildExpressionSubtree(std::stack<SubExpression> postfixForm, ast::Node::Pt
ast::Node::Ptr node =
ParserContext::unshiftChildNode(currNode, ast::NodeType::StringLiteralValue, token.ref);
node->value = token.literal();
} else if (token.is(Keyword::False) || token.is(Keyword::True)) {
ast::Node::Ptr node =
ParserContext::unshiftChildNode(currNode, ast::NodeType::BooleanLiteralValue, token.ref);
if (token.is(Keyword::True))
node->value = true;
else if (token.is(Keyword::False))
node->value = false;
}
}
} else {
Expand Down
1 change: 1 addition & 0 deletions compiler/tests/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Checks: -*,cppcoreguidelines-avoid-goto
31 changes: 31 additions & 0 deletions compiler/tests/frontend/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,34 @@ TEST(Parser, can_parse_complex_nested_function_call) {
" VariableName: z\n";
ASSERT_EQ(tree_str, tree.dump());
}

TEST(Parser, can_parse_bool) {
StringVec source = {
"def main() -> None:",
" x: bool = True",
" x = (3 == 4) and False",
};
TokenList tokens = Lexer::process(source);

SyntaxTree tree = Parser::process(tokens);
std::string expected = "ProgramRoot\n"
" FunctionDefinition\n"
" FunctionName: main\n"
" FunctionArguments\n"
" FunctionReturnType: NoneType\n"
" BranchRoot\n"
" VariableDeclaration\n"
" TypeName: BoolType\n"
" VariableName: x\n"
" Expression\n"
" BooleanLiteralValue: True\n"
" Expression\n"
" BinaryOperation: Assign\n"
" VariableName: x\n"
" BinaryOperation: And\n"
" BinaryOperation: Equal\n"
" IntegerLiteralValue: 3\n"
" IntegerLiteralValue: 4\n"
" BooleanLiteralValue: False\n";
ASSERT_EQ(expected, tree.dump());
}

0 comments on commit 9973680

Please sign in to comment.