Match real and booleans
This commit is contained in:
@@ -1,13 +1,17 @@
|
|||||||
#include "Expression.h"
|
#include "Expression.h"
|
||||||
|
|
||||||
Expression::Expression(Kind kind):
|
Expression::Expression(Kind kind, ValueType valueType):
|
||||||
kind(kind) {
|
kind(kind), valueType(valueType) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Expression::Kind Expression::getKind() {
|
Expression::Kind Expression::getKind() {
|
||||||
return kind;
|
return kind;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Expression::ValueType Expression::getValueType() {
|
||||||
|
return valueType;
|
||||||
|
}
|
||||||
|
|
||||||
bool Expression::isValid() {
|
bool Expression::isValid() {
|
||||||
return kind != Expression::Kind::INVALID;
|
return kind != Expression::Kind::INVALID;
|
||||||
}
|
}
|
||||||
@@ -19,7 +23,7 @@ string Expression::toString() {
|
|||||||
//
|
//
|
||||||
// ExpressionBinary
|
// ExpressionBinary
|
||||||
ExpressionBinary::ExpressionBinary(shared_ptr<Token> token, shared_ptr<Expression> left, shared_ptr<Expression> right):
|
ExpressionBinary::ExpressionBinary(shared_ptr<Token> token, shared_ptr<Expression> left, shared_ptr<Expression> right):
|
||||||
Expression(Expression::Kind::BINARY), left(left), right(right) {
|
Expression(Expression::Kind::BINARY, Expression::ValueType::VOID), left(left), right(right) {
|
||||||
switch (token->getKind()) {
|
switch (token->getKind()) {
|
||||||
case Token::Kind::EQUAL:
|
case Token::Kind::EQUAL:
|
||||||
operation = EQUAL;
|
operation = EQUAL;
|
||||||
@@ -101,22 +105,41 @@ string ExpressionBinary::toString() {
|
|||||||
//
|
//
|
||||||
// ExpressionLiteral
|
// ExpressionLiteral
|
||||||
ExpressionLiteral::ExpressionLiteral(shared_ptr<Token> token):
|
ExpressionLiteral::ExpressionLiteral(shared_ptr<Token> token):
|
||||||
Expression(Expression::Kind::LITERAL) {
|
Expression(Expression::Kind::LITERAL, Expression::ValueType::VOID) {
|
||||||
integer = stoi(token->getLexme());
|
valueType = Expression::ValueType::SINT32;
|
||||||
|
sint32Value = stoi(token->getLexme());
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t ExpressionLiteral::getInteger() {
|
bool ExpressionLiteral::getBoolValue() {
|
||||||
return integer;
|
return boolValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t ExpressionLiteral::getSint32Value() {
|
||||||
|
return sint32Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
float ExpressionLiteral::getReal32Value() {
|
||||||
|
return real32Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
string ExpressionLiteral::toString() {
|
string ExpressionLiteral::toString() {
|
||||||
return to_string(integer);
|
//return to_string(integer);
|
||||||
|
switch (valueType) {
|
||||||
|
case Expression::ValueType::VOID:
|
||||||
|
return "VOID";
|
||||||
|
case Expression::ValueType::BOOL:
|
||||||
|
return to_string(boolValue);
|
||||||
|
case Expression::ValueType::SINT32:
|
||||||
|
return to_string(sint32Value);
|
||||||
|
case Expression::ValueType::REAL32:
|
||||||
|
return to_string(real32Value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// ExpressionGrouping
|
// ExpressionGrouping
|
||||||
ExpressionGrouping::ExpressionGrouping(shared_ptr<Expression> expression):
|
ExpressionGrouping::ExpressionGrouping(shared_ptr<Expression> expression):
|
||||||
Expression(Expression::Kind::GROUPING), expression(expression) {
|
Expression(Expression::Kind::GROUPING, Expression::ValueType::VOID), expression(expression) {
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<Expression> ExpressionGrouping::getExpression() {
|
shared_ptr<Expression> ExpressionGrouping::getExpression() {
|
||||||
@@ -130,7 +153,7 @@ string ExpressionGrouping::toString() {
|
|||||||
//
|
//
|
||||||
// ExpressionIfElse
|
// ExpressionIfElse
|
||||||
ExpressionIfElse::ExpressionIfElse(shared_ptr<Expression> condition, shared_ptr<StatementBlock> thenBlock, shared_ptr<StatementBlock> elseBlock):
|
ExpressionIfElse::ExpressionIfElse(shared_ptr<Expression> condition, shared_ptr<StatementBlock> thenBlock, shared_ptr<StatementBlock> elseBlock):
|
||||||
Expression(Expression::Kind::IF_ELSE), condition(condition), thenBlock(thenBlock), elseBlock(elseBlock) {
|
Expression(Expression::Kind::IF_ELSE, Expression::ValueType::VOID), condition(condition), thenBlock(thenBlock), elseBlock(elseBlock) {
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<Expression> ExpressionIfElse::getCondition() {
|
shared_ptr<Expression> ExpressionIfElse::getCondition() {
|
||||||
@@ -162,7 +185,7 @@ string ExpressionIfElse::toString() {
|
|||||||
//
|
//
|
||||||
// ExpressionInvalid
|
// ExpressionInvalid
|
||||||
ExpressionInvalid::ExpressionInvalid(shared_ptr<Token> token):
|
ExpressionInvalid::ExpressionInvalid(shared_ptr<Token> token):
|
||||||
Expression(Expression::Kind::INVALID), token(token) {
|
Expression(Expression::Kind::INVALID, Expression::ValueType::VOID), token(token) {
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<Token> ExpressionInvalid::getToken() {
|
shared_ptr<Token> ExpressionInvalid::getToken() {
|
||||||
|
|||||||
@@ -18,23 +18,39 @@ public:
|
|||||||
INVALID
|
INVALID
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum ValueType {
|
||||||
|
VOID,
|
||||||
|
BOOL,
|
||||||
|
SINT32,
|
||||||
|
REAL32
|
||||||
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Kind kind;
|
Kind kind;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
ValueType valueType;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Expression(Kind kind);
|
Expression(Kind kind, ValueType valueType);
|
||||||
Kind getKind();
|
Kind getKind();
|
||||||
|
ValueType getValueType();
|
||||||
bool isValid();
|
bool isValid();
|
||||||
virtual string toString();
|
virtual string toString();
|
||||||
};
|
};
|
||||||
|
|
||||||
class ExpressionLiteral: public Expression {
|
class ExpressionLiteral: public Expression {
|
||||||
private:
|
private:
|
||||||
int64_t integer = 0;
|
bool boolValue;
|
||||||
|
int32_t sint32Value;
|
||||||
|
float real32Value;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ExpressionLiteral(shared_ptr<Token> token);
|
ExpressionLiteral(shared_ptr<Token> token);
|
||||||
int64_t getInteger();
|
//int64_t getInteger();
|
||||||
|
bool getBoolValue();
|
||||||
|
int32_t getSint32Value();
|
||||||
|
float getReal32Value();
|
||||||
string toString() override;
|
string toString() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -179,7 +179,19 @@ shared_ptr<Token> Lexer::nextToken() {
|
|||||||
if (token != nullptr)
|
if (token != nullptr)
|
||||||
return token;
|
return token;
|
||||||
|
|
||||||
|
token = match(Token::Kind::BOOL, "true", true);
|
||||||
|
if (token != nullptr)
|
||||||
|
return token;
|
||||||
|
|
||||||
|
token = match(Token::Kind::BOOL, "false", true);
|
||||||
|
if (token != nullptr)
|
||||||
|
return token;
|
||||||
|
|
||||||
// literal
|
// literal
|
||||||
|
token = matchReal();
|
||||||
|
if (token != nullptr)
|
||||||
|
return token;
|
||||||
|
|
||||||
token = matchInteger();
|
token = matchInteger();
|
||||||
if (token != nullptr)
|
if (token != nullptr)
|
||||||
return token;
|
return token;
|
||||||
@@ -229,6 +241,29 @@ shared_ptr<Token> Lexer::matchInteger() {
|
|||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shared_ptr<Token> Lexer::matchReal() {
|
||||||
|
int nextIndex = currentIndex;
|
||||||
|
|
||||||
|
while (nextIndex < source.length() && isDigit(nextIndex))
|
||||||
|
nextIndex++;
|
||||||
|
|
||||||
|
if (nextIndex >= source.length() || source.at(nextIndex) != '.')
|
||||||
|
return nullptr;
|
||||||
|
else
|
||||||
|
nextIndex++;
|
||||||
|
|
||||||
|
while (nextIndex < source.length() && isDigit(nextIndex))
|
||||||
|
nextIndex++;
|
||||||
|
|
||||||
|
if (!isSeparator(nextIndex))
|
||||||
|
return matchInvalid();
|
||||||
|
|
||||||
|
string lexme = source.substr(currentIndex, nextIndex - currentIndex);
|
||||||
|
shared_ptr<Token> token = make_shared<Token>(Token::Kind::REAL, lexme, currentLine, currentColumn);
|
||||||
|
advanceWithToken(token);
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
shared_ptr<Token> Lexer::matchIdentifier() {
|
shared_ptr<Token> Lexer::matchIdentifier() {
|
||||||
int nextIndex = currentIndex;
|
int nextIndex = currentIndex;
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ private:
|
|||||||
shared_ptr<Token> nextToken();
|
shared_ptr<Token> nextToken();
|
||||||
shared_ptr<Token> match(Token::Kind kind, string lexme, bool needsSeparator);
|
shared_ptr<Token> match(Token::Kind kind, string lexme, bool needsSeparator);
|
||||||
shared_ptr<Token> matchInteger();
|
shared_ptr<Token> matchInteger();
|
||||||
|
shared_ptr<Token> matchReal();
|
||||||
shared_ptr<Token> matchIdentifier();
|
shared_ptr<Token> matchIdentifier();
|
||||||
shared_ptr<Token> matchEnd();
|
shared_ptr<Token> matchEnd();
|
||||||
shared_ptr<Token> matchInvalid();
|
shared_ptr<Token> matchInvalid();
|
||||||
|
|||||||
@@ -78,7 +78,8 @@ llvm::Value *ModuleBuilder::valueForExpression(shared_ptr<Expression> expression
|
|||||||
}
|
}
|
||||||
|
|
||||||
llvm::Value *ModuleBuilder::valueForLiteral(shared_ptr<ExpressionLiteral> expression) {
|
llvm::Value *ModuleBuilder::valueForLiteral(shared_ptr<ExpressionLiteral> expression) {
|
||||||
return llvm::ConstantInt::get(int32Type, expression->getInteger(), true);
|
//return llvm::ConstantInt::get(int32Type, expression->getInteger(), true);
|
||||||
|
return llvm::ConstantInt::get(int32Type, expression->getSint32Value(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
llvm::Value *ModuleBuilder::valueForGrouping(shared_ptr<ExpressionGrouping> expression) {
|
llvm::Value *ModuleBuilder::valueForGrouping(shared_ptr<ExpressionGrouping> expression) {
|
||||||
|
|||||||
@@ -108,8 +108,12 @@ string Token::toString() {
|
|||||||
case QUESTION:
|
case QUESTION:
|
||||||
return "QUESTION";
|
return "QUESTION";
|
||||||
|
|
||||||
|
case BOOL:
|
||||||
|
return "BOOL(" + lexme + ")";
|
||||||
case INTEGER:
|
case INTEGER:
|
||||||
return "INTEGER(" + lexme + ")";
|
return "INTEGER(" + lexme + ")";
|
||||||
|
case REAL:
|
||||||
|
return "REAL(" + lexme + ")";
|
||||||
case IDENTIFIER:
|
case IDENTIFIER:
|
||||||
return "IDENTIFIER(" + lexme + ")";
|
return "IDENTIFIER(" + lexme + ")";
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,9 @@ public:
|
|||||||
FUNCTION,
|
FUNCTION,
|
||||||
RETURN,
|
RETURN,
|
||||||
|
|
||||||
|
BOOL,
|
||||||
INTEGER,
|
INTEGER,
|
||||||
|
REAL,
|
||||||
IDENTIFIER,
|
IDENTIFIER,
|
||||||
|
|
||||||
NEW_LINE,
|
NEW_LINE,
|
||||||
|
|||||||
Reference in New Issue
Block a user