WIP: Functional pico REPL (broke native)

This commit is contained in:
2022-11-26 23:27:14 -05:00
parent fe4e42411c
commit bb2b137e2c
9 changed files with 150 additions and 22 deletions

View File

@@ -3,11 +3,13 @@
#include "uclisp.h"
#include "types.h"
#include "plat.h"
#ifdef PLATFORM_PICO_SDK
// TODO:remove
#include "pico/stdio.h"
#include "pico/stdlib.h"
#endif
#define UNUSED __attribute__((unused))
struct ucl *ucl;
@@ -15,30 +17,48 @@ struct ucl *ucl;
struct ucl_object *ucl_car(struct ucl*, struct ucl_object *);
void ucl_print_obj(struct ucl_object *);
// TODO: remove
char buffer[1024] = {0};
void pico_getline() {
int chr;
char *end = &buffer[0];
bool done = false;
while (!done) {
while((chr = getchar_timeout_us(0)) != PICO_ERROR_TIMEOUT)
{
if (chr == '\n' || chr == '\r') {
done = true;
break;
}
chr = chr & 0xFF;
*end = chr;
end++;
printf("%c", chr);
fflush(stdout);
}
}
*end = '\0';
printf("\n");
fflush(stdout);
}
int main() {
#ifdef PLATFORM_PICO_SDK
stdio_usb_init();
gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
bool gpio_state = false;
#endif
platform_init();
ucl = ucl_create();
while(1) {
#ifdef PLATFORM_PICO_SDK
gpio_put(PICO_DEFAULT_LED_PIN, gpio_state);
gpio_state = !gpio_state;
#endif
platform_repl_complete();
// TODO: Work out input for pico
size_t line_len = 512;
char *line = NULL;
printf("> ");
getline(&line, &line_len, stdin);
fflush(stdout);
struct ucl_object *sexp = ucl_parse(ucl, line);
pico_getline();
struct ucl_object *sexp = ucl_parse(ucl, buffer);
struct ucl_object *result = NULL;
if (sexp->type == UCL_TYPE_ERROR) {
result = sexp;
@@ -46,10 +66,38 @@ int main() {
result = ucl_evaluate(ucl, ucl_car(ucl, sexp));
}
ucl_print_obj(result);
printf("\n");
ucl_gc(ucl);
free(line);
printf("\n");
}
return 0;
}
/* int main() { */
/* platform_init(); */
/* ucl = ucl_create(); */
/* while(1) { */
/* platform_repl_complete(); */
/* size_t line_len = 512; */
/* char *line = NULL; */
/* printf("> "); */
/* getline(&line, &line_len, stdin); */
/* struct ucl_object *sexp = ucl_parse(ucl, line); */
/* struct ucl_object *result = NULL; */
/* if (sexp->type == UCL_TYPE_ERROR) { */
/* result = sexp; */
/* } else { */
/* result = ucl_evaluate(ucl, ucl_car(ucl, sexp)); */
/* } */
/* ucl_print_obj(result); */
/* printf("\n"); */
/* ucl_gc(ucl); */
/* } */
/* return 0; */
/* } */

7
src/native-plat.c Normal file
View File

@@ -0,0 +1,7 @@
#include "plat.h"
void platform_init() {
}
void platform_repl_complete() {
}

4
src/native-plat.h Normal file
View File

@@ -0,0 +1,4 @@
#ifndef _NATIVE_ENV_H_
#define _NATIVE_ENV_H_
#endif

18
src/pico-plat.c Normal file
View File

@@ -0,0 +1,18 @@
#include "plat.h"
#include <stdio.h>
#include "pico/stdio.h"
#include "pico/stdlib.h"
static bool gpio_state = false;
void platform_init() {
stdio_init_all();
gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
}
void platform_repl_complete() {
gpio_put(PICO_DEFAULT_LED_PIN, gpio_state);
gpio_state = !gpio_state;
}

7
src/pico-plat.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef _PICO_PLAT_H_
#define _PICO_PLAT_H_
// Not sure why this isn't defined on Wizio Pico-SDK (is it defined in the standard SDK?)
#define getline __getline
#endif

15
src/plat.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef _PLAT_H_
#define _PLAT_H_
#ifdef PLATFORM_PICO_SDK
#include "pico-plat.h"
#endif
#ifdef PLATFORM_NATIVE
#include "native-plat.h"
#endif
void platform_init();
void platform_repl_complete();
#endif