Skip to content

Commit

Permalink
Continue work on cleanups: Lexer, Token
Browse files Browse the repository at this point in the history
  • Loading branch information
wpmed92 committed Aug 13, 2024
1 parent a7aae8b commit f99685d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
10 changes: 4 additions & 6 deletions include/Lexer/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,18 @@ enum class SuffixCheckResult {
};

struct Error {
Error() : kind(ErrorKind::None) {}
Error(ErrorKind kind, const std::string &msg) : kind(kind), msg(msg) {}
Error();
Error(ErrorKind kind, const std::string &msg);

ErrorKind kind;
std::string msg;

bool none() { return kind == ErrorKind::None; }
bool none();
};

class Lexer {
public:
Lexer(const std::string &characters)
: characters(characters), curCharPos(0), savedCharPos(0), lineNum(1), col(1) {}

Lexer(const std::string &characters);
tl::expected<std::reference_wrapper<std::vector<std::unique_ptr<Token>>>,
Error>
lexCharacterStream();
Expand Down
16 changes: 8 additions & 8 deletions include/Lexer/Token.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,35 @@ class NumericLiteral {

class IntegerLiteral : public NumericLiteral {
public:
IntegerLiteral(int32_t val) : val(val) {}
int32_t getVal() { return val; }
IntegerLiteral(int32_t val);
int32_t getVal();

private:
int32_t val;
};

class UnsignedIntegerLiteral : public NumericLiteral {
public:
UnsignedIntegerLiteral(uint32_t val) : val(val) {}
uint32_t getVal() { return val; }
UnsignedIntegerLiteral(uint32_t val);
uint32_t getVal();

private:
uint32_t val;
};

class FloatLiteral : public NumericLiteral {
public:
FloatLiteral(float val) : val(val) {}
float getVal() { return val; }
FloatLiteral(float val);
float getVal();

private:
float val;
};

class DoubleLiteral : public NumericLiteral {
public:
DoubleLiteral(double val) : val(val) {}
double getVal() { return val; }
DoubleLiteral(double val);
double getVal();

private:
double val;
Expand Down
8 changes: 8 additions & 0 deletions lib/Lexer/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ namespace shaderpulse {

namespace lexer {

Lexer::Lexer(const std::string &characters) : characters(characters), curCharPos(0), savedCharPos(0), lineNum(1), col(1) { }

Error::Error() : kind(ErrorKind::None) { }

Error::Error(ErrorKind kind, const std::string &msg) : kind(kind), msg(msg) { }

bool Error::none() { return kind == ErrorKind::None; }

std::unordered_map<std::string, TokenKind> Lexer::stringKeywordToTokenKind = {
#define KEYWORD(X) {TOSTRING(X), TokenKind::kw_##X},
#include "Lexer/TokenDefs.h"
Expand Down
22 changes: 22 additions & 0 deletions lib/Lexer/Token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,30 @@ namespace shaderpulse {

namespace lexer {

// Literals
NumericLiteral::~NumericLiteral(){};

// signed
IntegerLiteral::IntegerLiteral(int32_t val) : val(val) {}

int32_t IntegerLiteral:: getVal() { return val; }

// unsigned
UnsignedIntegerLiteral::UnsignedIntegerLiteral(uint32_t val) : val(val) {}

uint32_t UnsignedIntegerLiteral::getVal() { return val; }

// float
FloatLiteral::FloatLiteral(float val) : val(val) {}

float FloatLiteral::getVal() { return val; }

// double
DoubleLiteral::DoubleLiteral(double val) : val(val) {}

double DoubleLiteral::getVal() { return val; }

// Token
void Token::setTokenKind(TokenKind kind) { tokenKind = kind; }

TokenKind Token::getTokenKind() const { return tokenKind; }
Expand Down

0 comments on commit f99685d

Please sign in to comment.