Parse var declaration
This commit is contained in:
@@ -29,6 +29,10 @@ shared_ptr<Statement> Parser::nextStatement() {
|
||||
if (statement != nullptr)
|
||||
return statement;
|
||||
|
||||
statement = matchStatementVarDeclaration();
|
||||
if (statement != nullptr)
|
||||
return statement;
|
||||
|
||||
statement = matchStatementReturn();
|
||||
if (statement != nullptr)
|
||||
return statement;
|
||||
@@ -62,6 +66,45 @@ shared_ptr<Statement> Parser::matchStatementFunctionDeclaration() {
|
||||
return make_shared<StatementFunctionDeclaration>(identifierToken->getLexme(), ValueType::SINT32, dynamic_pointer_cast<StatementBlock>(statementBlock));
|
||||
}
|
||||
|
||||
shared_ptr<Statement> Parser::matchStatementVarDeclaration() {
|
||||
if (!matchesTokenKinds({TokenKind::IDENTIFIER, TokenKind::COLON, TokenKind::TYPE}))
|
||||
return nullptr;
|
||||
|
||||
shared_ptr<Token> identifierToken = tokens.at(currentIndex);
|
||||
currentIndex++;
|
||||
currentIndex++; // skip colon
|
||||
shared_ptr<Token> valueTypeToken = tokens.at(currentIndex);
|
||||
currentIndex++; // skip fun
|
||||
|
||||
// Expect left arrow
|
||||
if (tokens.at(currentIndex)->getKind() != TokenKind::LEFT_ARROW)
|
||||
return matchStatementInvalid();
|
||||
currentIndex++;
|
||||
|
||||
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();
|
||||
|
||||
//if (tokens.at(currentIndex)->getKind() != TokenKind::NEW_LINE)
|
||||
// return matchStatementInvalid();
|
||||
|
||||
//currentIndex++;
|
||||
|
||||
return make_shared<StatementVarDeclaration>(identifierToken->getLexme(), valueType, expression);
|
||||
}
|
||||
|
||||
shared_ptr<Statement> Parser::matchStatementBlock() {
|
||||
vector<shared_ptr<Statement>> statements;
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ private:
|
||||
|
||||
shared_ptr<Statement> nextStatement();
|
||||
shared_ptr<Statement> matchStatementFunctionDeclaration();
|
||||
shared_ptr<Statement> matchStatementVarDeclaration();
|
||||
shared_ptr<Statement> matchStatementBlock();
|
||||
shared_ptr<Statement> matchStatementReturn();
|
||||
shared_ptr<Statement> matchStatementExpression();
|
||||
|
||||
@@ -50,8 +50,7 @@ string StatementFunctionDeclaration::toString(int indent) {
|
||||
//
|
||||
// StatementVarDeclaration
|
||||
StatementVarDeclaration::StatementVarDeclaration(string name, ValueType valueType, shared_ptr<Expression> expression):
|
||||
Statement(StatementKind::VAR_DECLARATION) {
|
||||
|
||||
Statement(StatementKind::VAR_DECLARATION), name(name), valueType(valueType), expression(expression) {
|
||||
}
|
||||
|
||||
string StatementVarDeclaration::getName() {
|
||||
|
||||
@@ -51,9 +51,9 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
cout << endl << endl;
|
||||
|
||||
ModuleBuilder moduleBuilder(statements);
|
||||
shared_ptr<llvm::Module> module = moduleBuilder.getModule();
|
||||
module->print(llvm::outs(), nullptr);
|
||||
//ModuleBuilder moduleBuilder(statements);
|
||||
//shared_ptr<llvm::Module> module = moduleBuilder.getModule();
|
||||
//module->print(llvm::outs(), nullptr);
|
||||
|
||||
//CodeGenerator codeGenerator(module);
|
||||
//codeGenerator.generateObjectFile("dummy.s");
|
||||
|
||||
Reference in New Issue
Block a user