Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"makefile.configureOnOpen": true
}
144 changes: 0 additions & 144 deletions Lexer/LexerClasses - Part 1.cpp

This file was deleted.

5 changes: 5 additions & 0 deletions src/common/token/token.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <token.hpp>
#include <string>

Token::Token(TokenType type, std::string val, int l, int c) : type(type), value(val), line(l), column(c){}

6 changes: 5 additions & 1 deletion src/common/token/token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ enum class TokenType {
COLON, // :
INDENT,
DEDENT,
COMMENT // #
COMMENT, // #
PRINT,
AMPERSAND, // &
PIPE, // |
SPACE,
};


Expand Down
27 changes: 27 additions & 0 deletions src/lexer/keywords.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <unordered_map>
#include <string>
#include <vector>

#include "token.hpp"

inline const std::unordered_map<std::string, TokenType> keywords = {
{"def", TokenType::DEF},
{"return", TokenType::RETURN},
{"if", TokenType::IF},
{"else", TokenType::ELSE},
{"print", TokenType::PRINT},
{"=", TokenType::ASSIGN},
{"+", TokenType::PLUS},
{"-", TokenType::MINUS},
{"*", TokenType::STAR},
{"/", TokenType::SLASH},
{"(", TokenType::LPAREN},
{")", TokenType::RPAREN},
{":", TokenType::COLON},
{",", TokenType::COMMA},
{"\n", TokenType::NEWLINE},
{"\t", TokenType::INDENT},
{" ", TokenType::SPACE}
};
117 changes: 117 additions & 0 deletions src/lexer/lexer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <token.hpp>
#include <keywords.hpp>
#include <lexer.hpp>
#include <string>
#include <stdexcept>

Lexer::Lexer(std::string input_string) : source_code(input_string){}
bool Lexer::isAtEnd(){
if (current_index >= source_code.size()){
return true;
}
return false;
}
char Lexer::peek(){
if (current_index >= source_code.size()){
return '\0';
}
return source_code[current_index];
}

char Lexer::peekNext(){
if (current_index+1 >= source_code.size()){
return '\0';
}
return source_code[current_index+1];
}

char Lexer::advance(){
if (current_index >= source_code.size()){
return '\0';
}
char c = source_code[current_index++];
if (c == '\n'){
line++;
column=1;
}
else {column++;}
return c;
}

void Lexer::scanNumber(std::string curr){
int start = column-1;
while (std::isdigit(peek())){
curr += Lexer::advance();
}
if (std::isalnum(peek())){
// throw an error
}
Token token(TokenType::NUMBER, curr, line, start);
tokens.push_back(token);
}

void Lexer::scanString(std::string quote){
int start = column-1;
while (!isAtEnd() && peek()!=quote[0] && peek()!='\n'){
quote += advance();
}
if (isAtEnd()){
// throw an error
}
else if (peek()=='\n'){
// throw an error
}
else{
quote += advance();
}
Token token(TokenType::STRING, quote, line, start);
tokens.push_back(token);
}

void Lexer::scanIdentifier(std::string curr){
int start = column-1;
while (std::isalnum(peek()) || peek()=='_'){
curr += advance();
}
Token token(TokenType::IDENTIFIER, curr, line, start);
if (keywords.count(curr)){
token.type = keywords.at(curr);
}
tokens.push_back(token);
}


std::vector<Token> Lexer::scan_Tokens(){
while (true){
if (isAtEnd()){
Token token(TokenType::EOF_TOKEN, "", line, column);
tokens.push_back(token);
break;
}
std::string curr = "";
curr += advance();
if (keywords.count(curr)){
TokenType type = keywords.at(curr);
if (type == TokenType::NEWLINE){
column = 0;
line++;
}
else if (type == TokenType::INDENT){
column += 3;
}
else if (type == TokenType::DEDENT){
column -= 5;
}
Token token(type, curr, line, column);
tokens.push_back(token);
}
else{
if (std::isdigit(curr[0])){
scanNumber(curr);
}
else if (std::isalpha(curr[0])){
scanIdentifier(curr);
}
}
}
}
38 changes: 12 additions & 26 deletions src/lexer/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,37 @@
#include <unordered_map>
#include <string>
#include <vector>
#include <cctype> //to check if char is alphanumeric

#include "token.hpp"

class Lexer {
public:
Lexer(std::string input_string);
std::vector<Token> scan_Tokens(void);
std::vector<Token> scan_Tokens();


private:
std::string source_code;
static inline const std::unordered_map<std::string, TokenType> keywords = {
{"def", TokenType::DEF},
{"return", TokenType::RETURN},
{"if", TokenType::IF},
{"else", TokenType::ELSE},
{"print", TokenType::PRINT},
{"=", TokenType::ASSIGN},
{"+", TokenType::PLUS},
{"-", TokenType::MINUS},
{"*", TokenType::STAR},
{"/", TokenType::SLASH},
{"(", TokenType::LPAREN},
{")", TokenType::RPAREN},
{":", TokenType::COLON},
{",", TokenType::COMMA},
{"\n", TokenType::NEWLINE},
{"\t", TokenType::INDENT}
};
std::vector<Token> tokens;
int start = 0;
int current_index = 0;
int line = 1;
int column = 1;


// FUNCTIONS NEEDED FOR LEXER TO WORK
bool isAtEnd(void); // Checks for last character
char advance(void); // Return current char and move forward
char peek(void); // Sometimes, we don't actually want to read a character and may only want to peek at it
char peekNext(void); // Peak at the next character
bool isAtEnd(); // Checks for last character
char advance(); // Return current char and move forward
char peek(); // Sometimes, we don't actually want to read a character and may only want to peek at it
char peekNext(); // Peak at the next character

void addToken(TokenType type);

// Specific scanners for complex types
void scanString(void);
void scanNumber(void);
void scanIdentifier(void);
void scanString(std::string first);
void scanNumber(std::string first);
void scanIdentifier(std::string first);

std::string preprocess_indents(std::string raw);
};
Empty file added src/parser/parser.cpp
Empty file.
3 changes: 3 additions & 0 deletions src/parser/parser.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#include "token.hpp"
Loading