Added break support to loops

This commit is contained in:
Benjamin Vedder 2022-03-31 23:22:26 +02:00
parent 4325ae1ab7
commit f85dd3e346
4 changed files with 37 additions and 11 deletions

View File

@ -24,7 +24,7 @@
#define FW_VERSION_MAJOR 6
#define FW_VERSION_MINOR 00
// Set to 0 for building a release and iterate during beta test builds
#define FW_TEST_VERSION_NUMBER 30
#define FW_TEST_VERSION_NUMBER 31
#include "datatypes.h"

View File

@ -1118,6 +1118,32 @@ f
```
#### break
```clj
(break retval)
```
break can be used to break out of a loop and return retval (the result of the loop will be retval, otherwise the result of the loop will be the result of the last expression in it). break works in all of the loops above. Example:
```clj
; Below we make a function to determine if
; the list lst contains number num
(defun contains (num lst)
(loopforeach it lst
(if (= it num)
(break t)
nil
)))
(contains 346 '(12 33 452 11 22 346 99 12))
> t
(contains 347 '(12 33 452 11 22 346 99 12))
> nil
```
### Useful Lisp Functions
There are a number of lisp functions that can be used from lispBM in the VESC firmware. They will be loaded to the environment the first time they are used, so they do not use up memory before the first use.

View File

@ -26,7 +26,7 @@
#include "timeout.h"
#include "lispbm.h"
#define HEAP_SIZE 1536
#define HEAP_SIZE 2048
#define LISP_MEM_SIZE LBM_MEMORY_SIZE_8K
#define LISP_MEM_BITMAP_SIZE LBM_MEMORY_BITMAP_SIZE_8K
#define GC_STACK_SIZE 160

View File

@ -104,21 +104,21 @@ static const char* macros[] = {
"(define defun (macro (name args body) `(define ,name (lambda ,args ,body))))",
"(define loopfor (macro (it start cond update body)"
"`(let ((loop (lambda (,it res)(if ,cond (loop ,update ,body) res"
"))))(loop ,start nil))))",
"`(let ((loop (lambda (,it res break)(if ,cond (loop ,update ,body break) res"
"))))(call-cc (lambda (brk) (loop ,start nil brk))))))",
"(define loopwhile (macro (cond body)"
"`(let ((loop (lambda (res)(if ,cond (loop ,body)res"
"))))(loop nil))))",
"`(let ((loop (lambda (res break)(if ,cond (loop ,body break) res"
"))))(call-cc (lambda (brk) (loop nil brk))))))",
"(define looprange (macro (it start end body)"
"`(let ((loop (lambda (,it res)(if (< ,it ,end)(loop (+ ,it 1),body)res"
"))))(loop ,start nil))))",
"`(let ((loop (lambda (,it res break)(if (< ,it ,end)(loop (+ ,it 1),body break) res"
"))))(call-cc (lambda (brk) (loop ,start nil brk))))))",
"(define loopforeach (macro (it lst body)"
"`(let ((loop (lambda (,it rst res)"
"(if (eq ,it nil) res (loop (car rst) (cdr rst) ,body)"
")))) (loop (car ,lst) (cdr ,lst) nil))))",
"`(let ((loop (lambda (,it rst res break)"
"(if (eq ,it nil) res (loop (car rst) (cdr rst) ,body break)"
"))))(call-cc (lambda (brk) (loop (car ,lst) (cdr ,lst) nil brk))))))",
};
static bool strmatch(const char *str1, const char *str2) {