Creates base of script class

This commit is contained in:
Deukhoofd 2019-05-21 12:59:15 +02:00
parent 37e770f1cb
commit ad3e61128c
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
7 changed files with 70 additions and 12 deletions

View File

@ -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)

View File

@ -198,6 +198,25 @@ TEST_CASE( "Parse simple negation", "[parser]" ) {
CHECK(((LiteralIntegerExpression*)operand)->GetValue() == 10);
}
TEST_CASE( "Parse logical negation", "[parser]" ) {
vector<IToken*> 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<IToken*> v {

View File

@ -28,6 +28,10 @@ public:
unsigned int GetLength(){
return Length;
}
virtual ~IToken(){
}
};
class SimpleToken : public IToken{

21
src/Script.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <utility>
#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();
}

24
src/Script.hpp Normal file
View File

@ -0,0 +1,24 @@
#include <utility>
#ifndef PORYGONLANG_SCRIPT_HPP
#define PORYGONLANG_SCRIPT_HPP
#include <string>
#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

View File

@ -1,3 +0,0 @@
#include "main.h++"

View File

@ -1,7 +0,0 @@
#ifndef PORYGONLANG_MAIN_H
#define PORYGONLANG_MAIN_H
#endif //PORYGONLANG_MAIN_H