Move reader related files to the reader namespace

This commit is contained in:
Sameer Rahmani 2021-03-28 23:14:32 +01:00
parent e9daeb9d13
commit f50e20567a
12 changed files with 153 additions and 35 deletions

View File

@ -1,11 +1,11 @@
add_executable(serene serene.cpp)
add_executable(serenec serene.cpp)
# Make sure to generate files related to the dialects first
add_dependencies(serene SereneDialectGen)
add_dependencies(serenec SereneDialectGen)
target_compile_features(serene PRIVATE cxx_std_14)
target_compile_features(serenec PRIVATE cxx_std_14)
target_link_libraries(serene PRIVATE
lserene
target_link_libraries(serenec PRIVATE
serene
${llvm_libs}
fmt::fmt
MLIRAnalysis
@ -19,6 +19,6 @@ target_link_libraries(serene PRIVATE
target_include_directories(serene SYSTEM PRIVATE $ENV{INCLUDE})
target_include_directories(serene PRIVATE ${INCLUDE_DIR})
install(TARGETS serene DESTINATION bin)
install(TARGETS serenec DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include)
cotire(serene)
cotire(serenec)

View File

@ -23,7 +23,7 @@
*/
#include "serene/serene.hpp"
#include "serene/reader.hpp"
#include "serene/reader/reader.hpp"
#include "serene/sir/sir.hpp"
#include <iostream>
#include <llvm/Support/CommandLine.h>
@ -52,13 +52,13 @@ int main(int argc, char *argv[]) {
switch (emitAction) {
case Action::DumpAST: {
FileReader *r = new FileReader(inputFile);
reader::FileReader *r = new reader::FileReader(inputFile);
r->dumpAST();
delete r;
return 0;
}
case Action::DumpIR: {
FileReader *r = new FileReader(inputFile);
reader::FileReader *r = new reader::FileReader(inputFile);
serene::sir::dumpSIR(*r->read());
delete r;

View File

@ -47,7 +47,7 @@ function clean() {
function run() {
pushed_build
$BUILD_DIR/bin/serene "$@"
$BUILD_DIR/bin/serenec "$@"
popd_build
}

View File

@ -0,0 +1,48 @@
/**
* Serene programming language.
*
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef LOCATION_H
#define LOCATION_H
namespace serene {
namespace reader {
/**
* It represents a location in the input string to the parser via `line`,
*/
struct Location {
int pos; // Position of in the input string.
int line;
int col;
};
struct LocationRange {
Location start;
Location end;
};
void inc_location(Location &, bool);
} // namespace reader
} // namespace serene
#endif

View File

@ -36,6 +36,7 @@
#include "serene/expr.hpp"
#include "serene/list.hpp"
#include "serene/logger.hpp"
#include "serene/reader/location.hpp"
#include "serene/serene.hpp"
#include "serene/symbol.hpp"
@ -46,6 +47,7 @@
#endif
namespace serene {
namespace reader {
class ReadError : public std::exception {
private:
@ -59,6 +61,7 @@ public:
class Reader {
private:
std::stringstream input_stream;
Location current_location{0, 0, 0};
char get_char(bool skip_whitespace);
void unget_char();
@ -99,6 +102,6 @@ public:
~FileReader();
};
} // namespace reader
} // namespace serene
#endif

View File

@ -74,7 +74,10 @@ def ValueOp: Serene_Op<"value"> {
// let verifier = [{ return serene::sir::verify(*this); }];
let builders = [
OpBuilder<(ins "int":$value)>,
OpBuilder<(ins "int":$value), [{
// Build from fix 64 bit int
build(odsBuilder, odsState, odsBuilder.getI64Type(), (uint64_t) value);
}]>,
];
}

View File

@ -1,37 +1,46 @@
# Optionally glob, but only for CMake 3.12 or later:
#file(GLOB HEADER_LIST CONFIGURE_DEPENDS "${INCLUDE_DIR}/**/*.hpp")
#file(GLOB SOURCES_LIST CONFIGURE_DEPENDS "${SOURCE_DIR}/**/*.cpp")
set(HEADER_LIST
"${INCLUDE_DIR}/serene/expr.hpp"
"${INCLUDE_DIR}/serene/serene.hpp"
"${INCLUDE_DIR}/serene/state.hpp"
# Reader
"${INCLUDE_DIR}/serene/reader/reader.hpp"
"${INCLUDE_DIR}/serene/reader/location.hpp"
"${INCLUDE_DIR}/serene/sir/sir.hpp"
"${INCLUDE_DIR}/serene/sir/dialect.hpp"
"${INCLUDE_DIR}/serene/sir/generator.hpp"
"${INCLUDE_DIR}/serene/sir/generator.hpp"
"${INCLUDE_DIR}/serene/namespace.hpp")
# Make an automatic library - will be static or dynamic based on user setting
add_library(lserene
add_library(serene
serene.cpp
reader.cpp
symbol.cpp
list.cpp
namespace.cpp
state.cpp
# Reader
reader/reader.cpp
reader/location.cpp
# IR
sir/sir.cpp
sir/dialect.cpp
sir/value_op.cpp
sir/generator.cpp
${HEADER_LIST})
# Make sure to generate files related to the dialects first
add_dependencies(lserene SereneDialectGen)
add_dependencies(serene SereneDialectGen)
# We need this directory, and users of our library will need it too
target_compile_features(lserene PUBLIC cxx_std_14)
target_include_directories(lserene PRIVATE ${INCLUDE_DIR})
target_include_directories(lserene PUBLIC ${PROJECT_BINARY_DIR})
target_compile_features(serene PUBLIC cxx_std_14)
target_include_directories(serene PRIVATE ${INCLUDE_DIR})
target_include_directories(serene PUBLIC ${PROJECT_BINARY_DIR})
# This depends on (header only) boost
target_link_libraries(lserene ${llvm_libs} fmt::fmt)
target_link_libraries(serene ${llvm_libs} fmt::fmt)
source_group(TREE "${INCLUDE_DIR}" PREFIX "Header Files" FILES ${HEADER_LIST})
#target_precompile_headers(serene PRIVATE ${HEADER_LIST})

48
src/reader/location.cpp Normal file
View File

@ -0,0 +1,48 @@
/**
* Serene programming language.
*
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "serene/reader/location.hpp"
namespace serene {
namespace reader {
/**
* Increase the given location by one and set the line/col value in respect to
* the `newline` in place.
* @param loc The `Location` data
* @param newline Whether or not we reached a new line
*/
void inc_location(Location &loc, bool newline) {
loc.pos++;
if (!newline) {
loc.col++;
} else {
loc.col = 0;
loc.line++;
}
}
} // namespace reader
} // namespace serene

View File

@ -22,7 +22,7 @@
* SOFTWARE.
*/
#include "serene/reader.hpp"
#include "serene/reader/reader.hpp"
#include "serene/error.hpp"
#include "serene/list.hpp"
#include "serene/symbol.hpp"
@ -37,18 +37,31 @@
using namespace std;
namespace serene {
namespace reader {
Reader::Reader(const string input) { this->setInput(input); };
/**
* Set the input of the reader.
* @param input Set the input to the given string
*/
void Reader::setInput(const string input) {
input_stream.write(input.c_str(), input.size());
};
Reader::~Reader() { READER_LOG("Destroying the reader"); }
/**
* Return the next character in the buffer.
* @param skip_whitespace If true it will skip whitespaces and EOL chars
*/
char Reader::get_char(bool skip_whitespace) {
for (;;) {
char c = input_stream.get();
inc_location(current_location, c == '\n');
if (skip_whitespace == true && isspace(c)) {
continue;
} else {
return c;
@ -205,5 +218,5 @@ FileReader::~FileReader() {
delete this->reader;
READER_LOG("Destroying the file reader");
}
} // namespace reader
} // namespace serene

View File

@ -23,5 +23,5 @@
*/
#include "serene/serene.hpp"
#include "serene/reader.hpp"
#include "serene/reader/reader.hpp"
#include <iostream>

View File

@ -28,11 +28,5 @@
#include "serene/sir/sir.hpp"
namespace serene {
namespace sir {
void ValueOp::build(::mlir::OpBuilder &odsBuilder,
::mlir::OperationState &odsState, int value) {
ValueOp::build(odsBuilder, odsState, odsBuilder.getI64Type(),
(uint64_t)value);
}
} // namespace sir
namespace sir {} // namespace sir
} // namespace serene

View File

@ -13,7 +13,7 @@ add_executable(tests serenetests.cpp)
target_compile_features(tests PRIVATE cxx_std_20)
# Should be linked to the main library, as well as the Catch2 testing library
target_link_libraries(tests PRIVATE lserene Catch2::Catch2)
target_link_libraries(tests PRIVATE serene Catch2::Catch2)
# If you register a test, then ctest and make test will run it.
# You can also run examples and check the output, as well.