Precompiled Header :)

This commit is contained in:
apio 2022-06-16 15:56:10 +02:00
parent 4f6a1235a0
commit 8b1bb00d75
24 changed files with 512 additions and 528 deletions

View File

@ -23,8 +23,6 @@ add_executable(
src/Location.cpp
src/Error.h
src/Error.cpp
src/StringConversion.h
src/StringConversion.cpp
src/FormatString/FormatString.hpp
src/FileIO.h
src/FileIO.cpp
@ -42,21 +40,24 @@ add_executable(
src/AST/ExprNode.h
src/AST/MulNode.cpp
src/AST/MulNode.h
src/AST/NumberNode.cpp
src/AST/NumberNode.h
src/AST/StatementNode.cpp
src/AST/StatementNode.h
src/AST/NumberNode.cpp
src/AST/NumberNode.h
src/AST/SumNode.cpp
src/AST/SumNode.h
src/replace.cpp
src/replace.h
src/utils.cpp
src/utils.h
src/Parser.cpp
src/Parser.h
src/sapphirepch.h
)
target_include_directories(sapphirec PUBLIC src)
target_include_directories(sapphirec PUBLIC src/tclap-1.2.5/include)
target_precompile_headers(sapphirec PUBLIC src/sapphirepch.h)
llvm_map_components_to_libnames(llvm_libs support core irreader)
# Link against LLVM libraries

View File

@ -1,22 +1,19 @@
namespace linux {
@sys_read (u32 fd, i8* buf, u64 size) {
let @sys_read (u32 fd, i8* buf, u64 size) in {
syscall3(0,fd,buf,size);
}
@sys_write (u32 fd, i8* buf, u64 size) {
let @sys_write (u32 fd, i8* buf, u64 size) in {
syscall3(1,fd,buf,size);
}
@sys_open (i8* name, i32 flags, u16 mode) {
let @sys_open (i8* name, i32 flags, u16 mode) in {
syscall3(2,name,flags,mode);
}
@sys_close (u32 fd) {
let @sys_close (u32 fd) in {
syscall1(3,fd);
}
@sys_exit (i32 code) {
let @sys_exit (i32 code) in {
syscall1(60,code);
}
}

View File

@ -1 +1,9 @@
#include "NumberNode.h"
NumberNode::NumberNode()
{
}
NumberNode::~NumberNode()
{
}

View File

@ -1,6 +1,6 @@
#pragma once
#include "sapphirepch.h"
#include <llvm/ADT/Triple.h>
#include <string>
struct Arguments
{

View File

@ -1,25 +1,15 @@
#include "Error.h"
#include "Importer.h"
#include "StringConversion.h"
#include "utils.h"
#include <algorithm>
#include <iostream>
std::string Error::get_spaces(const int& num)
{
std::string output;
for (int i = 0; i < num; i++)
{
output += " ";
}
return output;
}
void Error::show_import_line(const Location& loc, std::ostream& output_stream)
{
output_stream << "in file included from ";
output_stream << "\033[1;1m";
output_stream << loc.to_string();
output_stream << loc.str();
output_stream << ": ";
@ -52,10 +42,10 @@ void Error::show_import_lines(const Location& loc, void (*import_line_printer)(c
{
show_import_lines(loc, show_import_line, std::cerr);
std::string linestr = int_to_string(loc.line);
std::string linestr = to_string(loc.line);
std::cerr << "\033[1;1m";
std::cerr << loc.to_string();
std::cerr << loc.str();
std::cerr << ": ";
@ -67,11 +57,11 @@ void Error::show_import_lines(const Location& loc, void (*import_line_printer)(c
std::cerr << std::endl;
std::cerr << linestr;
std::cerr << get_spaces(4);
std::cerr << std::string(4, ' ');
std::cerr << line_text;
std::cerr << std::endl;
std::cerr << get_spaces(4 + linestr.size() + loc.column - 1);
std::cerr << std::string(4 + linestr.size() + loc.column - 1, ' ');
std::cerr << "\033[31;49m";
std::cerr << "^";
@ -99,10 +89,10 @@ void Error::throw_warning(const Location& loc, const std::string line_text, cons
{
show_import_lines(loc, show_import_line, std::cout);
std::string linestr = int_to_string(loc.line);
std::string linestr = to_string(loc.line);
std::cout << "\033[1;1m";
std::cout << loc.to_string();
std::cout << loc.str();
std::cout << ": ";
@ -114,11 +104,11 @@ void Error::throw_warning(const Location& loc, const std::string line_text, cons
std::cout << std::endl;
std::cout << linestr;
std::cout << get_spaces(4);
std::cout << std::string(4, ' ');
std::cout << line_text;
std::cout << std::endl;
std::cout << get_spaces(4 + linestr.size() + loc.column - 1);
std::cout << std::string(4 + linestr.size() + loc.column - 1, ' ');
std::cout << "\033[33;49m";
std::cout << "^";

View File

@ -13,6 +13,4 @@ void throw_warning(const Location& loc, const std::string line_text, const std::
void show_import_lines(const Location& loc, void (*import_line_printer)(const Location&, std::ostream&),
std::ostream& stream);
std::string get_spaces(const int& num);
} // namespace Error

View File

@ -1,10 +1,10 @@
#include "FileIO.h"
#include "Error.h"
#include "sapphirepch.h"
#include <cstring>
#include <errno.h>
#include <filesystem>
#include <fstream>
#include <vector>
std::string FileIO::read_all(const std::string& filename)
{
@ -19,7 +19,6 @@ std::string FileIO::read_all(const std::string& filename)
catch (const std::exception& e)
{
Error::throw_error_without_location("unable to open file " + filename + ": " + strerror(errno));
return "";
}
file.exceptions(std::ios::goodbit);
std::vector<char> file_chars;

View File

@ -1,5 +1,5 @@
#pragma once
#include <string>
#include "sapphirepch.h"
/* Namespace for simple file operations. */
namespace FileIO

View File

@ -2,9 +2,8 @@
#include "Arguments.h"
#include "Error.h"
#include "FileIO.h"
#include <algorithm>
#include "sapphirepch.h"
#include <fstream>
#include <iostream>
#define MAX_IMPORTS 100
int Importer::import_count = 0;
@ -49,6 +48,7 @@ TokenStream Importer::evaluate(const TokenStream& original)
}
if (import_count > MAX_IMPORTS)
Error::throw_error(current_token.loc, current_token.line(), "maximum import depth exceeded");
std::string input_file_name = next_token.string_value + ".sp";

View File

@ -1,6 +1,5 @@
#include "Lexer.h"
#include "Error.h"
#include <algorithm>
#define WHITESPACE "\t \n"
#define LETTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYZ_"

View File

@ -1,9 +1,7 @@
#pragma once
#include "Token.h"
#include "sapphirepch.h"
#include <array>
#include <memory>
#include <string>
#include <vector>
/* Let's redefine TokenStream, as if it wasn't already defined in Token.h*/
typedef std::vector<Token> TokenStream;

View File

@ -1,5 +1,4 @@
#include "Location.h"
#include "StringConversion.h"
#include <sstream>
Location::Location(int ln, int col, std::string file) : line(ln), column(col), fname(file)
@ -10,20 +9,16 @@ Location::~Location()
{
}
std::string Location::to_string() const
std::string Location::str() const
{
std::ostringstream ss;
ss << fname;
ss << ":";
ss << int_to_string(line);
ss << ":";
ss << int_to_string(column);
ss << fname << ":" << line << ":" << column;
return ss.str();
}
std::string Location::to_parenthesized_string() const
std::string Location::paren_str() const
{
return "(" + this->to_string() + ")";
return "(" + this->str() + ")";
}
void Location::advance()
@ -45,7 +40,7 @@ void Location::operator=(const Location& other)
this->parent = other.parent;
this->line = other.line;
this->column = other.column;
this->fname.assign(other.fname.c_str());
this->fname = other.fname;
}
void Location::copy(const Location& other)

View File

@ -1,6 +1,5 @@
#pragma once
#include <memory>
#include <string>
#include "sapphirepch.h"
/* Struct to represent a location in a file. */
struct Location
@ -18,9 +17,9 @@ struct Location
~Location();
/* Returns a string of the format FILE:LINE:COL. */
std::string to_string() const;
std::string str() const;
/* Returns a string of the format (FILE:LINE:COL). */
std::string to_parenthesized_string() const;
std::string paren_str() const;
/* Advance to the next column in the file. */
void advance();

View File

@ -2,8 +2,7 @@
#include "AST/NumberNode.h"
#include "Error.h"
#include "Lexer.h"
#include <cassert>
#include <memory>
#include "sapphirepch.h"
/* Parser class for the Sapphire compiler. */
class Parser
@ -46,7 +45,7 @@ class Parser
private:
bool m_is_error;
std::string m_error;
std::shared_ptr<Token> error_tok;
std::unique_ptr<Token> error_tok;
std::shared_ptr<T> m_ptr;
};

View File

@ -1,16 +0,0 @@
#include "StringConversion.h"
#include <cstdio>
std::string int_to_string(const int& value)
{
char buffer[12];
std::sprintf(buffer, "%d", value);
return {buffer};
}
std::string float_to_string(const float& value)
{
char buffer[50];
std::sprintf(buffer, "%f", value);
return {buffer};
}

View File

@ -1,8 +0,0 @@
#pragma once
#include <string>
/* Easy way of converting an int to a string without writing 5 lines of code every time you want to do it. */
std::string int_to_string(const int& value);
/* Easy way of converting a float to a string without writing 5 lines of code every time you want to do it. */
std::string float_to_string(const float& value);

View File

@ -1,7 +1,6 @@
#include "Token.h"
#include "FormatString/FormatString.hpp"
#include "StringConversion.h"
#include "replace.h"
#include "utils.h"
const std::string token_strings[] = {
"TT_IDENTIFIER", "TT_NUMBER", "TT_FLOAT", "TT_KEYWORD", "TT_STRING", "TT_PLUS",
@ -58,7 +57,7 @@ Token Token::copy_with_new_type(const TokenType& type)
std::string Token::to_string() const
{
std::string details = loc.to_parenthesized_string();
std::string details = loc.paren_str();
if (tk_type == TT_Number)
{
return format_string("INT:%d %s", int_value, details);

View File

@ -1,7 +1,6 @@
#pragma once
#include "Location.h"
#include <string>
#include <vector>
#include "sapphirepch.h"
/* All current token types. Will change in the future. */
enum TokenType

View File

@ -1,9 +0,0 @@
#include "replace.h"
bool replace(std::string& str, const std::string& from, const std::string& to)
{
size_t start_pos = str.find(from);
if (start_pos == std::string::npos) return false;
str.replace(start_pos, from.length(), to);
return true;
}

View File

@ -1,5 +0,0 @@
#pragma once
#include <string>
/* Simple function to replace a substring in a string. */
bool replace(std::string& str, const std::string& from, const std::string& to);

View File

@ -3,7 +3,7 @@
#include "Importer.h"
#include "Lexer.h"
#include "Normalizer.h"
#include <iostream>
#include "sapphirepch.h"
int main(int argc, char** argv)
{

6
src/sapphirepch.h Normal file
View File

@ -0,0 +1,6 @@
#include <algorithm>
#include <cassert>
#include <iostream>
#include <memory>
#include <string>
#include <vector>

24
src/utils.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "utils.h"
#include <sstream>
bool replace(std::string& str, const std::string& from, const std::string& to)
{
size_t start_pos = str.find(from);
if (start_pos == std::string::npos) return false;
str.replace(start_pos, from.length(), to);
return true;
}
std::string to_string(const int& value)
{
std::ostringstream result;
result << value;
return result.str();
}
std::string to_string(const float& value)
{
std::ostringstream result;
result << value;
return result.str();
}

11
src/utils.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "sapphirepch.h"
/* Simple function to replace a substring in a string. */
bool replace(std::string& str, const std::string& from, const std::string& to);
/* Easy way of converting an int to a string without writing 5 lines of code every time you want to do it. */
std::string to_string(const int& value);
/* Easy way of converting a float to a string without writing 5 lines of code every time you want to do it. */
std::string to_string(const float& value);