| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | Copyright 2018, 2020, 2021, 2024, 2025 Joel Svensson svenssonjoel@yahoo.se | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 3 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <lbm_types.h> | ||
| 19 | #include <stdio.h> | ||
| 20 | |||
| 21 | #include "symrepr.h" | ||
| 22 | #include "heap.h" | ||
| 23 | #include "print.h" | ||
| 24 | #include "env.h" | ||
| 25 | #include "lbm_memory.h" | ||
| 26 | |||
| 27 | static lbm_value env_global[GLOBAL_ENV_ROOTS]; | ||
| 28 | |||
| 29 | 29346 | bool lbm_init_env(void) { | |
| 30 |
2/2✓ Branch 0 taken 939072 times.
✓ Branch 1 taken 29346 times.
|
968418 | for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) { |
| 31 | 939072 | env_global[i] = ENC_SYM_NIL; | |
| 32 | } | ||
| 33 | 29346 | return true; | |
| 34 | } | ||
| 35 | |||
| 36 | 6 | lbm_uint lbm_get_global_env_size(void) { | |
| 37 | 6 | lbm_uint n = 0; | |
| 38 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 6 times.
|
198 | for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) { |
| 39 | 192 | lbm_value curr = env_global[i]; | |
| 40 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 192 times.
|
198 | while (lbm_is_cons(curr)) { |
| 41 | 6 | n++; | |
| 42 | 6 | curr = lbm_ref_cell(curr)->cdr; | |
| 43 | } | ||
| 44 | } | ||
| 45 | 6 | return n; | |
| 46 | } | ||
| 47 | |||
| 48 | 14908526 | lbm_value *lbm_get_global_env(void) { | |
| 49 | 14908526 | return env_global; | |
| 50 | } | ||
| 51 | |||
| 52 | // Copy the list structure of an environment. | ||
| 53 | 821254 | lbm_value lbm_env_copy_spine(lbm_value env) { | |
| 54 | |||
| 55 | 821254 | lbm_value r = ENC_SYM_MERROR; | |
| 56 | 821254 | lbm_uint len = lbm_list_length(env); | |
| 57 | |||
| 58 | 821254 | lbm_value new_env = lbm_heap_allocate_list(len); | |
| 59 |
2/2✓ Branch 0 taken 820432 times.
✓ Branch 1 taken 822 times.
|
821254 | if (new_env != ENC_SYM_MERROR) { |
| 60 | 820432 | lbm_value curr_tgt = new_env; | |
| 61 | 820432 | lbm_value curr_src = env; | |
| 62 | // if src environment is invalid, so is tgt. | ||
| 63 |
3/4✓ Branch 1 taken 2078229 times.
✓ Branch 2 taken 820432 times.
✓ Branch 4 taken 2078229 times.
✗ Branch 5 not taken.
|
2898661 | while (lbm_is_cons_rw(curr_tgt) && lbm_is_cons(curr_src)) { |
| 64 | 2078229 | lbm_cons_t *tgt_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr_tgt)]; | |
| 65 | 2078229 | lbm_cons_t *src_cell = lbm_ref_cell(curr_src); | |
| 66 | 2078229 | tgt_cell->car = src_cell->car; | |
| 67 | 2078229 | curr_tgt = tgt_cell->cdr; | |
| 68 | 2078229 | curr_src = src_cell->cdr; | |
| 69 | } | ||
| 70 | 820432 | r = new_env; | |
| 71 | } | ||
| 72 | 821254 | return r; | |
| 73 | } | ||
| 74 | |||
| 75 | // env_lookup that should be safe even in the presence of incorrectly | ||
| 76 | // structured env. Could be the case when user manually creates closure. | ||
| 77 | // Note that a closure can be stored in FLASH and if so | ||
| 78 | // its local environment will be in flash too. | ||
| 79 | 290346831 | bool lbm_env_lookup_b(lbm_value *res, lbm_value sym, lbm_value env) { | |
| 80 | 290346831 | lbm_value curr = env; | |
| 81 | |||
| 82 |
2/2✓ Branch 1 taken 873789758 times.
✓ Branch 2 taken 52255552 times.
|
926045310 | while (lbm_is_cons(curr)) { |
| 83 | 873789758 | lbm_cons_t *cr = lbm_ref_cell(curr); | |
| 84 |
2/2✓ Branch 1 taken 873789749 times.
✓ Branch 2 taken 9 times.
|
873789758 | if (lbm_is_cons(cr->car)) { |
| 85 | 873789749 | lbm_cons_t *pair = lbm_ref_cell(cr->car); | |
| 86 |
2/2✓ Branch 0 taken 238091387 times.
✓ Branch 1 taken 635698362 times.
|
873789749 | if ((pair->car == sym) |
| 87 |
2/2✓ Branch 0 taken 238091279 times.
✓ Branch 1 taken 108 times.
|
238091387 | && (pair->cdr != ENC_SYM_PLACEHOLDER)) { |
| 88 | 238091279 | *res = pair->cdr; | |
| 89 | 238091279 | return true; | |
| 90 | } | ||
| 91 | } | ||
| 92 | 635698479 | curr = cr->cdr; | |
| 93 | } | ||
| 94 | 52255552 | return false; | |
| 95 | } | ||
| 96 | |||
| 97 | // Global environment lookup | ||
| 98 | // Assumes that environment is structurally correct. | ||
| 99 | // Assumes that global environment structures are never constant. | ||
| 100 | 52250063 | bool lbm_global_env_lookup(lbm_value *res, lbm_value sym) { | |
| 101 | 52250063 | lbm_uint dec_sym = lbm_dec_sym(sym); | |
| 102 | 52250063 | lbm_uint ix = dec_sym & GLOBAL_ENV_MASK; | |
| 103 | 52250063 | lbm_value curr = env_global[ix]; | |
| 104 | |||
| 105 |
2/2✓ Branch 0 taken 52246169 times.
✓ Branch 1 taken 8374 times.
|
52254543 | while (curr) { // Uses the fact that nil is 0 and the assumption |
| 106 | // that global environments are structurally correct. | ||
| 107 | 52246169 | lbm_uint curr_ix = lbm_dec_ptr(curr); | |
| 108 | 52246169 | lbm_cons_t *curr_cell = &lbm_heaps[LBM_RAM_HEAP][curr_ix]; | |
| 109 | 52246169 | lbm_value c = curr_cell->car; | |
| 110 | 52246169 | lbm_uint c_ix = lbm_dec_ptr(c); // Assumes environment is correctly shaped. | |
| 111 | 52246169 | lbm_cons_t *c_cell = &lbm_heaps[LBM_RAM_HEAP][c_ix]; | |
| 112 |
2/2✓ Branch 0 taken 52241689 times.
✓ Branch 1 taken 4480 times.
|
52246169 | if (c_cell->car == sym) { |
| 113 | 52241689 | *res = c_cell->cdr; | |
| 114 | 52241689 | return true; | |
| 115 | } | ||
| 116 | 4480 | curr = curr_cell->cdr; | |
| 117 | } | ||
| 118 | 8374 | return false; | |
| 119 | } | ||
| 120 | |||
| 121 | // TODO: env set should ideally copy environment if it has to update | ||
| 122 | // in place. This has never come up as an issue, the rest of the code | ||
| 123 | // must be very well behaved. | ||
| 124 | 31808416 | lbm_value lbm_env_set(lbm_value env, lbm_value key, lbm_value val) { | |
| 125 | |||
| 126 | 31808416 | lbm_value curr = env; | |
| 127 | lbm_value new_env; | ||
| 128 | lbm_value keyval; | ||
| 129 | |||
| 130 |
2/2✓ Branch 1 taken 40138778 times.
✓ Branch 2 taken 2975068 times.
|
43113846 | while(lbm_is_cons_rw(curr)) { |
| 131 | 40138778 | lbm_cons_t *cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr)]; | |
| 132 | 40138778 | lbm_value car_val = cell->car; | |
| 133 |
1/2✓ Branch 1 taken 40138778 times.
✗ Branch 2 not taken.
|
40138778 | if (lbm_is_cons_rw(car_val)) { // else corrupt environment. |
| 134 | 40138778 | lbm_cons_t *car_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(car_val)]; | |
| 135 |
2/2✓ Branch 0 taken 28833348 times.
✓ Branch 1 taken 11305430 times.
|
40138778 | if (car_cell->car == key) { |
| 136 | 28833348 | car_cell->cdr = val; | |
| 137 | 28833348 | return env; | |
| 138 | } | ||
| 139 | } // Possibly add a else here to handle corrupt environment. | ||
| 140 | 11305430 | curr = cell->cdr; | |
| 141 | } | ||
| 142 | |||
| 143 | 2975068 | keyval = lbm_cons(key,val); | |
| 144 |
2/2✓ Branch 1 taken 1512 times.
✓ Branch 2 taken 2973556 times.
|
2975068 | if (lbm_is_symbol(keyval)){ |
| 145 | 1512 | return keyval; | |
| 146 | } | ||
| 147 | |||
| 148 | 2973556 | new_env = lbm_cons(keyval, env); | |
| 149 | 2973556 | return new_env; | |
| 150 | } | ||
| 151 | |||
| 152 | 34193239 | lbm_value lbm_env_modify_binding(lbm_value env, lbm_value key, lbm_value val) { | |
| 153 | |||
| 154 | 34193239 | lbm_value curr = env; | |
| 155 | |||
| 156 |
2/2✓ Branch 1 taken 57596200 times.
✓ Branch 2 taken 2737258 times.
|
60333458 | while (lbm_is_cons_rw(curr)) { |
| 157 | 57596200 | lbm_cons_t *curr_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr)]; | |
| 158 | 57596200 | lbm_value car_val = curr_cell->car; | |
| 159 |
1/2✓ Branch 1 taken 57596200 times.
✗ Branch 2 not taken.
|
57596200 | if (lbm_is_cons_rw(car_val)) { |
| 160 | 57596200 | lbm_cons_t *car_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(car_val)]; | |
| 161 |
2/2✓ Branch 0 taken 31455981 times.
✓ Branch 1 taken 26140219 times.
|
57596200 | if (car_cell->car == key) { |
| 162 | 31455981 | car_cell->cdr = val; | |
| 163 | 31455981 | return env; | |
| 164 | } | ||
| 165 | } // Else environment is invalid. | ||
| 166 | 26140219 | curr = curr_cell->cdr; | |
| 167 | } | ||
| 168 | 2737258 | return ENC_SYM_NOT_FOUND; | |
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | // TODO: Drop binding should really return a new environment | ||
| 173 | // where the drop key/val is missing. | ||
| 174 | // | ||
| 175 | // The internal use of drop_binding in fundamental undefine | ||
| 176 | // is probably fine as we do not generally treat environments | ||
| 177 | // as first order values. If we did, drop_binding is too destructive! | ||
| 178 | 2160390 | lbm_value lbm_env_drop_binding(lbm_value env, lbm_value key) { | |
| 179 | |||
| 180 |
2/2✓ Branch 1 taken 2160272 times.
✓ Branch 2 taken 118 times.
|
2160390 | if (lbm_is_cons_rw(env)) { |
| 181 | 2160272 | lbm_cons_t *cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(env)]; | |
| 182 | |||
| 183 | // If key is first in env | ||
| 184 |
2/2✓ Branch 1 taken 2160263 times.
✓ Branch 2 taken 9 times.
|
2160272 | if (lbm_car(cell->car) == key) { |
| 185 | 2160263 | return cell->cdr; // New head of env | |
| 186 | } | ||
| 187 | |||
| 188 | 9 | lbm_cons_t *prev = cell; | |
| 189 | 9 | lbm_value curr = cell->cdr; | |
| 190 | |||
| 191 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 2 taken 3 times.
|
15 | while (lbm_is_cons_rw(curr)) { |
| 192 | 12 | cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr)]; | |
| 193 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | if (lbm_is_cons_rw(cell->car)) { |
| 194 | 12 | lbm_cons_t *car_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(cell->car)]; | |
| 195 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (car_cell->car == key) { |
| 196 | 6 | prev->cdr = cell->cdr; // removes "cell" from list | |
| 197 | 6 | return env; | |
| 198 | } | ||
| 199 | } // the unhandled else here would be an invalid environment. | ||
| 200 | 6 | prev = cell; | |
| 201 | 6 | curr = cell->cdr; | |
| 202 | } | ||
| 203 | } | ||
| 204 | 121 | return ENC_SYM_NOT_FOUND; | |
| 205 | } | ||
| 206 |