-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "printAST.h" | ||
#include "parser/SyntaxTreeNode.h" | ||
#include <string> | ||
|
||
std::string parse_ast(SyntaxTreeNode ast, int level) { | ||
std::string output; | ||
std::string tabs; | ||
|
||
for (int i = 0; i < level; i++) tabs = tabs + "\t"; | ||
tabs = tabs + "-> "; | ||
|
||
if (!ast.children.empty()) { | ||
output = tabs + "Node (" + ast.token.token + ")\n"; | ||
} else { | ||
std::string type; | ||
if (ast.token.type == Token::Integer) type = "Integer"; | ||
else if (ast.token.type == Token::String) type = "String"; | ||
else type = "Symbol"; | ||
return tabs + type + " (" + ast.token.token + ")\n"; | ||
} | ||
|
||
for (const auto child: ast.children) { | ||
output = output + parse_ast(child, level + 1); | ||
} | ||
|
||
return output; | ||
} | ||
|
||
void print_ast(OutputSource *outputSource, SyntaxTreeNode ast) { | ||
outputSource->out(parse_ast(ast, 0)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef YASI_PRINTAST_H | ||
#define YASI_PRINTAST_H | ||
|
||
#include "parser/SyntaxTreeNode.h" | ||
#include "utils/OutputSource.h" | ||
|
||
void print_ast(OutputSource *outputSource, SyntaxTreeNode ast); | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "parser/SyntaxTreeNode.h" | ||
#include "utils/OutputSource.h" | ||
#include "utils/printAST.h" | ||
#include "gmock/gmock.h" | ||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
#include <memory> | ||
|
||
class MockOutputSource : public OutputSource { | ||
public: | ||
MOCK_METHOD(void, out, (std::string out), (override)); | ||
}; | ||
|
||
|
||
TEST(print_ast_test, ShouldPrintSimpleAST) { | ||
// (+ 1 2) | ||
const SyntaxTreeNode ast = SyntaxTreeNode( | ||
Token(Token::Symbol, "+"), | ||
{ | ||
SyntaxTreeNode(Token(Token::Integer, "1")), | ||
SyntaxTreeNode(Token(Token::Integer, "2")), | ||
}); | ||
std::shared_ptr<MockOutputSource> mockOutputSource(new MockOutputSource); | ||
|
||
EXPECT_CALL(*mockOutputSource, out("-> Node (+)\n" | ||
"\t-> Integer (1)\n" | ||
"\t-> Integer (2)\n")); | ||
print_ast(mockOutputSource.get(), ast); | ||
} | ||
|
||
TEST(print_ast_test, ShouldPrintNestedAST) { | ||
// (+ (* 5 2) 4) | ||
const SyntaxTreeNode ast = SyntaxTreeNode( | ||
Token(Token::Symbol, "+"), | ||
{ | ||
SyntaxTreeNode( | ||
Token(Token::Symbol, "*"), | ||
{ | ||
SyntaxTreeNode(Token(Token::Integer, "5")), | ||
SyntaxTreeNode(Token(Token::Integer, "2")), | ||
}), | ||
SyntaxTreeNode(Token(Token::Integer, "4")), | ||
}); | ||
const std::string expectedOutput = "-> Node (+)\n" | ||
"\t-> Node (*)\n" | ||
"\t\t-> Integer (5)\n" | ||
"\t\t-> Integer (2)\n" | ||
"\t-> Integer (4)\n"; | ||
|
||
std::shared_ptr<MockOutputSource> mockOutputSource(new MockOutputSource); | ||
|
||
EXPECT_CALL(*mockOutputSource, out(expectedOutput)); | ||
print_ast(mockOutputSource.get(), ast); | ||
} |