types in separate file
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
#include "Expression.h"
|
||||
|
||||
Expression::Expression(Kind kind, ValueType valueType):
|
||||
Expression::Expression(ExpressionKind kind, ValueType valueType):
|
||||
kind(kind), valueType(valueType) {
|
||||
}
|
||||
|
||||
Expression::Kind Expression::getKind() {
|
||||
ExpressionKind Expression::getKind() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
Expression::ValueType Expression::getValueType() {
|
||||
ValueType Expression::getValueType() {
|
||||
return valueType;
|
||||
}
|
||||
|
||||
bool Expression::isValid() {
|
||||
return kind != Expression::Kind::INVALID;
|
||||
return kind != ExpressionKind::INVALID;
|
||||
}
|
||||
|
||||
string Expression::toString(int indent) {
|
||||
@@ -23,57 +23,57 @@ string Expression::toString(int indent) {
|
||||
//
|
||||
// ExpressionBinary
|
||||
ExpressionBinary::ExpressionBinary(shared_ptr<Token> token, shared_ptr<Expression> left, shared_ptr<Expression> right):
|
||||
Expression(Expression::Kind::BINARY, Expression::ValueType::VOID), left(left), right(right) {
|
||||
Expression(ExpressionKind::BINARY, ValueType::VOID), left(left), right(right) {
|
||||
// Types must match
|
||||
if (left->getValueType() != right->getValueType())
|
||||
exit(1);
|
||||
|
||||
// Booleans can only do = or !=
|
||||
if (valueType == Expression::ValueType::BOOL && (token->getKind() != Token::Kind::EQUAL || token->getKind() != Token::Kind::NOT_EQUAL))
|
||||
if (valueType == ValueType::BOOL && (token->getKind() != TokenKind::EQUAL || token->getKind() != TokenKind::NOT_EQUAL))
|
||||
exit(1);
|
||||
|
||||
switch (token->getKind()) {
|
||||
case Token::Kind::EQUAL:
|
||||
case TokenKind::EQUAL:
|
||||
operation = EQUAL;
|
||||
valueType = Expression::ValueType::BOOL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case Token::Kind::NOT_EQUAL:
|
||||
case TokenKind::NOT_EQUAL:
|
||||
operation = NOT_EQUAL;
|
||||
valueType = Expression::ValueType::BOOL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case Token::Kind::LESS:
|
||||
case TokenKind::LESS:
|
||||
operation = LESS;
|
||||
valueType = Expression::ValueType::BOOL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case Token::Kind::LESS_EQUAL:
|
||||
case TokenKind::LESS_EQUAL:
|
||||
operation = LESS_EQUAL;
|
||||
valueType = Expression::ValueType::BOOL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case Token::Kind::GREATER:
|
||||
case TokenKind::GREATER:
|
||||
operation = GREATER;
|
||||
valueType = Expression::ValueType::BOOL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case Token::Kind::GREATER_EQUAL:
|
||||
case TokenKind::GREATER_EQUAL:
|
||||
operation = GREATER_EQUAL;
|
||||
valueType = Expression::ValueType::BOOL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case Token::Kind::PLUS:
|
||||
case TokenKind::PLUS:
|
||||
operation = ADD;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
case Token::Kind::MINUS:
|
||||
case TokenKind::MINUS:
|
||||
operation = SUB;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
case Token::Kind::STAR:
|
||||
case TokenKind::STAR:
|
||||
operation = MUL;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
case Token::Kind::SLASH:
|
||||
case TokenKind::SLASH:
|
||||
operation = DIV;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
case Token::Kind::PERCENT:
|
||||
case TokenKind::PERCENT:
|
||||
operation = MOD;
|
||||
valueType = left->getValueType();
|
||||
break;
|
||||
@@ -124,19 +124,19 @@ string ExpressionBinary::toString(int indent) {
|
||||
//
|
||||
// ExpressionLiteral
|
||||
ExpressionLiteral::ExpressionLiteral(shared_ptr<Token> token):
|
||||
Expression(Expression::Kind::LITERAL, Expression::ValueType::VOID) {
|
||||
Expression(ExpressionKind::LITERAL, ValueType::VOID) {
|
||||
switch (token->getKind()) {
|
||||
case Token::Kind::BOOL:
|
||||
case TokenKind::BOOL:
|
||||
boolValue = token->getLexme().compare("true") == 0;
|
||||
valueType = Expression::ValueType::BOOL;
|
||||
valueType = ValueType::BOOL;
|
||||
break;
|
||||
case Token::Kind::INTEGER:
|
||||
case TokenKind::INTEGER:
|
||||
sint32Value = stoi(token->getLexme());
|
||||
valueType = Expression::ValueType::SINT32;
|
||||
valueType = ValueType::SINT32;
|
||||
break;
|
||||
case Token::Kind::REAL:
|
||||
case TokenKind::REAL:
|
||||
real32Value = stof(token->getLexme());
|
||||
valueType = Expression::ValueType::REAL32;
|
||||
valueType = ValueType::REAL32;
|
||||
break;
|
||||
default:
|
||||
exit(1);
|
||||
@@ -147,7 +147,7 @@ bool ExpressionLiteral::getBoolValue() {
|
||||
return boolValue;
|
||||
}
|
||||
|
||||
int32_t ExpressionLiteral::getSint32Value() {
|
||||
int32_t ExpressionLiteral::getSInt32Value() {
|
||||
return sint32Value;
|
||||
}
|
||||
|
||||
@@ -157,13 +157,13 @@ float ExpressionLiteral::getReal32Value() {
|
||||
|
||||
string ExpressionLiteral::toString(int indent) {
|
||||
switch (valueType) {
|
||||
case Expression::ValueType::VOID:
|
||||
case ValueType::VOID:
|
||||
return "VOID";
|
||||
case Expression::ValueType::BOOL:
|
||||
return to_string(boolValue);
|
||||
case Expression::ValueType::SINT32:
|
||||
case ValueType::BOOL:
|
||||
return boolValue ? "true" : "false";
|
||||
case ValueType::SINT32:
|
||||
return to_string(sint32Value);
|
||||
case Expression::ValueType::REAL32:
|
||||
case ValueType::REAL32:
|
||||
return to_string(real32Value);
|
||||
}
|
||||
}
|
||||
@@ -171,7 +171,7 @@ string ExpressionLiteral::toString(int indent) {
|
||||
//
|
||||
// ExpressionGrouping
|
||||
ExpressionGrouping::ExpressionGrouping(shared_ptr<Expression> expression):
|
||||
Expression(Expression::Kind::GROUPING, expression->getValueType()), expression(expression) {
|
||||
Expression(ExpressionKind::GROUPING, expression->getValueType()), expression(expression) {
|
||||
}
|
||||
|
||||
shared_ptr<Expression> ExpressionGrouping::getExpression() {
|
||||
@@ -185,9 +185,9 @@ string ExpressionGrouping::toString(int indent) {
|
||||
//
|
||||
// ExpressionIfElse
|
||||
ExpressionIfElse::ExpressionIfElse(shared_ptr<Expression> condition, shared_ptr<StatementBlock> thenBlock, shared_ptr<StatementBlock> elseBlock):
|
||||
Expression(Expression::Kind::IF_ELSE, Expression::ValueType::VOID), condition(condition), thenBlock(thenBlock), elseBlock(elseBlock) {
|
||||
Expression(ExpressionKind::IF_ELSE, ValueType::VOID), condition(condition), thenBlock(thenBlock), elseBlock(elseBlock) {
|
||||
// Condition must evaluate to bool
|
||||
if (condition->getValueType() != Expression::ValueType::BOOL)
|
||||
if (condition->getValueType() != ValueType::BOOL)
|
||||
exit(1);
|
||||
|
||||
// Return types must match
|
||||
@@ -199,7 +199,7 @@ ExpressionIfElse::ExpressionIfElse(shared_ptr<Expression> condition, shared_ptr<
|
||||
exit(1);
|
||||
|
||||
// get type or default to void
|
||||
valueType = thenExpression ? thenExpression->getValueType() : Expression::ValueType::VOID;
|
||||
valueType = thenExpression ? thenExpression->getValueType() : ValueType::VOID;
|
||||
}
|
||||
|
||||
shared_ptr<Expression> ExpressionIfElse::getCondition() {
|
||||
@@ -235,7 +235,7 @@ string ExpressionIfElse::toString(int indent) {
|
||||
//
|
||||
// ExpressionInvalid
|
||||
ExpressionInvalid::ExpressionInvalid(shared_ptr<Token> token):
|
||||
Expression(Expression::Kind::INVALID, Expression::ValueType::VOID), token(token) {
|
||||
Expression(ExpressionKind::INVALID, ValueType::VOID), token(token) {
|
||||
}
|
||||
|
||||
shared_ptr<Token> ExpressionInvalid::getToken() {
|
||||
|
||||
Reference in New Issue
Block a user