Some basic tokens

This commit is contained in:
Rafał Grodziński
2025-05-27 14:25:29 +09:00
parent 45387b7638
commit 838dbbeb03
7 changed files with 73 additions and 5 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
.DS_Store
*.o
*.o
brb

View File

@@ -1,6 +1,9 @@
#include "Lexer.h"
#include "Token.h"
std::vector<Token> tokens() {
Lexer::Lexer(std::string source) : source(source) {
}
std::vector<Token> Lexer::tokens() {
return { Token::integer, Token::real, Token::integer, Token::eof };
}

View File

@@ -6,7 +6,11 @@
class Token;
class Lexer {
private:
std::string source;
public:
Lexer(std::string source);
std::vector<Token> tokens();
};

View File

@@ -1 +1,18 @@
#include "Token.h"
#include "Token.h"
Token::Token(Kind kind): kind(kind) {
}
std::string Token::toString() {
switch (kind) {
case integer:
return "INTEGER";
break;
case real:
return "REAL";
break;
case eof:
return "EOF";
break;
}
}

View File

@@ -1,6 +1,8 @@
#ifndef TOKEN_H
#define TOKEN_H
#include <iostream>
class Token {
public:
enum Kind {
@@ -8,6 +10,13 @@ public:
real,
eof
};
private:
Kind kind;
public:
Token(Kind kind);
std::string toString();
};
#endif

34
main.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include <iostream>
#include <fstream>
#include "Lexer.h"
#include "Token.h"
std::string readFile(std::string fileName) {
std::ifstream file(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
if (!file.is_open()) {
std::cerr << "Cannot open file " << fileName << std::endl;
exit(1);
}
std::streamsize fileSize = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> fileBytes(fileSize);
file.read(fileBytes.data(), fileSize);
return std::string(fileBytes.data(), fileSize);
}
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "Need to provide a file name" << std::endl;
exit(1);
}
std::string source = readFile(std::string(argv[1]));
Lexer lexer(source);
std::vector<Token> tokens = lexer.tokens();
for (Token &token : tokens)
std::cout << token.toString() << " ";
std::cout << std::endl;
return 0;
}

View File

@@ -1,3 +1,3 @@
#!/bin/bash
cc -c -std=c++17 *.cpp
cc -std=c++17 -lc++ *.cpp -o brb