Added function return type

This commit is contained in:
Rafał Grodziński
2025-06-13 12:29:59 +09:00
parent 6b67154260
commit 8579de4fba
2 changed files with 47 additions and 33 deletions

View File

@@ -53,9 +53,22 @@ shared_ptr<Statement> Parser::matchStatementFunctionDeclaration() {
currentIndex++; // skip colon
currentIndex++; // skip fun
// FIXME: implement function arguments
while (!tryMatchingTokenKinds({TokenKind::NEW_LINE}, true, false))
currentIndex++;
// Return type
ValueType returnType = ValueType::VOID;
if (tryMatchingTokenKinds({TokenKind::RIGHT_ARROW}, true, true)) {
shared_ptr<Token> valueTypeToken = tokens.at(currentIndex);
if (valueTypeToken->getLexme().compare("bool") == 0)
returnType = ValueType::BOOL;
else if (valueTypeToken->getLexme().compare("sint32") == 0)
returnType = ValueType::SINT32;
else if (valueTypeToken->getLexme().compare("real32") == 0)
returnType = ValueType::REAL32;
else
return matchStatementInvalid("Expected return type");
currentIndex++; // type
}
currentIndex++; // new line
shared_ptr<Statement> statementBlock = matchStatementBlock({TokenKind::SEMICOLON}, true);
@@ -67,7 +80,7 @@ shared_ptr<Statement> Parser::matchStatementFunctionDeclaration() {
if(!tryMatchingTokenKinds({TokenKind::NEW_LINE}, false, true))
return matchStatementInvalid("Expected a new line after a function declaration");
return make_shared<StatementFunctionDeclaration>(identifierToken->getLexme(), ValueType::SINT32, dynamic_pointer_cast<StatementBlock>(statementBlock));
return make_shared<StatementFunctionDeclaration>(identifierToken->getLexme(), returnType, dynamic_pointer_cast<StatementBlock>(statementBlock));
}
shared_ptr<Statement> Parser::matchStatementVarDeclaration() {
@@ -78,28 +91,27 @@ shared_ptr<Statement> Parser::matchStatementVarDeclaration() {
currentIndex++;
currentIndex++; // skip colon
shared_ptr<Token> valueTypeToken = tokens.at(currentIndex);
currentIndex++; // skip fun
ValueType valueType;
if (valueTypeToken->getLexme().compare("bool") == 0)
valueType = ValueType::BOOL;
else if (valueTypeToken->getLexme().compare("sint32") == 0)
valueType = ValueType::SINT32;
else if (valueTypeToken->getLexme().compare("real32") == 0)
valueType = ValueType::REAL32;
else
return matchStatementInvalid("Invalid type");
currentIndex++; // type
// Expect left arrow
if (!tryMatchingTokenKinds({TokenKind::LEFT_ARROW}, true, true))
return matchStatementInvalid();
return matchStatementInvalid("Expected left arrow");
shared_ptr<Expression> expression = nextExpression();
if (expression == nullptr || !expression->isValid())
return matchStatementInvalid();
ValueType valueType;
if (valueTypeToken->getLexme().compare("Void") == 0)
valueType = ValueType::VOID;
else if (valueTypeToken->getLexme().compare("Bool") == 0)
valueType = ValueType::BOOL;
else if (valueTypeToken->getLexme().compare("SInt32") == 0)
valueType = ValueType::SINT32;
else if (valueTypeToken->getLexme().compare("Real32") == 0)
valueType = ValueType::REAL32;
else
return matchStatementInvalid();
// Expect new line
if (!tryMatchingTokenKinds({TokenKind::NEW_LINE}, false, true))
return matchStatementInvalid("Expected a new line after variable declaration");

View File

@@ -1,5 +1,18 @@
#include "Statement.h"
string valueTypeToString(ValueType valueType) {
switch (valueType) {
case ValueType::VOID:
return "NONE";
case ValueType::BOOL:
return "BOOL";
case ValueType::SINT32:
return "SINT32";
case ValueType::REAL32:
return "REAL32";
}
}
//
// Statement
Statement::Statement(StatementKind kind): kind(kind) {
@@ -39,7 +52,9 @@ string StatementFunctionDeclaration::toString(int indent) {
string value = "";
for (int ind=0; ind<indent; ind++)
value += " ";
value += "FUNCTION(" + name + "):\n";
value += "FUNCTION(";
value += valueTypeToString(returnValueType);
value += ", " + name + "):\n";
value += statementBlock->toString(indent+1);
for (int ind=0; ind<indent; ind++)
value += " ";
@@ -70,20 +85,7 @@ string StatementVarDeclaration::toString(int indent) {
for (int ind=0; ind<indent; ind++)
value += " ";
value += name + "(";
switch (valueType) {
case ValueType::VOID:
value += "VOID";
break;
case ValueType::BOOL:
value += "BOOL";
break;
case ValueType::SINT32:
value += "SINT32";
break;
case ValueType::REAL32:
value += "REAL32";
break;
}
value += valueTypeToString(valueType);
value += "):\n";
for (int ind=0; ind<indent+1; ind++)
value += " ";