Add the first draft of a very basic reader, list and symbol impl

This commit is contained in:
Sameer Rahmani 2020-07-16 15:48:06 +01:00
parent edc9a3e52d
commit cbea0c8e28
22 changed files with 671 additions and 30 deletions

2
.ccls Normal file
View File

@ -0,0 +1,2 @@
%compile_commands.json
-Iinclude

3
.gitignore vendored
View File

@ -11,4 +11,5 @@ cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
_deps
.ccls-cache

View File

@ -6,6 +6,7 @@ project(Serene
DESCRIPTION "Serene language is a modern Lisp."
LANGUAGES CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
# Only do these if this is the main project, and not if it is included through add_subdirectory
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
@ -14,8 +15,9 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
# set(SRC_DIR ${CMAKE_SOURCE_DIR}/src)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(SRC_DIR ${CMAKE_SOURCE_DIR}/src)
set(BIN_DIR ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror")
set(CMAKE_CXX_CLANG_TIDY clang-tidy-10)
# Let's ensure -std=c++xx instead of -std=g++xx
@ -63,12 +65,12 @@ add_definitions(${LLVM_DEFINITIONS})
llvm_map_components_to_libnames(llvm_libs support core irreader)
# Formatting library
# FetchContent_Declare(
# fmtlib
# GIT_REPOSITORY https://github.com/fmtlib/fmt.git
# GIT_TAG 5.3.0
# )
# FetchContent_MakeAvailable(fmtlib)
FetchContent_Declare(
fmtlib
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 7.0.1
)
FetchContent_MakeAvailable(fmtlib)
# The compiled library code is here

19
LICENSE Normal file
View File

@ -0,0 +1,19 @@
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.

View File

@ -2,9 +2,9 @@ add_executable(serene serene.cpp)
target_compile_features(serene PRIVATE cxx_std_20)
target_link_libraries(serene PRIVATE
libserene
lserene
${llvm_libs}
#fmt::fmt
fmt::fmt
)
target_include_directories(serene SYSTEM PRIVATE $ENV{INCLUDE})

View File

@ -1,3 +1,47 @@
int main() {
/**
* 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 <iostream>
#include "serene/reader.hpp"
#include "serene/serene.hpp"
using namespace std;
using namespace serene;
int main(int argc, char *argv[]) {
UNUSED(argc);
cout << "Serene >>" << endl;
char *input_file = argv[1];
Reader *r = new Reader(input_file);
ast_tree &ast = r->read();
for(const ast_node& x : ast) {
cout << x->string_repr() << " >> ";
}
delete r;
cout << "\nEND<<" << endl;
return 0;
}

View File

@ -61,7 +61,8 @@ case "$command" in
sudo apt update
sudo apt install -y llvm-10 llvm-10-tools \
clang-10 clang-format-10 clang-tidy-10 \
clang-tools-10 valgrind cmake ninja-build
clang-tools-10 valgrind cmake ninja-build \
doxygen
;;
"build")
clean
@ -74,7 +75,7 @@ case "$command" in
build
;;
"compile")
compilep
compile
;;
"run")
run "$@"

View File

@ -5,6 +5,9 @@
- ninja
- clang
- clang-tidy
* TODOs
** TODO Add support for release builds with NODEBUG macro :: https://www.cplusplus.com/reference/cassert/assert/
* Parser
First of all you need to read [[https://tomassetti.me/guide-parsing-algorithms-terminology/][All you need to know about Parser algorithms]].
Then here is the list or parsers that we have considered
@ -20,8 +23,6 @@ Then here is the list or parsers that we have considered
- https://en.wikipedia.org/wiki/Collision_attack
* Resources
** Rust
- The Rust book :: https://doc.rust-lang.org/book/ https://www.reddit.com/r/rust/comments/2s1zj2/the_rust_programming_language_book_as_epub/
** LLVM
- Brief overview of LLVM :: https://www.infoworld.com/article/3247799/what-is-llvm-the-power-behind-swift-rust-clang-and-more.html
- A bit in depth details on LLVM :: https://aosabook.org/en/llvm.html

3
include/config.h.in Normal file
View File

@ -0,0 +1,3 @@
// the configured options and settings for Tutorial
#define SERENE_VERSION_MAJOR @Serene_VERSION_MAJOR@
#define SERENE_VERSION_MINOR @Serene_VERSION_MINOR@

42
include/serene/expr.hpp Normal file
View File

@ -0,0 +1,42 @@
/**
* 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 EXPR_H
#define EXPR_H
#include <string>
#include "serene/llvm/IR/Value.h"
namespace serene {
class AExpr {
public:
virtual std::string string_repr() = 0;
virtual ~AExpr() = default;
};
typedef std::unique_ptr<AExpr> ast_node;
typedef std::vector<ast_node> ast_tree;
}
#endif

62
include/serene/list.hpp Normal file
View File

@ -0,0 +1,62 @@
/**
* 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 LIST_H
#define LIST_H
#include <string>
#include "serene/expr.hpp"
#include "serene/llvm/IR/Value.h"
namespace serene {
class List: public AExpr {
public:
ast_node first;
std::unique_ptr<List> rest;
std::size_t len;
List(): first(nullptr), rest(nullptr), len(0) {};
List(ast_node f, std::unique_ptr<List> r): first(std::move(f)),
rest(std::move(r)),
len(r ? r->length() + 1 : 0)
{};
List(ast_tree list);
std::string string_repr();
std::size_t length();
std::unique_ptr<List> cons(ast_node f);
static std::unique_ptr<List> to_list(ast_tree lst);
~List();
};
typedef std::unique_ptr<List> ast_list_node;
}
#endif

View File

@ -0,0 +1,7 @@
#ifndef SERENE_LLVM_VALUE_H
#define SERENE_LLVM_VALUE_H
#pragma clang diagnostic ignored "-Wunused-parameter"
#include <llvm/IR/Value.h>
#endif

76
include/serene/reader.hpp Normal file
View File

@ -0,0 +1,76 @@
/**
* 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 READER_H
#define READER_H
#include <string>
#include <sstream>
#include <memory>
#include <vector>
#include <stdexcept>
#include <fmt/core.h>
#include "serene/expr.hpp"
#include "serene/list.hpp"
#include "serene/symbol.hpp"
#include "serene/serene.hpp"
#define ENABLE_READER_LOG true
#define READER_LOG(...) if(ENABLE_READER_LOG) { fmt::print(__VA_ARGS__); }
namespace serene {
class ReadError: public std::exception {
private:
char *message;
public:
ReadError(char *msg): message(msg) {};
const char* what() const throw() {
return message;
}
};
class Reader {
private:
std::stringstream input_stream;
char get_char(const bool skip_whitespace);
void unget_char();
int is_valid_for_identifier(char c);
// The property to store the ast tree
ast_tree ast;
ast_node read_symbol();
ast_list_node read_list();
ast_node read_expr();
public:
Reader(const std::string &input);
ast_tree &read();
};
}
#endif

View File

@ -1,5 +1,36 @@
/**
* 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 SERENE_H
#define SERENE_H
// Sometimes we need this to make both analyzer happy
// and the fn signature right.
#define UNUSED(x) (void)(x)
namespace serene {
}
#endif

45
include/serene/symbol.hpp Normal file
View File

@ -0,0 +1,45 @@
/**
* 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 SYMBOL_H
#define SYMBOL_H
#include <string>
#include "serene/expr.hpp"
#include "serene/llvm/IR/Value.h"
namespace serene {
class Symbol: public AExpr {
private:
std::string name;
public:
Symbol(const std::string &name): name(name) {};
std::string string_repr();
~Symbol();
};
}
#endif

View File

@ -1,20 +1,24 @@
# Note that headers are optional, and do not affect add_library, but they will not
# show up in IDEs unless they are listed in add_library.
# Optionally glob, but only for CMake 3.12 or later:
# file(GLOB HEADER_LIST CONFIGURE_DEPENDS "${ModernCMakeExample_SOURCE_DIR}/include/modern/*.hpp")
set(HEADER_LIST "${INCLUDE_DIR}/serene/serene.hpp")
file(GLOB HEADER_LIST CONFIGURE_DEPENDS "${INCLUDE_DIR}/**/*.hpp")
file(GLOB SOURCES_LIST CONFIGURE_DEPENDS "${SOURCE_DIR}/**/*.cpp")
#set(HEADER_LIST "${INCLUDE_DIR}/serene/serene.hpp")
# Make an automatic library - will be static or dynamic based on user setting
add_library(libserene libserene.cpp ${HEADER_LIST})
add_library(lserene
serene.cpp
reader.cpp
symbol.cpp
list.cpp
${SOURCES_LIST}
${HEADER_LIST})
# We need this directory, and users of our library will need it too
target_include_directories(libserene PUBLIC "${INCLUDE_DIR}")
target_compile_features(libserene PUBLIC cxx_std_20)
target_include_directories(lserene PUBLIC "${INCLUDE_DIR}")
target_compile_features(lserene PUBLIC cxx_std_20)
# This depends on (header only) boost
target_link_libraries(libserene ${llvm_libs})
target_link_libraries(lserene ${llvm_libs} fmt::fmt)
source_group(TREE "${INCLUDE_DIR}" PREFIX "Header Files" FILES ${HEADER_LIST})

View File

@ -1,3 +0,0 @@
int read() {
return 0;
}

63
src/list.cpp Normal file
View File

@ -0,0 +1,63 @@
/**
* 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 <string>
#include <fmt/core.h>
#include "serene/llvm/IR/Value.h"
#include "serene/expr.hpp"
#include "serene/list.hpp"
using namespace std;
namespace serene {
ast_list_node List::to_list(ast_tree lst) {
auto l = make_unique<List>();
// Using a for loop with iterator
for(auto node = rbegin(lst); node != rend(lst); ++node) {
l = l->cons(move(*node));
}
return l;
}
ast_list_node List::cons(ast_node f) {
return make_unique<List>(move(f), unique_ptr<List>(move(*this)));
}
string List::string_repr() {
return fmt::format("<List: '{}'>", first->string_repr());
};
size_t List::length() {
if (this->len) {
return this->len;
}
return 0;
}
List::~List() {};
}

175
src/reader.cpp Normal file
View File

@ -0,0 +1,175 @@
/**
* 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 <string>
#include <iostream>
#include <memory>
#include <assert.h>
#include "serene/reader.hpp"
#include "serene/symbol.hpp"
#include "serene/list.hpp"
using namespace std;
namespace serene {
Reader::Reader(const string &input) {
input_stream.write(input.c_str(), input.size());
};
char Reader::get_char(const bool skip_whitespace) {
for(;;) {
char c = input_stream.get();
if (skip_whitespace == true && isspace(c)) {
continue;
} else {
return c;
}
}
};
void Reader::unget_char() {
input_stream.unget();
};
int Reader::is_valid_for_identifier(char c) {
READER_LOG("IS: {}\n", c);
switch(c) {
case '!'
| '$'
| '%'
| '&'
| '*'
| '+'
| '-'
| '.'
| '~'
| '/'
| ':'
| '<'
| '='
| '>'
| '?'
| '@'
| '^'
| '_':
READER_LOG("#######\n");
return 1;
}
if((c >= 'a' && c <='z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9')) {
READER_LOG("#######@@@@2\n");
return 1;
}
READER_LOG("#####111111111111112\n");
return 0;
}
ast_node Reader::read_symbol() {
bool empty = true;
char c = get_char(false);
READER_LOG("Read symbol\n");
if(!this->is_valid_for_identifier(c)) {
// TODO: Fix this when we implement the error handling
throw ReadError((char *)"Invalid character at the start of a symbol");
}
string sym("");
while(c != EOF && (!(isspace(c)) && this->is_valid_for_identifier(c))) {
READER_LOG("ABC: {}\n", c);
sym += c;
c = get_char(false);
empty = false;
}
READER_LOG("44444444444444\n");
if (!empty) {
READER_LOG("<<<<\n");
unget_char();
return make_unique<Symbol>(sym);
}
READER_LOG(">>>>>>>>>>>>\n");
return nullptr;
};
ast_list_node Reader::read_list() {
char c = get_char(true);
assert(c == '(');
bool list_terminated = false;
ast_tree lst;
do {
char c = get_char(true);
unget_char();
switch(c) {
case EOF:
throw ReadError((char *)"EOF reached before closing of list");
case ')':
list_terminated = true;
break;
default:
lst.push_back(read_expr());
}
} while(!list_terminated);
return List::to_list(move(lst));
}
ast_node Reader::read_expr() {
char c = get_char(false);
READER_LOG("CHAR: {}\n", c);
unget_char();
switch(c) {
case '(':
return read_list();
case EOF:
return nullptr;
default:
return read_symbol();
}
}
ast_tree &Reader::read() {
char c = get_char(true);
while(c != EOF) {
unget_char();
this->ast.push_back(read_expr());
c = get_char(true);
}
return this->ast;
};
}

27
src/serene.cpp Normal file
View File

@ -0,0 +1,27 @@
/**
* 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 <iostream>
#include "serene/reader.hpp"
#include "serene/serene.hpp"

39
src/symbol.cpp Normal file
View File

@ -0,0 +1,39 @@
/**
* 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 <string>
#include <fmt/core.h>
#include "serene/llvm/IR/Value.h"
#include "serene/expr.hpp"
#include "serene/symbol.hpp"
using namespace std;
namespace serene {
string Symbol::string_repr() {
return fmt::format("<Symbol: '{}'>", name);
};
Symbol::~Symbol() {};
}

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 libserene Catch2::Catch2)
target_link_libraries(tests PRIVATE lserene 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.