-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
83 changed files
with
4,343 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
BasedOnStyle: Google | ||
IndentWidth: 4 | ||
AllowShortFunctionsOnASingleLine: None | ||
|
||
AccessModifierOffset: -4 | ||
AlignTrailingComments: true | ||
MaxEmptyLinesToKeep: 2 | ||
|
||
IndentCaseLabels: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
cmake_minimum_required(VERSION 3.6.2) | ||
project(mukyu_cablin) | ||
|
||
find_package(yaml-cpp REQUIRED) | ||
|
||
include_directories(${YAML_CPP_INCLUDE_DIRS}) | ||
|
||
include_directories(include) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
|
||
set(cablin_core_src | ||
src/core/controller.cpp | ||
"src/core/script.cpp") | ||
|
||
set(cablin_command_src | ||
"src/command/factory.cpp" | ||
"src/command/if.cpp" | ||
"src/command/block.cpp" | ||
"src/command/call.cpp" | ||
"src/command/var.cpp" | ||
"src/command/globalvar.cpp" | ||
"src/command/while.cpp" | ||
"src/command/assign.cpp") | ||
|
||
set(cablin_function_src | ||
src/function/print.cpp | ||
src/function/node.cpp) | ||
|
||
set(cablin_expr_src | ||
src/expr/call.cpp | ||
src/expr/getvar.cpp | ||
src/expr/factory.cpp) | ||
|
||
set(cablin_package_src | ||
src/package/builtin_io.cpp | ||
src/package/builtin_op.cpp | ||
src/package/builtin_cast.cpp | ||
src/package/factory.cpp | ||
src/package/userdefine.cpp) | ||
|
||
add_library(cablin | ||
${cablin_core_src} | ||
${cablin_command_src} | ||
${cablin_function_src} | ||
${cablin_package_src} | ||
${cablin_expr_src}) | ||
|
||
target_link_libraries(cablin | ||
${YAML_CPP_LIBRARIES}) | ||
|
||
# ---------- | ||
# Interpreter | ||
|
||
add_subdirectory(interpreter) | ||
|
||
# ---------- | ||
# Example | ||
|
||
add_subdirectory(example) | ||
|
||
# ---------- | ||
# Unit Test | ||
|
||
add_subdirectory(test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# Cablin README | ||
|
||
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) | ||
|
||
|
||
A Cablin interpreter written in c-plus-plus. | ||
|
||
## How to build and execute examples | ||
|
||
```bash | ||
# Build | ||
mkdir build | ||
cd build | ||
cmake .. | ||
|
||
# Execute interpreter | ||
interpreter/interpreter ../cablin_examples/test.yaml | ||
|
||
# Execute example | ||
example/example1 | ||
example/example2 | ||
|
||
``` | ||
|
||
## Use Cablin as an Embedding Script | ||
|
||
### Hello world | ||
|
||
Load the script and execute the `hello_world` function defined in script. | ||
|
||
```cpp | ||
#include <mukyu/cablin/core/script.hpp> | ||
|
||
namespace mccore = mukyu::cablin::core; | ||
|
||
int main(int argc, char** argv) { | ||
std::string body = R"( | ||
- import: io | ||
|
||
- func: | ||
name: hello_world | ||
body: | ||
- call: | ||
name: io::print | ||
params: | ||
- const: | ||
type: string | ||
value: Hello world! | ||
)"; | ||
auto node = YAML::Load(body); | ||
mccore::Script script(node); | ||
script.callFunction("hello_world", {}); | ||
return 0; | ||
} | ||
``` | ||
### Call c++ function from Cablin script | ||
[➡️ Complete code](example/example2.cpp) | ||
```cpp | ||
namespace mccore = mukyu::cablin::core; | ||
namespace mcfunc = mukyu::cablin::function; | ||
const std::string MYPACKAGE_NAME = "my"; | ||
class MyPackage : public mccore::Package { | ||
public: | ||
MyPackage() = default; | ||
~MyPackage() = default; | ||
// The prepare function register all the function and variable need in package | ||
void prepare(mccore::Controller* controller) { | ||
// Register package name | ||
controller->addPackage(MYPACKAGE_NAME); | ||
auto printFunc = [](mccore::ValueList params) { | ||
// ... | ||
}; | ||
// Register print function | ||
controller->addFunction( | ||
MYPACKAGE_NAME, | ||
std::make_shared<mcfunc::FunctionFunctor>("print", printFunc)); | ||
} | ||
}; | ||
int main(int argc, char** argv) { | ||
std::string body = R"( | ||
- import: my | ||
|
||
- func: | ||
name: hello_world | ||
body: | ||
- call: | ||
name: my::print | ||
params: | ||
- const: | ||
type: string | ||
value: Hello world call from script! | ||
)"; | ||
auto node = YAML::Load(body); | ||
// Prepare self-define package | ||
mccore::Script::PackagePtrMap packages = { | ||
{MYPACKAGE_NAME, std::make_shared<MyPackage>()}}; | ||
mccore::Script script(node, packages); | ||
script.callFunction("hello_world", {}); | ||
return 0; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
- import: op | ||
- import: io | ||
|
||
- func: | ||
name: main | ||
body: | ||
# var a = 1 | ||
- var: | ||
type: int | ||
name: a | ||
default_value: 1 | ||
|
||
# var b = 2 | ||
- var: | ||
type: int | ||
name: b | ||
default_value: 2 | ||
|
||
# print(a) | ||
- call: | ||
name: io::print | ||
params: | ||
- get: a | ||
|
||
# print(b) | ||
- call: | ||
name: io::print | ||
params: | ||
- get: b | ||
|
||
# var c | ||
- var: | ||
type: bool | ||
name: c | ||
default_value: false | ||
|
||
# a = a + b | ||
- assign: | ||
target: a | ||
source: | ||
# should be command:call | ||
call: | ||
name: op::plus | ||
params: | ||
- get: a | ||
- get: b | ||
|
||
# print(a) | ||
- call: | ||
name: io::print | ||
params: | ||
- get: a | ||
|
||
- if: | ||
condition: | ||
get: c | ||
then: | ||
- call: | ||
name: io::print | ||
params: | ||
- get: a | ||
else: | ||
- call: | ||
name: io::print | ||
params: | ||
- get: b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
- import: io | ||
- import: op | ||
|
||
- func: | ||
name: main | ||
body: | ||
# var a = 10 | ||
- var: | ||
type: int | ||
name: a | ||
default_value: 10 | ||
|
||
- while: | ||
condition: | ||
call: | ||
name: op::greater | ||
params: | ||
- get: a | ||
- const: | ||
type: int | ||
value: 1 | ||
body: | ||
# print(a) | ||
- call: | ||
name: io::print | ||
params: | ||
- get: a | ||
|
||
# a = a - one | ||
- assign: | ||
target: a | ||
source: | ||
call: | ||
name: op::minus | ||
params: | ||
- get: a | ||
- const: | ||
type: int | ||
value: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
- import: op | ||
- import: io | ||
|
||
- func: | ||
name: main | ||
body: | ||
# var a = 10 | ||
- var: | ||
type: int | ||
name: a | ||
default_value: 10 | ||
|
||
- while: | ||
condition: | ||
call: | ||
name: op::greater | ||
params: | ||
- get: a | ||
- const: | ||
type: int | ||
value: 1 | ||
body: | ||
# print(a) | ||
- call: | ||
name: io::print | ||
params: | ||
- get: a | ||
|
||
# a = a - one | ||
- assign: | ||
target: a | ||
source: | ||
call: | ||
name: op::minus | ||
params: | ||
- get: a | ||
- const: | ||
type: int | ||
value: 1 | ||
|
||
- break: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
- import: io | ||
|
||
- func: | ||
name: myFunc | ||
params: | ||
- name: a | ||
type: int | ||
body: | ||
- call: | ||
name: io::print | ||
params: | ||
- get: a | ||
|
||
- func: | ||
name: main | ||
body: | ||
# myFunc(10) | ||
- call: | ||
name: myFunc | ||
params: | ||
- const: | ||
type: int | ||
value: 10 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
- import: test5 | ||
|
||
- func: | ||
name: main | ||
body: | ||
# var a = 10 | ||
- var: | ||
type: int | ||
name: a | ||
default_value: 30 | ||
|
||
# test5::myFunc(a) | ||
- call: | ||
name: test5::myFunc | ||
params: | ||
- get: a |
Oops, something went wrong.