mirror of https://github.com/arendst/Tasmota.git
Merge pull request #11692 from s-hadinger/berry_reorg
Berry directory reorg
This commit is contained in:
commit
3c4ebbf1d3
|
@ -1,7 +0,0 @@
|
|||
name=Berry
|
||||
version=0.1.10
|
||||
author=Guan Wenliang <skiars@qq.com>,
|
||||
maintainer=Stephan Hadinger <stephan.hadinger@gmail.com>
|
||||
sentence=Berry scripting language for Tasmota32
|
||||
paragraph=Berry is a ultra-lightweight dynamically typed embedded scripting language.
|
||||
architectures=esp32
|
|
@ -1 +0,0 @@
|
|||
#include "port/berry_conf.h"
|
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
|
@ -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
|
|
@ -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')
|
|
@ -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());
|
|
@ -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))
|
|
@ -0,0 +1,12 @@
|
|||
import debug
|
||||
|
||||
def test_func()
|
||||
try
|
||||
compile('def +() end')()
|
||||
except .. as e, v
|
||||
print('catch execption:', str(e) + ' >>>\n ' + str(v))
|
||||
debug.traceback()
|
||||
end
|
||||
end
|
||||
|
||||
test_func()
|
|
@ -0,0 +1,12 @@
|
|||
import time
|
||||
|
||||
def fib(x)
|
||||
if x <= 2
|
||||
return 1
|
||||
end
|
||||
return fib(x - 1) + fib(x - 2)
|
||||
end
|
||||
|
||||
c = time.clock()
|
||||
print("fib:", fib(38)) # minimum stack size: 78!!
|
||||
print("time:", time.clock() - c, 's')
|
|
@ -0,0 +1,26 @@
|
|||
import time
|
||||
import math
|
||||
|
||||
math.srand(time.time())
|
||||
res = math.rand() % 100
|
||||
max_test = 7
|
||||
test = -1
|
||||
idx = 1
|
||||
print('Guess a number between 0 and 99. You have', max_test, 'chances.')
|
||||
while test != res && idx <= max_test
|
||||
test = number(input(str(idx) + ': enter the number you guessed: '))
|
||||
if type(test) != 'int'
|
||||
print('This is not an integer. Continue!')
|
||||
continue
|
||||
elif test > res
|
||||
print('This number is too large.')
|
||||
elif test < res
|
||||
print('This number is too small.')
|
||||
end
|
||||
idx = idx + 1
|
||||
end
|
||||
if test == res
|
||||
print('You win!')
|
||||
else
|
||||
print('You failed, the correct answer is', res)
|
||||
end
|
|
@ -0,0 +1,4 @@
|
|||
import json
|
||||
print(json.load('{"key": "value"}'))
|
||||
print(json.dump({'test key': nil}))
|
||||
print(json.dump({'key1': nil, 45: true}, 'format'))
|
|
@ -0,0 +1,8 @@
|
|||
# simple lambda example
|
||||
print((/a b c-> a * b + c)(2, 3, 4))
|
||||
|
||||
# Y-Combinator and factorial functions
|
||||
Y = /f-> (/x-> f(/n-> x(x)(n)))(/x-> f(/n-> x(x)(n)))
|
||||
F = /f-> /x-> x ? f(x - 1) * x : 1
|
||||
fact = Y(F)
|
||||
print('fact(10) == ' .. fact(10))
|
|
@ -0,0 +1,16 @@
|
|||
import os
|
||||
|
||||
def scandir(path)
|
||||
print('path: ' + path)
|
||||
for name : os.listdir(path)
|
||||
var fullname = os.path.join(path, name)
|
||||
if os.path.isfile(fullname)
|
||||
print('file: ' + fullname)
|
||||
else
|
||||
print('path: ' + fullname)
|
||||
scandir(fullname)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
scandir('.')
|
|
@ -0,0 +1,42 @@
|
|||
def qsort(data)
|
||||
# do once sort
|
||||
def once(left, right)
|
||||
var pivot = data[left] # use the 0th value as the pivot
|
||||
while left < right # check if sort is complete
|
||||
# put the value less than the pivot to the left
|
||||
while left < right && data[right] >= pivot
|
||||
right -= 1 # skip values greater than pivot
|
||||
end
|
||||
data[left] = data[right]
|
||||
# put the value greater than the pivot on the right
|
||||
while left < right && data[left] <= pivot
|
||||
left += 1 # skip values less than pivot
|
||||
end
|
||||
data[right] = data[left]
|
||||
end
|
||||
# now we have the index of the pivot, store it
|
||||
data[left] = pivot
|
||||
return left # return the index of the pivot
|
||||
end
|
||||
# recursive quick sort algorithm
|
||||
def _sort(left, right)
|
||||
if left < right # executed when the array is not empty
|
||||
var index = once(left, right) # get index of pivot for divide and conquer
|
||||
_sort(left, index - 1) # sort the data on the left
|
||||
_sort(index + 1, right) # sort the data on the right
|
||||
end
|
||||
end
|
||||
# start quick sort
|
||||
_sort(0, data.size() - 1)
|
||||
return data
|
||||
end
|
||||
|
||||
import time, math
|
||||
math.srand(time.time()) # sse system time as a random seed
|
||||
data = []
|
||||
# put 20 random numbers into the array
|
||||
for i : 1 .. 20
|
||||
data.push(math.rand() % 100)
|
||||
end
|
||||
# sort and print
|
||||
print(qsort(data))
|
|
@ -0,0 +1,61 @@
|
|||
do
|
||||
def ismult(msg)
|
||||
import string
|
||||
return string.split(msg, -5)[1] == '\'EOS\''
|
||||
end
|
||||
|
||||
def multline(src, msg)
|
||||
if !ismult(msg)
|
||||
print('syntax_error: ' + msg)
|
||||
return
|
||||
end
|
||||
while true
|
||||
try
|
||||
src += '\n' + input('>> ')
|
||||
return compile(src)
|
||||
except 'syntax_error' as e, m
|
||||
if !ismult(m)
|
||||
print('syntax_error: ' + m)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse()
|
||||
var fun, src = input('> ')
|
||||
try
|
||||
fun = compile('return (' + src + ')')
|
||||
except 'syntax_error' as e, m
|
||||
try
|
||||
fun = compile(src)
|
||||
except 'syntax_error' as e, m
|
||||
fun = multline(src, m)
|
||||
end
|
||||
end
|
||||
return fun
|
||||
end
|
||||
|
||||
def run(fun)
|
||||
try
|
||||
var res = fun()
|
||||
if res print(res) end
|
||||
except .. as e, m
|
||||
import debug
|
||||
print(e .. ': ' .. m)
|
||||
debug.traceback()
|
||||
end
|
||||
end
|
||||
|
||||
def repl()
|
||||
while true
|
||||
var fun = parse()
|
||||
if fun != nil
|
||||
run(fun)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
print("Berry Berry REPL!")
|
||||
repl()
|
||||
end
|
|
@ -0,0 +1,32 @@
|
|||
s = "This is a long string test. 0123456789 abcdefg ABCDEFG"
|
||||
print(s)
|
||||
|
||||
a = .5
|
||||
print(a)
|
||||
|
||||
import string as s
|
||||
|
||||
print(s.hex(0x45678ABCD, 16))
|
||||
|
||||
def bin(x, num)
|
||||
assert(type(x) == 'int', 'the type of \'x\' must be integer')
|
||||
# test the 'x' bits
|
||||
var bits = 1
|
||||
for i : 0 .. 62
|
||||
if x & (1 << 63 - i)
|
||||
bits = 64 - i
|
||||
break
|
||||
end
|
||||
end
|
||||
if type(num) == 'int' && num > 0 && num <= 64
|
||||
bits = bits < num ? num : bits
|
||||
end
|
||||
var result = ''
|
||||
bits -= 1
|
||||
for i : 0 .. bits
|
||||
result += x & (1 << (bits - i)) ? '1' : '0'
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
print(bin(33))
|
|
@ -0,0 +1,7 @@
|
|||
import string
|
||||
|
||||
print(string.format('%.3d', 12))
|
||||
print(string.format('%.3f', 12))
|
||||
print(string.format('%20.7f', 14.5))
|
||||
print(string.format('-- %-40s ---', 'this is a string format test'))
|
||||
print(string.format('-- %40s ---', 'this is a string format test'))
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"name":"Berry",
|
||||
"description":"Berry scripting language for Tasmota32",
|
||||
"keywords":"berry, script",
|
||||
"authors":
|
||||
{
|
||||
"name": "Guan Wenliang <skiars@qq.com>, ",
|
||||
"maintainer": true
|
||||
},
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/Skiars/berry"
|
||||
},
|
||||
"version": "7.0",
|
||||
"license": "MIT License",
|
||||
"frameworks": "*",
|
||||
"platforms": "*",
|
||||
"build": {
|
||||
"srcFilter": [
|
||||
"+<*.c>",
|
||||
"+<../default/*.c>",
|
||||
"+<../default/*.cpp>",
|
||||
"+<../default/*.hpp>",
|
||||
"+<*.cpp>",
|
||||
"+<*.h>"
|
||||
]
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue