Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
gemu2015 committed Nov 22, 2024
1 parent a5e92ff commit 28c9641
Show file tree
Hide file tree
Showing 125 changed files with 26,555 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/libesp32/berry/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018-2020 Guan Wenliang

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.
93 changes: 93 additions & 0 deletions lib/libesp32/berry/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
CFLAGS = -Wall -Wextra -std=c99 -O2 -Wno-zero-length-array -Wno-empty-translation-unit -DUSE_BERRY_INT64
DEBUG_FLAGS = -O0 -g -DBE_DEBUG
TEST_FLAGS = $(DEBUG_FLAGS) --coverage -fno-omit-frame-pointer -fsanitize=address -fsanitize=undefined
LIBS = -lm
TARGET = berry
CC = gcc
MKDIR = mkdir
LFLAGS =

INCPATH = src default ../re1.5 ../berry_mapping/src ../berry_int64/src generate
SRCPATH = src default ../re1.5 ../berry_mapping/src ../berry_int64/src
GENERATE = generate
CONFIG = default/berry_conf.h
COC = tools/coc/coc
CONST_TAB = $(GENERATE)/be_const_strtab.h

ifeq ($(OS), Windows_NT) # Windows
CFLAGS += -Wno-format # for "%I64d" warning
LFLAGS += -Wl,--out-implib,berry.lib # export symbols lib for dll linked
TARGET := $(TARGET).exe
PYTHON ?= python # only for windows and need python3
COC := $(PYTHON) $(COC)
else
CFLAGS += -DUSE_READLINE_LIB
LIBS += -lreadline -ldl
OS := $(shell uname)
ifeq ($(OS), Linux)
LFLAGS += -Wl,--export-dynamic
endif
endif

ifneq ($(V), 1)
Q=@
MSG=@echo
else
MSG=@true
endif

SRCS = $(foreach dir, $(SRCPATH), $(wildcard $(dir)/*.c))
OBJS = $(patsubst %.c, %.o, $(SRCS))
DEPS = $(patsubst %.c, %.d, $(SRCS))
INCFLAGS = $(foreach dir, $(INCPATH), -I"$(dir)")

.PHONY : clean

all: $(TARGET)

debug: CFLAGS += $(DEBUG_FLAGS)
debug: all

test: CFLAGS += $(TEST_FLAGS)
test: LFLAGS += $(TEST_FLAGS)
test: all
$(MSG) [Run Testcases...]
$(Q) ./testall.be
$(Q) $(RM) */*.gcno */*.gcda

$(TARGET): $(OBJS)
$(MSG) [Linking...]
$(Q) $(CC) $(OBJS) $(LFLAGS) $(LIBS) -o $@
$(MSG) done

$(OBJS): %.o: %.c
$(MSG) [Compile] $<
$(Q) $(CC) -MM $(CFLAGS) $(INCFLAGS) -MT"$*.d" -MT"$(<:.c=.o)" $< > $*.d
$(Q) $(CC) $(CFLAGS) $(INCFLAGS) -c $< -o $@

sinclude $(DEPS)

$(OBJS): $(CONST_TAB)

$(CONST_TAB): $(GENERATE) $(SRCS) $(CONFIG)
$(MSG) [Prebuild] generate resources
$(Q) $(COC) -o $(GENERATE) $(SRCPATH) -c $(CONFIG)

$(GENERATE):
$(Q) $(MKDIR) $(GENERATE)

install:
cp $(TARGET) /usr/local/bin

uninstall:
$(RM) /usr/local/bin/$(TARGET)

prebuild: $(GENERATE)
$(MSG) [Prebuild] generate resources
$(Q) $(COC) -o $(GENERATE) $(SRCPATH) -c $(CONFIG)
$(MSG) done

clean:
$(MSG) [Clean...]
$(Q) $(RM) $(OBJS) $(DEPS) $(GENERATE)/* berry.lib
$(MSG) done
161 changes: 161 additions & 0 deletions lib/libesp32/berry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<p align="center">
<h1 align="center">
<img src="https://gitee.com/mirrors/Berry/raw/master/berry-logo.png" alt="Berry" width=272 height=128>
</h1>
<p align="center">The Berry Script Language.</p>
</p>

## Introduction

Berry is a ultra-lightweight dynamically typed embedded scripting language. It is designed for lower-performance embedded devices. The Berry interpreter-core's code size is less than 40KiB and can run on less than 4KiB heap (on ARM Cortex M4 CPU, Thumb ISA and ARMCC compiler).

The interpreter of Berry include a one-pass compiler and register-based VM, all the code is written in ANSI C99. In Berry not every type is a class object. Some simple value types, such as int, real, boolean and string are not class object, but list, map and range are class object. This is a consideration about performance.
Register-based VM is the same meaning as above.

Berry has the following advantages:

* Lightweight: A well-optimized interpreter with very little resources. Ideal for use in microprocessors.
* Fast: optimized one-pass bytecode compiler and register-based virtual machine.
* Powerful: supports imperative programming, object-oriented programming, functional programming.
* Flexible: Berry is a dynamic type script, and it's intended for embedding in applications. It can provide good dynamic scalability for the host system.
* Simple: simple and natural syntax, support garbage collection, and easy to use FFI (foreign function interface).
* RAM saving: With compile-time object construction, most of the constant objects are stored in read-only code data segments, so the RAM usage of the interpreter is very low when it starts.

## Documents

Reference Manual: [Wiki](https://github.com/berry-lang/berry/wiki/Reference)

Short Manual (slightly outdated): [berry_short_manual.pdf](https://github.com/Skiars/berry_doc/releases/download/latest/berry_short_manual.pdf).

Berry's EBNF grammar definition: [tools/grammar/berry.ebnf](./tools/grammar/berry.ebnf)

## Features

* Base Type
* Nil: `nil`
* Boolean: `true` and `false`
* Numerical: Integer (`int`) and Real (`real`)
* String: Single quotation-mark string and double quotation-mark string
* Class: Instance template, read only
* Instance: Object constructed by class
* Module: Read-write key-value pair table
* List: Ordered container, like `[1, 2, 3]`
* Map: Hash Map container, like `{ 'a': 1, 2: 3, 'map': {} }`
* Range: include a lower and a upper integer value, like `0..5`
* Operator and Expression
* Assign operator: `=`, `+=`, `-=`, `*=`, `/=`, `%=`, `&=`, `|=`, `^=`, `<<=`, `>>=`
* Relational operator: `<`, `<=`, `==`, `!=`, `>`, `>=`
* Logic operator: `&&`, `||`, `!`
* Arithmetic operator: `+`, `-`, `*`, `/`, `%`
* Bitwise operator: `&`, `|`, `~`, `^`, `<<`, `>>`
* Field operator: `.`
* Subscript operator: `[]`
* Connect string operator: `+`
* Conditional operator: `? :`
* Brackets: `()`
* Control Structure
* Conditional statement: `if-else`
* Iteration statement: `while` and `for`
* Jump statement: `break` and `continue`
* Function
* Local variable and block scope
* Return statement
* Nested functions definition
* Closure based on Upvalue
* Anonymous function
* Lambda expression
* Class
* Inheritance (only public single inheritance)
* Method and Operator Overload
* Constructor method
* Destructive method
* Module Management
* Built-in module that takes almost no RAM
* Extension module support: script module, bytecode file module and shared library (like *.so, *.dll) module
* GC (Garbage collection)
* Mark-Sweep GC
* Exceptional Handling
* Throw any exception value using the `raise` statement
* Multiple catch mode
* Bytecode file support
* Export function to bytecode file
* Load the bytecode file and execute

## Build and Run

1. Install the readline library (Windows does not need):

``` bash
sudo apt install libreadline-dev # Ubuntu
brew install readline # MacOS
```

2. Build (The default compiler is GCC):

```
make
```

3. Run:

``` bash
./berry # Bash or PowerShell
berry # Windows CMD
```

4. Install (Only Unix-like):

``` bash
make install
```

## Editor plugins

[Visual Studio Code](https://code.visualstudio.com/) plugin are in this directory: [./tools/plugins/vscode](./tools/plugins/vscode).

## Examples

After compiling successfully, use the `berry` command with no parameters to enter the REPL environment:
```
Berry 0.0.1 (build in Dec 24 2018, 18:12:49)
[GCC 8.2.0] on Linux (default)
>
```

Now enter this code:

``` lua
print("Hello world!")
```

You will see this output:

```
Hello world!
```

You can copy this code to the REPL:

``` ruby
def fib(x)
if x <= 1
return x
end
return fib(x - 1) + fib(x - 2)
end
fib(10)
```

This example code will output the result `55` and you can save the above code to a plain text file (eg test.be) and run this command:

``` bash
./berry test.be
```

This will also get the correct output.

## License

Berry is free software distributed under the [MIT license](./LICENSE).

The Berry interpreter partly referred to [Lua](http://www.lua.org/)'s design. View Lua's license here: http://www.lua.org/license.html.
Binary file added lib/libesp32/berry/berry-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions lib/libesp32/berry/examples/anon_func.be
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# anonymous function and closure
def count(x)
var arr = []
for i : 0 .. x
arr.push(
def (n) # loop variable cannot be used directly as free variable
return def ()
return n * n
end
end (i) # define and call anonymous function
)
end
return arr
end

for xx : count(6)
print(xx()) # 0, 1, 4 ... n * n
end

return count
15 changes: 15 additions & 0 deletions lib/libesp32/berry/examples/bigloop.be
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import time

c = time.clock()
do
i = 0
while i < 100000000
i += 1
end
end
print('while iteration 100000000 times', time.clock() - c, 's')

c = time.clock()
for i : 1 .. 100000000
end
print('for iteration 100000000 times', time.clock() - c, 's')
60 changes: 60 additions & 0 deletions lib/libesp32/berry/examples/bintree.be
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Reference from https://github.com/BerryMathDevelopmentTeam/BerryMath/blob/master/testscript/BinaryTree.bm

class node
var v, l, r
def init(v, l, r)
self.v = v
self.l = l
self.r = r
end
def insert(v)
if v < self.v
if self.l
self.l.insert(v)
else
self.l = node(v)
end
else
if self.r
self.r.insert(v)
else
self.r = node (v)
end
end
end
def sort(l)
if (self.l) self.l.sort(l) end
l.push(self.v)
if (self.r) self.r.sort(l) end
end
end

class btree
var root
def insert(v)
if self.root
self.root.insert(v)
else
self.root = node(v)
end
end
def sort()
var l = []
if self.root
self.root.sort(l)
end
return l
end
end

var tree = btree()
tree.insert(-100)
tree.insert(5);
tree.insert(3);
tree.insert(9);
tree.insert(10);
tree.insert(10000000);
tree.insert(1);
tree.insert(-1);
tree.insert(-10);
print(tree.sort());
16 changes: 16 additions & 0 deletions lib/libesp32/berry/examples/calcpi.be
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def cpi(n)
i = 2
pi = 3
while i <= n
term = 4.0 / (i * (i + 1) * (i + 2))
if i % 4
pi = pi + term
else
pi = pi - term
end
i = i + 2
end
return pi
end

print("pi =", cpi(100))
Loading

0 comments on commit 28c9641

Please sign in to comment.