diff --git a/CMakeLists.txt b/CMakeLists.txt index 2911d96..42680f6 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,8 +5,8 @@ set(CMAKE_CXX_STANDARD 17) #add_subdirectory(extern) include_directories(extern) -add_library(PorygonLang src/main.cpp src/main.h++ src/Parser/Lexer.cpp src/Parser/Lexer.hpp src/Parser/TokenKind.hpp src/Parser/Token.hpp src/Parser/Parser.cpp src/Parser/Parser.hpp src/Parser/ParsedStatements/ParsedStatement.hpp src/Parser/ParsedExpressions/ParsedExpression.hpp src/Parser/BinaryOperatorKind.hpp) -add_executable(PorygonLangTests src/main.cpp src/main.h++ src/Parser/Lexer.cpp src/Parser/Lexer.hpp src/Parser/TokenKind.hpp src/Parser/Token.hpp src/Parser/LexerTests.cpp src/Parser/Parser.cpp src/Parser/Parser.hpp src/Parser/ParsedStatements/ParsedStatement.hpp src/Parser/ParsedExpressions/ParsedExpression.hpp src/Parser/BinaryOperatorKind.hpp) +add_library(PorygonLang src/Parser/Lexer.cpp src/Parser/Lexer.hpp src/Parser/TokenKind.hpp src/Parser/Token.hpp src/Parser/Parser.cpp src/Parser/Parser.hpp src/Parser/ParsedStatements/ParsedStatement.hpp src/Parser/ParsedExpressions/ParsedExpression.hpp src/Parser/BinaryOperatorKind.hpp src/Script.cpp src/Script.hpp) +add_executable(PorygonLangTests src/Parser/Lexer.cpp src/Parser/Lexer.hpp src/Parser/TokenKind.hpp src/Parser/Token.hpp src/Parser/LexerTests.cpp src/Parser/Parser.cpp src/Parser/Parser.hpp src/Parser/ParsedStatements/ParsedStatement.hpp src/Parser/ParsedExpressions/ParsedExpression.hpp src/Parser/BinaryOperatorKind.hpp) target_compile_definitions(PorygonLangTests PRIVATE TESTS_BUILD) diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index db3887e..da83350 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -198,6 +198,25 @@ TEST_CASE( "Parse simple negation", "[parser]" ) { CHECK(((LiteralIntegerExpression*)operand)->GetValue() == 10); } +TEST_CASE( "Parse logical negation", "[parser]" ) { + vector v { + new SimpleToken(TokenKind::NotKeyword,0,0), + new SimpleToken(TokenKind::FalseKeyword,0,0), + new SimpleToken(TokenKind::EndOfFile,0,0) + }; + Parser parser = Parser(v); + auto parsedStatements = parser.Parse() -> GetStatements(); + REQUIRE(parsedStatements.size() == 1); + auto firstStatement = parsedStatements[0]; + REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression); + auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression(); + REQUIRE(expression -> GetKind() == ParsedExpressionKind::Unary); + auto unary = ((UnaryExpression*)expression); + CHECK(unary -> GetOperatorKind() == UnaryOperatorKind::LogicalNegation); + auto operand = unary->GetOperand(); + REQUIRE(operand->GetKind() == ParsedExpressionKind::LiteralBool); + CHECK(((LiteralBoolExpression*)operand)->GetValue() == false); +} TEST_CASE( "Assert binary precedence", "[parser]" ) { vector v { diff --git a/src/Parser/Token.hpp b/src/Parser/Token.hpp index bc7da2d..78ebeb3 100644 --- a/src/Parser/Token.hpp +++ b/src/Parser/Token.hpp @@ -28,6 +28,10 @@ public: unsigned int GetLength(){ return Length; } + + virtual ~IToken(){ + + } }; class SimpleToken : public IToken{ diff --git a/src/Script.cpp b/src/Script.cpp new file mode 100644 index 0000000..63b130b --- /dev/null +++ b/src/Script.cpp @@ -0,0 +1,21 @@ +#include + + +#include "Script.hpp" + +Script Script::Create(string script) { + auto s = Script(); + s.Parse(std::move(script)); + return s; +} + +void Script::Parse(string script) { + auto lexer = Lexer(std::move(script)); + auto lexResult = lexer.Lex(); + auto parser = Parser(lexResult); + auto parseResult = parser.Parse(); + for (auto token : lexResult){ + delete token; + } + lexResult.clear(); +} diff --git a/src/Script.hpp b/src/Script.hpp new file mode 100644 index 0000000..cb478e5 --- /dev/null +++ b/src/Script.hpp @@ -0,0 +1,24 @@ +#include + + +#ifndef PORYGONLANG_SCRIPT_HPP +#define PORYGONLANG_SCRIPT_HPP + +#include +#include "Parser/Lexer.hpp" +#include "Parser/Parser.hpp" + +using namespace std; + + +class Script { + explicit Script(){ + } + + void Parse(string script); +public: + static Script Create(string script); +}; + + +#endif //PORYGONLANG_SCRIPT_HPP diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index 489d364..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "main.h++" - - diff --git a/src/main.h++ b/src/main.h++ deleted file mode 100644 index f0521be..0000000 --- a/src/main.h++ +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef PORYGONLANG_MAIN_H -#define PORYGONLANG_MAIN_H - - - - -#endif //PORYGONLANG_MAIN_H