Squashed 'lispBM/lispBM/' changes from 18d9001b..1108de58

1108de58 added possibility to take n steps in the evaluator
9fb1863e added match-binders for u32 and i32
b591bd3e fix to match that allows matching on boxed types

git-subtree-dir: lispBM/lispBM
git-subtree-split: 1108de58965232fcfb2cb173db6e8696319f1bb5
This commit is contained in:
Benjamin Vedder 2022-02-03 12:49:33 +01:00
parent 3307ff377b
commit 6aa01e2cfc
7 changed files with 76 additions and 11 deletions

View File

@ -122,6 +122,10 @@ extern void lbm_pause_eval_with_gc(uint32_t num_free);
* EVAL_CPS_STATE_PUASED state. * EVAL_CPS_STATE_PUASED state.
*/ */
extern void lbm_step_eval(void); extern void lbm_step_eval(void);
/** Perform multiple steps of evaluation.
* \param n Number of eval steps to perform.
*/
extern void lbm_step_n_eval(uint32_t n);
/** Resume from being in EVAL_CPS_STATE_PAUSED. /** Resume from being in EVAL_CPS_STATE_PAUSED.
* *
*/ */

View File

@ -594,6 +594,8 @@ static inline bool lbm_is_match_binder(lbm_value exp) {
((lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_ANY) || ((lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_ANY) ||
(lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_I28) || (lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_I28) ||
(lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_U28) || (lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_U28) ||
(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_FLOAT) ||
(lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_CONS))); (lbm_dec_sym(lbm_car(exp)) == SYM_MATCH_CONS)));
} }

View File

@ -70,8 +70,10 @@
#define SYM_MATCH_ANY 0x2B #define SYM_MATCH_ANY 0x2B
#define SYM_MATCH_I28 0x2C #define SYM_MATCH_I28 0x2C
#define SYM_MATCH_U28 0x2D #define SYM_MATCH_U28 0x2D
#define SYM_MATCH_FLOAT 0x2E #define SYM_MATCH_U32 0x2E
#define SYM_MATCH_CONS 0x2F #define SYM_MATCH_I32 0x2F
#define SYM_MATCH_FLOAT 0x30
#define SYM_MATCH_CONS 0x31
// Type identifying symbols // Type identifying symbols
#define SYM_TYPE_LIST 0x50 #define SYM_TYPE_LIST 0x50

View File

@ -491,7 +491,10 @@ int main(int argc, char **argv) {
} else if (strncmp(str, ":continue", 9) == 0) { } else if (strncmp(str, ":continue", 9) == 0) {
lbm_continue_eval(); lbm_continue_eval();
} else if (strncmp(str, ":step", 5) == 0) { } else if (strncmp(str, ":step", 5) == 0) {
lbm_step_eval();
int num = atoi(str + 5);
lbm_step_n_eval((uint32_t)num);
} else { } else {
/* Get exclusive access to the heap */ /* Get exclusive access to the heap */
lbm_pause_eval(); lbm_pause_eval();

View File

@ -660,6 +660,25 @@ static bool match(lbm_value p, lbm_value e, lbm_value *env, bool *gc) {
} }
} }
return false; return false;
case SYM_MATCH_I32:
if (lbm_type_of(e) == LBM_PTR_TYPE_BOXED_I) {
if (lbm_dec_sym(var) == SYM_DONTCARE) {
return true;
} else {
break;
}
}
return false;
case SYM_MATCH_U32:
if (lbm_type_of(e) == LBM_PTR_TYPE_BOXED_U) {
if (lbm_dec_sym(var) == SYM_DONTCARE) {
return true;
} else {
break;
}
}
return false;
case SYM_MATCH_FLOAT: case SYM_MATCH_FLOAT:
if (lbm_type_of(e) == LBM_PTR_TYPE_BOXED_F) { if (lbm_type_of(e) == LBM_PTR_TYPE_BOXED_F) {
if (lbm_dec_sym(var) == SYM_DONTCARE) { if (lbm_dec_sym(var) == SYM_DONTCARE) {
@ -705,6 +724,18 @@ static bool match(lbm_value p, lbm_value e, lbm_value *env, bool *gc) {
return false; return false;
} }
return match (lbm_cdr(p), lbm_cdr(e), env, gc); return match (lbm_cdr(p), lbm_cdr(e), env, gc);
} else if (lbm_type_of(p) == LBM_PTR_TYPE_BOXED_F &&
lbm_type_of(e) == LBM_PTR_TYPE_BOXED_F &&
lbm_dec_F(p) == lbm_dec_F(e)) {
return true;
} else if (lbm_type_of(p) == LBM_PTR_TYPE_BOXED_U &&
lbm_type_of(e) == LBM_PTR_TYPE_BOXED_U &&
lbm_dec_U(p) == lbm_dec_U(e)) {
return true;
} else if (lbm_type_of(p) == LBM_PTR_TYPE_BOXED_I &&
lbm_type_of(e) == LBM_PTR_TYPE_BOXED_I &&
lbm_dec_I(p) == lbm_dec_I(e)) {
return true;
} else if (p == e) { } else if (p == e) {
return true; return true;
} }
@ -931,9 +962,9 @@ static inline void eval_lambda(eval_context_t *ctx) {
static inline void eval_if(eval_context_t *ctx) { static inline void eval_if(eval_context_t *ctx) {
CHECK_STACK(lbm_push_u32_3(&ctx->K, CHECK_STACK(lbm_push_u32_3(&ctx->K,
lbm_car(lbm_cdr(lbm_cdr(lbm_cdr(ctx->curr_exp)))), // Else branch lbm_car(lbm_cdr(lbm_cdr(lbm_cdr(ctx->curr_exp)))), // Else branch
lbm_car(lbm_cdr(lbm_cdr(ctx->curr_exp))), // Then branch lbm_car(lbm_cdr(lbm_cdr(ctx->curr_exp))), // Then branch
lbm_enc_u(IF))); lbm_enc_u(IF)));
ctx->curr_exp = lbm_car(lbm_cdr(ctx->curr_exp)); ctx->curr_exp = lbm_car(lbm_cdr(ctx->curr_exp));
} }
@ -1896,6 +1927,12 @@ void lbm_pause_eval_with_gc(uint32_t num_free) {
void lbm_step_eval(void) { void lbm_step_eval(void) {
eval_cps_next_state_arg = 1;
eval_cps_next_state = EVAL_CPS_STATE_STEP;
}
void lbm_step_n_eval(uint32_t n) {
eval_cps_next_state_arg = n;
eval_cps_next_state = EVAL_CPS_STATE_STEP; eval_cps_next_state = EVAL_CPS_STATE_STEP;
} }
@ -1927,7 +1964,12 @@ void lbm_run_eval(void){
eval_cps_run_state = EVAL_CPS_STATE_RUNNING; eval_cps_run_state = EVAL_CPS_STATE_RUNNING;
break; break;
case EVAL_CPS_STATE_STEP: case EVAL_CPS_STATE_STEP:
eval_cps_next_state = EVAL_CPS_STATE_PAUSED; if (eval_cps_next_state_arg > 1) {
eval_cps_next_state = EVAL_CPS_STATE_STEP;
eval_cps_next_state_arg --;
} else {
eval_cps_next_state = EVAL_CPS_STATE_PAUSED;
}
break; break;
case EVAL_CPS_STATE_PAUSED: case EVAL_CPS_STATE_PAUSED:
if (eval_cps_run_state != EVAL_CPS_STATE_PAUSED) { if (eval_cps_run_state != EVAL_CPS_STATE_PAUSED) {

View File

@ -24,7 +24,7 @@
#include "symrepr.h" #include "symrepr.h"
#define NUM_SPECIAL_SYMBOLS 92 #define NUM_SPECIAL_SYMBOLS 94
#define NAME 0 #define NAME 0
#define ID 1 #define ID 1
@ -56,6 +56,8 @@ special_sym const special_symbols[NUM_SPECIAL_SYMBOLS] = {
{"?" , SYM_MATCH_ANY}, {"?" , SYM_MATCH_ANY},
{"?i28" , SYM_MATCH_I28}, {"?i28" , SYM_MATCH_I28},
{"?u28" , SYM_MATCH_U28}, {"?u28" , SYM_MATCH_U28},
{"?u32" , SYM_MATCH_U32},
{"?i32" , SYM_MATCH_I32},
{"?float" , SYM_MATCH_FLOAT}, {"?float" , SYM_MATCH_FLOAT},
{"?cons" , SYM_MATCH_CONS}, {"?cons" , SYM_MATCH_CONS},

View File

@ -52,8 +52,10 @@
#define TOKMATCHANY 17u #define TOKMATCHANY 17u
#define TOKMATCHI28 18u #define TOKMATCHI28 18u
#define TOKMATCHU28 19u #define TOKMATCHU28 19u
#define TOKMATCHFLOAT 20u #define TOKMATCHU32 20u
#define TOKMATCHCONS 21u #define TOKMATCHI32 21u
#define TOKMATCHFLOAT 22u
#define TOKMATCHCONS 23u
#define TOKENIZER_ERROR 1024u #define TOKENIZER_ERROR 1024u
#define TOKENIZER_END 2048u #define TOKENIZER_END 2048u
@ -86,7 +88,7 @@ typedef struct {
uint32_t len; uint32_t len;
} matcher; } matcher;
#define NUM_FIXED_SIZE_TOKENS 13 #define NUM_FIXED_SIZE_TOKENS 15
const matcher match_table[NUM_FIXED_SIZE_TOKENS] = { const matcher match_table[NUM_FIXED_SIZE_TOKENS] = {
{"(", TOKOPENPAR, 1}, {"(", TOKOPENPAR, 1},
{")", TOKCLOSEPAR, 1}, {")", TOKCLOSEPAR, 1},
@ -98,6 +100,8 @@ const matcher match_table[NUM_FIXED_SIZE_TOKENS] = {
{",", TOKCOMMA, 1}, {",", TOKCOMMA, 1},
{"?i28", TOKMATCHI28, 4}, {"?i28", TOKMATCHI28, 4},
{"?u28", TOKMATCHU28, 4}, {"?u28", TOKMATCHU28, 4},
{"?u32", TOKMATCHU32, 4},
{"?i32", TOKMATCHI32, 4},
{"?float", TOKMATCHFLOAT, 6}, {"?float", TOKMATCHFLOAT, 6},
{"?cons", TOKMATCHCONS, 5}, {"?cons", TOKMATCHCONS, 5},
{"?", TOKMATCHANY, 1} {"?", TOKMATCHANY, 1}
@ -511,6 +515,12 @@ lbm_value lbm_get_next_token(lbm_tokenizer_char_stream_t *str) {
case TOKMATCHU28: case TOKMATCHU28:
res = lbm_enc_sym(SYM_MATCH_U28); res = lbm_enc_sym(SYM_MATCH_U28);
break; break;
case TOKMATCHI32:
res = lbm_enc_sym(SYM_MATCH_I32);
break;
case TOKMATCHU32:
res = lbm_enc_sym(SYM_MATCH_U32);
break;
case TOKMATCHFLOAT: case TOKMATCHFLOAT:
res = lbm_enc_sym(SYM_MATCH_FLOAT); res = lbm_enc_sym(SYM_MATCH_FLOAT);
break; break;