Each statement class in separate file
This commit is contained in:
@@ -6,6 +6,7 @@ project(
|
|||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
set(CMAKE_COLOR_DIAGNOSTICS OFF)
|
set(CMAKE_COLOR_DIAGNOSTICS OFF)
|
||||||
|
set(CMAKE_VERBOSE_MAKEFILE ON)
|
||||||
|
|
||||||
find_package(LLVM REQUIRED CONFIG)
|
find_package(LLVM REQUIRED CONFIG)
|
||||||
include_directories(${LLVM_INCLUDE_DIRS})
|
include_directories(${LLVM_INCLUDE_DIRS})
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
#include "ModuleBuilder.h"
|
#include "ModuleBuilder.h"
|
||||||
|
|
||||||
|
#include "Parser/Statement/StatementExpression.h"
|
||||||
|
#include "Parser/Statement/StatementBlock.h"
|
||||||
|
#include "Parser/Statement/StatementFunction.h"
|
||||||
|
#include "Parser/Statement/StatementVariable.h"
|
||||||
|
#include "Parser/Statement/StatementReturn.h"
|
||||||
|
#include "Parser/Statement/StatementMetaExternFunction.h"
|
||||||
|
|
||||||
ModuleBuilder::ModuleBuilder(string moduleName, string sourceFileName, vector<shared_ptr<Statement>> statements):
|
ModuleBuilder::ModuleBuilder(string moduleName, string sourceFileName, vector<shared_ptr<Statement>> statements):
|
||||||
moduleName(moduleName), sourceFileName(sourceFileName), statements(statements) {
|
moduleName(moduleName), sourceFileName(sourceFileName), statements(statements) {
|
||||||
context = make_shared<llvm::LLVMContext>();
|
context = make_shared<llvm::LLVMContext>();
|
||||||
@@ -23,10 +30,10 @@ shared_ptr<llvm::Module> ModuleBuilder::getModule() {
|
|||||||
void ModuleBuilder::buildStatement(shared_ptr<Statement> statement) {
|
void ModuleBuilder::buildStatement(shared_ptr<Statement> statement) {
|
||||||
switch (statement->getKind()) {
|
switch (statement->getKind()) {
|
||||||
case StatementKind::FUNCTION_DECLARATION:
|
case StatementKind::FUNCTION_DECLARATION:
|
||||||
buildFunctionDeclaration(dynamic_pointer_cast<StatementFunctionDeclaration>(statement));
|
buildFunctionDeclaration(dynamic_pointer_cast<StatementFunction>(statement));
|
||||||
break;
|
break;
|
||||||
case StatementKind::VAR_DECLARATION:
|
case StatementKind::VAR_DECLARATION:
|
||||||
buildVarDeclaration(dynamic_pointer_cast<StatementVarDeclaration>(statement));
|
buildVarDeclaration(dynamic_pointer_cast<StatementVariable>(statement));
|
||||||
break;
|
break;
|
||||||
case StatementKind::BLOCK:
|
case StatementKind::BLOCK:
|
||||||
buildBlock(dynamic_pointer_cast<StatementBlock>(statement));
|
buildBlock(dynamic_pointer_cast<StatementBlock>(statement));
|
||||||
@@ -45,7 +52,7 @@ void ModuleBuilder::buildStatement(shared_ptr<Statement> statement) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModuleBuilder::buildFunctionDeclaration(shared_ptr<StatementFunctionDeclaration> statement) {
|
void ModuleBuilder::buildFunctionDeclaration(shared_ptr<StatementFunction> statement) {
|
||||||
// get argument types
|
// get argument types
|
||||||
vector<llvm::Type *> types;
|
vector<llvm::Type *> types;
|
||||||
for (pair<string, ValueType> &arg : statement->getArguments()) {
|
for (pair<string, ValueType> &arg : statement->getArguments()) {
|
||||||
@@ -85,7 +92,7 @@ void ModuleBuilder::buildFunctionDeclaration(shared_ptr<StatementFunctionDeclara
|
|||||||
failWithMessage(errorMessage);
|
failWithMessage(errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModuleBuilder::buildVarDeclaration(shared_ptr<StatementVarDeclaration> statement) {
|
void ModuleBuilder::buildVarDeclaration(shared_ptr<StatementVariable> statement) {
|
||||||
llvm::Value *value = valueForExpression(statement->getExpression());
|
llvm::Value *value = valueForExpression(statement->getExpression());
|
||||||
llvm::AllocaInst *alloca = builder->CreateAlloca(typeForValueType(statement->getValueType()), nullptr, statement->getName());
|
llvm::AllocaInst *alloca = builder->CreateAlloca(typeForValueType(statement->getValueType()), nullptr, statement->getName());
|
||||||
allocaMap[statement->getName()] = alloca;
|
allocaMap[statement->getName()] = alloca;
|
||||||
|
|||||||
@@ -11,7 +11,13 @@
|
|||||||
#include <llvm/IR/Verifier.h>
|
#include <llvm/IR/Verifier.h>
|
||||||
|
|
||||||
#include "Parser/Expression.h"
|
#include "Parser/Expression.h"
|
||||||
#include "Parser/Statement.h"
|
#include "Parser/Statement/Statement.h"
|
||||||
|
|
||||||
|
class StatementBlock;
|
||||||
|
class StatementFunction;
|
||||||
|
class StatementVariable;
|
||||||
|
class StatementReturn;
|
||||||
|
class StatementMetaExternFunction;
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -34,8 +40,8 @@ private:
|
|||||||
map<string, llvm::Function*> funMap;
|
map<string, llvm::Function*> funMap;
|
||||||
|
|
||||||
void buildStatement(shared_ptr<Statement> statement);
|
void buildStatement(shared_ptr<Statement> statement);
|
||||||
void buildFunctionDeclaration(shared_ptr<StatementFunctionDeclaration> statement);
|
void buildFunctionDeclaration(shared_ptr<StatementFunction> statement);
|
||||||
void buildVarDeclaration(shared_ptr<StatementVarDeclaration> statement);
|
void buildVarDeclaration(shared_ptr<StatementVariable> statement);
|
||||||
void buildBlock(shared_ptr<StatementBlock> statement);
|
void buildBlock(shared_ptr<StatementBlock> statement);
|
||||||
void buildReturn(shared_ptr<StatementReturn> statement);
|
void buildReturn(shared_ptr<StatementReturn> statement);
|
||||||
void buildMetaExternFunction(shared_ptr<StatementMetaExternFunction> statement);
|
void buildMetaExternFunction(shared_ptr<StatementMetaExternFunction> statement);
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
#include "Expression.h"
|
#include "Expression.h"
|
||||||
|
|
||||||
|
#include "Parser/Statement/StatementExpression.h"
|
||||||
|
#include "Parser/Statement/StatementBlock.h"
|
||||||
|
|
||||||
Expression::Expression(ExpressionKind kind, ValueType valueType):
|
Expression::Expression(ExpressionKind kind, ValueType valueType):
|
||||||
kind(kind), valueType(valueType) {
|
kind(kind), valueType(valueType) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
#define EXPRESSION_H
|
#define EXPRESSION_H
|
||||||
|
|
||||||
#include "Lexer/Token.h"
|
#include "Lexer/Token.h"
|
||||||
#include "Statement.h"
|
#include "Parser/Statement/Statement.h"
|
||||||
#include "Types.h"
|
#include "Types.h"
|
||||||
|
|
||||||
class StatementBlock;
|
class StatementBlock;
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
#include "Parser.h"
|
#include "Parser.h"
|
||||||
|
|
||||||
|
#include "Parser/Statement/StatementExpression.h"
|
||||||
|
#include "Parser/Statement/StatementBlock.h"
|
||||||
|
#include "Parser/Statement/StatementFunction.h"
|
||||||
|
#include "Parser/Statement/StatementVariable.h"
|
||||||
|
#include "Parser/Statement/StatementReturn.h"
|
||||||
|
#include "Parser/Statement/StatementMetaExternFunction.h"
|
||||||
|
#include "Parser/Statement/StatementInvalid.h"
|
||||||
|
|
||||||
Parser::Parser(vector<shared_ptr<Token>> tokens): tokens(tokens) {
|
Parser::Parser(vector<shared_ptr<Token>> tokens): tokens(tokens) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,7 +111,7 @@ shared_ptr<Statement> Parser::matchStatementFunctionDeclaration() {
|
|||||||
if(!tryMatchingTokenKinds({TokenKind::NEW_LINE}, false, true))
|
if(!tryMatchingTokenKinds({TokenKind::NEW_LINE}, false, true))
|
||||||
return matchStatementInvalid("Expected a new line after a function declaration");
|
return matchStatementInvalid("Expected a new line after a function declaration");
|
||||||
|
|
||||||
return make_shared<StatementFunctionDeclaration>(identifierToken->getLexme(), arguments, returnType, dynamic_pointer_cast<StatementBlock>(statementBlock));
|
return make_shared<StatementFunction>(identifierToken->getLexme(), arguments, returnType, dynamic_pointer_cast<StatementBlock>(statementBlock));
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<Statement> Parser::matchStatementVarDeclaration() {
|
shared_ptr<Statement> Parser::matchStatementVarDeclaration() {
|
||||||
@@ -138,7 +146,7 @@ shared_ptr<Statement> Parser::matchStatementVarDeclaration() {
|
|||||||
if (!tryMatchingTokenKinds({TokenKind::NEW_LINE}, false, true))
|
if (!tryMatchingTokenKinds({TokenKind::NEW_LINE}, false, true))
|
||||||
return matchStatementInvalid("Expected a new line after variable declaration");
|
return matchStatementInvalid("Expected a new line after variable declaration");
|
||||||
|
|
||||||
return make_shared<StatementVarDeclaration>(identifierToken->getLexme(), valueType, expression);
|
return make_shared<StatementVariable>(identifierToken->getLexme(), valueType, expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<Statement> Parser::matchStatementBlock(vector<TokenKind> terminalTokenKinds, bool shouldConsumeTerminal) {
|
shared_ptr<Statement> Parser::matchStatementBlock(vector<TokenKind> terminalTokenKinds, bool shouldConsumeTerminal) {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
#include "Lexer/Token.h"
|
#include "Lexer/Token.h"
|
||||||
#include "Expression.h"
|
#include "Expression.h"
|
||||||
#include "Statement.h"
|
#include "Parser/Statement/Statement.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|||||||
@@ -1,223 +0,0 @@
|
|||||||
#include "Statement.h"
|
|
||||||
|
|
||||||
string valueTypeToString(ValueType valueType) {
|
|
||||||
switch (valueType) {
|
|
||||||
case ValueType::NONE:
|
|
||||||
return "NONE";
|
|
||||||
case ValueType::BOOL:
|
|
||||||
return "BOOL";
|
|
||||||
case ValueType::SINT32:
|
|
||||||
return "SINT32";
|
|
||||||
case ValueType::REAL32:
|
|
||||||
return "REAL32";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Statement
|
|
||||||
Statement::Statement(StatementKind kind): kind(kind) {
|
|
||||||
}
|
|
||||||
|
|
||||||
StatementKind Statement::getKind() {
|
|
||||||
return kind;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Statement::isValid() {
|
|
||||||
return kind != StatementKind::INVALID;
|
|
||||||
}
|
|
||||||
|
|
||||||
string Statement::toString(int indent) {
|
|
||||||
return "STATEMENT";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// StatementFunctionDeclaration
|
|
||||||
StatementFunctionDeclaration::StatementFunctionDeclaration(string name, vector<pair<string, ValueType>> arguments, ValueType returnValueType, shared_ptr<StatementBlock> statementBlock):
|
|
||||||
Statement(StatementKind::FUNCTION_DECLARATION), name(name), arguments(arguments), returnValueType(returnValueType), statementBlock(statementBlock) {
|
|
||||||
}
|
|
||||||
|
|
||||||
string StatementFunctionDeclaration::getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<pair<string, ValueType>> StatementFunctionDeclaration::getArguments() {
|
|
||||||
return arguments;
|
|
||||||
}
|
|
||||||
|
|
||||||
ValueType StatementFunctionDeclaration::getReturnValueType() {
|
|
||||||
return returnValueType;
|
|
||||||
}
|
|
||||||
|
|
||||||
shared_ptr<StatementBlock> StatementFunctionDeclaration::getStatementBlock() {
|
|
||||||
return statementBlock;
|
|
||||||
}
|
|
||||||
|
|
||||||
string StatementFunctionDeclaration::toString(int indent) {
|
|
||||||
string value = "";
|
|
||||||
for (int ind=0; ind<indent; ind++)
|
|
||||||
value += " ";
|
|
||||||
value += "FUNCTION(";
|
|
||||||
value += valueTypeToString(returnValueType);
|
|
||||||
value += ", " + name + "):\n";
|
|
||||||
value += statementBlock->toString(indent+1);
|
|
||||||
for (int ind=0; ind<indent; ind++)
|
|
||||||
value += " ";
|
|
||||||
value += ";";
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// StatementVarDeclaration
|
|
||||||
StatementVarDeclaration::StatementVarDeclaration(string name, ValueType valueType, shared_ptr<Expression> expression):
|
|
||||||
Statement(StatementKind::VAR_DECLARATION), name(name), valueType(valueType), expression(expression) {
|
|
||||||
}
|
|
||||||
|
|
||||||
string StatementVarDeclaration::getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
ValueType StatementVarDeclaration::getValueType() {
|
|
||||||
return valueType;
|
|
||||||
}
|
|
||||||
|
|
||||||
shared_ptr<Expression> StatementVarDeclaration::getExpression() {
|
|
||||||
return expression;
|
|
||||||
}
|
|
||||||
|
|
||||||
string StatementVarDeclaration::toString(int indent) {
|
|
||||||
string value;
|
|
||||||
for (int ind=0; ind<indent; ind++)
|
|
||||||
value += " ";
|
|
||||||
value += name + "(";
|
|
||||||
value += valueTypeToString(valueType);
|
|
||||||
value += "):\n";
|
|
||||||
for (int ind=0; ind<indent+1; ind++)
|
|
||||||
value += " ";
|
|
||||||
value += expression->toString(indent+1);
|
|
||||||
value += "\n";
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// StatementBlock
|
|
||||||
StatementBlock::StatementBlock(vector<shared_ptr<Statement>> statements):
|
|
||||||
Statement(StatementKind::BLOCK), statements(statements) {
|
|
||||||
if (!statements.empty() && statements.back()->getKind() == StatementKind::EXPRESSION) {
|
|
||||||
statementExpression = dynamic_pointer_cast<StatementExpression>(statements.back());
|
|
||||||
this->statements.pop_back();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<shared_ptr<Statement>> StatementBlock::getStatements() {
|
|
||||||
return statements;
|
|
||||||
}
|
|
||||||
|
|
||||||
shared_ptr<StatementExpression> StatementBlock::getStatementExpression() {
|
|
||||||
return statementExpression;
|
|
||||||
}
|
|
||||||
|
|
||||||
string StatementBlock::toString(int indent) {
|
|
||||||
string value;
|
|
||||||
for (int i=0; i<statements.size(); i++) {
|
|
||||||
//for (int ind=0; ind<indent; ind++)
|
|
||||||
// value += " ";
|
|
||||||
value += statements.at(i)->toString(indent);
|
|
||||||
}
|
|
||||||
if (statementExpression != nullptr) {
|
|
||||||
for (int ind=0; ind<indent; ind++)
|
|
||||||
value += " ";
|
|
||||||
value += "WRAP_UP:\n";
|
|
||||||
value += statementExpression->toString(indent);
|
|
||||||
}
|
|
||||||
for (int ind=0; ind<indent; ind++)
|
|
||||||
value += " ";
|
|
||||||
value += "#\n";
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// StatementReturn
|
|
||||||
StatementReturn::StatementReturn(shared_ptr<Expression> expression):
|
|
||||||
Statement(StatementKind::RETURN), expression(expression) {
|
|
||||||
}
|
|
||||||
|
|
||||||
shared_ptr<Expression> StatementReturn::getExpression() {
|
|
||||||
return expression;
|
|
||||||
}
|
|
||||||
|
|
||||||
string StatementReturn::toString(int indent) {
|
|
||||||
string value;
|
|
||||||
for (int ind=0; ind<indent; ind++)
|
|
||||||
value += " ";
|
|
||||||
value += "RETURN";
|
|
||||||
if (expression != nullptr) {
|
|
||||||
value += ":\n";
|
|
||||||
for (int ind=0; ind<indent+1; ind++)
|
|
||||||
value += " ";
|
|
||||||
value += expression->toString(indent+1);
|
|
||||||
}
|
|
||||||
value += "\n";
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// StatementExpression
|
|
||||||
StatementExpression::StatementExpression(shared_ptr<Expression> expression):
|
|
||||||
Statement(StatementKind::EXPRESSION), expression(expression) {
|
|
||||||
}
|
|
||||||
|
|
||||||
shared_ptr<Expression> StatementExpression::getExpression() {
|
|
||||||
return expression;
|
|
||||||
}
|
|
||||||
|
|
||||||
string StatementExpression::toString(int indent) {
|
|
||||||
string value;
|
|
||||||
for (int ind=0; ind<indent; ind++)
|
|
||||||
value += " ";
|
|
||||||
value += expression->toString(indent);
|
|
||||||
value += "\n";
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Statement @ Function
|
|
||||||
StatementMetaExternFunction::StatementMetaExternFunction(string name, vector<pair<string, ValueType>> arguments, ValueType returnValueType):
|
|
||||||
Statement(StatementKind::META_EXTERN_FUNCTION), name(name), arguments(arguments), returnValueType(returnValueType) {
|
|
||||||
}
|
|
||||||
|
|
||||||
string StatementMetaExternFunction::getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<pair<string, ValueType>> StatementMetaExternFunction::getArguments() {
|
|
||||||
return arguments;
|
|
||||||
}
|
|
||||||
|
|
||||||
ValueType StatementMetaExternFunction::getReturnValueType() {
|
|
||||||
return returnValueType;
|
|
||||||
}
|
|
||||||
|
|
||||||
string StatementMetaExternFunction::toString(int indent) {
|
|
||||||
string value;
|
|
||||||
for (int ind=0; ind<indent; ind++)
|
|
||||||
value += " ";
|
|
||||||
value += "EXTERN_FUN(";
|
|
||||||
value += name + ", ";
|
|
||||||
value += valueTypeToString(returnValueType);
|
|
||||||
value += ")\n";
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// StatementInvalid
|
|
||||||
StatementInvalid::StatementInvalid(shared_ptr<Token> token, string message):
|
|
||||||
Statement(StatementKind::INVALID), token(token), message(message) {
|
|
||||||
}
|
|
||||||
|
|
||||||
string StatementInvalid::toString(int indent) {
|
|
||||||
return "Error for token " + token->toString() + " at " + to_string(token->getLine()) + ":" + to_string(token->getColumn()) + ": " + message + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
string StatementInvalid::getMessage() {
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
@@ -1,143 +0,0 @@
|
|||||||
#ifndef STATEMENT_H
|
|
||||||
#define STATEMENT_H
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include "Lexer/Token.h"
|
|
||||||
#include "Expression.h"
|
|
||||||
#include "Types.h"
|
|
||||||
|
|
||||||
class Expression;
|
|
||||||
class Statement;
|
|
||||||
class StatementBlock;
|
|
||||||
class StatementReturn;
|
|
||||||
class StatementExpression;
|
|
||||||
class StatementInvalid;
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
enum class StatementKind {
|
|
||||||
FUNCTION_DECLARATION,
|
|
||||||
VAR_DECLARATION,
|
|
||||||
BLOCK,
|
|
||||||
RETURN,
|
|
||||||
EXPRESSION,
|
|
||||||
META_EXTERN_FUNCTION,
|
|
||||||
INVALID
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
|
||||||
// Statement
|
|
||||||
class Statement {
|
|
||||||
private:
|
|
||||||
StatementKind kind;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Statement(StatementKind kind);
|
|
||||||
StatementKind getKind();
|
|
||||||
bool isValid();
|
|
||||||
virtual string toString(int indent);
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
|
||||||
// StatementFunctionDeclaration
|
|
||||||
class StatementFunctionDeclaration: public Statement {
|
|
||||||
private:
|
|
||||||
string name;
|
|
||||||
vector<pair<string, ValueType>> arguments;
|
|
||||||
ValueType returnValueType;
|
|
||||||
shared_ptr<StatementBlock> statementBlock;
|
|
||||||
|
|
||||||
public:
|
|
||||||
StatementFunctionDeclaration(string name, vector<pair<string, ValueType>> arguments, ValueType returnValueType, shared_ptr<StatementBlock> statementBlock);
|
|
||||||
string getName();
|
|
||||||
vector<pair<string, ValueType>> getArguments();
|
|
||||||
ValueType getReturnValueType();
|
|
||||||
shared_ptr<StatementBlock> getStatementBlock();
|
|
||||||
string toString(int indent) override;
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
|
||||||
// StatementVarDeclaration
|
|
||||||
class StatementVarDeclaration: public Statement {
|
|
||||||
private:
|
|
||||||
string name;
|
|
||||||
ValueType valueType;
|
|
||||||
shared_ptr<Expression> expression;
|
|
||||||
|
|
||||||
public:
|
|
||||||
StatementVarDeclaration(string name, ValueType valueType, shared_ptr<Expression> expression);
|
|
||||||
string getName();
|
|
||||||
ValueType getValueType();
|
|
||||||
shared_ptr<Expression> getExpression();
|
|
||||||
string toString(int indent) override;
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
|
||||||
// StatementBlock
|
|
||||||
class StatementBlock: public Statement {
|
|
||||||
private:
|
|
||||||
vector<shared_ptr<Statement>> statements;
|
|
||||||
shared_ptr<StatementExpression> statementExpression;
|
|
||||||
|
|
||||||
public:
|
|
||||||
StatementBlock(vector<shared_ptr<Statement>> statements);
|
|
||||||
vector<shared_ptr<Statement>> getStatements();
|
|
||||||
shared_ptr<StatementExpression> getStatementExpression();
|
|
||||||
string toString(int indent) override;
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
|
||||||
// StatementReturn
|
|
||||||
class StatementReturn: public Statement {
|
|
||||||
private:
|
|
||||||
shared_ptr<Expression> expression;
|
|
||||||
|
|
||||||
public:
|
|
||||||
StatementReturn(shared_ptr<Expression> expression);
|
|
||||||
shared_ptr<Expression> getExpression();
|
|
||||||
string toString(int indent) override;
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
|
||||||
// StatementExpression
|
|
||||||
class StatementExpression: public Statement {
|
|
||||||
private:
|
|
||||||
shared_ptr<Expression> expression;
|
|
||||||
|
|
||||||
public:
|
|
||||||
StatementExpression(shared_ptr<Expression> expression);
|
|
||||||
shared_ptr<Expression> getExpression();
|
|
||||||
string toString(int indent) override;
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
|
||||||
// Statement @ Extern Function
|
|
||||||
class StatementMetaExternFunction: public Statement {
|
|
||||||
private:
|
|
||||||
string name;
|
|
||||||
vector<pair<string, ValueType>> arguments;
|
|
||||||
ValueType returnValueType;
|
|
||||||
|
|
||||||
public:
|
|
||||||
StatementMetaExternFunction(string name, vector<pair<string, ValueType>> arguments, ValueType returnValueType);
|
|
||||||
string getName();
|
|
||||||
vector<pair<string, ValueType>> getArguments();
|
|
||||||
ValueType getReturnValueType();
|
|
||||||
string toString(int indent) override;
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
|
||||||
// StatementInvalid
|
|
||||||
class StatementInvalid: public Statement {
|
|
||||||
private:
|
|
||||||
shared_ptr<Token> token;
|
|
||||||
string message;
|
|
||||||
|
|
||||||
public:
|
|
||||||
StatementInvalid(shared_ptr<Token> token, string message);
|
|
||||||
string toString(int indent) override;
|
|
||||||
string getMessage();
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
16
src/Parser/Statement/Statement.cpp
Normal file
16
src/Parser/Statement/Statement.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include "Statement.h"
|
||||||
|
|
||||||
|
Statement::Statement(StatementKind kind):
|
||||||
|
kind(kind) { }
|
||||||
|
|
||||||
|
StatementKind Statement::getKind() {
|
||||||
|
return kind;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Statement::isValid() {
|
||||||
|
return kind != StatementKind::INVALID;
|
||||||
|
}
|
||||||
|
|
||||||
|
string Statement::toString(int indent) {
|
||||||
|
return "STATEMENT";
|
||||||
|
}
|
||||||
40
src/Parser/Statement/Statement.h
Normal file
40
src/Parser/Statement/Statement.h
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#ifndef STATEMENT_H
|
||||||
|
#define STATEMENT_H
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "Lexer/Token.h"
|
||||||
|
#include "Parser/Expression.h"
|
||||||
|
#include "Types.h"
|
||||||
|
|
||||||
|
class Expression;
|
||||||
|
class Statement;
|
||||||
|
class StatementBlock;
|
||||||
|
class StatementReturn;
|
||||||
|
class StatementExpression;
|
||||||
|
class StatementInvalid;
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
enum class StatementKind {
|
||||||
|
FUNCTION_DECLARATION,
|
||||||
|
VAR_DECLARATION,
|
||||||
|
BLOCK,
|
||||||
|
RETURN,
|
||||||
|
EXPRESSION,
|
||||||
|
META_EXTERN_FUNCTION,
|
||||||
|
INVALID
|
||||||
|
};
|
||||||
|
|
||||||
|
class Statement {
|
||||||
|
private:
|
||||||
|
StatementKind kind;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Statement(StatementKind kind);
|
||||||
|
StatementKind getKind();
|
||||||
|
bool isValid();
|
||||||
|
virtual string toString(int indent);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
38
src/Parser/Statement/StatementBlock.cpp
Normal file
38
src/Parser/Statement/StatementBlock.cpp
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#include "StatementBlock.h"
|
||||||
|
|
||||||
|
#include "Parser/Statement/StatementExpression.h"
|
||||||
|
|
||||||
|
StatementBlock::StatementBlock(vector<shared_ptr<Statement>> statements):
|
||||||
|
Statement(StatementKind::BLOCK), statements(statements) {
|
||||||
|
if (!statements.empty() && statements.back()->getKind() == StatementKind::EXPRESSION) {
|
||||||
|
statementExpression = dynamic_pointer_cast<StatementExpression>(statements.back());
|
||||||
|
this->statements.pop_back();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<shared_ptr<Statement>> StatementBlock::getStatements() {
|
||||||
|
return statements;
|
||||||
|
}
|
||||||
|
|
||||||
|
shared_ptr<StatementExpression> StatementBlock::getStatementExpression() {
|
||||||
|
return statementExpression;
|
||||||
|
}
|
||||||
|
|
||||||
|
string StatementBlock::toString(int indent) {
|
||||||
|
string value;
|
||||||
|
for (int i=0; i<statements.size(); i++) {
|
||||||
|
//for (int ind=0; ind<indent; ind++)
|
||||||
|
// value += " ";
|
||||||
|
value += statements.at(i)->toString(indent);
|
||||||
|
}
|
||||||
|
if (statementExpression != nullptr) {
|
||||||
|
for (int ind=0; ind<indent; ind++)
|
||||||
|
value += " ";
|
||||||
|
value += "WRAP_UP:\n";
|
||||||
|
value += statementExpression->toString(indent);
|
||||||
|
}
|
||||||
|
for (int ind=0; ind<indent; ind++)
|
||||||
|
value += " ";
|
||||||
|
value += "#\n";
|
||||||
|
return value;
|
||||||
|
}
|
||||||
13
src/Parser/Statement/StatementBlock.h
Normal file
13
src/Parser/Statement/StatementBlock.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#include "Parser/Statement/Statement.h"
|
||||||
|
|
||||||
|
class StatementBlock: public Statement {
|
||||||
|
private:
|
||||||
|
vector<shared_ptr<Statement>> statements;
|
||||||
|
shared_ptr<StatementExpression> statementExpression;
|
||||||
|
|
||||||
|
public:
|
||||||
|
StatementBlock(vector<shared_ptr<Statement>> statements);
|
||||||
|
vector<shared_ptr<Statement>> getStatements();
|
||||||
|
shared_ptr<StatementExpression> getStatementExpression();
|
||||||
|
string toString(int indent) override;
|
||||||
|
};
|
||||||
17
src/Parser/Statement/StatementExpression.cpp
Normal file
17
src/Parser/Statement/StatementExpression.cpp
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include "StatementExpression.h"
|
||||||
|
|
||||||
|
StatementExpression::StatementExpression(shared_ptr<Expression> expression):
|
||||||
|
Statement(StatementKind::EXPRESSION), expression(expression) { }
|
||||||
|
|
||||||
|
shared_ptr<Expression> StatementExpression::getExpression() {
|
||||||
|
return expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
string StatementExpression::toString(int indent) {
|
||||||
|
string value;
|
||||||
|
for (int ind=0; ind<indent; ind++)
|
||||||
|
value += " ";
|
||||||
|
value += expression->toString(indent);
|
||||||
|
value += "\n";
|
||||||
|
return value;
|
||||||
|
}
|
||||||
11
src/Parser/Statement/StatementExpression.h
Normal file
11
src/Parser/Statement/StatementExpression.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#include "Parser/Statement/Statement.h"
|
||||||
|
|
||||||
|
class StatementExpression: public Statement {
|
||||||
|
private:
|
||||||
|
shared_ptr<Expression> expression;
|
||||||
|
|
||||||
|
public:
|
||||||
|
StatementExpression(shared_ptr<Expression> expression);
|
||||||
|
shared_ptr<Expression> getExpression();
|
||||||
|
string toString(int indent) override;
|
||||||
|
};
|
||||||
49
src/Parser/Statement/StatementFunction.cpp
Normal file
49
src/Parser/Statement/StatementFunction.cpp
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#include "StatementFunction.h"
|
||||||
|
|
||||||
|
#include "Parser/Statement/StatementBlock.h"
|
||||||
|
|
||||||
|
static string valueTypeToString(ValueType valueType) {
|
||||||
|
switch (valueType) {
|
||||||
|
case ValueType::NONE:
|
||||||
|
return "NONE";
|
||||||
|
case ValueType::BOOL:
|
||||||
|
return "BOOL";
|
||||||
|
case ValueType::SINT32:
|
||||||
|
return "SINT32";
|
||||||
|
case ValueType::REAL32:
|
||||||
|
return "REAL32";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
StatementFunction::StatementFunction(string name, vector<pair<string, ValueType>> arguments, ValueType returnValueType, shared_ptr<StatementBlock> statementBlock):
|
||||||
|
Statement(StatementKind::FUNCTION_DECLARATION), name(name), arguments(arguments), returnValueType(returnValueType), statementBlock(statementBlock) { }
|
||||||
|
|
||||||
|
string StatementFunction::getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<pair<string, ValueType>> StatementFunction::getArguments() {
|
||||||
|
return arguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
ValueType StatementFunction::getReturnValueType() {
|
||||||
|
return returnValueType;
|
||||||
|
}
|
||||||
|
|
||||||
|
shared_ptr<StatementBlock> StatementFunction::getStatementBlock() {
|
||||||
|
return statementBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
string StatementFunction::toString(int indent) {
|
||||||
|
string value = "";
|
||||||
|
for (int ind=0; ind<indent; ind++)
|
||||||
|
value += " ";
|
||||||
|
value += "FUNCTION(";
|
||||||
|
value += valueTypeToString(returnValueType);
|
||||||
|
value += ", " + name + "):\n";
|
||||||
|
value += statementBlock->toString(indent+1);
|
||||||
|
for (int ind=0; ind<indent; ind++)
|
||||||
|
value += " ";
|
||||||
|
value += ";";
|
||||||
|
return value;
|
||||||
|
}
|
||||||
19
src/Parser/Statement/StatementFunction.h
Normal file
19
src/Parser/Statement/StatementFunction.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#include "Parser/Statement/Statement.h"
|
||||||
|
|
||||||
|
class StatementBlock;
|
||||||
|
|
||||||
|
class StatementFunction: public Statement {
|
||||||
|
private:
|
||||||
|
string name;
|
||||||
|
vector<pair<string, ValueType>> arguments;
|
||||||
|
ValueType returnValueType;
|
||||||
|
shared_ptr<StatementBlock> statementBlock;
|
||||||
|
|
||||||
|
public:
|
||||||
|
StatementFunction(string name, vector<pair<string, ValueType>> arguments, ValueType returnValueType, shared_ptr<StatementBlock> statementBlock);
|
||||||
|
string getName();
|
||||||
|
vector<pair<string, ValueType>> getArguments();
|
||||||
|
ValueType getReturnValueType();
|
||||||
|
shared_ptr<StatementBlock> getStatementBlock();
|
||||||
|
string toString(int indent) override;
|
||||||
|
};
|
||||||
12
src/Parser/Statement/StatementInvalid.cpp
Normal file
12
src/Parser/Statement/StatementInvalid.cpp
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#include "Parser/Statement/StatementInvalid.h"
|
||||||
|
|
||||||
|
StatementInvalid::StatementInvalid(shared_ptr<Token> token, string message):
|
||||||
|
Statement(StatementKind::INVALID), token(token), message(message) { }
|
||||||
|
|
||||||
|
string StatementInvalid::toString(int indent) {
|
||||||
|
return "Error for token " + token->toString() + " at " + to_string(token->getLine()) + ":" + to_string(token->getColumn()) + ": " + message + "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
string StatementInvalid::getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
12
src/Parser/Statement/StatementInvalid.h
Normal file
12
src/Parser/Statement/StatementInvalid.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#include "Parser/Statement/Statement.h"
|
||||||
|
|
||||||
|
class StatementInvalid: public Statement {
|
||||||
|
private:
|
||||||
|
shared_ptr<Token> token;
|
||||||
|
string message;
|
||||||
|
|
||||||
|
public:
|
||||||
|
StatementInvalid(shared_ptr<Token> token, string message);
|
||||||
|
string toString(int indent) override;
|
||||||
|
string getMessage();
|
||||||
|
};
|
||||||
40
src/Parser/Statement/StatementMetaExternFunction.cpp
Normal file
40
src/Parser/Statement/StatementMetaExternFunction.cpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#include "Parser/Statement/StatementMetaExternFunction.h"
|
||||||
|
|
||||||
|
static string valueTypeToString(ValueType valueType) {
|
||||||
|
switch (valueType) {
|
||||||
|
case ValueType::NONE:
|
||||||
|
return "NONE";
|
||||||
|
case ValueType::BOOL:
|
||||||
|
return "BOOL";
|
||||||
|
case ValueType::SINT32:
|
||||||
|
return "SINT32";
|
||||||
|
case ValueType::REAL32:
|
||||||
|
return "REAL32";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
StatementMetaExternFunction::StatementMetaExternFunction(string name, vector<pair<string, ValueType>> arguments, ValueType returnValueType):
|
||||||
|
Statement(StatementKind::META_EXTERN_FUNCTION), name(name), arguments(arguments), returnValueType(returnValueType) { }
|
||||||
|
|
||||||
|
string StatementMetaExternFunction::getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<pair<string, ValueType>> StatementMetaExternFunction::getArguments() {
|
||||||
|
return arguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
ValueType StatementMetaExternFunction::getReturnValueType() {
|
||||||
|
return returnValueType;
|
||||||
|
}
|
||||||
|
|
||||||
|
string StatementMetaExternFunction::toString(int indent) {
|
||||||
|
string value;
|
||||||
|
for (int ind=0; ind<indent; ind++)
|
||||||
|
value += " ";
|
||||||
|
value += "EXTERN_FUN(";
|
||||||
|
value += name + ", ";
|
||||||
|
value += valueTypeToString(returnValueType);
|
||||||
|
value += ")\n";
|
||||||
|
return value;
|
||||||
|
}
|
||||||
15
src/Parser/Statement/StatementMetaExternFunction.h
Normal file
15
src/Parser/Statement/StatementMetaExternFunction.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#include "Parser/Parser.h"
|
||||||
|
|
||||||
|
class StatementMetaExternFunction: public Statement {
|
||||||
|
private:
|
||||||
|
string name;
|
||||||
|
vector<pair<string, ValueType>> arguments;
|
||||||
|
ValueType returnValueType;
|
||||||
|
|
||||||
|
public:
|
||||||
|
StatementMetaExternFunction(string name, vector<pair<string, ValueType>> arguments, ValueType returnValueType);
|
||||||
|
string getName();
|
||||||
|
vector<pair<string, ValueType>> getArguments();
|
||||||
|
ValueType getReturnValueType();
|
||||||
|
string toString(int indent) override;
|
||||||
|
};
|
||||||
25
src/Parser/Statement/StatementReturn.cpp
Normal file
25
src/Parser/Statement/StatementReturn.cpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#include "Parser/Statement/StatementReturn.h"
|
||||||
|
|
||||||
|
#include "Parser/Expression.h"
|
||||||
|
|
||||||
|
StatementReturn::StatementReturn(shared_ptr<Expression> expression):
|
||||||
|
Statement(StatementKind::RETURN), expression(expression) { }
|
||||||
|
|
||||||
|
shared_ptr<Expression> StatementReturn::getExpression() {
|
||||||
|
return expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
string StatementReturn::toString(int indent) {
|
||||||
|
string value;
|
||||||
|
for (int ind=0; ind<indent; ind++)
|
||||||
|
value += " ";
|
||||||
|
value += "RETURN";
|
||||||
|
if (expression != nullptr) {
|
||||||
|
value += ":\n";
|
||||||
|
for (int ind=0; ind<indent+1; ind++)
|
||||||
|
value += " ";
|
||||||
|
value += expression->toString(indent+1);
|
||||||
|
}
|
||||||
|
value += "\n";
|
||||||
|
return value;
|
||||||
|
}
|
||||||
13
src/Parser/Statement/StatementReturn.h
Normal file
13
src/Parser/Statement/StatementReturn.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#include "Parser/Statement/Statement.h"
|
||||||
|
|
||||||
|
class Expression;
|
||||||
|
|
||||||
|
class StatementReturn: public Statement {
|
||||||
|
private:
|
||||||
|
shared_ptr<Expression> expression;
|
||||||
|
|
||||||
|
public:
|
||||||
|
StatementReturn(shared_ptr<Expression> expression);
|
||||||
|
shared_ptr<Expression> getExpression();
|
||||||
|
string toString(int indent) override;
|
||||||
|
};
|
||||||
43
src/Parser/Statement/StatementVariable.cpp
Normal file
43
src/Parser/Statement/StatementVariable.cpp
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#include "StatementVariable.h"
|
||||||
|
|
||||||
|
static string valueTypeToString(ValueType valueType) {
|
||||||
|
switch (valueType) {
|
||||||
|
case ValueType::NONE:
|
||||||
|
return "NONE";
|
||||||
|
case ValueType::BOOL:
|
||||||
|
return "BOOL";
|
||||||
|
case ValueType::SINT32:
|
||||||
|
return "SINT32";
|
||||||
|
case ValueType::REAL32:
|
||||||
|
return "REAL32";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
StatementVariable::StatementVariable(string name, ValueType valueType, shared_ptr<Expression> expression):
|
||||||
|
Statement(StatementKind::VAR_DECLARATION), name(name), valueType(valueType), expression(expression) { }
|
||||||
|
|
||||||
|
string StatementVariable::getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
ValueType StatementVariable::getValueType() {
|
||||||
|
return valueType;
|
||||||
|
}
|
||||||
|
|
||||||
|
shared_ptr<Expression> StatementVariable::getExpression() {
|
||||||
|
return expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
string StatementVariable::toString(int indent) {
|
||||||
|
string value;
|
||||||
|
for (int ind=0; ind<indent; ind++)
|
||||||
|
value += " ";
|
||||||
|
value += name + "(";
|
||||||
|
value += valueTypeToString(valueType);
|
||||||
|
value += "):\n";
|
||||||
|
for (int ind=0; ind<indent+1; ind++)
|
||||||
|
value += " ";
|
||||||
|
value += expression->toString(indent+1);
|
||||||
|
value += "\n";
|
||||||
|
return value;
|
||||||
|
}
|
||||||
15
src/Parser/Statement/StatementVariable.h
Normal file
15
src/Parser/Statement/StatementVariable.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#include "Parser/Statement/Statement.h"
|
||||||
|
|
||||||
|
class StatementVariable: public Statement {
|
||||||
|
private:
|
||||||
|
string name;
|
||||||
|
ValueType valueType;
|
||||||
|
shared_ptr<Expression> expression;
|
||||||
|
|
||||||
|
public:
|
||||||
|
StatementVariable(string name, ValueType valueType, shared_ptr<Expression> expression);
|
||||||
|
string getName();
|
||||||
|
ValueType getValueType();
|
||||||
|
shared_ptr<Expression> getExpression();
|
||||||
|
string toString(int indent) override;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user