Skip to content

Commit

Permalink
Project architecture adjustment. It does not affect normal operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ax-6 committed Feb 20, 2024
1 parent 779bb98 commit f32f612
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 84 deletions.
62 changes: 61 additions & 1 deletion compiler/compiler.cc
Original file line number Diff line number Diff line change
@@ -1 +1,61 @@
#include "compiler/compiler.h"
// Copyright 2024 AQ authors, All Rights Reserved.
// This program is licensed under the AQ License. You can find the AQ license in
// the root directory.

#include "compiler/compiler.h"

#include <chrono>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>

#include "debugger/debugger.h"
#include "compiler/lexer/lexer.h"
#include "compiler/lexer/token.h"

namespace Aq {
Compiler::Compiler() = default;
Compiler::~Compiler() = default;

int Compiler::CompileFile(const char* filename) {
// TODO: Waiting for improvements.
auto start = std::chrono::high_resolution_clock::now();
std::ifstream file;
file.open(filename);
if (!file.is_open()) {
Aq::Debugger error_info(Aq::Debugger::Level::ERROR, "Aq::Main",
"Main_ReadFileError", "Can't open file.", nullptr);
return -1;
}

std::vector<char> code;
char ch;
while (file.get(ch)) {
code.push_back(ch);
}
code.push_back('\0');
file.close();
Lexer lexer(code.data(), code.size() - 1);
Token token;
while (true) {
int return_value = lexer.LexToken(token);
if (token.length == 0) {
std::cout << "END OF THE CODE.";
} else {
std::cout << std::string(token.location, token.length) << std::endl;
}
if (lexer.IsReadEnd()) {
break;
}
}
auto end = std::chrono::high_resolution_clock::now();
auto duration_in_milliseconds =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
double duration_in_seconds =
static_cast<double>(duration_in_milliseconds.count()) / 1000.0;
std::cout << "Execution time: " << duration_in_seconds << " seconds.\n";
return 0;
}
} // namespace Aq
22 changes: 19 additions & 3 deletions compiler/compiler.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
// Copyright 2024 AQ authors, All Rights Reserved.
// This program is licensed under the AQ License. You can find the AQ license in
// the root directory.

#ifndef AQ_COMPILE_COMPILE_H_
#define AQ_COMPILE_COMPILE_H_

namespace Aq {
namespace Compiler {
extern int CompileFile(const char* filename);
}
class Compiler {
public:
Compiler();
~Compiler();

int CompileFile(const char* filename);

private:
class Lexer;
struct Token;
template <typename T>
class LexMap;
class TokenMap;
class Parser;
};
} // namespace Aq

#endif // AQ_COMPILE_H_
6 changes: 2 additions & 4 deletions compiler/lexer/lex_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#include <cstring>
#include <limits>

#include "compiler/compiler.h"
#include "debugger/debugger.h"

namespace Aq {
namespace Compiler {
// A hash table for the lexer. Used to find special definitions such as compiler
// keywords.
template <typename T>
class LexMap {
class Compiler::LexMap {
public:
// Construct a LexMap class, and the default hash table memory size is 1024.
// Do not modify it unless necessary.
Expand Down Expand Up @@ -196,7 +196,5 @@ class LexMap {
return 0;
};
};
} // namespace Compiler
} // namespace Aq

#endif
7 changes: 3 additions & 4 deletions compiler/lexer/lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

#include <string>

#include "compiler/compiler.h"
#include "compiler/lexer/token.h"
#include "debugger/debugger.h"

namespace Aq {
namespace Compiler {
bool Lexer::IsReadEnd() const {
bool Compiler::Lexer::IsReadEnd() const {
if (buffer_ptr_ >= buffer_end_) {
return true;
} else {
return false;
}
}

int Lexer::LexToken(Token& return_token) {
int Compiler::Lexer::LexToken(Token& return_token) {
// Set the return token type to start.
return_token.type = Token::Type::START;

Expand Down Expand Up @@ -415,5 +415,4 @@ int Lexer::LexToken(Token& return_token) {
return 0;
}
}
} // namespace Compiler
} // namespace Aq
5 changes: 2 additions & 3 deletions compiler/lexer/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

#include <cstddef>

#include "compiler/compiler.h"
#include "compiler/lexer/token.h"

namespace Aq {
namespace Compiler {
class Lexer {
class Compiler::Lexer {
public:
// Initialize the Lexer class and store |source_code| to |buffer_ptr_|.
Lexer(char* source_code, size_t length)
Expand All @@ -35,7 +35,6 @@ class Lexer {
char* buffer_end_;
TokenMap token_map_;
};
} // namespace Compiler
} // namespace Aq

#endif
59 changes: 3 additions & 56 deletions compiler/lexer/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,10 @@
// This program is licensed under the AQ License. You can find the AQ license in
// the root directory.

#include <chrono>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>

#include "aq/aq.h"
#include "compiler/lexer/lex_map.h"
#include "compiler/lexer/lexer.h"
#include "compiler/lexer/token.h"
#include "debugger/debugger.h"

int LexerTest(int argc, char* argv[]) {
auto start = std::chrono::high_resolution_clock::now();
std::ifstream file;
const char* filename = argv[1];
file.open(filename);
if (!file.is_open()) {
Aq::Debugger error_info(Aq::Debugger::Level::ERROR, "Aq::Main",
"Main_ReadFileError", "Can't open file.", nullptr);
return -1;
}

std::vector<char> code;
char ch;
while (file.get(ch)) {
code.push_back(ch);
}
code.push_back('\0');
file.close();
Aq::Compiler::Lexer lexer(code.data(), code.size() - 1);
Aq::Compiler::Token token;
while (true) {
int return_value = lexer.LexToken(token);
if (token.length == 0) {
std::cout << "END OF THE CODE.";
} else {
std::cout << std::string(token.location, token.length) << std::endl;
}
if (lexer.IsReadEnd()) {
break;
}
}
auto end = std::chrono::high_resolution_clock::now();
auto duration_in_milliseconds =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
double duration_in_seconds =
static_cast<double>(duration_in_milliseconds.count()) / 1000.0;
std::cout << "Execution time: " << duration_in_seconds << " seconds.\n";
return 0;
}

int LexMapTest(int argc, char* argv[]) { return 0; }
#include "compiler/compiler.h"

int main(int argc, char* argv[]) {
LexerTest(argc, argv);
LexMapTest(argc, argv);
Aq::Compiler compiler;
compiler.CompileFile(argv[1]);
return 0;
}
15 changes: 7 additions & 8 deletions compiler/lexer/token.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

#include "compiler/lexer/token.h"

#include "compiler/compiler.h"
#include "compiler/lexer/lex_map.h"
#include "debugger/debugger.h"

namespace Aq {
namespace Compiler {
Token::Token() = default;
Token::~Token() {
Compiler::Token::Token() = default;
Compiler::Token::~Token() {
if (type == Type::NUMBER) {
delete[] value.Number;
} else if (type == Type::IDENTIFIER) {
Expand All @@ -22,7 +22,7 @@ Token::~Token() {
}
}

TokenMap::TokenMap() {
Compiler::TokenMap::TokenMap() {
keyword_map.Insert("auto", Token::KeywordType::Auto);
keyword_map.Insert("and", Token::KeywordType::And);
keyword_map.Insert("bitand", Token::KeywordType::Bitand);
Expand Down Expand Up @@ -143,13 +143,12 @@ TokenMap::TokenMap() {
operator_map.Insert(">>>", Token::OperatorType::greatergreatergreater);
operator_map.Insert("^^", Token::OperatorType::caretcaret);
}
TokenMap::~TokenMap() = default;
Compiler::TokenMap::~TokenMap() = default;

Token::KeywordType TokenMap::GetKeywordValue(const char* keyword) {
Compiler::Token::KeywordType Compiler::TokenMap::GetKeywordValue(const char* keyword) {
return keyword_map.Find(keyword);
}
Token::OperatorType TokenMap::GetOperatorValue(const char* _operator) {
Compiler::Token::OperatorType Compiler::TokenMap::GetOperatorValue(const char* _operator) {
return operator_map.Find(_operator);
}
} // namespace Compiler
} // namespace Aq
8 changes: 3 additions & 5 deletions compiler/lexer/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#ifndef AQ_COMPILER_LEXER_TOKEN_H_
#define AQ_COMPILER_LEXER_TOKEN_H_

#include "compiler/compiler.h"
#include "compiler/lexer/lex_map.h"

namespace Aq {
namespace Compiler {
struct Token {
struct Compiler::Token {
enum class Type {
START,
KEYWORD,
Expand Down Expand Up @@ -168,7 +168,7 @@ struct Token {
Token& operator=(Token&&) noexcept = default;
};

class TokenMap {
class Compiler::TokenMap {
public:
TokenMap();
~TokenMap();
Expand All @@ -185,7 +185,5 @@ class TokenMap {
LexMap<Token::KeywordType> keyword_map;
LexMap<Token::OperatorType> operator_map;
};

} // namespace Compiler
} // namespace Aq
#endif

0 comments on commit f32f612

Please sign in to comment.