Skip to content

Commit

Permalink
FunC kernel (#2)
Browse files Browse the repository at this point in the history
FunC kernel
  • Loading branch information
m-kus authored Dec 1, 2019
2 parents 6dd10af + 57df150 commit c30f24c
Show file tree
Hide file tree
Showing 41 changed files with 1,390 additions and 361 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
build/
share/jupyter/kernels/fift/kernel.json
share/jupyter/kernels/func/kernel.json
.ipynb_checkpoints
.idea/
__pycache__/
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
url = https://github.com/QuantStack/xeus.git
[submodule "third-party/ton"]
path = third-party/ton
url = https://github.com/ton-blockchain/ton.git
url = https://github.com/m-kus/ton.git
[submodule "third-party/nlohmann_json"]
path = third-party/nlohmann_json
url = https://github.com/nlohmann/json.git
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ before_script:
script:
- cd $TRAVIS_BUILD_DIR/build
- export FIFTPATH=$TRAVIS_BUILD_DIR/third-party/ton/crypto/fift/lib
- export FUNCPATH=$TRAVIS_BUILD_DIR/third-party/ton/crypto/smartcont
- ./xeus-fift-test

before_deploy:
Expand Down
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
"fft": "cpp",
"*.in": "cpp",
"filesystem": "cpp",
"colab.ipynb": "json"
"colab.ipynb": "json",
"cerrno": "cpp",
"*.inc": "cpp"
}
}
100 changes: 85 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
cmake_minimum_required(VERSION 3.15.2)
project(xeus-fift)

set(EXECUTABLE_NAME xeus-fift)
set(BUILD_SHARED_LIBS NO CACHE BOOL "" FORCE)
set(BUILD_STATIC_LIBS YES CACHE BOOL "" FORCE)

Expand All @@ -10,11 +9,18 @@ set(BUILD_STATIC_LIBS YES CACHE BOOL "" FORCE)

include(GNUInstallDirs)

set(XFIFT_EXE xeus-fift)
set(XFUNC_EXE xeus-func)

# We generate the kernel.json file, given the installation prefix and the executable name
configure_file (
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/fift/kernel.json.in"
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/fift/kernel.json"
)
configure_file (
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/func/kernel.json.in"
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/func/kernel.json"
)

# Dependencies
# ============
Expand Down Expand Up @@ -61,42 +67,95 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU"
endif()
endif()

# Target and link
# ===============
# Target and link xeus-fift
# =========================

set(XFIFT_SRC
"${CMAKE_SOURCE_DIR}/src/xfift/xfift.cpp"
"${CMAKE_SOURCE_DIR}/src/xresult.cpp"
"${CMAKE_SOURCE_DIR}/src/xutil.cpp"
)

add_executable(${XFIFT_EXE} src/main.cpp ${XFIFT_SRC})
include_directories(${XFIFT_EXE} PUBLIC ${CMAKE_SOURCE_DIR}/src)
include_directories(${XFIFT_EXE} SYSTEM PUBLIC
${CMAKE_SOURCE_DIR}/third-party
${CMAKE_SOURCE_DIR}/third-party/xeus/include
${CMAKE_SOURCE_DIR}/third-party/nlohmann_json/include
${CMAKE_SOURCE_DIR}/third-party/libzmq/include
${CMAKE_SOURCE_DIR}/third-party/xtl/include
${CMAKE_SOURCE_DIR}/third-party/cppzmq
)
target_link_libraries(${XFIFT_EXE} PUBLIC
libzmq-static
xeus_static
fift-lib
func-lib
stdc++fs
)

if (APPLE)
set_target_properties(${XFIFT_EXE} PROPERTIES
MACOSX_RPATH ON
)
else()
set_target_properties(${XFIFT_EXE} PROPERTIES
BUILD_WITH_INSTALL_RPATH 1
SKIP_BUILD_RPATH FALSE
)
endif()

set_target_properties(${XFIFT_EXE} PROPERTIES
INSTALL_RPATH_USE_LINK_PATH TRUE
)

file(GLOB_RECURSE XFIFT_SRC "${CMAKE_SOURCE_DIR}/src/*")
# Target and link xeus-func
# =========================

set(XFUNC_SRC
"${CMAKE_SOURCE_DIR}/src/xfunc/xfunc.cpp"
"${CMAKE_SOURCE_DIR}/src/xfunc/symguard.cpp"
"${CMAKE_SOURCE_DIR}/src/xfunc/codegen.cpp"
"${CMAKE_SOURCE_DIR}/src/xfunc/preproc.cpp"
"${CMAKE_SOURCE_DIR}/src/xfift/xfift.cpp"
"${CMAKE_SOURCE_DIR}/src/xresult.cpp"
"${CMAKE_SOURCE_DIR}/src/xutil.cpp"
)

add_executable(${EXECUTABLE_NAME} src/main.cpp ${XFIFT_SRC})
include_directories(${EXECUTABLE_NAME} PUBLIC
add_executable(${XFUNC_EXE} src/main.cpp ${XFUNC_SRC})
target_compile_definitions(${XFUNC_EXE} PUBLIC -DXFUNC_KERNEL)
target_include_directories(${XFUNC_EXE} SYSTEM PUBLIC
${CMAKE_SOURCE_DIR}/third-party
${CMAKE_SOURCE_DIR}/third-party/xeus/include
${CMAKE_SOURCE_DIR}/third-party/nlohmann_json/include
${CMAKE_SOURCE_DIR}/third-party/libzmq/include
${CMAKE_SOURCE_DIR}/third-party/xtl/include
${CMAKE_SOURCE_DIR}/third-party/cppzmq
)
target_link_libraries(${EXECUTABLE_NAME} PUBLIC
target_link_libraries(${XFUNC_EXE} PUBLIC
libzmq-static
xeus_static
fift-lib
func-lib
stdc++fs
)

if (APPLE)
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
set_target_properties(${XFUNC_EXE} PROPERTIES
MACOSX_RPATH ON
)
else()
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
set_target_properties(${XFUNC_EXE} PROPERTIES
BUILD_WITH_INSTALL_RPATH 1
SKIP_BUILD_RPATH FALSE
)
endif()

set_target_properties(${EXECUTABLE_NAME} PROPERTIES
set_target_properties(${XFUNC_EXE} PROPERTIES
INSTALL_RPATH_USE_LINK_PATH TRUE
)


# Testing
# =======

Expand All @@ -105,9 +164,15 @@ set(XFIFT_TEST_EXE "xeus-fift-test")
add_subdirectory(${CMAKE_SOURCE_DIR}/third-party/googletest EXCLUDE_FROM_ALL)

set(SRC_TESTS
"${CMAKE_SOURCE_DIR}/src/xfift.cpp"
"${CMAKE_SOURCE_DIR}/src/xfift.hpp"
"${CMAKE_SOURCE_DIR}/src/xfunc/xfunc.cpp"
"${CMAKE_SOURCE_DIR}/src/xfunc/symguard.cpp"
"${CMAKE_SOURCE_DIR}/src/xfunc/codegen.cpp"
"${CMAKE_SOURCE_DIR}/src/xfunc/preproc.cpp"
"${CMAKE_SOURCE_DIR}/src/xfift/xfift.cpp"
"${CMAKE_SOURCE_DIR}/src/xresult.cpp"
"${CMAKE_SOURCE_DIR}/src/xutil.cpp"
"${CMAKE_SOURCE_DIR}/test/test_xfift.cpp"
"${CMAKE_SOURCE_DIR}/test/test_xfunc.cpp"
"${CMAKE_SOURCE_DIR}/test/main.cpp"
)

Expand All @@ -121,8 +186,9 @@ target_link_libraries(${XFIFT_TEST_EXE} PUBLIC
gtest
gtest_main
libzmq-static
xeus_static
xeus_static
fift-lib
func-lib
stdc++fs
)

Expand All @@ -134,7 +200,7 @@ add_test(NAME "test" COMMAND test)
set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY TRUE)

# Install my_kernel
install(TARGETS ${EXECUTABLE_NAME}
install(TARGETS ${XFIFT_EXE} ${XFUNC_EXE}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

# Configuration and data directories for jupyter and my_kernel
Expand All @@ -148,4 +214,8 @@ install(DIRECTORY ${MY_KERNELSPEC_DIR}

# Copy Fift libraries
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/third-party/ton/crypto/fift/lib/
DESTINATION /usr/lib/fift)
DESTINATION /usr/lib/fift)

# Copy FunC libraries
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/third-party/ton/crypto/smartcont/stdlib.fc
DESTINATION /usr/lib/func)
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ COPY . /xeus-fift/

WORKDIR /xeus-fift/build
RUN cmake ..
RUN make xeus-fift
RUN make

RUN export XFIFT_VERSION=$(bash /xeus-fift/scripts/xfift_version.sh) \
&& echo "Jupyter kernel for the Fift language\n" > description-pak \
Expand Down
52 changes: 49 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,63 @@
[![made_with xeus](https://img.shields.io/badge/made_with-xeus-yellowgreen.svg)](https://github.com/QuantStack/xeus)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Jupyter kernel for the Fift language (and TVM assembler)
Jupyter kernels for the Fift and FunC languages (and TVM assembler)

![Xeus-Fift](https://i.imgur.com/0UtNcmL.gif)

## Features

### Fift kernel
Aside from the convenient Jupyter interactive workflow this kernel provides:

* Syntax highlighting
* Words autocomplete via `Tab` (including those which are included or defined by you)
* In-place docstrings via `Shift+Tab` (for words defined in FiftBase and TVM pdf files)

### FunC kernel
* Syntax highlighting including (non)const methods and pseudo-namespaces
* Autoindentation
* Autocomplete by keywords and global functions
* Inspect function signature by `Shift+Tab`
* Extra: `#include` macro and `print` debug helper

## FunC Workflow
1. At the top-level of each cell you can use `#include "path/to/file"` macro to load functions from a `.fc` file. They will be available throughout the notebook.
2. You can define multiple functions in any cell, they will also be available from everywhere.
3. At the end of the cell (using separate cell is recommended) you can write piece of code not wrapped by a function definition. It will be automatically used as `main` body. Such code cannot be accessed from another cell.
4. Alternatively, you can specify `main` function explicitly.
5. You can omit `return` and trailing `;` in the main code block - they will be added during the execution.
6. Note, that every time you re-run a cell, all functions defined in this cell are being overwritten.

### Examples
Return constant
```
<<< 2 + 2
>>> 4
```

Return variable
```
<<< int i = 42
>>> 42
```

Return function result
```
<<< int sum(int a, int b) { return a + b; }
... sum(2, 2)
>>> 4
```

Print variables
```
<<< int i = 1;
... int j = 2;
... print(i);
... print(j);
>>> 2 1
```

## How to use

### Run in docker
Expand All @@ -32,7 +78,7 @@ docker run --rm -it -p 127.0.0.1:8888:8888 -v $(pwd):/home/jupyter/notebooks ato
wget https://github.com/atomex-me/xeus-fift/releases/download/0.1.0/xeus-fift_0.1.0-1_amd64.deb -P /tmp/
sudo dpkg -i /tmp/xeus-fift_0.1.0-1_amd64.deb
```
3. Check that Jupyter is now supporting the Fift kernel
3. Check that Jupyter is now supporting Fift and FunC kernels
```
jupyter kernelspec list
```
Expand All @@ -49,7 +95,7 @@ cmake ..
make xeus-fift
sudo make install
```
3. Check that Jupyter is now supporting the Fift kernel
3. Check that Jupyter is now supporting Fift and FunC kernels
```
jupyter kernelspec list
```
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ def gen_docstring_hpp():
word_definitions = get_word_definitions()
asm_definitions = get_asm_definitions()
register_definitions = get_register_definitions() + extra_definitions
template = read_file('src/docstring.hpp.in')
template = read_file('src/xfift/docstring.hpp.in')
placeholder = next(line for line in template.split('\n') if line.endswith('item_template'))
item_template = placeholder.rstrip('// item_template')
docs = '\n'.join(map(lambda x: item_template.format(**x), word_definitions + asm_definitions + register_definitions))
data = template.replace(placeholder, docs)
write_file('src/docstring.hpp', data)
write_file('src/xfift/docstring.hpp', data)


if __name__ == '__main__':
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions scripts/xfift_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
# This script extracts the Xeus-Fift version from ../src/xfift.hpp (working dir is build/ or scriots/), which is the master
# location for this information.
#
if [ ! -f ../src/xfift.hpp ]; then
echo "version.sh: error: ../src/xfift.hpp does not exist" 1>&2
if [ ! -f ../src/version.h ]; then
echo "version.sh: error: ../src/version.h does not exist" 1>&2
exit 1
fi
MAJOR=`egrep '^#define +XEUS_FIFT_VERSION_MAJOR +[0-9]+$' ../src/xfift.hpp`
MINOR=`egrep '^#define +XEUS_FIFT_VERSION_MINOR +[0-9]+$' ../src/xfift.hpp`
PATCH=`egrep '^#define +XEUS_FIFT_VERSION_PATCH +[0-9]+$' ../src/xfift.hpp`
MAJOR=`egrep '^#define +XEUS_FIFT_VERSION_MAJOR +[0-9]+$' ../src/version.h`
MINOR=`egrep '^#define +XEUS_FIFT_VERSION_MINOR +[0-9]+$' ../src/version.h`
PATCH=`egrep '^#define +XEUS_FIFT_VERSION_PATCH +[0-9]+$' ../src/version.h`
if [ -z "$MAJOR" -o -z "$MINOR" -o -z "$PATCH" ]; then
echo "xfift_version.sh: error: could not extract version from ../src/xfift.hpp" 1>&2
echo "xfift_version.sh: error: could not extract version from ../src/version.h" 1>&2
exit 1
fi
MAJOR=`echo $MAJOR | awk '{ print $3 }'`
Expand Down
Loading

0 comments on commit c30f24c

Please sign in to comment.