Skip to content

Commit

Permalink
Merge commit '9319f188428fc9f45b7ed4ac9c81b20e00b76a2b'
Browse files Browse the repository at this point in the history
  • Loading branch information
vedderb committed Jul 20, 2022
2 parents 647d751 + 9319f18 commit 8a94e49
Show file tree
Hide file tree
Showing 17 changed files with 402 additions and 303 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
File, Load time (s), Eval time (s), GC avg time (us), GC min time (us), GC max time (us), GC invocations, GC least free
q2.lisp, 0.002300000, 1.304999947, 523.728820800, 300, 600, 59, 1920
fibonacci_tail.lisp, 0.002799999, 0.005499999, 300.000000000, 300, 300, 1, 2047
dec_cnt3.lisp, 0.001900000, 1.073099970, 480.000000000, 300, 500, 15, 1996
dec_cnt1.lisp, 0.001500000, 3.050299882, 476.000000000, 300, 500, 100, 2014
fibonacci.lisp, 0.001799999, 3.178699970, 491.489349365, 300, 500, 94, 1968
tak.lisp, 0.002799999, 2.837100028, 538.423645019, 300, 600, 203, 1854
dec_cnt2.lisp, 0.001500000, 2.323600053, 482.000000000, 300, 500, 100, 2014
insertionsort.lisp, 0.004499999, 0.007199999, 300.000000000, 300, 300, 1, 2047
Binary file added lispBM/lispBM/doc/images/cons_cell.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lispBM/lispBM/doc/images/list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lispBM/lispBM/doc/images/pair.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 43 additions & 2 deletions lispBM/lispBM/doc/lbmref.md
Original file line number Diff line number Diff line change
Expand Up @@ -786,12 +786,27 @@ has been extended with the binding `(apa 1)`.

## Lists and cons cells

Lists are build using cons cells. A cons cell is represented by the \ref lbm_cons_t struct in the
Lists are build using cons cells. A cons cell is represented by the lbm_cons_t struct in the
implementation and consists of two fields named the `car` and the `cdr`.
There is no special meaning associated with the `car` and the `cdr` each can hold
a \ref lbm_value. See <a href="#cons">cons</a> and <a href="#list">list</a> for two ways to create structures of
a lbm_value. See <a href="#cons">cons</a> and <a href="#list">list</a> for two ways to create structures of
cons cells on the heap.

![cons cell](images/cons_cell.png?raw=true "cons cell")

A cons cell can be used to store a pair of values. You create a pair by
sticking a value in both the car and cdr field of a cons cell using either `'(1 . 2)` or
`(cons 1 2)`.

![pair](images/pair.png?raw=true "pair")

A list is a number of cons cells linked together where the car fields hold values
and the cdr fields hold pointers (the last cdr field is nil). The list below
can be created either as `'(1 2 3)` or as `(list 1 2 3)`.

![list](images/list.png?raw=true "pair")


### car

Use `car` to access the `car` field of a cons cell. A
Expand All @@ -814,6 +829,19 @@ The `car` operation accesses the head element of a list. The following program e

---

### first

`first` is an alternative (and one that makes some sense) name for the `car` operation.

Use `first` to access the first element of a list or pair. A `first` expression has the form `(first expr)`.

```lisp
# (first (list 1 2 3 4))
> 1
```

---

### cdr

Use `cdr` to access the `cdr` field of a cons cell. A
Expand All @@ -830,6 +858,19 @@ The `cdr` operation gives you the rest of a list. The example below evaluates to

---

### rest

`rest` is an alternative name for the `cdr` operation.

Use `rest` to access all elements except the first one of a list, or to access the second element in a pair. A `rest` expression has the form `(rest expr)`.

```lisp
# (rest (list 1 2 3 4))
> (2 3 4)
```

---

### cons

The `cons` operation allocates a cons cell from the heap and populates the
Expand Down
128 changes: 42 additions & 86 deletions lispBM/lispBM/esp-examples/repl/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_spi_flash.h"
#include "driver/uart.h"

#include "lispbm.h"
#include "lbm_llama_ascii.h"
Expand All @@ -36,67 +35,23 @@

#include "lbm_custom_type.h"

#define UART_NUM 0
#define UART_BAUDRATE 115200
#define UART_TX 21
#define UART_RX 20


void uart_init(void) {
uart_config_t uart_config = {
.baud_rate = UART_BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_DEFAULT,
};

uart_driver_install(UART_NUM, 512, 512, 0, 0, 0);
uart_param_config(UART_NUM, &uart_config);
uart_set_pin(UART_NUM, UART_TX, UART_RX, -1, -1);
}

int get_char(void) {
uint8_t c;
int r = 0;
do {
r = uart_read_bytes(UART_NUM, &c, 1, portMAX_DELAY);
} while (r == 0);
return (int)c;
}

void uart_printf(const char* fmt, ...) {
char buffer[256];
va_list args;
va_start (args, fmt);
int n = vsnprintf (buffer,256,fmt, args);
va_end (args);
if (n > 0) {
uart_write_bytes(UART_NUM, buffer, n);
}
}

void put_char(char c) {
uart_write_bytes(UART_NUM, &c, 1);
}

int inputline(char *buffer, int size) {
int n = 0;
unsigned char c;

for (n = 0; n < size - 1; n++) {

c = get_char();
c = getchar();
switch (c) {
case 127: /* fall through to below */
case '\b': /* backspace character received */
if (n > 0)
n--;
buffer[n] = 0;
put_char(0x8); /* output backspace character */
put_char(' ');
put_char(0x8);
putchar(0x8); /* output backspace character */
putchar(' ');
putchar(0x8);
n--; /* set up next iteration to deal with preceding char location */
break;
case '\n': /* fall through to \r */
Expand All @@ -105,10 +60,13 @@ int inputline(char *buffer, int size) {
return n;
default:
if (isprint(c)) { /* ignore non-printable characters */
put_char(c);
putchar(c);
buffer[n] = c;
} else {
n -= 1;
// Yield or get a complaint from the watchdog.
// No character received or unprintable.
vTaskDelay(1);
n -= 1;
}
break;
}
Expand Down Expand Up @@ -156,9 +114,9 @@ void done_callback(eval_context_t *ctx) {
int print_ret = lbm_print_value(output, PRINT_SIZE, t);

if (print_ret >= 0) {
uart_printf("<< Context %d finished with value %s >>\r\n", cid, output);
printf("<< Context %d finished with value %s >>\r\n", cid, output);
} else {
uart_printf("<< Context %d finished with value %s >>\r\n", cid, output);
printf("<< Context %d finished with value %s >>\r\n", cid, output);
}
}

Expand Down Expand Up @@ -186,21 +144,21 @@ lbm_value ext_print(lbm_value *args, lbm_uint argn) {
lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(t);
switch (array->elt_type){
case LBM_TYPE_CHAR:
uart_printf("%s", (char*)array->data);
printf("%s", (char*)array->data);
break;
default:
return lbm_enc_sym(SYM_NIL);
break;
}
} else if (lbm_type_of(t) == LBM_TYPE_CHAR) {
if (lbm_dec_char(t) =='\n') {
uart_printf("\r\n");
printf("\r\n");
} else {
uart_printf("%c", lbm_dec_char(t));
printf("%c", lbm_dec_char(t));
}
} else {
lbm_print_value(output, 1024, t);
uart_printf("%s", output);
printf("%s", output);
}
}
return lbm_enc_sym(SYM_TRUE);
Expand All @@ -211,7 +169,7 @@ static char outbuf[1024];
void print_ctx_info(eval_context_t *ctx, void *arg1, void *arg2) {
(void)arg2;
lbm_print_value(outbuf, 1024, ctx->r);
uart_printf("%s %x %u %u %s\r\n", (char*)arg1, (uint32_t)ctx, ctx->id, ctx->K.sp, outbuf);
printf("%s %x %u %u %s\r\n", (char*)arg1, (uint32_t)ctx, ctx->id, ctx->K.sp, outbuf);
}

void ctx_exists(eval_context_t *ctx, void *arg1, void *arg2) {
Expand All @@ -234,29 +192,27 @@ void app_main(void)
lbm_heap_state_t heap_state;

vTaskDelay(1000);
uart_init();


if (!lbm_init(heap, HEAP_SIZE,
gc_stack_storage, GC_STACK_SIZE,
memory, LBM_MEMORY_SIZE_8K,
bitmap, LBM_MEMORY_BITMAP_SIZE_8K,
print_stack_storage, PRINT_STACK_SIZE,
extension_storage, EXTENSION_STORAGE_SIZE)) {
uart_printf("LispBM Init failed.\r\n");
printf("LispBM Init failed.\r\n");
return;
}
uart_printf("LispBM Initialized\r\n");
printf("LispBM Initialized\r\n");

lbm_set_ctx_done_callback(done_callback);
lbm_set_timestamp_us_callback(timestamp_callback);
lbm_set_usleep_callback(sleep_callback);

res = lbm_add_extension("print", ext_print);
if (res)
uart_printf("Extension added.\r\n");
printf("Extension added.\r\n");
else
uart_printf("Error adding extension.\r\n");
printf("Error adding extension.\r\n");


TaskHandle_t eval_thd = NULL;
Expand All @@ -269,40 +225,40 @@ void app_main(void)
);

if( status == pdPASS ) {
uart_printf("Evaluator thread started\r\n");
printf("Evaluator thread started\r\n");
}

uart_printf("LispBM Version %d.%d.%d\r\n", LBM_MAJOR_VERSION, LBM_MINOR_VERSION, LBM_PATCH_VERSION);
uart_printf("Lisp REPL started (ESP32C3)\r\n");
printf("LispBM Version %d.%d.%d\r\n", LBM_MAJOR_VERSION, LBM_MINOR_VERSION, LBM_PATCH_VERSION);
printf("Lisp REPL started (ESP32C3)\r\n");

while (1) {
uart_printf("# ");
printf("# ");
memset(str,0,1024);
memset(outbuf,0, 1024);
inputline(str, 1024);
uart_printf("\r\n");
printf("\r\n");
if (strncmp(str, ":info", 5) == 0) {
uart_printf("------------------------------------------------------------\r\n");
uart_printf("Used cons cells: %lu \r\n", HEAP_SIZE - lbm_heap_num_free());
uart_printf("Free cons cells: %lu\r\n", lbm_heap_num_free());
printf("------------------------------------------------------------\r\n");
printf("Used cons cells: %lu \r\n", HEAP_SIZE - lbm_heap_num_free());
printf("Free cons cells: %lu\r\n", lbm_heap_num_free());
lbm_get_heap_state(&heap_state);
uart_printf("GC counter: %lu\r\n", heap_state.gc_num);
uart_printf("Recovered: %lu\r\n", heap_state.gc_recovered);
uart_printf("Marked: %lu\r\n", heap_state.gc_marked);

uart_printf("Array and symbol string memory:\r\n");
uart_printf(" Size: %u 32Bit words\r\n", lbm_memory_num_words());
uart_printf(" Free: %u 32Bit words\r\n", lbm_memory_num_free());
uart_printf("------------------------------------------------------------\r\n");
printf("GC counter: %lu\r\n", heap_state.gc_num);
printf("Recovered: %lu\r\n", heap_state.gc_recovered);
printf("Marked: %lu\r\n", heap_state.gc_marked);

printf("Array and symbol string memory:\r\n");
printf(" Size: %u 32Bit words\r\n", lbm_memory_num_words());
printf(" Free: %u 32Bit words\r\n", lbm_memory_num_free());
printf("------------------------------------------------------------\r\n");
memset(outbuf,0, 1024);
} else if (strncmp(str, ":env", 4) == 0) {
lbm_value curr = *lbm_get_env_ptr();
uart_printf("Environment:\r\n");
printf("Environment:\r\n");
while (lbm_type_of(curr) == LBM_TYPE_CONS) {
res = lbm_print_value(outbuf,1024, lbm_car(curr));
curr = lbm_cdr(curr);

uart_printf(" %s \r\n", outbuf);
printf(" %s \r\n", outbuf);
}
} else if (strncmp(str, ":ctxs", 5) == 0) {
lbm_running_iterator(print_ctx_info, "RUNNABLE", NULL);
Expand All @@ -320,15 +276,15 @@ void app_main(void)
while(lbm_get_eval_state() != EVAL_CPS_STATE_PAUSED) {
sleep_callback(10);
}
uart_printf("Evaluator paused\r\nEnter command :continue to unpause or :step to perform single stepping\r\n");
printf("Evaluator paused\r\nEnter command :continue to unpause or :step to perform single stepping\r\n");
} else if (strncmp(str, ":continue", 9) == 0) {
lbm_continue_eval();
} else if (strncmp(str, ":step", 5) == 0) {
lbm_step_eval();
while(lbm_get_eval_state() != EVAL_CPS_STATE_PAUSED) {
sleep_callback(1);
}
uart_printf("Evaluator paused\r\nEnter command :continue to unpause or :step to perform single stepping\r\n");
printf("Evaluator paused\r\nEnter command :continue to unpause or :step to perform single stepping\r\n");
} else if (strncmp(str, ":reset", 6) == 0) {
lbm_pause_eval();
while(lbm_get_eval_state() != EVAL_CPS_STATE_PAUSED) {
Expand All @@ -341,7 +297,7 @@ void app_main(void)
bitmap, LBM_MEMORY_BITMAP_SIZE_8K,
print_stack_storage, PRINT_STACK_SIZE,
extension_storage, EXTENSION_STORAGE_SIZE)) {
uart_printf("LispBM Init failed.\r\n");
printf("LispBM Init failed.\r\n");
return;
}
lbm_add_extension("print", ext_print);
Expand All @@ -368,7 +324,7 @@ void app_main(void)

lbm_continue_eval();

uart_printf("started ctx: %u\r\n", cid);
printf("started ctx: %u\r\n", cid);
lbm_wait_ctx((lbm_cid)cid, WAIT_TIMEOUT);
}
}
Expand Down
3 changes: 3 additions & 0 deletions lispBM/lispBM/include/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,9 @@ static inline bool lbm_is_match_binder(lbm_value exp) {
(lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_I32) ||
(lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_U32) ||
(lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_FLOAT) ||
(lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_I64) ||
(lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_U64) ||
(lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_DOUBLE) ||
(lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_CONS)));
}

Expand Down
4 changes: 4 additions & 0 deletions lispBM/lispBM/include/lbm_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

/*! \page changelog Changelog
Jul 18 2022: Version 0.5.4
- Added pattern matching support for i64, u64 and double.
- Fixed issue with pattern matching on i32, u32.
Jul 17 2022: Version 0.5.4
- Refactoring with readability in focus.
- Computing encodings of commonly used symbol constants (for eval_cps) at compile time
Expand Down
Loading

0 comments on commit 8a94e49

Please sign in to comment.