Split expressions into separate files
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
#include "ModuleBuilder.h"
|
||||
|
||||
#include "Parser/Expression/Expression.h"
|
||||
#include "Parser/Expression/ExpressionLiteral.h"
|
||||
#include "Parser/Expression/ExpressionIfElse.h"
|
||||
#include "Parser/Expression/ExpressionGrouping.h"
|
||||
#include "Parser/Expression/ExpressionBinary.h"
|
||||
#include "Parser/Expression/ExpressionVariable.h"
|
||||
#include "Parser/Expression/ExpressionCall.h"
|
||||
|
||||
#include "Parser/Statement/StatementExpression.h"
|
||||
#include "Parser/Statement/StatementBlock.h"
|
||||
@@ -152,7 +157,7 @@ llvm::Value *ModuleBuilder::valueForExpression(shared_ptr<Expression> expression
|
||||
case ExpressionKind::IF_ELSE:
|
||||
return valueForIfElse(dynamic_pointer_cast<ExpressionIfElse>(expression));
|
||||
case ExpressionKind::VAR:
|
||||
return valueForVar(dynamic_pointer_cast<ExpressionVar>(expression));
|
||||
return valueForVar(dynamic_pointer_cast<ExpressionVariable>(expression));
|
||||
case ExpressionKind::CALL:
|
||||
return valueForCall(dynamic_pointer_cast<ExpressionCall>(expression));
|
||||
default:
|
||||
@@ -194,67 +199,67 @@ llvm::Value *ModuleBuilder::valueForBinary(shared_ptr<ExpressionBinary> expressi
|
||||
failWithMessage("Unexpected operation");
|
||||
}
|
||||
|
||||
llvm::Value *ModuleBuilder::valueForBinaryBool(ExpressionBinary::Operation operation, llvm::Value *leftValue, llvm::Value *rightValue) {
|
||||
llvm::Value *ModuleBuilder::valueForBinaryBool(ExpressionBinaryOperation operation, llvm::Value *leftValue, llvm::Value *rightValue) {
|
||||
switch (operation) {
|
||||
case ExpressionBinary::Operation::EQUAL:
|
||||
case ExpressionBinaryOperation::EQUAL:
|
||||
return builder->CreateICmpEQ(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::NOT_EQUAL:
|
||||
case ExpressionBinaryOperation::NOT_EQUAL:
|
||||
return builder->CreateICmpNE(leftValue, rightValue);
|
||||
default:
|
||||
failWithMessage("Undefined operation for boolean operands");
|
||||
}
|
||||
}
|
||||
|
||||
llvm::Value *ModuleBuilder::valueForBinaryInteger(ExpressionBinary::Operation operation, llvm::Value *leftValue, llvm::Value *rightValue) {
|
||||
llvm::Value *ModuleBuilder::valueForBinaryInteger(ExpressionBinaryOperation operation, llvm::Value *leftValue, llvm::Value *rightValue) {
|
||||
switch (operation) {
|
||||
case ExpressionBinary::Operation::EQUAL:
|
||||
case ExpressionBinaryOperation::EQUAL:
|
||||
return builder->CreateICmpEQ(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::NOT_EQUAL:
|
||||
case ExpressionBinaryOperation::NOT_EQUAL:
|
||||
return builder->CreateICmpNE(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::LESS:
|
||||
case ExpressionBinaryOperation::LESS:
|
||||
return builder->CreateICmpSLT(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::LESS_EQUAL:
|
||||
case ExpressionBinaryOperation::LESS_EQUAL:
|
||||
return builder->CreateICmpSLE(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::GREATER:
|
||||
case ExpressionBinaryOperation::GREATER:
|
||||
return builder->CreateICmpSGT(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::GREATER_EQUAL:
|
||||
case ExpressionBinaryOperation::GREATER_EQUAL:
|
||||
return builder->CreateICmpSGE(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::ADD:
|
||||
case ExpressionBinaryOperation::ADD:
|
||||
return builder->CreateNSWAdd(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::SUB:
|
||||
case ExpressionBinaryOperation::SUB:
|
||||
return builder->CreateNSWSub(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::MUL:
|
||||
case ExpressionBinaryOperation::MUL:
|
||||
return builder->CreateNSWMul(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::DIV:
|
||||
case ExpressionBinaryOperation::DIV:
|
||||
return builder->CreateSDiv(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::MOD:
|
||||
case ExpressionBinaryOperation::MOD:
|
||||
return builder->CreateSRem(leftValue, rightValue);
|
||||
}
|
||||
}
|
||||
|
||||
llvm::Value *ModuleBuilder::valueForBinaryReal(ExpressionBinary::Operation operation, llvm::Value *leftValue, llvm::Value *rightValue) {
|
||||
llvm::Value *ModuleBuilder::valueForBinaryReal(ExpressionBinaryOperation operation, llvm::Value *leftValue, llvm::Value *rightValue) {
|
||||
switch (operation) {
|
||||
case ExpressionBinary::Operation::EQUAL:
|
||||
case ExpressionBinaryOperation::EQUAL:
|
||||
return builder->CreateFCmpOEQ(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::NOT_EQUAL:
|
||||
case ExpressionBinaryOperation::NOT_EQUAL:
|
||||
return builder->CreateFCmpONE(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::LESS:
|
||||
case ExpressionBinaryOperation::LESS:
|
||||
return builder->CreateFCmpOLT(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::LESS_EQUAL:
|
||||
case ExpressionBinaryOperation::LESS_EQUAL:
|
||||
return builder->CreateFCmpOLE(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::GREATER:
|
||||
case ExpressionBinaryOperation::GREATER:
|
||||
return builder->CreateFCmpOGT(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::GREATER_EQUAL:
|
||||
case ExpressionBinaryOperation::GREATER_EQUAL:
|
||||
return builder->CreateFCmpOGE(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::ADD:
|
||||
case ExpressionBinaryOperation::ADD:
|
||||
return builder->CreateNSWAdd(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::SUB:
|
||||
case ExpressionBinaryOperation::SUB:
|
||||
return builder->CreateNSWSub(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::MUL:
|
||||
case ExpressionBinaryOperation::MUL:
|
||||
return builder->CreateNSWMul(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::DIV:
|
||||
case ExpressionBinaryOperation::DIV:
|
||||
return builder->CreateSDiv(leftValue, rightValue);
|
||||
case ExpressionBinary::Operation::MOD:
|
||||
case ExpressionBinaryOperation::MOD:
|
||||
return builder->CreateSRem(leftValue, rightValue);
|
||||
}
|
||||
}
|
||||
@@ -304,7 +309,7 @@ llvm::Value *ModuleBuilder::valueForIfElse(shared_ptr<ExpressionIfElse> expressi
|
||||
return phi;
|
||||
}
|
||||
|
||||
llvm::Value *ModuleBuilder::valueForVar(shared_ptr<ExpressionVar> expression) {
|
||||
llvm::Value *ModuleBuilder::valueForVar(shared_ptr<ExpressionVariable> expression) {
|
||||
llvm::AllocaInst *alloca = allocaMap[expression->getName()];
|
||||
if (alloca == nullptr)
|
||||
failWithMessage("Variable " + expression->getName() + " not defined");
|
||||
|
||||
@@ -10,17 +10,24 @@
|
||||
#include <llvm/Support/raw_ostream.h>
|
||||
#include <llvm/IR/Verifier.h>
|
||||
|
||||
#include "Parser/Expression.h"
|
||||
#include "Parser/Statement/Statement.h"
|
||||
#include "Types.h"
|
||||
|
||||
class Expression;
|
||||
class ExpressionLiteral;
|
||||
class ExpressionIfElse;
|
||||
class ExpressionGrouping;
|
||||
class ExpressionBinary;
|
||||
enum class ExpressionBinaryOperation;
|
||||
class ExpressionVariable;
|
||||
class ExpressionCall;
|
||||
|
||||
class Statement;
|
||||
class StatementBlock;
|
||||
class StatementReturn;
|
||||
class StatementFunction;
|
||||
class StatementVariable;
|
||||
class StatementMetaExternFunction;
|
||||
class StatementExpression;
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -54,11 +61,11 @@ private:
|
||||
llvm::Value *valueForLiteral(shared_ptr<ExpressionLiteral> expression);
|
||||
llvm::Value *valueForGrouping(shared_ptr<ExpressionGrouping> expression);
|
||||
llvm::Value *valueForBinary(shared_ptr<ExpressionBinary> expression);
|
||||
llvm::Value *valueForBinaryBool(ExpressionBinary::Operation operation, llvm::Value *leftValue, llvm::Value *rightValue);
|
||||
llvm::Value *valueForBinaryInteger(ExpressionBinary::Operation operation, llvm::Value *leftValue, llvm::Value *rightValue);
|
||||
llvm::Value *valueForBinaryReal(ExpressionBinary::Operation operation, llvm::Value *leftValue, llvm::Value *rightValue);
|
||||
llvm::Value *valueForBinaryBool(ExpressionBinaryOperation operation, llvm::Value *leftValue, llvm::Value *rightValue);
|
||||
llvm::Value *valueForBinaryInteger(ExpressionBinaryOperation operation, llvm::Value *leftValue, llvm::Value *rightValue);
|
||||
llvm::Value *valueForBinaryReal(ExpressionBinaryOperation operation, llvm::Value *leftValue, llvm::Value *rightValue);
|
||||
llvm::Value *valueForIfElse(shared_ptr<ExpressionIfElse> expression);
|
||||
llvm::Value *valueForVar(shared_ptr<ExpressionVar> expression);
|
||||
llvm::Value *valueForVar(shared_ptr<ExpressionVariable> expression);
|
||||
llvm::Value *valueForCall(shared_ptr<ExpressionCall> expression);
|
||||
|
||||
llvm::Type *typeForValueType(ValueType valueType);
|
||||
|
||||
@@ -1,195 +0,0 @@
|
||||
#include "Expression.h"
|
||||
|
||||
#include "Parser/Statement/StatementExpression.h"
|
||||
#include "Parser/Statement/StatementBlock.h"
|
||||
|
||||
Expression::Expression(ExpressionKind kind, ValueType valueType):
|
||||
kind(kind), valueType(valueType) {
|
||||
}
|
||||
|
||||
ExpressionKind Expression::getKind() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
ValueType Expression::getValueType() {
|
||||
return valueType;
|
||||
}
|
||||
|
||||
bool Expression::isValid() {
|
||||
return kind != ExpressionKind::INVALID;
|
||||
}
|
||||
|
||||
string Expression::toString(int indent) {
|
||||
return "EXPRESSION";
|
||||
}
|
||||
|
||||
//
|
||||
// ExpressionBinary
|
||||
ExpressionBinary::ExpressionBinary(shared_ptr<Token> token, shared_ptr<Expression> left, shared_ptr<Expression> right):
|
||||
Expression(ExpressionKind::BINARY, ValueType::NONE), left(left), right(right) {
|
||||
// Types must match
|
||||
if (left->getValueType() != right->getValueType())
|
||||
exit(1);
|
||||
|
||||
// Booleans can only do = or !=
|
||||
if (valueType == ValueType::BOOL && (token->getKind() != TokenKind::EQUAL || token->getKind() != TokenKind::NOT_EQUAL))
|
||||
exit(1);
|
||||
|
||||
switch (token->getKind()) {
|
||||
case TokenKind::EQUAL:
|
||||
operation = EQUAL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case TokenKind::NOT_EQUAL:
|
||||
operation = NOT_EQUAL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case TokenKind::LESS:
|
||||
operation = LESS;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case TokenKind::LESS_EQUAL:
|
||||
operation = LESS_EQUAL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case TokenKind::GREATER:
|
||||
operation = GREATER;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case TokenKind::GREATER_EQUAL:
|
||||
operation = GREATER_EQUAL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case TokenKind::PLUS:
|
||||
operation = ADD;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
case TokenKind::MINUS:
|
||||
operation = SUB;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
case TokenKind::STAR:
|
||||
operation = MUL;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
case TokenKind::SLASH:
|
||||
operation = DIV;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
case TokenKind::PERCENT:
|
||||
operation = MOD;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
default:
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
ExpressionBinary::Operation ExpressionBinary::getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
shared_ptr<Expression> ExpressionBinary::getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
shared_ptr<Expression> ExpressionBinary::getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
string ExpressionBinary::toString(int indent) {
|
||||
switch (operation) {
|
||||
case EQUAL:
|
||||
return "{= " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case NOT_EQUAL:
|
||||
return "{!= " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case LESS:
|
||||
return "{< " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case LESS_EQUAL:
|
||||
return "{<= " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case GREATER:
|
||||
return "{> " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case GREATER_EQUAL:
|
||||
return "{<= " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case ADD:
|
||||
return "{+ " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case SUB:
|
||||
return "{- " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case MUL:
|
||||
return "{* " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case DIV:
|
||||
return "{/ " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case MOD:
|
||||
return "{% " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// ExpressionGrouping
|
||||
ExpressionGrouping::ExpressionGrouping(shared_ptr<Expression> expression):
|
||||
Expression(ExpressionKind::GROUPING, expression->getValueType()), expression(expression) {
|
||||
}
|
||||
|
||||
shared_ptr<Expression> ExpressionGrouping::getExpression() {
|
||||
return expression;
|
||||
}
|
||||
|
||||
string ExpressionGrouping::toString(int indent) {
|
||||
return "( " + expression->toString(0) + " )";
|
||||
}
|
||||
|
||||
//
|
||||
// ExpressionVar
|
||||
ExpressionVar::ExpressionVar(string name):
|
||||
Expression(ExpressionKind::VAR, ValueType::NONE), name(name) {
|
||||
}
|
||||
|
||||
string ExpressionVar::getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
string ExpressionVar::toString(int indent) {
|
||||
return "VAR(" + name + ")";
|
||||
}
|
||||
|
||||
//
|
||||
// Expression Call
|
||||
ExpressionCall::ExpressionCall(string name, vector<shared_ptr<Expression>> argumentExpressions):
|
||||
Expression(ExpressionKind::CALL, ValueType::NONE), name(name), argumentExpressions(argumentExpressions) {
|
||||
}
|
||||
|
||||
string ExpressionCall::getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
vector<shared_ptr<Expression>> ExpressionCall::getArgumentExpressions() {
|
||||
return argumentExpressions;
|
||||
}
|
||||
|
||||
string ExpressionCall::toString(int indent) {
|
||||
string value;
|
||||
|
||||
value += "CALL(" + name + "):";
|
||||
for (shared_ptr<Expression> &argumentExpression : argumentExpressions) {
|
||||
value += "\n";
|
||||
for (int ind=0; ind<indent+1; ind++)
|
||||
value += " ";
|
||||
value += argumentExpression->toString(indent+1) + ",";
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
//
|
||||
// ExpressionInvalid
|
||||
ExpressionInvalid::ExpressionInvalid(shared_ptr<Token> token):
|
||||
Expression(ExpressionKind::INVALID, ValueType::NONE), token(token) {
|
||||
}
|
||||
|
||||
shared_ptr<Token> ExpressionInvalid::getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
string ExpressionInvalid::toString(int indent) {
|
||||
return "Invalid token " + token->toString() + " at " + to_string(token->getLine()) + ":" + to_string(token->getColumn()) + "\n";
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
#ifndef EXPRESSION_H
|
||||
#define EXPRESSION_H
|
||||
|
||||
#include "Lexer/Token.h"
|
||||
#include "Parser/Statement/Statement.h"
|
||||
#include "Types.h"
|
||||
|
||||
class StatementBlock;
|
||||
class StatementExpression;
|
||||
|
||||
using namespace std;
|
||||
|
||||
enum class ExpressionKind {
|
||||
LITERAL,
|
||||
GROUPING,
|
||||
BINARY,
|
||||
IF_ELSE,
|
||||
VAR,
|
||||
CALL,
|
||||
BLOCK,
|
||||
INVALID
|
||||
};
|
||||
|
||||
//
|
||||
// Expression
|
||||
class Expression {
|
||||
private:
|
||||
ExpressionKind kind;
|
||||
|
||||
protected:
|
||||
ValueType valueType;
|
||||
|
||||
public:
|
||||
Expression(ExpressionKind kind, ValueType valueType);
|
||||
ExpressionKind getKind();
|
||||
ValueType getValueType();
|
||||
bool isValid();
|
||||
virtual string toString(int indent);
|
||||
};
|
||||
|
||||
//
|
||||
// ExpressionGrouping
|
||||
class ExpressionGrouping: public Expression {
|
||||
private:
|
||||
shared_ptr<Expression> expression;
|
||||
|
||||
public:
|
||||
ExpressionGrouping(shared_ptr<Expression> expression);
|
||||
shared_ptr<Expression> getExpression();
|
||||
string toString(int indent) override;
|
||||
};
|
||||
|
||||
//
|
||||
// ExpressionBinary
|
||||
class ExpressionBinary: public Expression {
|
||||
public:
|
||||
enum Operation {
|
||||
EQUAL,
|
||||
NOT_EQUAL,
|
||||
LESS,
|
||||
LESS_EQUAL,
|
||||
GREATER,
|
||||
GREATER_EQUAL,
|
||||
ADD,
|
||||
SUB,
|
||||
MUL,
|
||||
DIV,
|
||||
MOD
|
||||
};
|
||||
|
||||
private:
|
||||
Operation operation;
|
||||
shared_ptr<Expression> left;
|
||||
shared_ptr<Expression> right;
|
||||
|
||||
public:
|
||||
ExpressionBinary(shared_ptr<Token> token, shared_ptr<Expression> left, shared_ptr<Expression> right);
|
||||
Operation getOperation();
|
||||
shared_ptr<Expression> getLeft();
|
||||
shared_ptr<Expression> getRight();
|
||||
string toString(int indent) override;
|
||||
};
|
||||
|
||||
//
|
||||
// ExpressionVar
|
||||
class ExpressionVar: public Expression {
|
||||
private:
|
||||
string name;
|
||||
|
||||
public:
|
||||
ExpressionVar(string name);
|
||||
string getName();
|
||||
string toString(int indent) override;
|
||||
};
|
||||
|
||||
//
|
||||
// Expression Call
|
||||
class ExpressionCall: public Expression {
|
||||
private:
|
||||
string name;
|
||||
vector<shared_ptr<Expression>> argumentExpressions;
|
||||
|
||||
public:
|
||||
ExpressionCall(string name, vector<shared_ptr<Expression>> argumentExpressions);
|
||||
string getName();
|
||||
vector<shared_ptr<Expression>> getArgumentExpressions();
|
||||
string toString(int indent) override;
|
||||
};
|
||||
|
||||
//
|
||||
// ExpressionInvalid
|
||||
class ExpressionInvalid: public Expression {
|
||||
private:
|
||||
shared_ptr<Token> token;
|
||||
|
||||
public:
|
||||
ExpressionInvalid(shared_ptr<Token> token);
|
||||
shared_ptr<Token> getToken();
|
||||
string toString(int indent) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
21
src/Parser/Expression/Expression.cpp
Normal file
21
src/Parser/Expression/Expression.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "Expression.h"
|
||||
|
||||
Expression::Expression(ExpressionKind kind, ValueType valueType):
|
||||
kind(kind), valueType(valueType) {
|
||||
}
|
||||
|
||||
ExpressionKind Expression::getKind() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
ValueType Expression::getValueType() {
|
||||
return valueType;
|
||||
}
|
||||
|
||||
bool Expression::isValid() {
|
||||
return kind != ExpressionKind::INVALID;
|
||||
}
|
||||
|
||||
string Expression::toString(int indent) {
|
||||
return "EXPRESSION";
|
||||
}
|
||||
37
src/Parser/Expression/Expression.h
Normal file
37
src/Parser/Expression/Expression.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef EXPRESSION_H
|
||||
#define EXPRESSION_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "Lexer/Token.h"
|
||||
#include "Types.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
enum class ExpressionKind {
|
||||
LITERAL,
|
||||
GROUPING,
|
||||
BINARY,
|
||||
IF_ELSE,
|
||||
VAR,
|
||||
CALL,
|
||||
BLOCK,
|
||||
INVALID
|
||||
};
|
||||
|
||||
class Expression {
|
||||
private:
|
||||
ExpressionKind kind;
|
||||
|
||||
protected:
|
||||
ValueType valueType;
|
||||
|
||||
public:
|
||||
Expression(ExpressionKind kind, ValueType valueType);
|
||||
ExpressionKind getKind();
|
||||
ValueType getValueType();
|
||||
bool isValid();
|
||||
virtual string toString(int indent);
|
||||
};
|
||||
|
||||
#endif
|
||||
100
src/Parser/Expression/ExpressionBinary.cpp
Normal file
100
src/Parser/Expression/ExpressionBinary.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "ExpressionBinary.h"
|
||||
|
||||
ExpressionBinary::ExpressionBinary(shared_ptr<Token> token, shared_ptr<Expression> left, shared_ptr<Expression> right):
|
||||
Expression(ExpressionKind::BINARY, ValueType::NONE), left(left), right(right) {
|
||||
// Types must match
|
||||
if (left->getValueType() != right->getValueType())
|
||||
exit(1);
|
||||
|
||||
// Booleans can only do = or !=
|
||||
if (valueType == ValueType::BOOL && (token->getKind() != TokenKind::EQUAL || token->getKind() != TokenKind::NOT_EQUAL))
|
||||
exit(1);
|
||||
|
||||
switch (token->getKind()) {
|
||||
case TokenKind::EQUAL:
|
||||
operation = ExpressionBinaryOperation::EQUAL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case TokenKind::NOT_EQUAL:
|
||||
operation = ExpressionBinaryOperation::NOT_EQUAL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case TokenKind::LESS:
|
||||
operation = ExpressionBinaryOperation::LESS;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case TokenKind::LESS_EQUAL:
|
||||
operation = ExpressionBinaryOperation::LESS_EQUAL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case TokenKind::GREATER:
|
||||
operation = ExpressionBinaryOperation::GREATER;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case TokenKind::GREATER_EQUAL:
|
||||
operation = ExpressionBinaryOperation::GREATER_EQUAL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case TokenKind::PLUS:
|
||||
operation = ExpressionBinaryOperation::ADD;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
case TokenKind::MINUS:
|
||||
operation = ExpressionBinaryOperation::SUB;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
case TokenKind::STAR:
|
||||
operation = ExpressionBinaryOperation::MUL;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
case TokenKind::SLASH:
|
||||
operation = ExpressionBinaryOperation::DIV;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
case TokenKind::PERCENT:
|
||||
operation = ExpressionBinaryOperation::MOD;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
default:
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
ExpressionBinaryOperation ExpressionBinary::getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
shared_ptr<Expression> ExpressionBinary::getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
shared_ptr<Expression> ExpressionBinary::getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
string ExpressionBinary::toString(int indent) {
|
||||
switch (operation) {
|
||||
case ExpressionBinaryOperation::EQUAL:
|
||||
return "{= " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case ExpressionBinaryOperation::NOT_EQUAL:
|
||||
return "{!= " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case ExpressionBinaryOperation::LESS:
|
||||
return "{< " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case ExpressionBinaryOperation::LESS_EQUAL:
|
||||
return "{<= " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case ExpressionBinaryOperation::GREATER:
|
||||
return "{> " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case ExpressionBinaryOperation::GREATER_EQUAL:
|
||||
return "{<= " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case ExpressionBinaryOperation::ADD:
|
||||
return "{+ " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case ExpressionBinaryOperation::SUB:
|
||||
return "{- " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case ExpressionBinaryOperation::MUL:
|
||||
return "{* " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case ExpressionBinaryOperation::DIV:
|
||||
return "{/ " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
case ExpressionBinaryOperation::MOD:
|
||||
return "{% " + left->toString(0) + " " + right->toString(0) + "}";
|
||||
}
|
||||
}
|
||||
29
src/Parser/Expression/ExpressionBinary.h
Normal file
29
src/Parser/Expression/ExpressionBinary.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "Parser/Expression/Expression.h"
|
||||
|
||||
enum class ExpressionBinaryOperation {
|
||||
EQUAL,
|
||||
NOT_EQUAL,
|
||||
LESS,
|
||||
LESS_EQUAL,
|
||||
GREATER,
|
||||
GREATER_EQUAL,
|
||||
ADD,
|
||||
SUB,
|
||||
MUL,
|
||||
DIV,
|
||||
MOD
|
||||
};
|
||||
|
||||
class ExpressionBinary: public Expression {
|
||||
private:
|
||||
ExpressionBinaryOperation operation;
|
||||
shared_ptr<Expression> left;
|
||||
shared_ptr<Expression> right;
|
||||
|
||||
public:
|
||||
ExpressionBinary(shared_ptr<Token> token, shared_ptr<Expression> left, shared_ptr<Expression> right);
|
||||
ExpressionBinaryOperation getOperation();
|
||||
shared_ptr<Expression> getLeft();
|
||||
shared_ptr<Expression> getRight();
|
||||
string toString(int indent) override;
|
||||
};
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "Parser/Expression.h"
|
||||
#include "Parser/Expression/Expression.h"
|
||||
|
||||
class Statement;
|
||||
class StatementExpression;
|
||||
|
||||
class ExpressionBlock: public Expression {
|
||||
private:
|
||||
|
||||
26
src/Parser/Expression/ExpressionCall.cpp
Normal file
26
src/Parser/Expression/ExpressionCall.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "ExpressionCall.h"
|
||||
|
||||
ExpressionCall::ExpressionCall(string name, vector<shared_ptr<Expression>> argumentExpressions):
|
||||
Expression(ExpressionKind::CALL, ValueType::NONE), name(name), argumentExpressions(argumentExpressions) { }
|
||||
|
||||
string ExpressionCall::getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
vector<shared_ptr<Expression>> ExpressionCall::getArgumentExpressions() {
|
||||
return argumentExpressions;
|
||||
}
|
||||
|
||||
string ExpressionCall::toString(int indent) {
|
||||
string value;
|
||||
|
||||
value += "CALL(" + name + "):";
|
||||
for (shared_ptr<Expression> &argumentExpression : argumentExpressions) {
|
||||
value += "\n";
|
||||
for (int ind=0; ind<indent+1; ind++)
|
||||
value += " ";
|
||||
value += argumentExpression->toString(indent+1) + ",";
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
13
src/Parser/Expression/ExpressionCall.h
Normal file
13
src/Parser/Expression/ExpressionCall.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "Parser/Expression/Expression.h"
|
||||
|
||||
class ExpressionCall: public Expression {
|
||||
private:
|
||||
string name;
|
||||
vector<shared_ptr<Expression>> argumentExpressions;
|
||||
|
||||
public:
|
||||
ExpressionCall(string name, vector<shared_ptr<Expression>> argumentExpressions);
|
||||
string getName();
|
||||
vector<shared_ptr<Expression>> getArgumentExpressions();
|
||||
string toString(int indent) override;
|
||||
};
|
||||
12
src/Parser/Expression/ExpressionGrouping.cpp
Normal file
12
src/Parser/Expression/ExpressionGrouping.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "ExpressionGrouping.h"
|
||||
|
||||
ExpressionGrouping::ExpressionGrouping(shared_ptr<Expression> expression):
|
||||
Expression(ExpressionKind::GROUPING, expression->getValueType()), expression(expression) { }
|
||||
|
||||
shared_ptr<Expression> ExpressionGrouping::getExpression() {
|
||||
return expression;
|
||||
}
|
||||
|
||||
string ExpressionGrouping::toString(int indent) {
|
||||
return "( " + expression->toString(0) + " )";
|
||||
}
|
||||
11
src/Parser/Expression/ExpressionGrouping.h
Normal file
11
src/Parser/Expression/ExpressionGrouping.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "Parser/Expression/Expression.h"
|
||||
|
||||
class ExpressionGrouping: public Expression {
|
||||
private:
|
||||
shared_ptr<Expression> expression;
|
||||
|
||||
public:
|
||||
ExpressionGrouping(shared_ptr<Expression> expression);
|
||||
shared_ptr<Expression> getExpression();
|
||||
string toString(int indent) override;
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "Parser/Expression.h"
|
||||
#include "Parser/Expression/Expression.h"
|
||||
|
||||
class ExpressionBlock;
|
||||
|
||||
|
||||
13
src/Parser/Expression/ExpressionInvalid.cpp
Normal file
13
src/Parser/Expression/ExpressionInvalid.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "ExpressionInvalid.h"
|
||||
|
||||
ExpressionInvalid::ExpressionInvalid(shared_ptr<Token> token):
|
||||
Expression(ExpressionKind::INVALID, ValueType::NONE), token(token) {
|
||||
}
|
||||
|
||||
shared_ptr<Token> ExpressionInvalid::getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
string ExpressionInvalid::toString(int indent) {
|
||||
return "Invalid token " + token->toString() + " at " + to_string(token->getLine()) + ":" + to_string(token->getColumn()) + "\n";
|
||||
}
|
||||
11
src/Parser/Expression/ExpressionInvalid.h
Normal file
11
src/Parser/Expression/ExpressionInvalid.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "Parser/Expression/Expression.h"
|
||||
|
||||
class ExpressionInvalid: public Expression {
|
||||
private:
|
||||
shared_ptr<Token> token;
|
||||
|
||||
public:
|
||||
ExpressionInvalid(shared_ptr<Token> token);
|
||||
shared_ptr<Token> getToken();
|
||||
string toString(int indent) override;
|
||||
};
|
||||
@@ -1,8 +1,4 @@
|
||||
#include "Parser/Expression.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
#include "Parser/Expression/Expression.h"
|
||||
|
||||
class ExpressionLiteral: public Expression {
|
||||
private:
|
||||
|
||||
12
src/Parser/Expression/ExpressionVariable.cpp
Normal file
12
src/Parser/Expression/ExpressionVariable.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "ExpressionVariable.h"
|
||||
|
||||
ExpressionVariable::ExpressionVariable(string name):
|
||||
Expression(ExpressionKind::VAR, ValueType::NONE), name(name) { }
|
||||
|
||||
string ExpressionVariable::getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
string ExpressionVariable::toString(int indent) {
|
||||
return "VAR(" + name + ")";
|
||||
}
|
||||
11
src/Parser/Expression/ExpressionVariable.h
Normal file
11
src/Parser/Expression/ExpressionVariable.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "Parser/Expression/Expression.h"
|
||||
|
||||
class ExpressionVariable: public Expression {
|
||||
private:
|
||||
string name;
|
||||
|
||||
public:
|
||||
ExpressionVariable(string name);
|
||||
string getName();
|
||||
string toString(int indent) override;
|
||||
};
|
||||
@@ -1,8 +1,14 @@
|
||||
#include "Parser.h"
|
||||
|
||||
#include "Parser/Expression/Expression.h"
|
||||
#include "Parser/Expression/ExpressionLiteral.h"
|
||||
#include "Parser/Expression/ExpressionGrouping.h"
|
||||
#include "Parser/Expression/ExpressionIfElse.h"
|
||||
#include "Parser/Expression/ExpressionBlock.h"
|
||||
#include "Parser/Expression/ExpressionBinary.h"
|
||||
#include "Parser/Expression/ExpressionVariable.h"
|
||||
#include "Parser/Expression/ExpressionCall.h"
|
||||
#include "Parser/Expression/ExpressionInvalid.h"
|
||||
|
||||
#include "Parser/Statement/StatementExpression.h"
|
||||
#include "Parser/Statement/StatementBlock.h"
|
||||
@@ -41,7 +47,7 @@ shared_ptr<Statement> Parser::nextStatement() {
|
||||
if (statement != nullptr)
|
||||
return statement;
|
||||
|
||||
statement = matchStatementVarDeclaration();
|
||||
statement = matchStatementVariable();
|
||||
if (statement != nullptr)
|
||||
return statement;
|
||||
|
||||
@@ -118,7 +124,7 @@ shared_ptr<Statement> Parser::matchStatementFunctionDeclaration() {
|
||||
return make_shared<StatementFunction>(identifierToken->getLexme(), arguments, returnType, dynamic_pointer_cast<StatementBlock>(statementBlock));
|
||||
}
|
||||
|
||||
shared_ptr<Statement> Parser::matchStatementVarDeclaration() {
|
||||
shared_ptr<Statement> Parser::matchStatementVariable() {
|
||||
if (!tryMatchingTokenKinds({TokenKind::IDENTIFIER, TokenKind::TYPE}, true, false))
|
||||
return nullptr;
|
||||
|
||||
@@ -446,7 +452,7 @@ shared_ptr<Expression> Parser::matchExpressionVar() {
|
||||
shared_ptr<Token> token = tokens.at(currentIndex);
|
||||
|
||||
if (tryMatchingTokenKinds({TokenKind::IDENTIFIER}, true, true))
|
||||
return make_shared<ExpressionVar>(token->getLexme());
|
||||
return make_shared<ExpressionVariable>(token->getLexme());
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
#include <vector>
|
||||
|
||||
#include "Lexer/Token.h"
|
||||
#include "Expression.h"
|
||||
#include "Parser/Statement/Statement.h"
|
||||
|
||||
class Expression;
|
||||
class ExpressionInvalid;
|
||||
|
||||
class Statement;
|
||||
class StatementInvalid;
|
||||
|
||||
using namespace std;
|
||||
@@ -18,13 +20,11 @@ private:
|
||||
|
||||
shared_ptr<Statement> nextStatement();
|
||||
shared_ptr<Statement> matchStatementFunctionDeclaration();
|
||||
shared_ptr<Statement> matchStatementVarDeclaration();
|
||||
shared_ptr<Statement> matchStatementVariable();
|
||||
shared_ptr<Statement> matchStatementBlock(vector<TokenKind> terminalTokenKinds, bool shouldConsumeTerminal);
|
||||
shared_ptr<Statement> matchStatementReturn();
|
||||
shared_ptr<Statement> matchStatementExpression();
|
||||
|
||||
shared_ptr<Statement> matchStatementMetaExternFunction();
|
||||
|
||||
shared_ptr<StatementInvalid> matchStatementInvalid(string message = "");
|
||||
|
||||
shared_ptr<Expression> nextExpression();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "StatementExpression.h"
|
||||
|
||||
#include "Parser/Expression.h"
|
||||
#include "Parser/Expression/Expression.h"
|
||||
|
||||
StatementExpression::StatementExpression(shared_ptr<Expression> expression):
|
||||
Statement(StatementKind::EXPRESSION), expression(expression) { }
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "Parser/Parser.h"
|
||||
#include "Parser/Statement/Statement.h"
|
||||
|
||||
class StatementMetaExternFunction: public Statement {
|
||||
private:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "Parser/Statement/StatementReturn.h"
|
||||
|
||||
#include "Parser/Expression.h"
|
||||
#include "Parser/Expression/Expression.h"
|
||||
|
||||
StatementReturn::StatementReturn(shared_ptr<Expression> expression):
|
||||
Statement(StatementKind::RETURN), expression(expression) { }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "StatementVariable.h"
|
||||
|
||||
#include "Parser/Expression.h"
|
||||
#include "Parser/Expression/Expression.h"
|
||||
|
||||
static string valueTypeToString(ValueType valueType) {
|
||||
switch (valueType) {
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
#include "Lexer/Token.h"
|
||||
#include "Lexer/Lexer.h"
|
||||
|
||||
#include "Parser/Expression.h"
|
||||
#include "Parser/Parser.h"
|
||||
#include "Parser/Statement/Statement.h"
|
||||
|
||||
#include "Compiler/ModuleBuilder.h"
|
||||
#include "Compiler/CodeGenerator.h"
|
||||
|
||||
Reference in New Issue
Block a user