diff --git a/bin/CMakeLists.txt b/bin/CMakeLists.txt index 3dde7a4..5b88c5d 100644 --- a/bin/CMakeLists.txt +++ b/bin/CMakeLists.txt @@ -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) diff --git a/bin/serene.cpp b/bin/serene.cpp index 7f8de4d..9b8ddbf 100644 --- a/bin/serene.cpp +++ b/bin/serene.cpp @@ -23,7 +23,7 @@ */ #include "serene/serene.hpp" -#include "serene/reader.hpp" +#include "serene/reader/reader.hpp" #include "serene/sir/sir.hpp" #include #include @@ -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; diff --git a/builder b/builder index 0f7fb35..6304aa7 100755 --- a/builder +++ b/builder @@ -47,7 +47,7 @@ function clean() { function run() { pushed_build - $BUILD_DIR/bin/serene "$@" + $BUILD_DIR/bin/serenec "$@" popd_build } diff --git a/include/serene/reader/location.hpp b/include/serene/reader/location.hpp new file mode 100644 index 0000000..c51dc2a --- /dev/null +++ b/include/serene/reader/location.hpp @@ -0,0 +1,48 @@ +/** + * Serene programming language. + * + * Copyright (c) 2020 Sameer Rahmani + * + * 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 diff --git a/include/serene/reader.hpp b/include/serene/reader/reader.hpp similarity index 95% rename from include/serene/reader.hpp rename to include/serene/reader/reader.hpp index 14a61cb..fd03719 100644 --- a/include/serene/reader.hpp +++ b/include/serene/reader/reader.hpp @@ -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 diff --git a/include/serene/sir/dialect.td b/include/serene/sir/dialect.td index 58e9810..bb45e19 100644 --- a/include/serene/sir/dialect.td +++ b/include/serene/sir/dialect.td @@ -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); + }]>, ]; } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 63a68a3..7ffab84 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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}) diff --git a/src/reader/location.cpp b/src/reader/location.cpp new file mode 100644 index 0000000..ae38d16 --- /dev/null +++ b/src/reader/location.cpp @@ -0,0 +1,48 @@ +/** + * Serene programming language. + * + * Copyright (c) 2020 Sameer Rahmani + * + * 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 diff --git a/src/reader.cpp b/src/reader/reader.cpp similarity index 93% rename from src/reader.cpp rename to src/reader/reader.cpp index 08ea7fd..fa8f54e 100644 --- a/src/reader.cpp +++ b/src/reader/reader.cpp @@ -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 diff --git a/src/serene.cpp b/src/serene.cpp index 903ebed..196c3d8 100644 --- a/src/serene.cpp +++ b/src/serene.cpp @@ -23,5 +23,5 @@ */ #include "serene/serene.hpp" -#include "serene/reader.hpp" +#include "serene/reader/reader.hpp" #include diff --git a/src/sir/value_op.cpp b/src/sir/value_op.cpp index 4721cd8..50249b6 100644 --- a/src/sir/value_op.cpp +++ b/src/sir/value_op.cpp @@ -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 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6442977..e82ba0f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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.