Skip to content

Commit

Permalink
Dev (#1)
Browse files Browse the repository at this point in the history
version 0.1.0
  • Loading branch information
mudream4869 authored Nov 17, 2020
1 parent b99f2f4 commit 07a116a
Show file tree
Hide file tree
Showing 83 changed files with 4,343 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .clang-format
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
65 changes: 65 additions & 0 deletions CMakeLists.txt
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)
115 changes: 115 additions & 0 deletions README.md
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;
}
```
66 changes: 66 additions & 0 deletions cablin_examples/test.yaml
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
39 changes: 39 additions & 0 deletions cablin_examples/test2.yaml
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
41 changes: 41 additions & 0 deletions cablin_examples/test3.yaml
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:
24 changes: 24 additions & 0 deletions cablin_examples/test4.yaml
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

16 changes: 16 additions & 0 deletions cablin_examples/test5-1.yaml
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
Loading

0 comments on commit 07a116a

Please sign in to comment.