Merge branch 'mlir'

This commit is contained in:
Sameer Rahmani 2021-04-13 12:03:27 +01:00
commit 202f041515
72 changed files with 9280 additions and 354 deletions

13
.clang-format Normal file
View File

@ -0,0 +1,13 @@
---
# Global Options Go Here
IndentWidth: 2
ColumnLimit: 100
---
Language: Cpp
BasedOnStyle: LLVM
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlignConsecutiveMacros: true
ForEachMacros: []
IndentWidth: 2
...

35
.gitignore vendored
View File

@ -1,12 +1,25 @@
# will have compiled files and executables
debug/
target/
# These are backup files generated by rustfmt
**/*.rs.bk
\#*#
.\#*
/target
*~
bootstrap/serene
site/public/
/build
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
.ccls-cache
.clangd
.cache/
.vscode/
CMakeDoxyfile.in
CMakeDoxygenDefaults.cmake
DartConfiguration.tcl
bin/serenec_CXX_cotire.cmake
/config.h
docs/Doxyfile.docs

132
CMakeLists.txt Normal file
View File

@ -0,0 +1,132 @@
cmake_minimum_required(VERSION 3.16)
# Project name and a few useful settings. Other commands can pick up the results
project(Serene
VERSION 0.1.0
DESCRIPTION "Serene language is a modern Lisp."
LANGUAGES CXX C)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
option(CPP_20_SUPPORT "C++20 Support" OFF)
# 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)
## Settings -----------------------------------------
# specify the C++ standard
if (CPP_20_SUPPORT)
set(CMAKE_CXX_STANDARD 20)
else()
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
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)
# Let's ensure -std=c++xx instead of -std=g++xx
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS_DEBUG
"${CMAKE_CXX_FLAGS_DEBUG} -g -fno-omit-frame-pointer -fsanitize=address")
set(CMAKE_LINKER_FLAGS_DEBUG
"${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set(CMAKE_CXX_FLAGS_RELEASE
"${CMAKE_CXX_FLAGS_RELEASE} -O3")
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
set(MemoryCheckCommand "valgrind")
add_compile_options(-fno-rtti)
configure_file(${INCLUDE_DIR}/config.h.in config.h)
# Let's nicely support folders in IDEs
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
## Options ------------------------------------------
option(ENABLE_LOG "Enable logging" OFF)
option(ENABLE_EXPR_LOG "Enable AExpr logging" OFF)
option(ENABLE_READER_LOG "Enable reader logging" OFF)
option(BUILD_TESTING "Enable tests" OFF)
include(cotire)
include(FetchContent)
find_package(LLVM REQUIRED CONFIG)
find_package(MLIR REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR})
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
include(TableGen)
include(AddLLVM)
include(AddMLIR)
include(HandleLLVMOptions)
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${MLIR_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_BINARY_DIR}/include)
link_directories(${LLVM_BUILD_LIBRARY_DIR})
add_definitions(${LLVM_DEFINITIONS})
include_directories(${LLVM_INCLUDE_DIRS})
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 7.0.1
)
FetchContent_MakeAvailable(fmtlib)
# The compiled library code is here
add_subdirectory(src)
# The executable code is here
add_subdirectory(bin)
add_subdirectory(include)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include/)
# Testing only available if this is the main app
# Emergency override SERENE_CMAKE_BUILD_TESTING provided as well
if(BUILD_TESTING)
message("Build the test binary")
add_subdirectory(tests)
endif()
if (CMAKE_BUILD_TYPE STREQUAL "Release")
# Docs only available if this is the main app
find_package(Doxygen
REQUIRED dot
OPTIONAL_COMPONENTS dia)
if(Doxygen_FOUND)
add_subdirectory(docs)
else()
message(STATUS "Doxygen not found, not building docs")
endif()
endif()
endif()

352
LICENSE
View File

@ -1,339 +1,19 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
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:
Preamble
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.
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

@ -1,9 +1,72 @@
#+OPTIONS: num:t toc:nil
* Serene language
* Serene lang
#+TOC: headlines 2
** Setup development environment
Setup the githook and install dependencies using the following commands:
** Where to get help
- ~IRC~: ~#5hit~ on the freenode server
- ~Gitter~: [[https://gitter.im/Serene-lang/serene]]
- ~MailingList~: https://www.freelists.org/list/serene
#+BEGIN_SRC bash
./builder setup
#+END_SRC
*** Dependencies
You would need the following dependencies to start get started with *Serene* development
- LLVM ( LLVM Instructions coming up.)
- cmake
- ninja
- doxygen (If you want to build the docs as well)
- Valgrind
- CCache (If you want faster builds specially with the LLVM)
** LLVM Installation
MLIR is a part of the [[https://llvm.org][LLVM]] project and in order to build it we need to build the LLVM itself as well.
Here is a quick guide to build the latest version of the LLVM and MLIR.
#+BEGIN_SRC bash
## YES we're using the development version of MLIR
git clone https://github.com/llvm/llvm-project.git
mkdir llvm-project/build
cd llvm-project/build
cmake -G Ninja ../llvm \
-DCMAKE_INSTALL_PREFIX=/your/target/path \
-DLLVM_PARALLEL_COMPILE_JOBS=7 \
-DLLVM_PARALLEL_LINK_JOBS=1 \
-DLLVM_BUILD_EXAMPLES=ON \
-DLLVM_TARGETS_TO_BUILD="X86;NVPTX;AMDGPU" \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_CCACHE_BUILD=ON \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DLLVM_ENABLE_PROJECTS='clang;lldb;lld;mlir;clang-tools-extra;compiler-rt' \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DLLVM_ENABLE_LLD=ON
cmake --build . --target check-mlir
cmake -DCMAKE_INSTALL_PREFIX=/your/target/location -P cmake_install.cmake
#+END_SRC
You need to have =clang= and =lld= installed to compile the LLVM with the above command. Also if you
are not using =ccache= just remove the option related to it from the above command.
*** Emacs
If you're using Emacs as your development environment just install =clangd= and =lsp=.
* How to build
In order to build for development (Debug mode) just use =./builder build= to setup the build and build
the project once and then you can just use =./builder compile= to build the changed files only.
Check out the =builder= script for more subcommands and details.
* Get Help
If you need help or you just want to hangout, you can find us at:
- *IRC*: *#serene-lang* on the freenode server
- *Matrix Network*: https://matrix.to/#/#serene:matrix.org?via=matrix.org&via=gitter.im
- *MailingList*: https://www.freelists.org/list/serene

31
bin/CMakeLists.txt Normal file
View File

@ -0,0 +1,31 @@
add_executable(serenec serene.cpp)
# Make sure to generate files related to the dialects first
add_dependencies(serenec SereneDialectGen)
if (CPP_20_SUPPORT)
target_compile_features(serenec PRIVATE cxx_std_20)
else()
target_compile_features(serenec PRIVATE cxx_std_17)
endif()
target_link_libraries(serenec PRIVATE
serene
${llvm_libs}
fmt::fmt
MLIRAnalysis
MLIRIR
MLIRParser
MLIRSideEffectInterfaces
MLIRTransforms
)
target_include_directories(serene SYSTEM PRIVATE $ENV{INCLUDE})
target_include_directories(serene PRIVATE ${INCLUDE_DIR})
install(TARGETS serenec DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include)
cotire(serenec)

78
bin/serene.cpp Normal file
View File

@ -0,0 +1,78 @@
/**
* 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/serene.h"
#include "serene/reader/reader.h"
#include "serene/sir/sir.hpp"
#include <iostream>
#include <llvm/Support/CommandLine.h>
using namespace std;
using namespace serene;
namespace cl = llvm::cl;
namespace {
enum Action { None, DumpAST, DumpIR };
}
static cl::opt<std::string> inputFile(cl::Positional,
cl::desc("The Serene file to compile"),
cl::init("-"),
cl::value_desc("filename"));
static cl::opt<enum Action>
emitAction("emit", cl::desc("Select what to dump."),
cl::values(clEnumValN(DumpIR, "sir", "Output the SLIR only")),
cl::values(clEnumValN(DumpAST, "ast", "Output the AST only")));
int main(int argc, char *argv[]) {
cl::ParseCommandLineOptions(argc, argv, "Serene compiler \n");
switch (emitAction) {
case Action::DumpAST: {
reader::FileReader *r = new reader::FileReader(inputFile);
r->toString();
delete r;
return 0;
}
case Action::DumpIR: {
reader::FileReader *r = new reader::FileReader(inputFile);
auto ast = r->read();
if (!ast) {
throw ast.takeError();
}
serene::sir::dumpSIR(*ast);
delete r;
return 0;
}
default: {
llvm::errs() << "No action specified. TODO: Print out help here";
}
}
return 1;
}

169
builder Executable file
View File

@ -0,0 +1,169 @@
#! /bin/bash
command=$1
export CCC_CC=clang
export CCC_CXX=clang++
export CC=clang
export CXX=clang++
ROOT_DIR=`pwd`
BUILD_DIR=$ROOT_DIR/build
scanbuild=scan-build-11
function pushed_build() {
pushd $BUILD_DIR > /dev/null
}
function popd_build() {
popd > /dev/null
}
function compile() {
pushed_build
ninja
popd_build
}
function build() {
pushed_build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug "$@" $ROOT_DIR
ninja -j `nproc`
popd_build
}
function build-20() {
pushed_build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCPP_20_SUPPORT=ON "$@" $ROOT_DIR
ninja -j `nproc`
popd_build
}
function build-release() {
pushed_build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release $ROOT_DIR
ninja -j `nproc`
popd_build
}
function build-docs() {
pushed_build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Docs $ROOT_DIR
ninja -j `nproc`
popd_build
}
function clean() {
rm -rf $BUILD_DIR
}
function run() {
pushed_build
$BUILD_DIR/bin/serenec "$@"
popd_build
}
function memcheck() {
pushed_build
ctest -T memcheck
popd_build
}
function run-tests() {
$BUILD_DIR/tests/tests
}
function tests() {
pushed_build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON $ROOT_DIR
ninja -j `nproc`
popd_build
}
case "$command" in
"setup")
pushd ./scripts
./git-pre-commit-format install
popd
;;
"build")
clean
mkdir -p $BUILD_DIR
build "${@:2}"
;;
"build-20")
clean
mkdir -p $BUILD_DIR
build-20 "${@:2}"
;;
"build-docs")
clean
mkdir -p $BUILD_DIR
build-docs "${@:2}"
;;
"build-release")
clean
mkdir -p $BUILD_DIR
build-release "${@:2}"
;;
"compile")
compile
;;
"compile-and-test")
compile
run-tests
;;
"run")
run "${@:2}"
;;
"run-tests")
run-tests "${@:2}"
;;
"scan-build")
clean
mkdir -p $BUILD_DIR
pushed_build
exec $scanbuild cmake $ROOT_DIR
exec $scanbuild scan-build make -j 4
popd_build
;;
"memcheck")
memcheck
;;
"tests")
clean
mkdir -p $BUILD_DIR
tests
run-tests
;;
"clean")
rm -rf $BUILD_DIR
;;
"full-build")
clean
mkdir -p $BUILD_DIR
build
tests
run-tests
memcheck
;;
*)
echo "Commands: "
echo "full-build - Build and test Serene."
echo "build - Build Serene from scratch in DEBUG mode."
echo "build-release - Build Serene from scratch in RELEASE mode."
echo "compile - reCompiles the project using the already exist cmake configuration"
echo "compile-and-tests - reCompiles the project using the already exist cmake configuration and runs the tests"
echo "run - Runs the serene executable"
echo "scan-build - Compiles serene with static analyzer"
echo "tests - Runs the test cases"
echo "memcheck - Runs the memcheck tool."
echo "clean - :D"
;;
esac

4212
cmake/cotire.cmake Normal file

File diff suppressed because it is too large Load Diff

16
docs/CMakeLists.txt Normal file
View File

@ -0,0 +1,16 @@
set(DOXYGEN_GENERATE_HTML YES)
set(DOXYGEN_GENERATE_MAN YES)
set(DOXYGEN_EXTRACT_ALL YES)
set(DOXYGEN_BUILTIN_STL_SUPPORT YES)
set(DOXYGEN_PROJECT_BRIEF "Just another Lisp")
message("<<<<<<<<<<<<<<<<<<<<<<<<<<<")
message(${PROJECT_SOURCE_DIR})
message(${INCLUDE_DIR})
doxygen_add_docs(docs
#"${CMAKE_CURRENT_SOURCE_DIR}/index.md"
"${PROJECT_SOURCE_DIR}/src/"
${INCLUDE_DIR})
add_dependencies(serenec docs)

View File

@ -0,0 +1,4 @@
(print 4)
(print -43)
(print 3.4)
(println asd)

1
include/CMakeLists.txt Normal file
View File

@ -0,0 +1 @@
add_subdirectory("serene/sir/")

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

@ -0,0 +1,11 @@
#ifndef CONFIG_H
#define CONFIG_H
// the configured options and settings for Tutorial
#define SERENE_VERSION_MAJOR @Serene_VERSION_MAJOR@
#define SERENE_VERSION_MINOR @Serene_VERSION_MINOR@
#cmakedefine ENABLE_READER_LOG
#cmakedefine ENABLE_EXPR_LOG
#cmakedefine ENABLE_LOG
#endif

49
include/serene/error.hpp Normal file
View File

@ -0,0 +1,49 @@
/**
* 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 ERROR_H
#define ERROR_H
#include "serene/expr.hpp"
#include "serene/llvm/IR/Value.h"
#include "serene/state.hpp"
#include <string>
namespace serene {
class Error : public AExpr {
const std::string msg;
public:
Error(const std::string &msg) : msg(msg) {}
virtual ~Error();
const std::string &message() const;
SereneType getType() const override { return SereneType::Error; }
std::string string_repr() const override;
std::string dumpAST() const override;
};
} // namespace serene
#endif

31
include/serene/errors.h Normal file
View File

@ -0,0 +1,31 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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_ERRORS_H
#define SERENE_ERRORS_H
#include "serene/errors/errc.h"
#include "serene/errors/error.h"
#endif

View File

@ -0,0 +1,83 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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_ERRORS_ERRC_H
#define SERENE_ERRORS_ERRC_H
#include "llvm/Support/Errc.h"
namespace serene {
/// A collection of common error codes in Serene
enum class errc {
argument_list_too_long = int(std::errc::argument_list_too_long),
argument_out_of_domain = int(std::errc::argument_out_of_domain),
bad_address = int(std::errc::bad_address),
bad_file_descriptor = int(std::errc::bad_file_descriptor),
broken_pipe = int(std::errc::broken_pipe),
device_or_resource_busy = int(std::errc::device_or_resource_busy),
directory_not_empty = int(std::errc::directory_not_empty),
executable_format_error = int(std::errc::executable_format_error),
file_exists = int(std::errc::file_exists),
file_too_large = int(std::errc::file_too_large),
filename_too_long = int(std::errc::filename_too_long),
function_not_supported = int(std::errc::function_not_supported),
illegal_byte_sequence = int(std::errc::illegal_byte_sequence),
inappropriate_io_control_operation =
int(std::errc::inappropriate_io_control_operation),
interrupted = int(std::errc::interrupted),
invalid_argument = int(std::errc::invalid_argument),
invalid_seek = int(std::errc::invalid_seek),
io_error = int(std::errc::io_error),
is_a_directory = int(std::errc::is_a_directory),
no_child_process = int(std::errc::no_child_process),
no_lock_available = int(std::errc::no_lock_available),
no_space_on_device = int(std::errc::no_space_on_device),
no_such_device_or_address = int(std::errc::no_such_device_or_address),
no_such_device = int(std::errc::no_such_device),
no_such_file_or_directory = int(std::errc::no_such_file_or_directory),
no_such_process = int(std::errc::no_such_process),
not_a_directory = int(std::errc::not_a_directory),
not_enough_memory = int(std::errc::not_enough_memory),
not_supported = int(std::errc::not_supported),
operation_not_permitted = int(std::errc::operation_not_permitted),
permission_denied = int(std::errc::permission_denied),
read_only_file_system = int(std::errc::read_only_file_system),
resource_deadlock_would_occur = int(std::errc::resource_deadlock_would_occur),
resource_unavailable_try_again =
int(std::errc::resource_unavailable_try_again),
result_out_of_range = int(std::errc::result_out_of_range),
too_many_files_open_in_system = int(std::errc::too_many_files_open_in_system),
too_many_files_open = int(std::errc::too_many_files_open),
too_many_links = int(std::errc::too_many_links)
};
/// The **official way** to create `std::error_code` in context of Serene.
inline std::error_code make_error_code(errc E) {
return std::error_code(static_cast<int>(E), std::generic_category());
};
}; // namespace serene
#endif

View File

@ -0,0 +1,30 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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_ERRORS_ERROR_H
#define SERENE_ERRORS_ERROR_H
#include "llvm/Support/Error.h"
#endif

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

@ -0,0 +1,64 @@
/**
* 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 "serene/logger.hpp"
#include "serene/reader/location.h"
#include <string>
#if defined(ENABLE_LOG) || defined(ENABLE_EXPR_LOG)
#define EXPR_LOG(...) __LOG("EXPR", __VA_ARGS__);
#else
#define EXPR_LOG(...) ;
#endif
namespace serene {
enum class SereneType {
Expression,
Symbol,
List,
Error,
Number,
};
class AExpr {
public:
std::unique_ptr<reader::LocationRange> location;
virtual ~AExpr() = default;
virtual SereneType getType() const = 0;
virtual std::string string_repr() const = 0;
virtual std::string dumpAST() const = 0;
};
using ast_node = std::shared_ptr<AExpr>;
using ast_tree = std::vector<ast_node>;
} // namespace serene
#endif

View File

@ -0,0 +1,106 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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 EXPRS_EXPRESSION_H
#define EXPRS_EXPRESSION_H
#include "serene/reader/location.h"
#include "serene/utils.h"
#include <memory>
namespace serene {
namespace reader {
class SemanticContext;
}
/// Contains all the builtin AST expressions including those which do not appear
/// in the syntax directly. Like function definitions.
namespace exprs {
/// This enum represent the expression type and **not** the value type.
enum class ExprType {
Symbol,
List,
Number,
};
/// The base class of the expressions which provides the common interface for
/// the expressions to implement.
class Expression {
public:
/// The location range provide information regarding to where in the input
/// string the current expression is used.
reader::LocationRange location;
Expression(const reader::LocationRange &loc) : location(loc){};
virtual ~Expression() = default;
/// Returns the type of the expression. We need this funciton to perform
/// dynamic casting of expression object to implementations such as lisp or
/// symbol.
virtual ExprType getType() const = 0;
/// The AST representation of an expression
virtual std::string toString() const = 0;
virtual Result<Expression *> analyze(reader::SemanticContext &) = 0;
};
using node = std::shared_ptr<Expression>;
using ast = llvm::SmallVector<node, 0>;
/// Create a new `node` of type `T` and forwards any given parameter
/// to the constructor of type `T`. This is the **official way** to create
/// a new `Expression`. Here is an example:
/// \code
/// auto list = make<List>();
/// \endcode
///
/// \param[args] Any argument with any type passed to this function will be
/// passed to the constructor of type T.
/// \return A shared pointer to an Expression
template <typename T, typename... Args> node make(Args &&...args) {
return std::shared_ptr<T>(new T(std::forward<Args>(args)...));
};
/// Create a new `node` of type `T` and forwards any given parameter
/// to the constructor of type `T`. This is the **official way** to create
/// a new `Expression`. Here is an example:
/// \code
/// auto list = make<List>();
/// \endcode
///
/// \param[args] Any argument with any type passed to this function will be
/// passed to the constructor of type T.
/// \return A shared pointer to a value of type T.
template <typename T, typename... Args>
std::shared_ptr<T> makeAndCast(Args &&...args) {
return std::shared_ptr<T>(new T(std::forward<Args>(args)...));
};
} // namespace exprs
} // namespace serene
#endif

View File

@ -0,0 +1,65 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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 EXPRS_LIST_H
#define EXPRS_LIST_H
#include "serene/exprs/expression.h"
#include "llvm/ADT/SmallVector.h"
#include <string>
namespace serene {
namespace exprs {
/// This class represents a List in the AST level and not the List as the data
/// type.
class List : public Expression {
public:
// Internal elements of the lest (small vector of shared pointers to
// expressions)
ast elements;
List(const List &l); // Copy ctor
List(List &&e) noexcept = default; // Move ctor
List(const reader::LocationRange &loc) : Expression(loc){};
List(const reader::LocationRange &loc, node e);
List(const reader::LocationRange &loc, llvm::ArrayRef<node> elems);
ExprType getType() const;
std::string toString() const;
void append(node);
Result<Expression *> analyze(reader::SemanticContext &);
static bool classof(const Expression *e);
~List() = default;
};
} // namespace exprs
} // namespace serene
#endif

View File

@ -0,0 +1,61 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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 EXPRS_NUMBER_H
#define EXPRS_NUMBER_H
#include "serene/exprs/expression.h"
#include "llvm/Support/FormatVariadic.h"
namespace serene {
namespace exprs {
/// This data structure represent a number. I handles float points, integers,
/// positive and negative numbers. This is not a data type representative.
/// So it won't cast to actual numeric types and it has a string container
/// to hold the parsed value.
struct Number : public Expression {
std::string value;
bool isNeg;
bool isFloat;
Number(reader::LocationRange &loc, const std::string &num, bool isNeg,
bool isFloat)
: Expression(loc), value(num), isNeg(isNeg), isFloat(isFloat){};
ExprType getType() const;
std::string toString() const;
Result<Expression *> analyze(reader::SemanticContext &ctx);
static bool classof(const Expression *e);
~Number() = default;
};
} // namespace exprs
} // namespace serene
#endif

View File

@ -0,0 +1,62 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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 EXPRS_SYMBOL_H
#define EXPRS_SYMBOL_H
#include "serene/exprs/expression.h"
#include "llvm/ADT/StringRef.h"
#include <string>
namespace serene {
namespace exprs {
/// This data structure represent the Lisp symbol. Just a symbol
/// in the context of the AST and nothing else.
class Symbol : public Expression {
// private:
// using Expression::analyze;
public:
std::string name;
Symbol(reader::LocationRange &loc, llvm::StringRef name)
: Expression(loc), name(name){};
ExprType getType() const;
std::string toString() const;
static bool classof(const Expression *e);
Result<Expression *> analyze(reader::SemanticContext &);
~Symbol() = default;
};
} // namespace exprs
} // namespace serene
#endif

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

@ -0,0 +1,69 @@
/**
* 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 "serene/expr.hpp"
#include "serene/llvm/IR/Value.h"
#include "serene/reader/location.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include <list>
#include <string>
namespace serene {
class List : public AExpr {
std::vector<ast_node> nodes_;
public:
List(){};
List(std::vector<ast_node> elements);
List(reader::Location start);
SereneType getType() const override { return SereneType::List; }
std::string dumpAST() const override;
std::string string_repr() const override;
size_t count() const;
void append(ast_node t);
std::unique_ptr<List> from(uint begin);
llvm::Optional<ast_node> at(uint index) const;
std::vector<ast_node>::const_iterator begin();
std::vector<ast_node>::const_iterator end();
llvm::ArrayRef<ast_node> asArrayRef();
static bool classof(const AExpr *);
};
std::unique_ptr<List> makeList(reader::Location);
std::unique_ptr<List> makeList(reader::Location, List *);
using ast_list_node = std::unique_ptr<List>;
} // namespace serene
#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

37
include/serene/logger.hpp Normal file
View File

@ -0,0 +1,37 @@
/**
* 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 LOGGER_H
#define LOGGER_H
#include "config.h"
#include <fmt/core.h>
// DO NOT USE this macro directly. USE module specific macro.
// Checkout `reader.cpp` for example.
#define __LOG(M, ...) \
fmt::print("[{}] <{}:{}> in '{}': {}\n", M, __FILE__, __LINE__, __func__, \
fmt::format(__VA_ARGS__));
#endif

View File

@ -0,0 +1,71 @@
/**
* 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 NAMESPACE_H
#define NAMESPACE_H
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Value.h"
#include "serene/exprs/expression.h"
#include "serene/llvm/IR/Value.h"
#include "serene/logger.hpp"
#include "llvm/ADT/DenseMap.h"
#include <llvm/IR/Module.h>
#include <string>
#define NAMESPACE_LOG(...) \
DEBUG_WITH_TYPE("NAMESPACE", llvm::dbgs() << __VA_ARGS__ << "\n");
using ScopeMap = llvm::DenseMap<llvm::StringRef, mlir::Value>;
using PairT = std::pair<llvm::StringRef, mlir::Value>;
namespace serene {
class AExpr;
class Namespace {
private:
exprs::ast tree{};
bool initialized = false;
ScopeMap rootScope;
public:
llvm::Optional<llvm::StringRef> filename;
mlir::StringRef name;
Namespace(llvm::StringRef ns_name, llvm::Optional<llvm::StringRef> filename);
exprs::ast &Tree();
mlir::LogicalResult setTree(exprs::ast &);
// TODO: Fix it to return llvm::Optional<mlir::Value> instead
llvm::Optional<mlir::Value> lookup(llvm::StringRef name);
mlir::LogicalResult insert_symbol(llvm::StringRef name, mlir::Value v);
void print_scope();
~Namespace();
};
} // namespace serene
#endif

58
include/serene/number.hpp Normal file
View File

@ -0,0 +1,58 @@
/**
* 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 NUMBER_H
#define NUMBER_H
#include "serene/expr.hpp"
#include "serene/llvm/IR/Value.h"
#include "serene/reader/location.h"
#include "serene/state.hpp"
#include <string>
namespace serene {
class Number : public AExpr {
const std::string num_;
public:
bool isNeg;
bool isFloat;
Number(reader::LocationRange loc, const std::string &, bool, bool);
~Number();
SereneType getType() const override { return SereneType::Number; }
std::string string_repr() const override;
std::string dumpAST() const override;
int64_t toI64();
static bool classof(const AExpr *);
};
std::unique_ptr<Number> makeNumber(reader::LocationRange, std::string, bool,
bool);
} // namespace serene
#endif

View File

@ -0,0 +1,64 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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_READER_ERRORS_H
#define SERENE_READER_ERRORS_H
#include "serene/errors.h"
namespace serene {
namespace reader {
class ReadError : public std::exception {
private:
char *message;
public:
ReadError(char *msg) : message(msg){};
const char *what() const throw() { return message; }
};
class MissingFileError : public llvm::ErrorInfo<MissingFileError> {
using llvm::ErrorInfo<MissingFileError>::log;
using llvm::ErrorInfo<MissingFileError>::convertToErrorCode;
public:
static char ID;
std::string path;
// TODO: Move this to an error namespace somewhere.
int file_is_missing = int();
void log(llvm::raw_ostream &os) const {
os << "File does not exist: " << path << "\n";
}
MissingFileError(llvm::StringRef path) : path(path.str()){};
std::error_code convertToErrorCode() const {
return make_error_code(errc::no_such_file_or_directory);
}
};
} // namespace reader
} // namespace serene
#endif

View File

@ -0,0 +1,63 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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
#include "mlir/IR/Location.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/OpDefinition.h"
#include <string>
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;
::std::string toString() const;
static Location unit() { return {0, 0, 0}; };
};
class LocationRange {
public:
Location start;
Location end;
LocationRange() : start(Location{0, 0, 0}), end(Location{0, 0, 0}){};
LocationRange(Location _start) : start(_start), end(_start){};
LocationRange(Location _start, Location _end) : start(_start), end(_end){};
LocationRange(const LocationRange &);
};
void inc_location(Location &, bool);
void dec_location(Location &, bool);
} // namespace reader
} // namespace serene
#endif

View File

@ -0,0 +1,101 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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 <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <system_error>
#include <vector>
#include "serene/errors.h"
#include "serene/exprs/expression.h"
#include "serene/exprs/list.h"
#include "serene/exprs/symbol.h"
#include "serene/logger.hpp"
#include "serene/reader/errors.h"
#include "serene/reader/location.h"
#include "serene/serene.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#define READER_LOG(...) \
DEBUG_WITH_TYPE("READER", llvm::dbgs() << __VA_ARGS__ << "\n");
namespace serene {
namespace reader {
/// Base reader class which reads from a string directly.
class Reader {
private:
char current_char = ';'; // Some arbitary char to begin with
std::stringstream input_stream;
Location current_location{0, 0, 0};
char getChar(bool skip_whitespace);
void ungetChar();
bool isValidForIdentifier(char c);
// The property to store the ast tree
exprs::ast ast;
exprs::node readSymbol();
exprs::node readNumber(bool);
exprs::node readList();
exprs::node readExpr();
public:
Reader() : input_stream(""){};
Reader(const llvm::StringRef string);
void setInput(const llvm::StringRef string);
llvm::Expected<exprs::ast> read();
// Dumps the AST data to stdout
void toString();
~Reader();
};
/// A reader to read the content of a file as AST
class FileReader {
std::string file;
Reader *reader;
public:
FileReader(const std::string file_name)
: file(file_name), reader(new Reader()) {}
// Dumps the AST data to stdout
void toString();
llvm::Expected<exprs::ast> read();
~FileReader();
};
} // namespace reader
} // namespace serene
#endif

View File

@ -0,0 +1,44 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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_SEMANTICS_H
#define READER_SEMANTICS_H
#include "serene/exprs/expression.h"
namespace serene::reader {
class SemanticContext {};
class Semantics {
SemanticContext context;
public:
Semantics(){};
exprs::ast analyze(exprs::ast &);
};
}; // namespace serene::reader
#endif

33
include/serene/serene.h Normal file
View File

@ -0,0 +1,33 @@
/*
* Serene programming language.
*
* Copyright (c) 2019-2021 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

View File

@ -0,0 +1,5 @@
set(LLVM_TARGET_DEFINITIONS dialect.td)
mlir_tablegen(ops.hpp.inc -gen-op-decls)
mlir_tablegen(ops.cpp.inc -gen-op-defs)
mlir_tablegen(dialect.hpp.inc -gen-dialect-decls)
add_public_tablegen_target(SereneDialectGen)

View File

@ -0,0 +1,43 @@
/**
* 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 DIALECT_H_
#define DIALECT_H_
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Dialect.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
// Include the auto-generated header file containing the declaration of the
// serene's dialect.
#include "serene/sir/dialect.hpp.inc"
// Include the auto-generated header file containing the declarations of the
// serene's operations.
// for more on GET_OP_CLASSES: https://mlir.llvm.org/docs/OpDefinitions/
#define GET_OP_CLASSES
#include "serene/sir/ops.hpp.inc"
#endif // DIALECT_H_

View File

@ -0,0 +1,104 @@
#ifndef SERENE_DIALECT
#define SERENE_DIALECT
include "mlir/IR/OpBase.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
// Dialect definition. It will directly generate the SereneDialect class
def Serene_Dialect : Dialect {
let name = "serene";
let cppNamespace = "::serene::sir";
let summary = "Primary IR of serene language.";
let description = [{
This dialect tries to map the special forms of a lisp into
IR level operations.
}];
}
// Base class for Serene dialect operations. This operation inherits from the base
// `Op` class in OpBase.td, and provides:
// * The parent dialect of the operation.
// * The mnemonic for the operation, or the name without the dialect prefix.
// * A list of traits for the operation.
class Serene_Op<string mnemonic, list<OpTrait> traits = []> :
Op<Serene_Dialect, mnemonic, traits>;
// All of the types will extend this class.
class Serene_Type<string name> : TypeDef<Serene_Dialect, name> { }
// def SymbolType : Serene_Type<"Symbol"> {
// let mnemonic = "symbol";
// let summary = "A typical Lisp symbol";
// let description = [{
// A symbol is just a name and nothing more. Just a name
// to give to a value or to use it as it is.
// }];
// // let cppNamespace = "::serene::sir";
// let parameters = (ins "std::string":$name);
// // We define the printer inline.
// let printer = [{
// $_printer << "Symbol<" << getImpl()->name << ">";
// }];
// // The parser is defined here also.
// let parser = [{
// if ($_parser.parseLess())
// return Type();
// std::string name;
// if ($_parser.parseInteger(name))
// return Type();
// return get($_ctxt, name);
// }];
// }
def ValueOp: Serene_Op<"value"> {
let summary = "This operation represent a value";
let description = [{
some description
}];
let arguments = (ins I64Attr:$value);
let results = (outs I64);
// let verifier = [{ return serene::sir::verify(*this); }];
let builders = [
OpBuilder<(ins "int":$value), [{
// Build from fix 64 bit int
build(odsBuilder, odsState, odsBuilder.getI64Type(), (uint64_t) value);
}]>,
];
}
def FnIdOp: Serene_Op<"fn_id"> {
let summary = "This operation is just a place holder for an anonymouse function";
let description = [{
A place holder for an anonymous function. For example consider an expression
like `(def a (fn (x) x))`, in this case we don't immediately create an anonymous
function since we need to set the name and create the function later.
}];
let arguments = (ins StrAttr:$name);
let results = (outs NoneType);
let builders = [
OpBuilder<(ins "std::string":$name), [{
// Build from fix 64 bit int
build(odsBuilder, odsState, odsBuilder.getNoneType(), odsBuilder.getStringAttr(name));
}]>,
];
}
#endif // SERENE_DIALECT

View File

@ -0,0 +1,79 @@
/**
* 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 GENERATOR_H
#define GENERATOR_H
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Identifier.h"
#include "mlir/IR/MLIRContext.h"
#include "serene/expr.hpp"
#include "serene/list.hpp"
#include "serene/namespace.h"
#include "serene/number.hpp"
#include "serene/symbol.hpp"
#include "llvm/ADT/ScopedHashTable.h"
#include <atomic>
#include <memory>
#include <utility>
namespace serene {
namespace sir {
using FnIdPair = std::pair<mlir::Identifier, mlir::FuncOp>;
class Generator {
private:
::mlir::OpBuilder builder;
::mlir::ModuleOp module;
std::unique_ptr<::serene::Namespace> ns;
std::atomic_int anonymousFnCounter{1};
llvm::DenseMap<mlir::Identifier, mlir::FuncOp> anonymousFunctions;
llvm::ScopedHashTable<llvm::StringRef, mlir::Value> symbolTable;
// TODO: Should we use builder here? maybe there is a better option
::mlir::Location toMLIRLocation(serene::reader::Location *);
// mlir::FuncOp generateFn(serene::reader::Location, std::string, List *,
// List *);
public:
Generator(mlir::MLIRContext &context, ::serene::Namespace *ns)
: builder(&context),
module(mlir::ModuleOp::create(builder.getUnknownLoc(), ns->name)) {
this->ns.reset(ns);
}
mlir::Operation *generate(Number *);
mlir::Operation *generate(AExpr *);
mlir::Value generate(List *);
mlir::ModuleOp generate();
~Generator();
};
} // namespace sir
} // namespace serene
#endif

View File

@ -0,0 +1,54 @@
/**
* 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 SIR_H
#define SIR_H
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/MLIRContext.h"
#include "serene/exprs/expression.h"
#include "serene/sir/generator.hpp"
#include <memory>
namespace serene {
namespace sir {
class SIR {
private:
mlir::MLIRContext context;
public:
SIR();
mlir::OwningModuleRef generate(serene::Namespace *ns);
~SIR();
};
void dumpSIR(exprs::ast &t);
} // namespace sir
} // namespace serene
#endif

42
include/serene/state.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 STATE_H
#define STATE_H
#include "serene/llvm/IR/Value.h"
#include "serene/logger.hpp"
//#include "serene/namespace.h"
#include <llvm/IR/Module.h>
#include <string>
#if defined(ENABLE_LOG) || defined(ENABLE_STATE_LOG)
#define STATE_LOG(...) __LOG("STATE", __VA_ARGS__);
#else
#define STATE_LOG(...) ;
#endif
namespace serene {} // namespace serene
#endif

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

@ -0,0 +1,54 @@
/**
* 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 "serene/expr.hpp"
#include "serene/llvm/IR/Value.h"
#include "serene/reader/location.h"
#include "serene/state.hpp"
#include <string>
namespace serene {
class Symbol : public AExpr {
const std::string name_;
public:
Symbol(reader::LocationRange loc, const std::string &);
virtual ~Symbol();
const std::string &getName() const;
SereneType getType() const override { return SereneType::Symbol; }
std::string string_repr() const override;
std::string dumpAST() const override;
static bool classof(const AExpr *);
};
std::unique_ptr<Symbol> makeSymbol(reader::LocationRange, std::string);
} // namespace serene
#endif

88
include/serene/utils.h Normal file
View File

@ -0,0 +1,88 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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_UTILS_H
#define SERENE_UTILS_H
#include "llvm/Support/Error.h"
#include <variant>
// C++17 required. We can't go back to 14 any more :))
namespace serene {
/// A similar type to Rust's Result data structure. It either holds a value of
/// type `T` successfully or holds a value of type `E` errorfully. It is
/// designed to be used in situations which the return value of a function might
/// contains some errors. The official way to use this type is to use the
/// factory functions `Success` and `Error`. For example:
///
/// \code
/// auto successfulResult = Result<int>::Success(3);
/// auto notOkResult = Result<int>::Error(SomeLLVMError());
// \endcode
///
/// In order check for a value being errorful or successful checkout the `ok`
/// method or simply use the value as a conditiona.
template <typename T, typename E = llvm::Error> class Result {
// The actual data container
std::variant<T, E> contents;
/// The main constructor which we made private to avoid ambiguousness in
/// input type. `Success` and `Error` call this ctor.
template <typename InPlace, typename Content>
Result(InPlace i, Content &&c) : contents(i, std::forward<Content>(c)){};
public:
/// Create a succesfull result with the given value of type `T`.
static Result Success(T v) {
return Result(std::in_place_index_t<0>(), std::move(v));
}
/// Create an errorful result with the given value of type `E` (default
/// `llvm::Error`).
static Result Error(E e) {
return Result(std::in_place_index_t<1>(), std::move(e));
}
// using std::get, it'll throw if contents doesn't contain what you ask for
/// Return the value if it's successful otherwise throw an error
T &getValue() { return std::get<0>(contents); };
/// Return the error value if it's errorful otherwise throw an error
E &getError() { return std::get<1>(contents); };
const T &getValue() const { return std::get<0>(contents); }
const E &getError() const { return std::get<1>(contents); }
/// Return the a boolean value indicating whether the value is succesful
/// or errorful.
bool ok() const { return std::holds_alternative<1>(contents); };
operator bool() const { return ok(); }
};
} // namespace serene
#endif

View File

@ -0,0 +1,85 @@
;;; magit-diff.el --- inspect Git diffs -*- lexical-binding: t -*-
;; Copyright (C) 2010-2020 The Magit Project Contributors
;;
;; You should have received a copy of the AUTHORS.md file which
;; lists all contributors. If not, see http://magit.vc/authors.
;; Author: Jonas Bernoulli <jonas@bernoul.li>
;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
;; Magit is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; Magit is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
;; License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Magit. If not, see http://www.gnu.org/licenses.
;;; Commentary:
;; This library implements support for looking at Git diffs and
;; commits.
;;; Code:
(eval-when-compile
(require 'ansi-color)
(require 'subr-x))
(require 'git-commit)
(require 'magit-core)
;; For `magit-diff-popup'
(declare-function magit-stash-show "magit-stash" (stash optional args files))
;; For `magit-diff-visit-file'
(declare-function dired-jump "dired-x" (optional other-window file-name))
(declare-function magit-find-file-noselect "magit-files" (rev file))
(declare-function magit-status-setup-buffer "magit-status" (directory))
;; For `magit-diff-while-committing'
(declare-function magit-commit-message-buffer "magit-commit" ())
;; For `magit-insert-revision-gravatar'
(defvar gravatar-size)
;; For `magit-show-commit' and `magit-diff-show-or-scroll'
(declare-function magit-current-blame-chunk "magit-blame" ())
(declare-function magit-blame-mode "magit-blame" (optional arg))
(defvar magit-blame-mode)
;; For `magit-diff-show-or-scroll'
(declare-function git-rebase-current-line "git-rebase" ())
;; For `magit-diff-unmerged'
(declare-function magit-merge-in-progress-p "magit-merge" ())
(declare-function magit--merge-range "magit-merge" (optional head))
;; For `magit-diff--dwim'
(declare-function forge--pullreq-ref "forge-pullreq" (pullreq))
;; For `magit-diff-wash-diff'
(declare-function ansi-color-apply-on-region "ansi-color" (begin end))
(eval-when-compile
(cl-pushnew 'base-ref eieio--known-slot-names)
(cl-pushnew 'orig-rev eieio--known-slot-names)
(cl-pushnew 'action-type eieio--known-slot-names)
(cl-pushnew 'target eieio--known-slot-names))
(require 'diff-mode)
(require 'smerge-mode)
;;; Options
;;;; Diff Mode
(defgroup magit-diff nil
"Inspect and manipulate Git diffs."
link '(info-link "(magit)Diffing")
group 'magit-modes
type 'float)
(asd 234 (asd zxc wqe asd asd rewe asd (asd asd (asd asd asd (123 3213123 'asd 'asd "sadasd" (asd asdsad (zxczxc (asdasd asd asd (asdas asd as 234234 324 (234 234 asd 342 ("asdasd" sadf 234 asd ((((((((((((((((((((((((((((((((asd asd asdasd (((((((((((((asdasdasd (((((((((((((((asdasdasd (asdasdasdasd asdas ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
-234234
ascent
9393

342
scripts/apply-format Executable file
View File

@ -0,0 +1,342 @@
#! /bin/bash
#
# Copyright 2018 Undo Ltd.
#
# https://github.com/barisione/clang-format-hooks
# Force variable declaration before access.
set -u
# Make any failure in piped commands be reflected in the exit code.
set -o pipefail
readonly bash_source="${BASH_SOURCE[0]:-$0}"
##################
# Misc functions #
##################
function error_exit() {
for str in "$@"; do
echo -n "$str" >&2
done
echo >&2
exit 1
}
########################
# Command line parsing #
########################
function show_help() {
if [ -t 1 ] && hash tput 2> /dev/null; then
local -r b=$(tput bold)
local -r i=$(tput sitm)
local -r n=$(tput sgr0)
else
local -r b=
local -r i=
local -r n=
fi
cat << EOF
${b}SYNOPSIS${n}
To reformat git diffs:
${i}$bash_source [OPTIONS] [FILES-OR-GIT-DIFF-OPTIONS]${n}
To reformat whole files, including unchanged parts:
${i}$bash_source [-f | --whole-file] FILES${n}
${b}DESCRIPTION${n}
Reformat C or C++ code to match a specified formatting style.
This command can either work on diffs, to reformat only changed parts of
the code, or on whole files (if -f or --whole-file is used).
${b}FILES-OR-GIT-DIFF-OPTIONS${n}
List of files to consider when applying clang-format to a diff. This is
passed to "git diff" as is, so it can also include extra git options or
revisions.
For example, to apply clang-format on the changes made in the last few
revisions you could use:
${i}\$ $bash_source HEAD~3${n}
${b}FILES${n}
List of files to completely reformat.
${b}-f, --whole-file${n}
Reformat the specified files completely (including parts you didn't
change).
The fix is printed on stdout by default. Use -i if you want to modify
the files on disk.
${b}--staged, --cached${n}
Reformat only code which is staged for commit.
The fix is printed on stdout by default. Use -i if you want to modify
the files on disk.
${b}-i${n}
Reformat the code and apply the changes to the files on disk (instead
of just printing the fix on stdout).
${b}--apply-to-staged${n}
This is like specifying both --staged and -i, but the formatting
changes are also staged for commit (so you can just use "git commit"
to commit what you planned to, but formatted correctly).
${b}--style STYLE${n}
The style to use for reformatting code.
If no style is specified, then it's assumed there's a .clang-format
file in the current directory or one of its parents.
${b}--help, -h, -?${n}
Show this help.
EOF
}
# getopts doesn't support long options.
# getopt mangles stuff.
# So we parse manually...
declare positionals=()
declare has_positionals=false
declare whole_file=false
declare apply_to_staged=false
declare staged=false
declare in_place=false
declare style=file
declare ignored=()
while [ $# -gt 0 ]; do
declare arg="$1"
shift # Past option.
case "$arg" in
-h | -\? | --help )
show_help
exit 0
;;
-f | --whole-file )
whole_file=true
;;
--apply-to-staged )
apply_to_staged=true
;;
--cached | --staged )
staged=true
;;
-i )
in_place=true
;;
--style=* )
style="${arg//--style=/}"
;;
--style )
[ $# -gt 0 ] || \
error_exit "No argument for --style option."
style="$1"
shift
;;
--internal-opt-ignore-regex=* )
ignored+=("${arg//--internal-opt-ignore-regex=/}")
;;
--internal-opt-ignore-regex )
ignored+=("${arg//--internal-opt-ignore-regex=/}")
[ $# -gt 0 ] || \
error_exit "No argument for --internal-opt-ignore-regex option."
ignored+=("$1")
shift
;;
-- )
# Stop processing further arguments.
if [ $# -gt 0 ]; then
positionals+=("$@")
has_positionals=true
fi
break
;;
-* )
error_exit "Unknown argument: $arg"
;;
*)
positionals+=("$arg")
;;
esac
done
# Restore positional arguments, access them from "$@".
if [ ${#positionals[@]} -gt 0 ]; then
set -- "${positionals[@]}"
has_positionals=true
fi
[ -n "$style" ] || \
error_exit "If you use --style you need to specify a valid style."
#######################################
# Detection of clang-format & friends #
#######################################
# clang-format.
declare format="${CLANG_FORMAT:-}"
if [ -z "$format" ]; then
format=$(type -p clang-format)
fi
if [ -z "$format" ]; then
error_exit \
$'You need to install clang-format.\n' \
$'\n' \
$'On Ubuntu/Debian this is available in the clang-format package or, in\n' \
$'older distro versions, clang-format-VERSION.\n' \
$'On Fedora it\'s available in the clang package.\n' \
$'You can also specify your own path for clang-format by setting the\n' \
$'$CLANG_FORMAT environment variable.'
fi
# clang-format-diff.
if [ "$whole_file" = false ]; then
invalid="/dev/null/invalid/path"
if [ "${OSTYPE:-}" = "linux-gnu" ]; then
readonly sort_version=-V
else
# On macOS, sort doesn't have -V.
readonly sort_version=-n
fi
declare paths_to_try=()
# .deb packages directly from upstream.
# We try these first as they are probably newer than the system ones.
while read -r f; do
paths_to_try+=("$f")
done < <(compgen -G "/usr/share/clang/clang-format-*/clang-format-diff.py" | sort "$sort_version" -r)
# LLVM official releases (just untarred in /usr/local).
while read -r f; do
paths_to_try+=("$f")
done < <(compgen -G "/usr/local/clang+llvm*/share/clang/clang-format-diff.py" | sort "$sort_version" -r)
# Maybe it's in the $PATH already? This is true for Ubuntu and Debian.
paths_to_try+=( \
"$(type -p clang-format-diff 2> /dev/null || echo "$invalid")" \
"$(type -p clang-format-diff.py 2> /dev/null || echo "$invalid")" \
)
# Fedora.
paths_to_try+=( \
/usr/share/clang/clang-format-diff.py \
)
# Gentoo.
while read -r f; do
paths_to_try+=("$f")
done < <(compgen -G "/usr/lib/llvm/*/share/clang/clang-format-diff.py" | sort -n -r)
# Homebrew.
while read -r f; do
paths_to_try+=("$f")
done < <(compgen -G "/usr/local/Cellar/clang-format/*/share/clang/clang-format-diff.py" | sort -n -r)
declare format_diff=
# Did the user specify a path?
if [ -n "${CLANG_FORMAT_DIFF:-}" ]; then
format_diff="$CLANG_FORMAT_DIFF"
else
for path in "${paths_to_try[@]}"; do
if [ -e "$path" ]; then
# Found!
format_diff="$path"
if [ ! -x "$format_diff" ]; then
format_diff="python $format_diff"
fi
break
fi
done
fi
if [ -z "$format_diff" ]; then
error_exit \
$'Cannot find clang-format-diff which should be shipped as part of the same\n' \
$'package where clang-format is.\n' \
$'\n' \
$'Please find out where clang-format-diff is in your distro and report an issue\n' \
$'at https://github.com/barisione/clang-format-hooks/issues with details about\n' \
$'your operating system and setup.\n' \
$'\n' \
$'You can also specify your own path for clang-format-diff by setting the\n' \
$'$CLANG_FORMAT_DIFF environment variable, for instance:\n' \
$'\n' \
$' CLANG_FORMAT_DIFF="python /.../clang-format-diff.py" \\\n' \
$' ' "$bash_source"
fi
readonly format_diff
fi
############################
# Actually run the command #
############################
if [ "$whole_file" = true ]; then
[ "$has_positionals" = true ] || \
error_exit "No files to reformat specified."
[ "$staged" = false ] || \
error_exit "--staged/--cached only make sense when applying to a diff."
read -r -a format_args <<< "$format"
format_args+=("-style=file")
[ "$in_place" = true ] && format_args+=("-i")
"${format_args[@]}" "$@"
else # Diff-only.
if [ "$apply_to_staged" = true ]; then
[ "$staged" = false ] || \
error_exit "You don't need --staged/--cached with --apply-to-staged."
[ "$in_place" = false ] || \
error_exit "You don't need -i with --apply-to-staged."
staged=true
readonly patch_dest=$(mktemp)
trap '{ rm -f "$patch_dest"; }' EXIT
else
readonly patch_dest=/dev/stdout
fi
declare git_args=(git diff -U0 --no-color)
[ "$staged" = true ] && git_args+=("--staged")
# $format_diff may contain a command ("python") and the script to excute, so we
# need to split it.
read -r -a format_diff_args <<< "$format_diff"
[ "$in_place" = true ] && format_diff_args+=("-i")
# Build the regex for paths to consider or ignore.
# We use negative lookahead assertions which preceed the list of allowed patterns
# (that is, the extensions we want).
exclusions_regex=
if [ "${#ignored[@]}" -gt 0 ]; then
for pattern in "${ignored[@]}"; do
exclusions_regex="$exclusions_regex(?!$pattern)"
done
fi
"${git_args[@]}" "$@" \
| "${format_diff_args[@]}" \
-p1 \
-style="$style" \
-iregex="$exclusions_regex"'.*\.(c|cpp|cxx|cc|h|hpp|m|mm|js|java)' \
> "$patch_dest" \
|| exit 1
if [ "$apply_to_staged" = true ]; then
if [ ! -s "$patch_dest" ]; then
echo "No formatting changes to apply."
exit 0
fi
patch -p0 < "$patch_dest" || \
error_exit "Cannot apply fix to local files."
git apply -p0 --cached < "$patch_dest" || \
error_exit "Cannot apply fix to git staged changes."
fi
fi

411
scripts/git-pre-commit-format Executable file
View File

@ -0,0 +1,411 @@
#! /bin/bash
#
# Copyright 2018 Undo Ltd.
#
# https://github.com/barisione/clang-format-hooks
# Force variable declaration before access.
set -u
# Make any failure in piped commands be reflected in the exit code.
set -o pipefail
readonly bash_source="${BASH_SOURCE[0]:-$0}"
if [ -t 1 ] && hash tput 2> /dev/null; then
readonly b=$(tput bold)
readonly i=$(tput sitm)
readonly n=$(tput sgr0)
else
readonly b=
readonly i=
readonly n=
fi
function error_exit() {
for str in "$@"; do
echo -n "$b$str$n" >&2
done
echo >&2
exit 1
}
# realpath is not available everywhere.
function realpath() {
if [ "${OSTYPE:-}" = "linux-gnu" ]; then
readlink -m "$@"
else
# Python should always be available on macOS.
# We use sys.stdout.write instead of print so it's compatible with both Python 2 and 3.
python -c "import sys; import os.path; sys.stdout.write(os.path.realpath('''$1''') + '\\n')"
fi
}
# realpath --relative-to is only available on recent Linux distros.
# This function behaves identical to Python's os.path.relpath() and doesn't need files to exist.
function rel_realpath() {
local -r path=$(realpath "$1")
local -r rel_to=$(realpath "${2:-$PWD}")
# Split the paths into components.
IFS='/' read -r -a path_parts <<< "$path"
IFS='/' read -r -a rel_to_parts <<< "$rel_to"
# Search for the first different component.
for ((idx=1; idx<${#path_parts[@]}; idx++)); do
if [ "${path_parts[idx]}" != "${rel_to_parts[idx]:-}" ]; then
break
fi
done
result=()
# Add the required ".." to the $result array.
local -r first_different_idx="$idx"
for ((idx=first_different_idx; idx<${#rel_to_parts[@]}; idx++)); do
result+=("..")
done
# Add the required components from $path.
for ((idx=first_different_idx; idx<${#path_parts[@]}; idx++)); do
result+=("${path_parts[idx]}")
done
if [ "${#result[@]}" -gt 0 ]; then
# Join the array with a "/" as separator.
echo "$(export IFS='/'; echo "${result[*]}")"
else
echo .
fi
}
# Find the top-level git directory (taking into account we could be in a submodule).
declare git_test_dir=.
declare top_dir
while true; do
top_dir=$(git -C "$git_test_dir" rev-parse --show-toplevel) || \
error_exit "You need to be in the git repository to run this script."
# Try to handle git worktree.
# The best way to deal both with git submodules and worktrees would be to
# use --show-superproject-working-tree, but it's not supported in git 2.7.4
# which is shipped in Ubuntu 16.04.
declare git_common_dir
if git_common_dir=$(git -C "$git_test_dir" rev-parse --git-common-dir 2>/dev/null); then
# The common dir could be relative, so we make it absolute.
git_common_dir=$(cd "$git_test_dir" && realpath "$git_common_dir")
declare maybe_top_dir
maybe_top_dir=$(realpath "$git_common_dir/..")
if [ -e "$maybe_top_dir/.git" ]; then
# We are not in a submodules, otherwise common dir would have been
# something like PROJ/.git/modules/SUBMODULE and there would not be
# a .git directory in PROJ/.git/modules/.
top_dir="$maybe_top_dir"
fi
fi
[ -e "$top_dir/.git" ] || \
error_exit "No .git directory in $top_dir."
if [ -d "$top_dir/.git" ]; then
# We are done! top_dir is the root git directory.
break
elif [ -f "$top_dir/.git" ]; then
# We are in a submodule.
git_test_dir="$git_test_dir/.."
fi
done
readonly top_dir
hook_path="$top_dir/.git/hooks/pre-commit"
readonly hook_path
me=$(realpath "$bash_source") || exit 1
readonly me
me_relative_to_hook=$(rel_realpath "$me" "$(dirname "$hook_path")") || exit 1
readonly me_relative_to_hook
my_dir=$(dirname "$me") || exit 1
readonly my_dir
apply_format="$my_dir/apply-format"
readonly apply_format
apply_format_relative_to_top_dir=$(rel_realpath "$apply_format" "$top_dir") || exit 1
readonly apply_format_relative_to_top_dir
function is_installed() {
if [ ! -e "$hook_path" ]; then
echo nothing
else
existing_hook_target=$(realpath "$hook_path") || exit 1
readonly existing_hook_target
if [ "$existing_hook_target" = "$me" ]; then
# Already installed.
echo installed
else
# There's a hook, but it's not us.
echo different
fi
fi
}
function install() {
if ln -s "$me_relative_to_hook" "$hook_path" 2> /dev/null; then
echo "Pre-commit hook installed."
else
local -r res=$(is_installed)
if [ "$res" = installed ]; then
error_exit "The hook is already installed."
elif [ "$res" = different ]; then
error_exit "There's already an existing pre-commit hook, but for something else."
elif [ "$res" = nothing ]; then
error_exit "There's no pre-commit hook, but we couldn't create a symlink."
else
error_exit "Unexpected failure."
fi
fi
}
function uninstall() {
local -r res=$(is_installed)
if [ "$res" = installed ]; then
rm "$hook_path" || \
error_exit "Couldn't remove the pre-commit hook."
elif [ "$res" = different ]; then
error_exit "There's a pre-commit hook installed, but for something else. Not removing."
elif [ "$res" = nothing ]; then
error_exit "There's no pre-commit hook, nothing to uninstall."
else
error_exit "Unexpected failure detecting the pre-commit hook status."
fi
}
function show_help() {
cat << EOF
${b}SYNOPSIS${n}
$bash_source [install|uninstall]
${b}DESCRIPTION${n}
Git hook to verify and fix formatting before committing.
The script is invoked automatically when you commit, so you need to call it
directly only to set up the hook or remove it.
To setup the hook run this script passing "install" on the command line.
To remove the hook run passing "uninstall".
${b}CONFIGURATION${n}
You can configure the hook using the "git config" command.
${b}hooks.clangFormatDiffInteractive${n} (default: true)
By default, the hook requires user input. If you don't run git from a
terminal, you can disable the interactive prompt with:
${i}\$ git config hooks.clangFormatDiffInteractive false${n}
${b}hooks.clangFormatDiffStyle${n} (default: file)
Unless a different style is specified, the hook expects a file named
.clang-format to exist in the repository. This file should contain the
configuration for clang-format.
You can specify a different style (in this example, the WebKit one)
with:
${i}\$ git config hooks.clangFormatDiffStyle WebKit${n}
EOF
}
if [ $# = 1 ]; then
case "$1" in
-h | -\? | --help )
show_help
exit 0
;;
install )
install
exit 0
;;
uninstall )
uninstall
exit 0
;;
esac
fi
[ $# = 0 ] || error_exit "Invalid arguments: $*"
# This is a real run of the hook, not a install/uninstall run.
if [ -z "${GIT_DIR:-}" ] && [ -z "${GIT_INDEX_FILE:-}" ]; then
error_exit \
$'It looks like you invoked this script directly, but it\'s supposed to be used\n' \
$'as a pre-commit git hook.\n' \
$'\n' \
$'To install the hook try:\n' \
$' ' "$bash_source" $' install\n' \
$'\n' \
$'For more details on this script try:\n' \
$' ' "$bash_source" $' --help\n'
fi
[ -x "$apply_format" ] || \
error_exit \
$'Cannot find the apply-format script.\n' \
$'I expected it here:\n' \
$' ' "$apply_format"
readonly style=$(cd "$top_dir" && git config hooks.clangFormatDiffStyle || echo file)
apply_format_opts=(
"--style=$style"
--cached
)
readonly exclusions_file="$top_dir/.clang-format-hook-exclude"
if [ -e "$exclusions_file" ]; then
while IFS= read -r line; do
if [[ "$line" && "$line" != "#"* ]]; then
apply_format_opts+=("--internal-opt-ignore-regex=$line")
fi
done < "$exclusions_file"
fi
readonly patch=$(mktemp)
trap '{ rm -f "$patch"; }' EXIT
"$apply_format" --style="$style" --cached "${apply_format_opts[@]}" > "$patch" || \
error_exit $'\nThe apply-format script failed.'
if [ "$(wc -l < "$patch")" -eq 0 ]; then
echo "The staged content is formatted correctly."
exit 0
fi
# The code is not formatted correctly.
interactive=$(cd "$top_dir" && git config --bool hooks.clangFormatDiffInteractive)
if [ "$interactive" != false ]; then
# Interactive is the default, so anything that is not false is converted to
# true, including possibly invalid values.
interactive=true
fi
readonly interactive
if [ "$interactive" = false ]; then
echo "${b}The staged content is not formatted correctly.${n}"
echo "You can fix the formatting with:"
echo " ${i}\$ ./$apply_format_relative_to_top_dir --apply-to-staged${n}"
echo
echo "You can also make this script interactive (if you use git from a terminal) with:"
echo " ${i}\$ git config hooks.clangFormatDiffInteractive true${n}"
exit 1
fi
if hash colordiff 2> /dev/null; then
colordiff < "$patch"
else
echo "${b}(Install colordiff to see this diff in color!)${n}"
echo
cat "$patch"
fi
# We don't want to suggest applying clang-format after merge resolution
if git rev-parse MERGE_HEAD >/dev/null 2>&1; then
readonly this_is_a_merge=true
else
readonly this_is_a_merge=false
fi
echo
echo "${b}The staged content is not formatted correctly.${n}"
echo "The fix shown above can be applied automatically to the commit."
echo
if $this_is_a_merge; then
echo "${b}You appear to be committing the result of a merge. It is not${n}"
echo "${b}recommended to apply the fix if it will reformat any code you${n}"
echo "${b}did not modify in your branch.${n}"
echo
readonly recommend_apply=" (not recommended for merge!)"
readonly recommend_force=""
readonly bold_apply=""
readonly bold_force="${b}"
else
readonly recommend_apply=""
readonly recommend_force=" (not recommended!)"
readonly bold_apply="${b}"
readonly bold_force=""
fi
echo "You can:"
echo " ${bold_apply}[a]: Apply the fix${recommend_apply}${n}"
echo " ${bold_force}[f]: Force and commit anyway${recommend_force}${n}"
echo " [c]: Cancel the commit"
echo " [?]: Show help"
echo
readonly tty=${PRE_COMMIT_HOOK_TTY:-/dev/tty}
while true; do
echo -n "What would you like to do? [a/f/c/?] "
read -r answer < "$tty"
case "$answer" in
[aA] )
patch -p0 < "$patch" || \
error_exit \
$'\n' \
$'Cannot apply fix to local files.\n' \
$'Have you modified the file locally after starting the commit?'
git apply -p0 --cached < "$patch" || \
error_exit \
$'\n' \
$'Cannot apply fix to git staged changes.\n' \
$'This may happen if you have some overlapping unstaged changes. To solve\n' \
$'you need to stage or reset changes manually.'
if $this_is_a_merge; then
echo
echo "Applied the fix to reformat the merge commit."
echo "You can always abort by quitting your editor with no commit message."
echo
echo -n "Press return to continue."
read -r < "$tty"
fi
;;
[fF] )
echo
if ! $this_is_a_merge; then
echo "Will commit anyway!"
echo "You can always abort by quitting your editor with no commit message."
echo
echo -n "Press return to continue."
read -r < "$tty"
fi
exit 0
;;
[cC] )
error_exit "Commit aborted as requested."
;;
\? )
echo
show_help
echo
continue
;;
* )
echo 'Invalid answer. Type "a", "f" or "c".'
echo
continue
esac
break
done

82
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,82 @@
set(HEADER_LIST
"${INCLUDE_DIR}/serene/serene.h"
"${INCLUDE_DIR}/serene/utils.h"
"${INCLUDE_DIR}/serene/exprs/expression.h"
"${INCLUDE_DIR}/serene/exprs/symbol.h"
"${INCLUDE_DIR}/serene/exprs/list.h"
"${INCLUDE_DIR}/serene/exprs/number.h"
# Reader
"${INCLUDE_DIR}/serene/reader/reader.h"
"${INCLUDE_DIR}/serene/reader/location.h"
"${INCLUDE_DIR}/serene/reader/errors.h"
"${INCLUDE_DIR}/serene/errors.h"
"${INCLUDE_DIR}/serene/errors/error.h"
"${INCLUDE_DIR}/serene/errors/errc.h"
"${INCLUDE_DIR}/serene/expr.hpp"
"${INCLUDE_DIR}/serene/number.hpp"
"${INCLUDE_DIR}/serene/symbol.hpp"
"${INCLUDE_DIR}/serene/list.hpp"
"${INCLUDE_DIR}/serene/error.hpp"
"${INCLUDE_DIR}/serene/state.hpp"
"${INCLUDE_DIR}/serene/sir/sir.hpp"
"${INCLUDE_DIR}/serene/sir/dialect.hpp"
"${INCLUDE_DIR}/serene/sir/generator.hpp"
"${INCLUDE_DIR}/serene/namespace.h")
# Make an automatic library - will be static or dynamic based on user setting
add_library(serene
exprs/expression.cpp
exprs/symbol.cpp
exprs/list.cpp
exprs/number.cpp
serene.cpp
symbol.cpp
list.cpp
namespace.cpp
state.cpp
error.cpp
number.cpp
# Reader
reader/reader.cpp
reader/location.cpp
reader/errors.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(serene SereneDialectGen)
if (CPP_20_SUPPORT)
target_compile_features(serene PUBLIC cxx_std_20)
else()
target_compile_features(serene PUBLIC cxx_std_17)
endif()
# We need this directory, and users of our library will need it too
target_include_directories(serene PRIVATE ${INCLUDE_DIR})
target_include_directories(serene PUBLIC ${PROJECT_BINARY_DIR})
# This depends on (header only) boost
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})

46
src/error.cpp Normal file
View File

@ -0,0 +1,46 @@
/**
* 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/error.hpp"
#include "serene/expr.hpp"
#include "serene/llvm/IR/Value.h"
#include "serene/namespace.h"
#include "serene/state.hpp"
#include <assert.h>
#include <fmt/core.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Type.h>
#include <string>
using namespace std;
using namespace llvm;
namespace serene {
string Error::string_repr() const { return fmt::format("Error: {}", msg); }
string Error::dumpAST() const { return fmt::format("<Error: {}>", this->msg); }
Error::~Error() { EXPR_LOG("Destroying Error"); }
} // namespace serene

36
src/exprs/expression.cpp Normal file
View File

@ -0,0 +1,36 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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/exprs/expression.h"
namespace serene {
namespace exprs {
// template <typename T, typename... Args>
// T* make(Args &&...args) {
// return new T(std::forward<Args>(args)...);
// };
} // namespace exprs
} // namespace serene

62
src/exprs/list.cpp Normal file
View File

@ -0,0 +1,62 @@
/*
* Serene programming language.
*
* Copyright (c) 2019-2021 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/exprs/list.h"
#include "llvm/Support/FormatVariadic.h"
namespace serene {
namespace exprs {
List::List(const List &l) : Expression(l.location){};
List::List(const reader::LocationRange &loc, node e) : Expression(loc) {
elements.push_back(std::move(e));
};
List::List(const reader::LocationRange &loc, llvm::ArrayRef<node> elems)
: Expression(loc), elements(elems.begin(), elems.end()){};
ExprType List::getType() const { return ExprType::List; };
std::string List::toString() const {
std::string s{this->elements.empty() ? "-" : ""};
for (auto &n : this->elements) {
s = llvm::formatv("{0} {1}", s, n->toString());
}
return llvm::formatv("<List [loc: {0} | {1}]: {2}>",
this->location.start.toString(),
this->location.end.toString(), s);
};
Result<Expression *> List::analyze(reader::SemanticContext &ctx) {
return Result<Expression *>::Success(this);
};
bool List::classof(const Expression *e) {
return e->getType() == ExprType::List;
};
void List::append(node n) { elements.push_back(n); }
} // namespace exprs
} // namespace serene

48
src/exprs/number.cpp Normal file
View File

@ -0,0 +1,48 @@
/*
* Serene programming language.
*
* Copyright (c) 2019-2021 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/exprs/number.h"
namespace serene {
namespace exprs {
ExprType Number::getType() const { return ExprType::Number; };
std::string Number::toString() const {
return llvm::formatv("<Number [loc: {0} | {1}]: {2}>",
this->location.start.toString(),
this->location.end.toString(), value);
}
Result<Expression *> Number::analyze(reader::SemanticContext &ctx) {
return Result<Expression *>::Success(this);
};
bool Number::classof(const Expression *e) {
return e->getType() == ExprType::Number;
};
} // namespace exprs
} // namespace serene

48
src/exprs/symbol.cpp Normal file
View File

@ -0,0 +1,48 @@
/*
* Serene programming language.
*
* Copyright (c) 2019-2021 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/exprs/symbol.h"
#include "llvm/Support/FormatVariadic.h"
namespace serene {
namespace exprs {
ExprType Symbol::getType() const { return ExprType::Symbol; };
std::string Symbol::toString() const {
return llvm::formatv("<Symbol [loc: {0} | {1}]: {2}>",
this->location.start.toString(),
this->location.end.toString(), this->name);
}
Result<Expression *> Symbol::analyze(reader::SemanticContext &ctx) {
return Result<Expression *>::Success(this);
};
bool Symbol::classof(const Expression *e) {
return e->getType() == ExprType::List;
};
} // namespace exprs
} // namespace serene

139
src/list.cpp Normal file
View File

@ -0,0 +1,139 @@
/**
* 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/list.hpp"
#include "serene/expr.hpp"
#include "serene/llvm/IR/Value.h"
#include "serene/symbol.hpp"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include <bits/c++config.h>
#include <fmt/core.h>
#include <string>
using namespace llvm;
namespace serene {
List::List(reader::Location startLoc) {
this->location.reset(new reader::LocationRange(startLoc));
}
List::List(std::vector<ast_node> elements) {
auto startLoc = elements[0]->location->start;
auto endLoc = elements[elements.size() - 1]->location->end;
this->location.reset(new reader::LocationRange(startLoc, endLoc));
this->nodes_ = elements;
}
llvm::Optional<ast_node> List::at(uint index) const {
if (index >= nodes_.size()) {
return llvm::None;
}
return llvm::Optional<ast_node>(this->nodes_[index]);
}
void List::append(ast_node node) { nodes_.push_back(std::move(node)); }
std::string List::string_repr() const {
std::string s;
for (auto &n : nodes_) {
// TODO: Fix the tailing space for the last element
s = s + n->string_repr() + " ";
}
return fmt::format("({})", s);
}
std::string List::dumpAST() const {
std::string s;
for (auto &n : nodes_) {
s = fmt::format("{} {}", s, n->dumpAST());
}
return fmt::format("<List [loc: {} | {}]: {} >",
this->location->start.toString(),
this->location->end.toString(), s);
}
/**
* Return an iterator to be used with the `for` loop. It's implicitly called by
* the for loop.
*/
std::vector<ast_node>::const_iterator List::begin() { return nodes_.begin(); }
/**
* Return an iterator to be used with the `for` loop. It's implicitly called by
* the for loop.
*/
std::vector<ast_node>::const_iterator List::end() { return nodes_.end(); }
/**
* Return a sub set of elements starting from the `begin` index to the end
* and an empty list otherwise.
*/
std::unique_ptr<List> List::from(uint begin) {
if (this->count() - begin < 1) {
return makeList(this->location->end);
}
std::vector<ast_node>::const_iterator first = this->nodes_.begin() + begin;
std::vector<ast_node>::const_iterator last = this->nodes_.end();
std::vector<ast_node> newCopy(first, last);
return std::make_unique<List>(newCopy);
};
llvm::ArrayRef<ast_node> List::asArrayRef() {
return llvm::makeArrayRef<ast_node>(this->nodes_);
}
size_t List::count() const { return nodes_.size(); }
/**
* `classof` is a enabler static method that belongs to the LLVM RTTI interface
* `llvm::isa`, `llvm::cast` and `llvm::dyn_cast` use this method.
*/
bool List::classof(const AExpr *expr) {
return expr->getType() == SereneType::List;
}
/**
* Make an empty List in starts at the given location `loc`.
*/
std::unique_ptr<List> makeList(reader::Location loc) {
return std::make_unique<List>(loc);
}
/**
* Make a list out of the given pointer to a List
*/
std::unique_ptr<List> makeList(List *list) {
return std::unique_ptr<List>(list);
}
} // namespace serene

74
src/namespace.cpp Normal file
View File

@ -0,0 +1,74 @@
/**
* 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/namespace.h"
#include "serene/exprs/expression.h"
#include "serene/llvm/IR/Value.h"
#include "llvm/ADT/StringRef.h"
#include <fmt/core.h>
#include <string>
using namespace std;
using namespace llvm;
namespace serene {
Namespace::Namespace(llvm::StringRef ns_name,
llvm::Optional<llvm::StringRef> filename) {
this->filename = filename;
this->name = ns_name;
};
exprs::ast &Namespace::Tree() { return this->tree; }
llvm::Optional<mlir::Value> Namespace::lookup(llvm::StringRef name) {
if (auto value = rootScope.lookup(name)) {
return value;
}
return llvm::None;
};
mlir::LogicalResult Namespace::setTree(exprs::ast &t) {
if (initialized) {
return mlir::failure();
}
this->tree = t;
this->initialized = true;
return mlir::success();
}
mlir::LogicalResult Namespace::insert_symbol(mlir::StringRef name,
mlir::Value v) {
rootScope.insert(PairT(name, v));
return mlir::success();
}
void Namespace::print_scope(){};
Namespace::~Namespace() {}
} // namespace serene

71
src/number.cpp Normal file
View File

@ -0,0 +1,71 @@
/**
* 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/number.hpp"
#include "serene/expr.hpp"
#include "serene/llvm/IR/Value.h"
#include "serene/namespace.h"
#include "serene/state.hpp"
#include <assert.h>
#include <fmt/core.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Type.h>
#include <string>
namespace serene {
std::string Number::string_repr() const { return num_; }
std::string Number::dumpAST() const {
return fmt::format("<Number [loc: {} | {}]: {}>",
this->location->start.toString(),
this->location->end.toString(), this->num_);
}
Number::Number(reader::LocationRange loc, const std::string &num, bool isNeg,
bool isFloat)
: num_(num), isNeg(isNeg), isFloat(isFloat) {
this->location.reset(new reader::LocationRange(loc));
}
int64_t Number::toI64() {
// TODO: Handle float case as well
return std::stoi(num_);
};
/**
* `classof` is a enabler static method that belongs to the LLVM RTTI interface
* `llvm::isa`, `llvm::cast` and `llvm::dyn_cast` use this method.
*/
bool Number::classof(const AExpr *expr) {
return expr->getType() == SereneType::Number;
}
Number::~Number() { EXPR_LOG("Destroying number"); }
std::unique_ptr<Number> makeNumber(reader::LocationRange loc, std::string num,
bool isNeg, bool isFloat) {
return std::make_unique<Number>(loc, num, isNeg, isFloat);
}
} // namespace serene

32
src/reader/errors.cpp Normal file
View File

@ -0,0 +1,32 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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/errors.h"
namespace serene {
namespace reader {
/// This one should be here, llvm's rules
char MissingFileError::ID;
}; // namespace reader
}; // namespace serene

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

@ -0,0 +1,73 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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.h"
#include "mlir/IR/Identifier.h"
#include "llvm/Support/FormatVariadic.h"
namespace serene {
namespace reader {
LocationRange::LocationRange(const LocationRange &loc) {
start = loc.start;
end = loc.end;
}
/// Return the string represenation of the location.
std::string Location::toString() const {
return llvm::formatv("{0}:{1}:{2}", line, col, pos);
};
/// 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++;
}
}
/// decrease 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 dec_location(Location &loc, bool newline) {
loc.pos = loc.pos == 0 ? 0 : loc.pos - 1;
if (newline) {
loc.line = loc.line == 0 ? 0 : loc.line - 1;
// We don't move back the `col` value because we simply don't know it
} else {
loc.col = loc.col == 0 ? 0 : loc.col - 1;
}
}
} // namespace reader
} // namespace serene

337
src/reader/reader.cpp Normal file
View File

@ -0,0 +1,337 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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/reader.h"
#include "serene/error.hpp"
#include "serene/exprs/list.h"
#include "serene/exprs/number.h"
#include "serene/exprs/symbol.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/MemoryBuffer.h"
#include <assert.h>
#include <fstream>
#include <memory>
#include <string>
namespace serene {
namespace reader {
Reader::Reader(const llvm::StringRef input) { this->setInput(input); };
/// Set the input of the reader.
///\param input Set the input to the given string
void Reader::setInput(const llvm::StringRef input) {
current_location = Location::unit();
ast.clear();
input_stream.clear();
input_stream.write(input.str().c_str(), input.size());
};
Reader::~Reader() { READER_LOG("Destroying the reader"); }
/// Return the next character in the buffer and moves the location.
///\param skip_whitespace If true it will skip whitespaces and EOL chars
/// \return next char in the buffer.
char Reader::getChar(bool skip_whitespace) {
for (;;) {
char c = input_stream.get();
this->current_char = c;
// TODO: Handle the end of line with respect to the OS.
// increase the current position in the buffer with respect to the end
// of line.
inc_location(current_location, c == '\n');
if (skip_whitespace == true && isspace(c)) {
continue;
} else {
return c;
}
}
};
/// Moves back the location by one char. Basically unreads the last character.
void Reader::ungetChar() {
input_stream.unget();
// The char that we just unget
dec_location(current_location, this->current_char == '\n');
};
/// A predicate function indicating whether the given char `c` is a valid
/// char for the starting point of a symbol or not.
bool Reader::isValidForIdentifier(char c) {
switch (c) {
case '!':
case '$':
case '%':
case '&':
case '*':
case '+':
case '-':
case '.':
case '~':
case '/':
case ':':
case '<':
case '=':
case '>':
case '?':
case '@':
case '^':
case '_':
return true;
}
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9')) {
return true;
}
return false;
}
/// Reads a number,
/// \param neg whether to read a negative number or not.
exprs::node Reader::readNumber(bool neg) {
std::string number(neg ? "-" : "");
bool floatNum = false;
bool empty = false;
LocationRange loc;
char c = getChar(false);
loc.start = current_location;
while (c != EOF &&
((!(isspace(c)) && ((c >= '0' && c <= '9') | (c == '.'))))) {
if (c == '.' && floatNum == true) {
llvm::errs() << "Two float points in a number?\n";
// TODO: Return a proper error
return nullptr;
}
if (c == '.') {
floatNum = true;
}
number += c;
c = getChar(false);
empty = false;
}
if (!empty) {
ungetChar();
loc.end = current_location;
return exprs::make<exprs::Number>(loc, number, neg, floatNum);
}
return nullptr;
};
/// Reads a symbol. If the symbol looks like a number
/// If reads it as number
exprs::node Reader::readSymbol() {
bool empty = true;
char c = getChar(false);
READER_LOG("Reading symbol");
if (!this->isValidForIdentifier(c)) {
// TODO: Replece this with a tranceback function or something to raise
// synatx error.
llvm::errs() << llvm::formatv(
"Invalid character at the start of a symbol: '{}'\n", c);
exit(1);
}
if (c == '-') {
char next = getChar(false);
if (next >= '0' && next <= '9') {
ungetChar();
return readNumber(true);
}
}
if (c >= '0' && c <= '9') {
ungetChar();
return readNumber(false);
}
std::string sym("");
LocationRange loc;
loc.start = current_location;
while (c != EOF && ((!(isspace(c)) && this->isValidForIdentifier(c)))) {
sym += c;
c = getChar(false);
empty = false;
}
if (!empty) {
ungetChar();
loc.end = current_location;
return exprs::make<exprs::Symbol>(loc, sym);
}
// TODO: it should never happens
return nullptr;
};
/// Reads a list recursively
exprs::node Reader::readList() {
auto list = exprs::makeAndCast<exprs::List>(current_location);
char c = getChar(true);
assert(c == '(');
bool list_terminated = false;
do {
char c = getChar(true);
switch (c) {
case EOF:
throw ReadError(const_cast<char *>("EOF reached before closing of list"));
case ')':
list_terminated = true;
list->location.end = current_location;
break;
default:
ungetChar();
list->append(readExpr());
}
} while (!list_terminated);
return list;
};
/// Reads an expression by dispatching to the proper reader function.
exprs::node Reader::readExpr() {
char c = getChar(false);
READER_LOG("CHAR: " << c);
ungetChar();
switch (c) {
case '(': {
return readList();
}
case EOF:
return nullptr;
default:
return readSymbol();
}
};
/// Reads all the expressions in the reader's buffer as an AST.
/// Each expression type (from the reader perspective) has a
/// reader function.
llvm::Expected<exprs::ast> Reader::read() {
char c = getChar(true);
while (c != EOF) {
ungetChar();
auto tmp{readExpr()};
if (tmp) {
this->ast.push_back(move(tmp));
}
c = getChar(true);
}
return this->ast;
};
/// Reads the input into an AST and prints it out as string again.
void Reader::toString() {
auto maybeAst = read();
std::string result = "";
if (!maybeAst) {
throw maybeAst.takeError();
}
exprs::ast ast = *maybeAst;
for (auto &node : ast) {
result = llvm::formatv("{0} {1}", result, node->toString());
}
};
/// Reads all the expressions from the file provided via its path
// in the reader as an AST.
/// Each expression type (from the reader perspective) has a
/// reader function.
llvm::Expected<exprs::ast> FileReader::read() {
// TODO: Add support for relative path as well
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr =
llvm::MemoryBuffer::getFileOrSTDIN(file);
if (std::error_code EC = fileOrErr.getError()) {
llvm::errs() << "Could not open input file: " << EC.message() << "\n";
llvm::errs() << fmt::format("File: '{}'\n", file);
llvm::errs() << "Use absolute path for now\n";
return llvm::make_error<MissingFileError>(file);
}
reader->setInput(fileOrErr.get()->getBuffer().str());
return reader->read();
}
/// Reads the input into an AST and prints it out as string again.
void FileReader::toString() {
auto maybeAst = this->read();
exprs::ast ast;
if (!maybeAst) {
throw maybeAst.takeError();
}
ast = *maybeAst;
std::string result = "";
for (auto &node : ast) {
result = llvm::formatv("{0} {1}", result, node->toString());
}
llvm::outs() << result << "\n";
}
FileReader::~FileReader() {
delete this->reader;
READER_LOG("Destroying the file reader");
}
} // namespace reader
} // namespace serene

46
src/reader/semantics.cpp Normal file
View File

@ -0,0 +1,46 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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/semantics.h"
#include "serene/exprs/expression.h"
namespace serene::reader {
exprs::ast Semantics::analyze(exprs::ast &inputAst) {
// TODO: Fetch the current namespace from the JIT engine later and if it is
// `nil` then the given `ast` has to start with a namespace definition.
exprs::ast ast;
for (auto &element : inputAst) {
auto maybeNode = element->analyze(context);
if (!maybeNode) {
// TODO: Check the error type to see whether we can continue or not.
// If not, raise otherwise push it to the exception queue and move on.
auto err = maybeNode.takeError();
throw err;
}
}
};
}; // namespace serene::reader

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 "serene/serene.h"
#include "serene/reader/reader.h"
#include <iostream>

46
src/sir/dialect.cpp Normal file
View File

@ -0,0 +1,46 @@
/**
* 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/sir/dialect.hpp"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/OpImplementation.h"
namespace serene {
namespace sir {
/// Dialect initialization, the instance will be owned by the context. This is
/// the point of registration of types and operations for the dialect.
void SereneDialect::initialize() {
addOperations<
#define GET_OP_LIST
#include "serene/sir/ops.cpp.inc"
>();
}
} // namespace sir
} // namespace serene
#define GET_OP_CLASSES
#include "serene/sir/ops.cpp.inc"

200
src/sir/generator.cpp Normal file
View File

@ -0,0 +1,200 @@
/**
* 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/sir/generator.hpp"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Identifier.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Value.h"
#include "serene/expr.hpp"
#include "serene/sir/dialect.hpp"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/ScopedHashTable.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
namespace serene {
namespace sir {
mlir::ModuleOp Generator::generate() {
// for (auto x : ns->Tree()) {
// generate(x.get());
// }
return module;
};
mlir::Operation *Generator::generate(AExpr *x) {
// switch (x->getType()) {
// case SereneType::Number: {
// return generate(llvm::cast<Number>(x));
// }
// case SereneType::List: {
// generate(llvm::cast<List>(x));
// return nullptr;
// }
// default: {
return builder.create<ValueOp>(builder.getUnknownLoc(), (uint64_t)3);
// }
// }
};
mlir::Value Generator::generate(List *l) {
// auto first = l->at(0);
// if (!first) {
// // Empty list.
// // TODO: Return Nil or empty list.
// // Just for now.
// return builder.create<ValueOp>(builder.getUnknownLoc(), (uint64_t)0);
// }
// if (first->get()->getType() == SereneType::Symbol) {
// auto fnNameSymbol = llvm::dyn_cast<Symbol>(first->get());
// if (fnNameSymbol->getName() == "fn") {
// if (l->count() <= 3) {
// module.emitError("'fn' form needs exactly 2 arguments.");
// }
// auto args = llvm::dyn_cast<List>(l->at(1).getValue().get());
// auto body = llvm::dyn_cast<List>(l->from(2).get());
// if (!args) {
// module.emitError("The first element of 'def' has to be a symbol.");
// }
// // Create a new anonymous function and push it to the anonymous
// functions
// // map, later on we
// auto loc(fnNameSymbol->location->start);
// auto anonymousName = fmt::format("__fn_{}__", anonymousFnCounter);
// anonymousFnCounter++;
// auto fn = generateFn(loc, anonymousName, args, body);
// mlir::Identifier fnid = builder.getIdentifier(anonymousName);
// anonymousFunctions.insert({fnid, fn});
// return builder.create<FnIdOp>(builder.getUnknownLoc(), fnid.str());
// }
// }
// // auto rest = l->from(1);
// // auto loc = toMLIRLocation(&first->get()->location->start);
// // for (auto x : *rest) {
// // generate(x.get());
// // }
return builder.create<ValueOp>(builder.getUnknownLoc(), (uint64_t)100);
};
// mlir::FuncOp Generator::generateFn(serene::reader::Location loc,
// std::string name, List *args, List *body)
// {
// auto location = toMLIRLocation(&loc);
// llvm::SmallVector<mlir::Type, 4> arg_types(args->count(),
// builder.getI64Type());
// auto func_type = builder.getFunctionType(arg_types, builder.getI64Type());
// auto proto = mlir::FuncOp::create(location, name, func_type);
// mlir::FuncOp fn(proto);
// if (!fn) {
// module.emitError("Can not create the function.");
// }
// auto &entryBlock = *fn.addEntryBlock();
// llvm::ScopedHashTableScope<llvm::StringRef, mlir::Value>
// scope(symbolTable);
// // Declare all the function arguments in the symbol table.
// for (const auto arg :
// llvm::zip(args->asArrayRef(), entryBlock.getArguments())) {
// auto argSymbol = llvm::dyn_cast<Symbol>(std::get<0>(arg).get());
// if (!argSymbol) {
// module.emitError("Function parameters must be symbols");
// }
// if (symbolTable.count(argSymbol->getName())) {
// return nullptr;
// }
// symbolTable.insert(argSymbol->getName(), std::get<1>(arg));
// }
// // Set the insertion point in the builder to the beginning of the function
// // body, it will be used throughout the codegen to create operations in
// this
// // function.
// builder.setInsertionPointToStart(&entryBlock);
// // Emit the body of the function.
// if (!generate(body)) {
// fn.erase();
// return nullptr;
// }
// // // Implicitly return void if no return statement was emitted.
// // // FIXME: we may fix the parser instead to always return the last
// // expression
// // // (this would possibly help the REPL case later)
// // ReturnOp returnOp;
// // if (!entryBlock.empty())
// // returnOp = dyn_cast<ReturnOp>(entryBlock.back());
// // if (!returnOp) {
// // builder.create<ReturnOp>(loc(funcAST.getProto()->loc()));
// // } else if (returnOp.hasOperand()) {
// // // Otherwise, if this return operation has an operand then add a
// result
// // to
// // // the function.
// // function.setType(builder.getFunctionType(function.getType().getInputs(),
// // getType(VarType{})));
// // }
// return fn;
// }
mlir::Operation *Generator::generate(Number *x) {
return builder.create<ValueOp>(builder.getUnknownLoc(), x->toI64());
};
/**
* Convert a Serene location to MLIR FileLineLoc Location
*/
::mlir::Location Generator::toMLIRLocation(serene::reader::Location *loc) {
auto file = ns->filename;
std::string filename{file.getValueOr("REPL")};
return mlir::FileLineColLoc::get(builder.getIdentifier(filename), loc->line,
loc->col);
}
Generator::~Generator(){};
} // namespace sir
} // namespace serene

59
src/sir/sir.cpp Normal file
View File

@ -0,0 +1,59 @@
/**
* 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/sir/sir.hpp"
#include "mlir/IR/MLIRContext.h"
#include "serene/exprs/expression.h"
#include "serene/sir/dialect.hpp"
#include "serene/sir/generator.hpp"
#include <memory>
namespace serene {
namespace sir {
SIR::SIR() { context.getOrLoadDialect<::serene::sir::SereneDialect>(); }
mlir::OwningModuleRef SIR::generate(::serene::Namespace *ns) {
auto g = std::make_unique<Generator>(context, ns);
return g->generate();
};
SIR::~SIR() {}
void dumpSIR(exprs::ast &t) {
auto ns = new ::serene::Namespace("user", llvm::None);
SIR s{};
if (failed(ns->setTree(t))) {
llvm::errs() << "Can't set the body of the namespace";
return;
}
auto module = s.generate(ns);
module->dump();
};
} // namespace sir
} // namespace serene

34
src/sir/value_op.cpp Normal file
View File

@ -0,0 +1,34 @@
/**
* 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 "mlir/IR/Builders.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Value.h"
#include "serene/sir/dialect.hpp"
#include "serene/sir/sir.hpp"
#include "llvm/Support/Casting.h"
namespace serene {
namespace sir {} // namespace sir
} // namespace serene

104
src/state.cpp Normal file
View File

@ -0,0 +1,104 @@
/**
* 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/state.hpp"
#include "serene/llvm/IR/Value.h"
#include "serene/namespace.h"
#include <fmt/core.h>
#include <string>
using namespace std;
using namespace llvm;
namespace serene {
// State::State() { current_ns = nullptr; };
// void State::add_namespace(Namespace *ns, bool set_current, bool overwrite) {
// if (ns->name.empty()) {
// // TODO: Better error handling
// fmt::print("Error: namespace name is missing\n.");
// exit(1);
// }
// Namespace *already_exist_ns = namespaces[ns->name];
// if (already_exist_ns && !overwrite) {
// return;
// }
// if (already_exist_ns) {
// delete namespaces[ns->name];
// }
// namespaces[ns->name] = ns;
// if (set_current) {
// set_current_ns(ns);
// }
// };
// bool State::set_current_ns(Namespace *ns) {
// Namespace *already_exist_ns = namespaces[ns->name];
// if (already_exist_ns) {
// current_ns = ns;
// return true;
// }
// return false;
// };
// Value *State::lookup_in_current_scope(const string &name) {
// if (current_ns) {
// return current_ns->lookup(name);
// }
// fmt::print("FATAL ERROR: Current ns is not set.");
// // TODO: Come up with the ERRNO table and return the proper ERRNO
// exit(1);
// };
// void State::set_in_current_ns_root_scope(string name, Value *v) {
// if (current_ns) {
// current_ns->insert_symbol(name, v);
// return;
// }
// fmt::print("FATAL ERROR: Current ns is not set.");
// // TODO: Come up with the ERRNO table and return the proper ERRNO
// exit(1);
// };
// State::~State() {
// STATE_LOG("Deleting namespaces...")
// std::map<string, Namespace *>::iterator it = namespaces.begin();
// while (it != namespaces.end()) {
// STATE_LOG("DELETING {}", it->first);
// Namespace *tmp = it->second;
// namespaces[it->first] = nullptr;
// delete tmp;
// it++;
// }
// STATE_LOG("Clearing namespaces...");
// namespaces.clear();
// };
} // namespace serene

69
src/symbol.cpp Normal file
View File

@ -0,0 +1,69 @@
/**
* 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/symbol.hpp"
#include "serene/expr.hpp"
#include "serene/llvm/IR/Value.h"
#include "serene/namespace.h"
#include "serene/state.hpp"
#include <assert.h>
#include <fmt/core.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Type.h>
#include <string>
using namespace std;
using namespace llvm;
namespace serene {
string Symbol::string_repr() const { return name_; }
string Symbol::dumpAST() const {
return fmt::format("<Symbol [loc: {} | {}]: {}>",
this->location->start.toString(),
this->location->end.toString(), this->getName());
}
const string &Symbol::getName() const { return name_; }
Symbol::Symbol(reader::LocationRange loc, const string &name) : name_(name) {
this->location.reset(new reader::LocationRange(loc));
}
/**
* `classof` is a enabler static method that belongs to the LLVM RTTI interface
* `llvm::isa`, `llvm::cast` and `llvm::dyn_cast` use this method.
*/
bool Symbol::classof(const AExpr *expr) {
return expr->getType() == SereneType::Symbol;
}
Symbol::~Symbol() { EXPR_LOG("Destroying symbol"); }
std::unique_ptr<Symbol> makeSymbol(reader::LocationRange loc,
std::string name) {
return std::make_unique<Symbol>(loc, name);
}
} // namespace serene

21
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,21 @@
# Catch2 should be installed system wide
find_package(Catch2 REQUIRED)
# Tests need to be added as executables first
add_executable(tests serenetests.cpp)
add_dependencies(tests SereneDialectGen)
add_dependencies(tests serene)
target_compile_features(tests PRIVATE cxx_std_17)
# Should be linked to the main library, as well as the Catch2 testing library
target_link_libraries(tests PUBLIC serene Catch2::Catch2)
# target_include_directories(serene SYSTEM PRIVATE $ENV{INCLUDE})
# target_include_directories(serene PUBLIC ${INCLUDE_DIR})
# If you register a test, then ctest and make test will run it.
# You can also run examples and check the output, as well.
# add_test(NAME testlibtest serene testlib) # Command can be a target
include(CTest)
include(Catch)
catch_discover_tests(tests)

View File

@ -0,0 +1,48 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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 "../test_helpers.cpp.inc"
#include "serene/exprs/expression.h"
#include "serene/exprs/list.h"
#include "serene/exprs/symbol.h"
#include "llvm/ADT/ArrayRef.h"
namespace serene {
namespace exprs {
TEST_CASE("Public Expression API", "[expression]") {
std::unique_ptr<reader::LocationRange> range(dummyLocation());
auto sym = make<Symbol>(*range.get(), "example");
REQUIRE(sym->getType() == ExprType::Symbol);
CHECK(sym->toString() == "<Symbol [loc: 2:20:40 | 3:30:80]: example>");
auto list = makeAndCast<List>(*range.get(), sym);
CHECK(list->toString() == "<List [loc: 2:20:40 | 3:30:80]: <Symbol [loc: "
"2:20:40 | 3:30:80]: example>>");
};
} // namespace exprs
} // namespace serene

60
tests/exprs/list_test.cpp Normal file
View File

@ -0,0 +1,60 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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 "../test_helpers.cpp.inc"
#include "serene/exprs/list.h"
#include "serene/exprs/symbol.h"
namespace serene {
namespace exprs {
TEST_CASE("List Expression", "[expression]") {
std::unique_ptr<reader::LocationRange> range(dummyLocation());
auto sym = make<Symbol>(*range.get(), llvm::StringRef("example"));
auto list = make<List>(*range.get());
auto list2 = make<List>(*range.get(), list);
auto list3 = make<List>(*range.get(), llvm::ArrayRef<node>{list, list2, sym});
REQUIRE(list->getType() == ExprType::List);
CHECK(list->toString() == "<List [loc: 2:20:40 | 3:30:80]: ->");
CHECK(list2->toString() ==
"<List [loc: 2:20:40 | 3:30:80]: <List [loc: 2:20:40 | 3:30:80]: ->>");
CHECK(list3->toString() ==
"<List [loc: 2:20:40 | 3:30:80]: <List [loc: 2:20:40 | 3:30:80]: -> "
"<List [loc: 2:20:40 | 3:30:80]: <List [loc: 2:20:40 | 3:30:80]: "
"->> <Symbol [loc: 2:20:40 | 3:30:80]: example>>");
auto l = llvm::dyn_cast<List>(list);
l.append(sym);
REQUIRE(list->getType() == ExprType::List);
CHECK(list->toString() == "<List [loc: 2:20:40 | 3:30:80]: <Symbol [loc: "
"2:20:40 | 3:30:80]: example>>");
};
} // namespace exprs
} // namespace serene

View File

@ -0,0 +1,48 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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 "../test_helpers.cpp.inc"
#include "serene/exprs/number.h"
#include <catch2/catch.hpp>
namespace serene {
namespace exprs {
TEST_CASE("Number Expression", "[expression]") {
std::unique_ptr<reader::LocationRange> range(dummyLocation());
auto num1 = makeAndCast<Number>(*range.get(), "3", false, false);
auto num2 = makeAndCast<Number>(*range.get(), "3.4", false, true);
// Hence the `isNeg` being true. We need to provide the sign as the input
// anyway
auto num3 = makeAndCast<Number>(*range.get(), "3", true, false);
auto num4 = makeAndCast<Number>(*range.get(), "-3", true, false);
CHECK(num1->toString() == "<Number [loc: 2:20:40 | 3:30:80]: 3>");
CHECK(num2->toString() == "<Number [loc: 2:20:40 | 3:30:80]: 3.4>");
CHECK(num3->toString() == "<Number [loc: 2:20:40 | 3:30:80]: 3>");
CHECK(num4->toString() == "<Number [loc: 2:20:40 | 3:30:80]: -3>");
};
} // namespace exprs
} // namespace serene

View File

@ -0,0 +1,40 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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 "../test_helpers.cpp.inc"
#include "serene/exprs/expression.h"
#include "serene/exprs/symbol.h"
namespace serene {
namespace exprs {
TEST_CASE("Public Symbol API", "[expression]") {
std::unique_ptr<reader::LocationRange> range(dummyLocation());
auto sym = make<Symbol>(*range.get(), "example");
REQUIRE(sym->getType() == ExprType::Symbol);
CHECK(sym->toString() == "<Symbol [loc: 2:20:40 | 3:30:80]: example>");
};
} // namespace exprs
} // namespace serene

View File

@ -0,0 +1,126 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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 "../test_helpers.cpp.inc"
#include "serene/reader/reader.h"
#include <catch2/catch.hpp>
namespace serene {
namespace reader {
TEST_CASE("Read numbers", "[reader]") {
auto r = new reader::Reader("3");
auto maybeAst = r->read();
if (!maybeAst) {
FAIL();
}
auto ast = maybeAst.get();
REQUIRE_FALSE(ast.empty());
CHECK(ast.front()->toString() == "<Number [loc: 0:1:1 | 0:1:1]: 3>");
r->setInput("-34");
maybeAst = r->read();
if (!maybeAst) {
FAIL();
}
ast = maybeAst.get();
REQUIRE_FALSE(ast.empty());
CHECK(ast.front()->toString() == "<Number [loc: 0:2:2 | 0:3:3]: -34>");
r->setInput("-3.5434");
maybeAst = r->read();
if (!maybeAst) {
FAIL();
}
ast = maybeAst.get();
REQUIRE_FALSE(ast.empty());
CHECK(ast.front()->toString() == "<Number [loc: 0:2:2 | 0:7:7]: -3.5434>");
r->setInput("444323 2123 123123");
maybeAst = r->read();
if (!maybeAst) {
FAIL();
}
ast = maybeAst.get();
REQUIRE(ast.size() == 3);
CHECK(ast.front()->toString() == "<Number [loc: 0:1:1 | 0:6:6]: 444323>");
CHECK(ast[1]->toString() == "<Number [loc: 0:8:8 | 0:11:11]: 2123>");
CHECK(ast[2]->toString() == "<Number [loc: 0:13:13 | 0:18:18]: 123123>");
delete r;
};
TEST_CASE("Read Lists and Symbols", "[reader]") {
auto r = new reader::Reader("(x 1)");
auto maybeAst = r->read();
if (!maybeAst) {
FAIL();
}
auto ast = maybeAst.get();
REQUIRE_FALSE(ast.empty());
CHECK(ast.front()->toString() ==
"<List [loc: 0:0:0 | 0:5:5]: <Symbol [loc: 0:2:2 | 0:2:2]: x> <Number "
"[loc: 0:4:4 | 0:4:4]: 1>>");
r->setInput("(x (y (z)))");
maybeAst = r->read();
if (!maybeAst) {
FAIL();
}
ast = maybeAst.get();
REQUIRE_FALSE(ast.empty());
CHECK(ast.front()->toString() ==
"<List [loc: 0:0:0 | 0:11:11]: <Symbol [loc: 0:2:2 | 0:2:2]: x> <List "
"[loc: 0:3:3 | 0:10:10]: <Symbol [loc: 0:5:5 | 0:5:5]: y> <List [loc: "
"0:6:6 | 0:9:9]: <Symbol [loc: 0:8:8 | 0:8:8]: z>>>>");
r->setInput("(x \n y)");
maybeAst = r->read();
if (!maybeAst) {
FAIL();
}
ast = maybeAst.get();
REQUIRE_FALSE(ast.empty());
CHECK(ast.front()->toString() ==
"<List [loc: 0:0:0 | 1:3:7]: <Symbol [loc: 0:2:2 | 0:2:2]: x> <Symbol "
"[loc: 1:2:6 | 1:2:6]: y>>");
delete r;
};
} // namespace reader
} // namespace serene

31
tests/serenetests.cpp Normal file
View File

@ -0,0 +1,31 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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.
*/
#define CATCH_CONFIG_MAIN
#include "./exprs/expression_tests.cpp.inc"
#include "./exprs/number_tests.cpp.inc"
#include "./exprs/symbol_tests.cpp.inc"
#include "./reader/reader_tests.cpp.inc"
#include "./utils_tests.cpp.inc"
#include "catch2/catch.hpp"

View File

@ -0,0 +1,52 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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 TEST_HEALPERS_H
#define TEST_HEALPERS_H
#include "catch2/catch.hpp"
#include "serene/reader/location.h"
namespace serene {
namespace exprs {
reader::LocationRange *dummyLocation() {
reader::Location start;
reader::Location end;
start.line = 2;
start.col = 20;
start.pos = 40;
end.line = 3;
end.col = 30;
end.pos = 80;
return new reader::LocationRange(start, end);
};
} // namespace exprs
} // namespace serene
#endif

39
tests/utils_tests.cpp.inc Normal file
View File

@ -0,0 +1,39 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 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 "./test_helpers.cpp.inc"
#include "serene/utils.h"
namespace serene {
TEST_CASE("Result Type", "[utils]") {
auto r = Result<int>::Success(4);
REQUIRE(r == true);
CHECK(r.getValue() == 4);
auto r1 = Result<int, char>::Error('c');
REQUIRE_FALSE(r1);
CHECK(r1.getError() == 'c');
};
} // namespace serene