Added function return type
This commit is contained in:
@@ -53,9 +53,22 @@ shared_ptr<Statement> Parser::matchStatementFunctionDeclaration() {
|
|||||||
currentIndex++; // skip colon
|
currentIndex++; // skip colon
|
||||||
currentIndex++; // skip fun
|
currentIndex++; // skip fun
|
||||||
|
|
||||||
// FIXME: implement function arguments
|
// Return type
|
||||||
while (!tryMatchingTokenKinds({TokenKind::NEW_LINE}, true, false))
|
ValueType returnType = ValueType::VOID;
|
||||||
currentIndex++;
|
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
|
currentIndex++; // new line
|
||||||
shared_ptr<Statement> statementBlock = matchStatementBlock({TokenKind::SEMICOLON}, true);
|
shared_ptr<Statement> statementBlock = matchStatementBlock({TokenKind::SEMICOLON}, true);
|
||||||
@@ -67,7 +80,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(), ValueType::SINT32, dynamic_pointer_cast<StatementBlock>(statementBlock));
|
return make_shared<StatementFunctionDeclaration>(identifierToken->getLexme(), returnType, dynamic_pointer_cast<StatementBlock>(statementBlock));
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<Statement> Parser::matchStatementVarDeclaration() {
|
shared_ptr<Statement> Parser::matchStatementVarDeclaration() {
|
||||||
@@ -78,28 +91,27 @@ shared_ptr<Statement> Parser::matchStatementVarDeclaration() {
|
|||||||
currentIndex++;
|
currentIndex++;
|
||||||
currentIndex++; // skip colon
|
currentIndex++; // skip colon
|
||||||
shared_ptr<Token> valueTypeToken = tokens.at(currentIndex);
|
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
|
// Expect left arrow
|
||||||
if (!tryMatchingTokenKinds({TokenKind::LEFT_ARROW}, true, true))
|
if (!tryMatchingTokenKinds({TokenKind::LEFT_ARROW}, true, true))
|
||||||
return matchStatementInvalid();
|
return matchStatementInvalid("Expected left arrow");
|
||||||
|
|
||||||
shared_ptr<Expression> expression = nextExpression();
|
shared_ptr<Expression> expression = nextExpression();
|
||||||
if (expression == nullptr || !expression->isValid())
|
if (expression == nullptr || !expression->isValid())
|
||||||
return matchStatementInvalid();
|
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
|
// Expect new line
|
||||||
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");
|
||||||
|
|||||||
@@ -1,5 +1,18 @@
|
|||||||
#include "Statement.h"
|
#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::Statement(StatementKind kind): kind(kind) {
|
Statement::Statement(StatementKind kind): kind(kind) {
|
||||||
@@ -39,7 +52,9 @@ string StatementFunctionDeclaration::toString(int indent) {
|
|||||||
string value = "";
|
string value = "";
|
||||||
for (int ind=0; ind<indent; ind++)
|
for (int ind=0; ind<indent; ind++)
|
||||||
value += " ";
|
value += " ";
|
||||||
value += "FUNCTION(" + name + "):\n";
|
value += "FUNCTION(";
|
||||||
|
value += valueTypeToString(returnValueType);
|
||||||
|
value += ", " + name + "):\n";
|
||||||
value += statementBlock->toString(indent+1);
|
value += statementBlock->toString(indent+1);
|
||||||
for (int ind=0; ind<indent; ind++)
|
for (int ind=0; ind<indent; ind++)
|
||||||
value += " ";
|
value += " ";
|
||||||
@@ -70,20 +85,7 @@ string StatementVarDeclaration::toString(int indent) {
|
|||||||
for (int ind=0; ind<indent; ind++)
|
for (int ind=0; ind<indent; ind++)
|
||||||
value += " ";
|
value += " ";
|
||||||
value += name + "(";
|
value += name + "(";
|
||||||
switch (valueType) {
|
value += valueTypeToString(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 += "):\n";
|
value += "):\n";
|
||||||
for (int ind=0; ind<indent+1; ind++)
|
for (int ind=0; ind<indent+1; ind++)
|
||||||
value += " ";
|
value += " ";
|
||||||
|
|||||||
Reference in New Issue
Block a user