Added Expression Block and moved some expressions

This commit is contained in:
Rafał Grodziński
2025-06-23 16:08:58 +09:00
parent fefde721c0
commit 4bab8077af
14 changed files with 220 additions and 181 deletions

View File

@@ -1,5 +1,8 @@
#include "ModuleBuilder.h"
#include "Parser/Expression/ExpressionLiteral.h"
#include "Parser/Expression/ExpressionIfElse.h"
#include "Parser/Statement/StatementExpression.h"
#include "Parser/Statement/StatementBlock.h"
#include "Parser/Statement/StatementFunction.h"
@@ -100,11 +103,8 @@ void ModuleBuilder::buildVarDeclaration(shared_ptr<StatementVariable> statement)
}
void ModuleBuilder::buildBlock(shared_ptr<StatementBlock> statement) {
for (shared_ptr<Statement> &innerStatement : statement->getStatements()) {
for (shared_ptr<Statement> &innerStatement : statement->getStatements())
buildStatement(innerStatement);
}
if (statement->getStatementExpression() != nullptr)
buildStatement(statement->getStatementExpression());
}
void ModuleBuilder::buildReturn(shared_ptr<StatementReturn> statement) {
@@ -274,8 +274,9 @@ llvm::Value *ModuleBuilder::valueForIfElse(shared_ptr<ExpressionIfElse> expressi
// Then
builder->SetInsertPoint(thenBlock);
llvm::Value *thenValue = valueForExpression(expression->getThenBlock()->getStatementExpression()->getExpression());
buildStatement(expression->getThenBlock());
//llvm::Value *thenValue = valueForExpression(expression->getThenBlock()->getStatementExpression()->getExpression());
llvm::Value *thenValue = llvm::UndefValue::get(typeVoid);
//buildStatement(expression->getThenBlock());
builder->CreateBr(mergeBlock);
thenBlock = builder->GetInsertBlock();
@@ -285,8 +286,9 @@ llvm::Value *ModuleBuilder::valueForIfElse(shared_ptr<ExpressionIfElse> expressi
llvm::Value *elseValue = nullptr;
if (expression->getElseBlock() != nullptr) {
valuesCount++;
elseValue = valueForExpression(expression->getElseBlock()->getStatementExpression()->getExpression());
buildStatement(expression->getElseBlock());
//elseValue = valueForExpression(expression->getElseBlock()->getStatementExpression()->getExpression());
llvm::Value *elseValue = llvm::UndefValue::get(typeVoid);
//buildStatement(expression->getElseBlock());
}
builder->CreateBr(mergeBlock);
elseBlock = builder->GetInsertBlock();

View File

@@ -13,10 +13,13 @@
#include "Parser/Expression.h"
#include "Parser/Statement/Statement.h"
class ExpressionLiteral;
class ExpressionIfElse;
class StatementBlock;
class StatementReturn;
class StatementFunction;
class StatementVariable;
class StatementReturn;
class StatementMetaExternFunction;
using namespace std;

View File

@@ -124,71 +124,6 @@ string ExpressionBinary::toString(int indent) {
}
}
//
// ExpressionLiteral
ExpressionLiteral::ExpressionLiteral(shared_ptr<Token> token):
Expression(ExpressionKind::LITERAL, ValueType::NONE) {
switch (token->getKind()) {
case TokenKind::BOOL:
boolValue = token->getLexme().compare("true") == 0;
valueType = ValueType::BOOL;
break;
case TokenKind::INTEGER_DEC: {
string numString = token->getLexme();
erase(numString, '_');
sint32Value = stoi(numString, nullptr, 10);
valueType = ValueType::SINT32;
break;
}
case TokenKind::INTEGER_HEX: {
string numString = token->getLexme();
erase(numString, '_');
sint32Value = stoi(numString, nullptr, 16);
valueType = ValueType::SINT32;
break;
}
case TokenKind::INTEGER_BIN: {
string numString = token->getLexme();
erase(numString, '_');
numString = numString.substr(2, numString.size()-1);
sint32Value = stoi(numString, nullptr, 2);
valueType = ValueType::SINT32;
break;
}
case TokenKind::REAL:
real32Value = stof(token->getLexme());
valueType = ValueType::REAL32;
break;
default:
exit(1);
}
}
bool ExpressionLiteral::getBoolValue() {
return boolValue;
}
int32_t ExpressionLiteral::getSint32Value() {
return sint32Value;
}
float ExpressionLiteral::getReal32Value() {
return real32Value;
}
string ExpressionLiteral::toString(int indent) {
switch (valueType) {
case ValueType::NONE:
return "NONE";
case ValueType::BOOL:
return boolValue ? "true" : "false";
case ValueType::SINT32:
return to_string(sint32Value);
case ValueType::REAL32:
return to_string(real32Value);
}
}
//
// ExpressionGrouping
ExpressionGrouping::ExpressionGrouping(shared_ptr<Expression> expression):
@@ -203,56 +138,6 @@ string ExpressionGrouping::toString(int indent) {
return "( " + expression->toString(0) + " )";
}
//
// ExpressionIfElse
ExpressionIfElse::ExpressionIfElse(shared_ptr<Expression> condition, shared_ptr<StatementBlock> thenBlock, shared_ptr<StatementBlock> elseBlock):
Expression(ExpressionKind::IF_ELSE, ValueType::NONE), condition(condition), thenBlock(thenBlock), elseBlock(elseBlock) {
// Condition must evaluate to bool
if (condition->getValueType() != ValueType::BOOL)
exit(1);
// Return types must match
shared_ptr<StatementExpression> thenStatementExpression = thenBlock->getStatementExpression();
shared_ptr<Expression> thenExpression = thenStatementExpression != nullptr ? thenStatementExpression->getExpression() : nullptr;
shared_ptr<StatementExpression> elseStatementExpression = elseBlock != nullptr ? elseBlock->getStatementExpression() : nullptr;
shared_ptr<Expression> elseExpression = elseStatementExpression != nullptr ? elseStatementExpression->getExpression() : nullptr;
if (thenExpression != nullptr && elseExpression != nullptr && thenExpression->getValueType() != elseExpression->getValueType())
exit(1);
// get type or default to void
valueType = thenExpression ? thenExpression->getValueType() : ValueType::NONE;
}
shared_ptr<Expression> ExpressionIfElse::getCondition() {
return condition;
}
shared_ptr<StatementBlock> ExpressionIfElse::getThenBlock() {
return thenBlock;
}
shared_ptr<StatementBlock> ExpressionIfElse::getElseBlock() {
return elseBlock;
}
string ExpressionIfElse::toString(int indent) {
string value;
value += "IF(" + condition->toString(0) + "):\n";
value += thenBlock->toString(indent+1);
if (elseBlock != nullptr) {
for (int ind=0; ind<indent; ind++)
value += " ";
value += "ELSE:\n";
value += elseBlock->toString(indent+1);
}
for (int ind=0; ind<indent; ind++)
value += " ";
value += ";";
return value;
}
//
// ExpressionVar
ExpressionVar::ExpressionVar(string name):

View File

@@ -17,6 +17,7 @@ enum class ExpressionKind {
IF_ELSE,
VAR,
CALL,
BLOCK,
INVALID
};
@@ -37,22 +38,6 @@ public:
virtual string toString(int indent);
};
//
// ExpressionLiteral
class ExpressionLiteral: public Expression {
private:
bool boolValue;
int32_t sint32Value;
float real32Value;
public:
ExpressionLiteral(shared_ptr<Token> token);
bool getBoolValue();
int32_t getSint32Value();
float getReal32Value();
string toString(int indent) override;
};
//
// ExpressionGrouping
class ExpressionGrouping: public Expression {
@@ -96,22 +81,6 @@ public:
string toString(int indent) override;
};
//
// ExpressionIfElse
class ExpressionIfElse: public Expression {
private:
shared_ptr<Expression> condition;
shared_ptr<StatementBlock> thenBlock;
shared_ptr<StatementBlock> elseBlock;
public:
ExpressionIfElse(shared_ptr<Expression> condition, shared_ptr<StatementBlock> thenBlock, shared_ptr<StatementBlock> elseBlock);
shared_ptr<Expression> getCondition();
shared_ptr<StatementBlock> getThenBlock();
shared_ptr<StatementBlock> getElseBlock();
string toString(int indent) override;
};
//
// ExpressionVar
class ExpressionVar: public Expression {

View File

@@ -0,0 +1,19 @@
#include "ExpressionBlock.h"
#include "Parser/Expression/ExpressionLiteral.h"
#include "Parser/Statement/StatementExpression.h"
ExpressionBlock::ExpressionBlock(vector<shared_ptr<Statement>> statements):
Expression(ExpressionKind::BLOCK, ValueType::NONE), statements(statements) {
if (!statements.empty() && statements.back()->getKind() == StatementKind::EXPRESSION) {
resultExpression = dynamic_pointer_cast<StatementExpression>(statements.back());
this->statements.pop_back();
valueType = resultExpression->getExpression()->getValueType();
} else {
resultExpression = make_shared<StatementExpression>(ExpressionLiteral::none);
}
}
string ExpressionBlock::toString(int indent) {
return "";
}

View File

@@ -0,0 +1,11 @@
#include "Parser/Expression.h"
class ExpressionBlock: public Expression {
private:
vector<shared_ptr<Statement>> statements;
shared_ptr<StatementExpression> resultExpression;
public:
ExpressionBlock(vector<shared_ptr<Statement>> statements);
string toString(int indent) override;
};

View File

@@ -0,0 +1,36 @@
#include "ExpressionIfElse.h"
#include "Parser/Expression/ExpressionBlock.h"
ExpressionIfElse::ExpressionIfElse(shared_ptr<Expression> condition, shared_ptr<ExpressionBlock> thenBlock, shared_ptr<ExpressionBlock> elseBlock):
Expression(ExpressionKind::IF_ELSE, ValueType::NONE), condition(condition), thenBlock(thenBlock), elseBlock(elseBlock) { }
shared_ptr<Expression> ExpressionIfElse::getCondition() {
return condition;
}
shared_ptr<ExpressionBlock> ExpressionIfElse::getThenBlock() {
return thenBlock;
}
shared_ptr<ExpressionBlock> ExpressionIfElse::getElseBlock() {
return elseBlock;
}
string ExpressionIfElse::toString(int indent) {
string value;
value += "IF(" + condition->toString(0) + "):\n";
value += thenBlock->toString(indent+1);
if (elseBlock != nullptr) {
for (int ind=0; ind<indent; ind++)
value += " ";
value += "ELSE:\n";
value += elseBlock->toString(indent+1);
}
for (int ind=0; ind<indent; ind++)
value += " ";
value += ";";
return value;
}

View File

@@ -0,0 +1,17 @@
#include "Parser/Expression.h"
class ExpressionBlock;
class ExpressionIfElse: public Expression {
private:
shared_ptr<Expression> condition;
shared_ptr<ExpressionBlock> thenBlock;
shared_ptr<ExpressionBlock> elseBlock;
public:
ExpressionIfElse(shared_ptr<Expression> condition, shared_ptr<ExpressionBlock> thenBlock, shared_ptr<ExpressionBlock> elseBlock);
shared_ptr<Expression> getCondition();
shared_ptr<ExpressionBlock> getThenBlock();
shared_ptr<ExpressionBlock> getElseBlock();
string toString(int indent) override;
};

View File

@@ -0,0 +1,69 @@
#include "ExpressionLiteral.h"
shared_ptr<ExpressionLiteral> ExpressionLiteral::none;
ExpressionLiteral::ExpressionLiteral():
Expression(ExpressionKind::LITERAL, ValueType::NONE) { }
ExpressionLiteral::ExpressionLiteral(shared_ptr<Token> token):
Expression(ExpressionKind::LITERAL, ValueType::NONE) {
switch (token->getKind()) {
case TokenKind::BOOL:
boolValue = token->getLexme().compare("true") == 0;
valueType = ValueType::BOOL;
break;
case TokenKind::INTEGER_DEC: {
string numString = token->getLexme();
erase(numString, '_');
sint32Value = stoi(numString, nullptr, 10);
valueType = ValueType::SINT32;
break;
}
case TokenKind::INTEGER_HEX: {
string numString = token->getLexme();
erase(numString, '_');
sint32Value = stoi(numString, nullptr, 16);
valueType = ValueType::SINT32;
break;
}
case TokenKind::INTEGER_BIN: {
string numString = token->getLexme();
erase(numString, '_');
numString = numString.substr(2, numString.size()-1);
sint32Value = stoi(numString, nullptr, 2);
valueType = ValueType::SINT32;
break;
}
case TokenKind::REAL:
real32Value = stof(token->getLexme());
valueType = ValueType::REAL32;
break;
default:
exit(1);
}
}
bool ExpressionLiteral::getBoolValue() {
return boolValue;
}
int32_t ExpressionLiteral::getSint32Value() {
return sint32Value;
}
float ExpressionLiteral::getReal32Value() {
return real32Value;
}
string ExpressionLiteral::toString(int indent) {
switch (valueType) {
case ValueType::NONE:
return "NONE";
case ValueType::BOOL:
return boolValue ? "true" : "false";
case ValueType::SINT32:
return to_string(sint32Value);
case ValueType::REAL32:
return to_string(real32Value);
}
}

View File

@@ -0,0 +1,23 @@
#include "Parser/Expression.h"
#include <iostream>
using namespace std;
class ExpressionLiteral: public Expression {
private:
bool boolValue;
int32_t sint32Value;
float real32Value;
ExpressionLiteral();
public:
static shared_ptr<ExpressionLiteral> none;
ExpressionLiteral(shared_ptr<Token> token);
bool getBoolValue();
int32_t getSint32Value();
float getReal32Value();
string toString(int indent) override;
};

View File

@@ -1,5 +1,9 @@
#include "Parser.h"
#include "Parser/Expression/ExpressionLiteral.h"
#include "Parser/Expression/ExpressionIfElse.h"
#include "Parser/Expression/ExpressionBlock.h"
#include "Parser/Statement/StatementExpression.h"
#include "Parser/Statement/StatementBlock.h"
#include "Parser/Statement/StatementReturn.h"
@@ -166,7 +170,6 @@ shared_ptr<Statement> Parser::matchStatementBlock(vector<TokenKind> terminalToke
currentIndex--;
}
return make_shared<StatementBlock>(statements);
}
@@ -414,29 +417,29 @@ shared_ptr<Expression> Parser::matchExpressionIfElse() {
tryMatchingTokenKinds({TokenKind::NEW_LINE}, true, true);
// Match then block
shared_ptr<Statement> thenBlock = matchStatementBlock({TokenKind::COLON, TokenKind::SEMICOLON}, false);
shared_ptr<Expression> thenBlock = matchExpressionBlock({TokenKind::COLON, TokenKind::SEMICOLON}, false);
if (thenBlock == nullptr)
return matchExpressionInvalid();
else if (!thenBlock->isValid())
return matchExpressionInvalid(); // FIXME
return thenBlock;
// Match else block. Then and else block are separated by ':'
shared_ptr<Statement> elseBlock;
shared_ptr<Expression> elseBlock;
if (tryMatchingTokenKinds({TokenKind::COLON}, true, true)) {
bool isSingleLine = !tryMatchingTokenKinds({TokenKind::NEW_LINE}, true, true);
vector<TokenKind> terminalTokens = {TokenKind::SEMICOLON, TokenKind::COMMA, TokenKind::RIGHT_PAREN};
if (isSingleLine)
terminalTokens.push_back(TokenKind::NEW_LINE);
elseBlock = matchStatementBlock(terminalTokens, false);
elseBlock = matchExpressionBlock(terminalTokens, false);
if (elseBlock == nullptr)
return matchExpressionInvalid();
else if (!elseBlock->isValid())
return matchExpressionInvalid(); // FIXME
return elseBlock;
}
tryMatchingTokenKinds({TokenKind::SEMICOLON}, true, true);
return make_shared<ExpressionIfElse>(condition, dynamic_pointer_cast<StatementBlock>(thenBlock), dynamic_pointer_cast<StatementBlock>(elseBlock));
return make_shared<ExpressionIfElse>(condition, dynamic_pointer_cast<ExpressionBlock>(thenBlock), dynamic_pointer_cast<ExpressionBlock>(elseBlock));
}
shared_ptr<Expression> Parser::matchExpressionVar() {
@@ -473,6 +476,24 @@ shared_ptr<Expression> Parser::matchExpressionCall() {
return make_shared<ExpressionCall>(identifierToken->getLexme(), argumentExpressions);
}
shared_ptr<Expression> Parser::matchExpressionBlock(vector<TokenKind> terminalTokenKinds, bool shouldConsumeTerminal) {
vector<shared_ptr<Statement>> statements;
bool hasNewLineTerminal = find(terminalTokenKinds.begin(), terminalTokenKinds.end(), TokenKind::NEW_LINE) != terminalTokenKinds.end();
while (!tryMatchingTokenKinds(terminalTokenKinds, false, shouldConsumeTerminal)) {
shared_ptr<Statement> statement = nextStatement();
if (statement == nullptr || !statement->isValid())
return matchExpressionInvalid();
else
statements.push_back(statement);
if (hasNewLineTerminal && tokens.at(currentIndex-1)->getKind() == TokenKind::NEW_LINE)
currentIndex--;
}
return make_shared<ExpressionBlock>(statements);
}
shared_ptr<ExpressionInvalid> Parser::matchExpressionInvalid() {
return make_shared<ExpressionInvalid>(tokens.at(currentIndex));
}

View File

@@ -40,6 +40,7 @@ private:
shared_ptr<Expression> matchExpressionIfElse();
shared_ptr<Expression> matchExpressionVar();
shared_ptr<Expression> matchExpressionCall();
shared_ptr<Expression> matchExpressionBlock(vector<TokenKind> terminalTokenKinds, bool shouldConsumeTerminal);
shared_ptr<ExpressionInvalid> matchExpressionInvalid();
bool tryMatchingTokenKinds(vector<TokenKind> kinds, bool shouldMatchAll, bool shouldAdvance);

View File

@@ -3,21 +3,12 @@
#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();
}
}
Statement(StatementKind::BLOCK), statements(statements) { }
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++) {
@@ -25,12 +16,6 @@ string StatementBlock::toString(int indent) {
// 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";

View File

@@ -5,11 +5,9 @@ class StatementExpression;
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;
};