Match real and booleans
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
#include "Expression.h"
|
||||
|
||||
Expression::Expression(Kind kind):
|
||||
kind(kind) {
|
||||
Expression::Expression(Kind kind, ValueType valueType):
|
||||
kind(kind), valueType(valueType) {
|
||||
}
|
||||
|
||||
Expression::Kind Expression::getKind() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
Expression::ValueType Expression::getValueType() {
|
||||
return valueType;
|
||||
}
|
||||
|
||||
bool Expression::isValid() {
|
||||
return kind != Expression::Kind::INVALID;
|
||||
}
|
||||
@@ -19,7 +23,7 @@ string Expression::toString() {
|
||||
//
|
||||
// ExpressionBinary
|
||||
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()) {
|
||||
case Token::Kind::EQUAL:
|
||||
operation = EQUAL;
|
||||
@@ -101,22 +105,41 @@ string ExpressionBinary::toString() {
|
||||
//
|
||||
// ExpressionLiteral
|
||||
ExpressionLiteral::ExpressionLiteral(shared_ptr<Token> token):
|
||||
Expression(Expression::Kind::LITERAL) {
|
||||
integer = stoi(token->getLexme());
|
||||
Expression(Expression::Kind::LITERAL, Expression::ValueType::VOID) {
|
||||
valueType = Expression::ValueType::SINT32;
|
||||
sint32Value = stoi(token->getLexme());
|
||||
}
|
||||
|
||||
int64_t ExpressionLiteral::getInteger() {
|
||||
return integer;
|
||||
bool ExpressionLiteral::getBoolValue() {
|
||||
return boolValue;
|
||||
}
|
||||
|
||||
int32_t ExpressionLiteral::getSint32Value() {
|
||||
return sint32Value;
|
||||
}
|
||||
|
||||
float ExpressionLiteral::getReal32Value() {
|
||||
return real32Value;
|
||||
}
|
||||
|
||||
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(shared_ptr<Expression> expression):
|
||||
Expression(Expression::Kind::GROUPING), expression(expression) {
|
||||
Expression(Expression::Kind::GROUPING, Expression::ValueType::VOID), expression(expression) {
|
||||
}
|
||||
|
||||
shared_ptr<Expression> ExpressionGrouping::getExpression() {
|
||||
@@ -130,7 +153,7 @@ string ExpressionGrouping::toString() {
|
||||
//
|
||||
// ExpressionIfElse
|
||||
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() {
|
||||
@@ -162,7 +185,7 @@ string ExpressionIfElse::toString() {
|
||||
//
|
||||
// ExpressionInvalid
|
||||
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() {
|
||||
|
||||
@@ -18,23 +18,39 @@ public:
|
||||
INVALID
|
||||
};
|
||||
|
||||
enum ValueType {
|
||||
VOID,
|
||||
BOOL,
|
||||
SINT32,
|
||||
REAL32
|
||||
};
|
||||
|
||||
private:
|
||||
Kind kind;
|
||||
|
||||
protected:
|
||||
ValueType valueType;
|
||||
|
||||
public:
|
||||
Expression(Kind kind);
|
||||
Expression(Kind kind, ValueType valueType);
|
||||
Kind getKind();
|
||||
ValueType getValueType();
|
||||
bool isValid();
|
||||
virtual string toString();
|
||||
};
|
||||
|
||||
class ExpressionLiteral: public Expression {
|
||||
private:
|
||||
int64_t integer = 0;
|
||||
bool boolValue;
|
||||
int32_t sint32Value;
|
||||
float real32Value;
|
||||
|
||||
public:
|
||||
ExpressionLiteral(shared_ptr<Token> token);
|
||||
int64_t getInteger();
|
||||
//int64_t getInteger();
|
||||
bool getBoolValue();
|
||||
int32_t getSint32Value();
|
||||
float getReal32Value();
|
||||
string toString() override;
|
||||
};
|
||||
|
||||
|
||||
@@ -179,7 +179,19 @@ shared_ptr<Token> Lexer::nextToken() {
|
||||
if (token != nullptr)
|
||||
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
|
||||
token = matchReal();
|
||||
if (token != nullptr)
|
||||
return token;
|
||||
|
||||
token = matchInteger();
|
||||
if (token != nullptr)
|
||||
return token;
|
||||
@@ -229,6 +241,29 @@ shared_ptr<Token> Lexer::matchInteger() {
|
||||
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() {
|
||||
int nextIndex = currentIndex;
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ private:
|
||||
shared_ptr<Token> nextToken();
|
||||
shared_ptr<Token> match(Token::Kind kind, string lexme, bool needsSeparator);
|
||||
shared_ptr<Token> matchInteger();
|
||||
shared_ptr<Token> matchReal();
|
||||
shared_ptr<Token> matchIdentifier();
|
||||
shared_ptr<Token> matchEnd();
|
||||
shared_ptr<Token> matchInvalid();
|
||||
|
||||
@@ -78,7 +78,8 @@ llvm::Value *ModuleBuilder::valueForExpression(shared_ptr<Expression> 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) {
|
||||
|
||||
@@ -108,8 +108,12 @@ string Token::toString() {
|
||||
case QUESTION:
|
||||
return "QUESTION";
|
||||
|
||||
case BOOL:
|
||||
return "BOOL(" + lexme + ")";
|
||||
case INTEGER:
|
||||
return "INTEGER(" + lexme + ")";
|
||||
case REAL:
|
||||
return "REAL(" + lexme + ")";
|
||||
case IDENTIFIER:
|
||||
return "IDENTIFIER(" + lexme + ")";
|
||||
|
||||
|
||||
@@ -31,7 +31,9 @@ public:
|
||||
FUNCTION,
|
||||
RETURN,
|
||||
|
||||
BOOL,
|
||||
INTEGER,
|
||||
REAL,
|
||||
IDENTIFIER,
|
||||
|
||||
NEW_LINE,
|
||||
|
||||
Reference in New Issue
Block a user