Lex and parse array expression
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include "Parser/Expression/ExpressionGrouping.h"
|
#include "Parser/Expression/ExpressionGrouping.h"
|
||||||
#include "Parser/Expression/ExpressionLiteral.h"
|
#include "Parser/Expression/ExpressionLiteral.h"
|
||||||
|
#include "Parser/Expression/ExpressionArrayLiteral.h"
|
||||||
#include "Parser/Expression/ExpressionVariable.h"
|
#include "Parser/Expression/ExpressionVariable.h"
|
||||||
#include "Parser/Expression/ExpressionCall.h"
|
#include "Parser/Expression/ExpressionCall.h"
|
||||||
#include "Parser/Expression/ExpressionIfElse.h"
|
#include "Parser/Expression/ExpressionIfElse.h"
|
||||||
@@ -132,6 +133,21 @@ void ModuleBuilder::buildVarDeclaration(shared_ptr<StatementVariable> statement)
|
|||||||
if (!setAlloca(statement->getName(), alloca))
|
if (!setAlloca(statement->getName(), alloca))
|
||||||
return;
|
return;
|
||||||
builder->CreateStore(value, alloca);
|
builder->CreateStore(value, alloca);
|
||||||
|
|
||||||
|
/*auto *aType = llvm::ArrayType::get(typeSint32, 7);
|
||||||
|
llvm::AllocaInst *allocaArr = builder->CreateAlloca(aType, nullptr, statement->getName() + "_Arr");
|
||||||
|
|
||||||
|
//llvm::AllocaInst *allocaBrr = builder->CreateAlloca(typeSint32, nullptr, statement->getName() + "_Arr");
|
||||||
|
vector<llvm::Constant *> values;
|
||||||
|
auto *bType = llvm::ArrayType::get(typeSint32, 9);
|
||||||
|
for (int i=0; i<9; i++) {
|
||||||
|
llvm::Constant *cnst = llvm::ConstantInt::get(typeSint32, i, true);
|
||||||
|
values.push_back(cnst);
|
||||||
|
}
|
||||||
|
llvm::Constant *ar = (llvm::ConstantArray *)llvm::ConstantArray::get(bType, values);
|
||||||
|
//auto vAr = ar->getAggregateElement(0);
|
||||||
|
llvm::AllocaInst *arAlloca = builder->CreateAlloca(bType, nullptr, "arBtype");
|
||||||
|
builder->CreateStore(ar, arAlloca);*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModuleBuilder::buildAssignment(shared_ptr<StatementAssignment> statement) {
|
void ModuleBuilder::buildAssignment(shared_ptr<StatementAssignment> statement) {
|
||||||
@@ -234,6 +250,8 @@ llvm::Value *ModuleBuilder::valueForExpression(shared_ptr<Expression> expression
|
|||||||
switch (expression->getKind()) {
|
switch (expression->getKind()) {
|
||||||
case ExpressionKind::LITERAL:
|
case ExpressionKind::LITERAL:
|
||||||
return valueForLiteral(dynamic_pointer_cast<ExpressionLiteral>(expression));
|
return valueForLiteral(dynamic_pointer_cast<ExpressionLiteral>(expression));
|
||||||
|
case ExpressionKind::ARRAY_LITERAL:
|
||||||
|
return valueForArrayLiteral(dynamic_pointer_cast<ExpressionArrayLiteral>(expression));
|
||||||
case ExpressionKind::GROUPING:
|
case ExpressionKind::GROUPING:
|
||||||
return valueForExpression(dynamic_pointer_cast<ExpressionGrouping>(expression)->getExpression());
|
return valueForExpression(dynamic_pointer_cast<ExpressionGrouping>(expression)->getExpression());
|
||||||
case ExpressionKind::BINARY:
|
case ExpressionKind::BINARY:
|
||||||
@@ -266,6 +284,10 @@ llvm::Value *ModuleBuilder::valueForLiteral(shared_ptr<ExpressionLiteral> expres
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
llvm::Value *ModuleBuilder::valueForArrayLiteral(shared_ptr<ExpressionArrayLiteral> expression) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
llvm::Value *ModuleBuilder::valueForGrouping(shared_ptr<ExpressionGrouping> expression) {
|
llvm::Value *ModuleBuilder::valueForGrouping(shared_ptr<ExpressionGrouping> expression) {
|
||||||
return valueForExpression(expression->getExpression());
|
return valueForExpression(expression->getExpression());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ class ValueType;
|
|||||||
class Expression;
|
class Expression;
|
||||||
class ExpressionGrouping;
|
class ExpressionGrouping;
|
||||||
class ExpressionLiteral;
|
class ExpressionLiteral;
|
||||||
|
class ExpressionArrayLiteral;
|
||||||
class ExpressionVariable;
|
class ExpressionVariable;
|
||||||
class ExpressionCall;
|
class ExpressionCall;
|
||||||
class ExpressionIfElse;
|
class ExpressionIfElse;
|
||||||
@@ -70,6 +71,7 @@ private:
|
|||||||
|
|
||||||
llvm::Value *valueForExpression(shared_ptr<Expression> expression);
|
llvm::Value *valueForExpression(shared_ptr<Expression> expression);
|
||||||
llvm::Value *valueForLiteral(shared_ptr<ExpressionLiteral> expression);
|
llvm::Value *valueForLiteral(shared_ptr<ExpressionLiteral> expression);
|
||||||
|
llvm::Value *valueForArrayLiteral(shared_ptr<ExpressionArrayLiteral> expression);
|
||||||
llvm::Value *valueForGrouping(shared_ptr<ExpressionGrouping> expression);
|
llvm::Value *valueForGrouping(shared_ptr<ExpressionGrouping> expression);
|
||||||
llvm::Value *valueForBinary(shared_ptr<ExpressionBinary> expression);
|
llvm::Value *valueForBinary(shared_ptr<ExpressionBinary> expression);
|
||||||
llvm::Value *valueForBinaryBool(ExpressionBinaryOperation operation, llvm::Value *leftValue, llvm::Value *rightValue);
|
llvm::Value *valueForBinaryBool(ExpressionBinaryOperation operation, llvm::Value *leftValue, llvm::Value *rightValue);
|
||||||
|
|||||||
@@ -126,6 +126,14 @@ shared_ptr<Token> Lexer::nextToken() {
|
|||||||
if (token != nullptr)
|
if (token != nullptr)
|
||||||
return token;
|
return token;
|
||||||
|
|
||||||
|
token = match(TokenKind::LEFT_SQUARE_BRACKET, "[", false);
|
||||||
|
if (token != nullptr)
|
||||||
|
return token;
|
||||||
|
|
||||||
|
token = match(TokenKind::RIGHT_SQUARE_BRACKET, "]", false);
|
||||||
|
if (token != nullptr)
|
||||||
|
return token;
|
||||||
|
|
||||||
token = match(TokenKind::COMMA, ",", false);
|
token = match(TokenKind::COMMA, ",", false);
|
||||||
if (token != nullptr)
|
if (token != nullptr)
|
||||||
return token;
|
return token;
|
||||||
@@ -472,6 +480,8 @@ bool Lexer::isSeparator(int index) {
|
|||||||
case '>':
|
case '>':
|
||||||
case '(':
|
case '(':
|
||||||
case ')':
|
case ')':
|
||||||
|
case '[':
|
||||||
|
case ']':
|
||||||
case ',':
|
case ',':
|
||||||
case ':':
|
case ':':
|
||||||
case ';':
|
case ';':
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ enum class TokenKind {
|
|||||||
|
|
||||||
LEFT_PAREN,
|
LEFT_PAREN,
|
||||||
RIGHT_PAREN,
|
RIGHT_PAREN,
|
||||||
|
LEFT_SQUARE_BRACKET,
|
||||||
|
RIGHT_SQUARE_BRACKET,
|
||||||
COMMA,
|
COMMA,
|
||||||
COLON,
|
COLON,
|
||||||
SEMICOLON,
|
SEMICOLON,
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
#include "Parser/Expression/ExpressionVariable.h"
|
#include "Parser/Expression/ExpressionVariable.h"
|
||||||
#include "Parser/Expression/ExpressionGrouping.h"
|
#include "Parser/Expression/ExpressionGrouping.h"
|
||||||
#include "Parser/Expression/ExpressionLiteral.h"
|
#include "Parser/Expression/ExpressionLiteral.h"
|
||||||
|
#include "Parser/Expression/ExpressionArrayLiteral.h"
|
||||||
#include "Parser/Expression/ExpressionCall.h"
|
#include "Parser/Expression/ExpressionCall.h"
|
||||||
#include "Parser/Expression/ExpressionBlock.h"
|
#include "Parser/Expression/ExpressionBlock.h"
|
||||||
|
|
||||||
@@ -56,6 +57,10 @@ string Logger::toString(shared_ptr<Token> token) {
|
|||||||
return "(";
|
return "(";
|
||||||
case TokenKind::RIGHT_PAREN:
|
case TokenKind::RIGHT_PAREN:
|
||||||
return ")";
|
return ")";
|
||||||
|
case TokenKind::LEFT_SQUARE_BRACKET:
|
||||||
|
return "[";
|
||||||
|
case TokenKind::RIGHT_SQUARE_BRACKET:
|
||||||
|
return "]";
|
||||||
case TokenKind::COMMA:
|
case TokenKind::COMMA:
|
||||||
return ",";
|
return ",";
|
||||||
case TokenKind::COLON:
|
case TokenKind::COLON:
|
||||||
@@ -308,6 +313,8 @@ string Logger::toString(shared_ptr<Expression> expression) {
|
|||||||
return toString(dynamic_pointer_cast<ExpressionGrouping>(expression));
|
return toString(dynamic_pointer_cast<ExpressionGrouping>(expression));
|
||||||
case ExpressionKind::LITERAL:
|
case ExpressionKind::LITERAL:
|
||||||
return toString(dynamic_pointer_cast<ExpressionLiteral>(expression));
|
return toString(dynamic_pointer_cast<ExpressionLiteral>(expression));
|
||||||
|
case ExpressionKind::ARRAY_LITERAL:
|
||||||
|
return toString(dynamic_pointer_cast<ExpressionArrayLiteral>(expression));
|
||||||
case ExpressionKind::CALL:
|
case ExpressionKind::CALL:
|
||||||
return toString(dynamic_pointer_cast<ExpressionCall>(expression));
|
return toString(dynamic_pointer_cast<ExpressionCall>(expression));
|
||||||
case ExpressionKind::BLOCK:
|
case ExpressionKind::BLOCK:
|
||||||
@@ -382,6 +389,18 @@ string Logger::toString(shared_ptr<ExpressionLiteral> expression) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string Logger::toString(shared_ptr<ExpressionArrayLiteral> expression) {
|
||||||
|
string text;
|
||||||
|
text += "[";
|
||||||
|
for (int i=0; i<expression->getExpressions().size(); i++) {
|
||||||
|
text += toString(expression->getExpressions().at(i));
|
||||||
|
if (i < expression->getExpressions().size() - 1)
|
||||||
|
text += ", ";
|
||||||
|
}
|
||||||
|
text += "]";
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
string Logger::toString(shared_ptr<ExpressionCall> expression) {
|
string Logger::toString(shared_ptr<ExpressionCall> expression) {
|
||||||
string argsString;
|
string argsString;
|
||||||
for (int i = 0; i < expression->getArgumentExpressions().size(); i++) {
|
for (int i = 0; i < expression->getArgumentExpressions().size(); i++) {
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ class ExpressionIfElse;
|
|||||||
class ExpressionVariable;
|
class ExpressionVariable;
|
||||||
class ExpressionGrouping;
|
class ExpressionGrouping;
|
||||||
class ExpressionLiteral;
|
class ExpressionLiteral;
|
||||||
|
class ExpressionArrayLiteral;
|
||||||
class ExpressionCall;
|
class ExpressionCall;
|
||||||
class ExpressionBlock;
|
class ExpressionBlock;
|
||||||
|
|
||||||
@@ -52,6 +53,7 @@ private:
|
|||||||
static string toString(shared_ptr<ExpressionVariable> expression);
|
static string toString(shared_ptr<ExpressionVariable> expression);
|
||||||
static string toString(shared_ptr<ExpressionGrouping> expression);
|
static string toString(shared_ptr<ExpressionGrouping> expression);
|
||||||
static string toString(shared_ptr<ExpressionLiteral> expression);
|
static string toString(shared_ptr<ExpressionLiteral> expression);
|
||||||
|
static string toString(shared_ptr<ExpressionArrayLiteral> expression);
|
||||||
static string toString(shared_ptr<ExpressionCall> expression);
|
static string toString(shared_ptr<ExpressionCall> expression);
|
||||||
static string toString(shared_ptr<ExpressionBlock> expression);
|
static string toString(shared_ptr<ExpressionBlock> expression);
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ using namespace std;
|
|||||||
|
|
||||||
enum class ExpressionKind {
|
enum class ExpressionKind {
|
||||||
LITERAL,
|
LITERAL,
|
||||||
|
ARRAY_LITERAL,
|
||||||
GROUPING,
|
GROUPING,
|
||||||
BINARY,
|
BINARY,
|
||||||
IF_ELSE,
|
IF_ELSE,
|
||||||
|
|||||||
8
src/Parser/Expression/ExpressionArrayLiteral.cpp
Normal file
8
src/Parser/Expression/ExpressionArrayLiteral.cpp
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#include "ExpressionArrayLiteral.h"
|
||||||
|
|
||||||
|
ExpressionArrayLiteral::ExpressionArrayLiteral(vector<shared_ptr<Expression>> expressions):
|
||||||
|
Expression(ExpressionKind::ARRAY_LITERAL, nullptr), expressions(expressions) { }
|
||||||
|
|
||||||
|
vector<shared_ptr<Expression>> ExpressionArrayLiteral::getExpressions() {
|
||||||
|
return expressions;
|
||||||
|
}
|
||||||
15
src/Parser/Expression/ExpressionArrayLiteral.h
Normal file
15
src/Parser/Expression/ExpressionArrayLiteral.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef EXPRESSION_ARRAY_LITERAL_H
|
||||||
|
#define EXPRESSION_ARRAY_LITERAL_H
|
||||||
|
|
||||||
|
#include "Expression.h"
|
||||||
|
|
||||||
|
class ExpressionArrayLiteral: public Expression {
|
||||||
|
private:
|
||||||
|
vector<shared_ptr<Expression>> expressions;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ExpressionArrayLiteral(vector<shared_ptr<Expression>> expressions);
|
||||||
|
vector<shared_ptr<Expression>> getExpressions();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
#include "Parser/Expression/Expression.h"
|
#ifndef EXPRESSION_LITERAL_H
|
||||||
|
#define EXPRESSION_LITERAL_H
|
||||||
|
|
||||||
|
#include "Expression.h"
|
||||||
|
|
||||||
class ExpressionLiteral: public Expression {
|
class ExpressionLiteral: public Expression {
|
||||||
private:
|
private:
|
||||||
@@ -12,4 +15,6 @@ public:
|
|||||||
bool getBoolValue();
|
bool getBoolValue();
|
||||||
int32_t getSint32Value();
|
int32_t getSint32Value();
|
||||||
float getReal32Value();
|
float getReal32Value();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include "Parser/Expression/ExpressionGrouping.h"
|
#include "Parser/Expression/ExpressionGrouping.h"
|
||||||
#include "Parser/Expression/ExpressionLiteral.h"
|
#include "Parser/Expression/ExpressionLiteral.h"
|
||||||
|
#include "Parser/Expression/ExpressionArrayLiteral.h"
|
||||||
#include "Parser/Expression/ExpressionVariable.h"
|
#include "Parser/Expression/ExpressionVariable.h"
|
||||||
#include "Parser/Expression/ExpressionCall.h"
|
#include "Parser/Expression/ExpressionCall.h"
|
||||||
#include "Parser/Expression/ExpressionIfElse.h"
|
#include "Parser/Expression/ExpressionIfElse.h"
|
||||||
@@ -261,10 +262,8 @@ shared_ptr<Statement> Parser::matchStatementBlock(vector<TokenKind> terminalToke
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
// except new line
|
// except new line
|
||||||
if (statement != nullptr && !tryMatchingTokenKinds({TokenKind::NEW_LINE}, true, true)) {
|
if (statement != nullptr && !tryMatchingTokenKinds({TokenKind::NEW_LINE}, true, true))
|
||||||
markError(TokenKind::NEW_LINE, {});
|
markError(TokenKind::NEW_LINE, {});
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return make_shared<StatementBlock>(statements);
|
return make_shared<StatementBlock>(statements);
|
||||||
@@ -446,6 +445,10 @@ shared_ptr<Expression> Parser::matchPrimary() {
|
|||||||
if (expression != nullptr)
|
if (expression != nullptr)
|
||||||
return expression;
|
return expression;
|
||||||
|
|
||||||
|
expression = matchExpressionArrayLiteral();
|
||||||
|
if (expression != nullptr)
|
||||||
|
return expression;
|
||||||
|
|
||||||
expression = matchExpressionCall();
|
expression = matchExpressionCall();
|
||||||
if (expression != nullptr)
|
if (expression != nullptr)
|
||||||
return expression;
|
return expression;
|
||||||
@@ -458,7 +461,6 @@ shared_ptr<Expression> Parser::matchPrimary() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<Expression> Parser::matchExpressionGrouping() {
|
shared_ptr<Expression> Parser::matchExpressionGrouping() {
|
||||||
shared_ptr<Token> token = tokens.at(currentIndex);
|
|
||||||
if (tryMatchingTokenKinds({TokenKind::LEFT_PAREN}, true, true)) {
|
if (tryMatchingTokenKinds({TokenKind::LEFT_PAREN}, true, true)) {
|
||||||
shared_ptr<Expression> expression = matchTerm();
|
shared_ptr<Expression> expression = matchTerm();
|
||||||
// has grouped expression failed?
|
// has grouped expression failed?
|
||||||
@@ -483,6 +485,28 @@ shared_ptr<Expression> Parser::matchExpressionLiteral() {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shared_ptr<Expression> Parser::matchExpressionArrayLiteral() {
|
||||||
|
if (!tryMatchingTokenKinds({TokenKind::LEFT_SQUARE_BRACKET}, true, true))
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
vector<shared_ptr<Expression>> expressions;
|
||||||
|
if (!tryMatchingTokenKinds({TokenKind::RIGHT_SQUARE_BRACKET}, true, true)) {
|
||||||
|
do {
|
||||||
|
shared_ptr<Expression> expression = nextExpression();
|
||||||
|
if (expression != nullptr)
|
||||||
|
expressions.push_back(expression);
|
||||||
|
} while (tryMatchingTokenKinds({TokenKind::COMMA}, true, true));
|
||||||
|
|
||||||
|
if (!tryMatchingTokenKinds({TokenKind::RIGHT_SQUARE_BRACKET}, true, true)) {
|
||||||
|
markError(TokenKind::RIGHT_SQUARE_BRACKET, {});
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return make_shared<ExpressionArrayLiteral>(expressions);
|
||||||
|
}
|
||||||
|
|
||||||
shared_ptr<Expression> Parser::matchExpressionVariable() {
|
shared_ptr<Expression> Parser::matchExpressionVariable() {
|
||||||
shared_ptr<Token> token = tokens.at(currentIndex);
|
shared_ptr<Token> token = tokens.at(currentIndex);
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ private:
|
|||||||
|
|
||||||
shared_ptr<Expression> matchExpressionGrouping();
|
shared_ptr<Expression> matchExpressionGrouping();
|
||||||
shared_ptr<Expression> matchExpressionLiteral();
|
shared_ptr<Expression> matchExpressionLiteral();
|
||||||
|
shared_ptr<Expression> matchExpressionArrayLiteral();
|
||||||
shared_ptr<Expression> matchExpressionVariable();
|
shared_ptr<Expression> matchExpressionVariable();
|
||||||
shared_ptr<Expression> matchExpressionCall();
|
shared_ptr<Expression> matchExpressionCall();
|
||||||
shared_ptr<Expression> matchExpressionIfElse();
|
shared_ptr<Expression> matchExpressionIfElse();
|
||||||
|
|||||||
Reference in New Issue
Block a user