Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
steinerkelvin committed May 25, 2024
1 parent 50e18c4 commit 9c4c432
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"cSpell.words": [
"benda",
"bjit",
"Clippy",
"dataclass",
"dataclasses",
"nixpkgs",
"Numba"
]
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This is in conceptual stage.

```py
from dataclasses import dataclass
from benda import bend, u24
from benda import bjit, u24

@dataclass
class Leaf:
Expand All @@ -25,11 +25,11 @@ class Node:

Tree = Node | Leaf

# The `bend` decorator will introspect and translate the function to HVM/Bend
# The `bjit` decorator will introspect and translate the function to HVM/Bend
# code, replacing it with a wrapper that converts the Python-level types of the
# inputs and result value, Numba-style.

@bend
@bjit
def sum_tree(tree: Tree) -> u24:
match tree:
case Leaf(value=value):
Expand All @@ -52,7 +52,8 @@ class Leaf2:
- Install Nix with [Determinate Nix Installer]

```sh
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
curl --proto '=https' --tlsv1.2 -sSf -L \
https://install.determinate.systems/nix | sh -s -- install
```

- You can run `nix develop` to enter a shell with the dependencies installed.
Expand Down
20 changes: 20 additions & 0 deletions examples/simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import ast, inspect

import benda
from benda import u24

def test_1():
return 4

def test_2():
x = u24(3)
y = x - u24(2)
return y

def get_ast():
my_ast = ast.dump(ast.parse(inspect.getsource(test_2)))
return my_ast

if __name__ == "__main__":
print(get_ast())
print(test_2())
40 changes: 40 additions & 0 deletions examples/tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from dataclasses import dataclass
# from benda import bjit, u24

u24 = int


@dataclass
class Leaf:
value: u24


@dataclass
class Node:
left: 'Tree'
right: 'Tree'


Tree = Node | Leaf


# @bjit
def sum_tree(tree: Tree) -> u24:
match tree:
case Leaf(value):
return value
case Node(left, right):
return sum_tree(left) + sum_tree(right)


def gen_tree(depth: int, n: int) -> Tree:
if depth == 0:
return Leaf(value=n)
else:
return Node(left=gen_tree(depth-1, n-1), right=gen_tree(depth-1, n+1))


if __name__ == "__main__":
tree = gen_tree(4, 10)
print(tree)
print(sum_tree(tree))
7 changes: 6 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
pkgs = import nixpkgs {
inherit system overlays;
};

python3 = pkgs.python312;
rust_toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;

nativeBuildInputs = [
Expand All @@ -24,7 +26,7 @@
pkgs.clang
rust_toolchain
# Python
pkgs.python312
python3
];

dev_packages = [
Expand All @@ -46,6 +48,9 @@
name = "shell";
inherit nativeBuildInputs;
buildInputs = dev_packages;
shellHook = ''
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${python3}/lib
'';
};

packages.default = naersk'.buildPackage {
Expand Down

0 comments on commit 9c4c432

Please sign in to comment.