| 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 | 66578 | bool lbm_init_env(void) { | |
| 30 |
2/2✓ Branch 0 taken 2130496 times.
✓ Branch 1 taken 66578 times.
|
2197074 | for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) { |
| 31 | 2130496 | env_global[i] = ENC_SYM_NIL; | |
| 32 | } | ||
| 33 | 66578 | return true; | |
| 34 | } | ||
| 35 | |||
| 36 | 2 | lbm_uint lbm_get_global_env_size(void) { | |
| 37 | 2 | lbm_uint n = 0; | |
| 38 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 2 times.
|
66 | for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) { |
| 39 | 64 | lbm_value curr = env_global[i]; | |
| 40 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 64 times.
|
66 | while (lbm_is_cons(curr)) { |
| 41 | 2 | n++; | |
| 42 | 2 | curr = lbm_ref_cell(curr)->cdr; | |
| 43 | } | ||
| 44 | } | ||
| 45 | 2 | return n; | |
| 46 | } | ||
| 47 | |||
| 48 | 31821163 | lbm_value *lbm_get_global_env(void) { | |
| 49 | 31821163 | return env_global; | |
| 50 | } | ||
| 51 | |||
| 52 | // Copy the list structure of an environment. | ||
| 53 | 1899502 | lbm_value lbm_env_copy_spine(lbm_value env) { | |
| 54 | |||
| 55 | 1899502 | lbm_value r = ENC_SYM_MERROR; | |
| 56 | 1899502 | lbm_uint len = lbm_list_length(env); | |
| 57 | |||
| 58 | 1899502 | lbm_value new_env = lbm_heap_allocate_list(len); | |
| 59 |
2/2✓ Branch 0 taken 1897325 times.
✓ Branch 1 taken 2177 times.
|
1899502 | if (new_env != ENC_SYM_MERROR) { |
| 60 | 1897325 | lbm_value curr_tgt = new_env; | |
| 61 | 1897325 | lbm_value curr_src = env; | |
| 62 | // if src environment is invalid, so is tgt. | ||
| 63 |
3/4✓ Branch 1 taken 4660325 times.
✓ Branch 2 taken 1897325 times.
✓ Branch 4 taken 4660325 times.
✗ Branch 5 not taken.
|
6557650 | while (lbm_is_cons_rw(curr_tgt) && lbm_is_cons(curr_src)) { |
| 64 | 4660325 | lbm_cons_t *tgt_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr_tgt)]; | |
| 65 | 4660325 | lbm_cons_t *src_cell = lbm_ref_cell(curr_src); | |
| 66 | 4660325 | tgt_cell->car = src_cell->car; | |
| 67 | 4660325 | curr_tgt = tgt_cell->cdr; | |
| 68 | 4660325 | curr_src = src_cell->cdr; | |
| 69 | } | ||
| 70 | 1897325 | r = new_env; | |
| 71 | } | ||
| 72 | 1899502 | 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 | 656982574 | bool lbm_env_lookup_b(lbm_value *res, lbm_value sym, lbm_value env) { | |
| 80 | 656982574 | lbm_value curr = env; | |
| 81 | |||
| 82 |
2/2✓ Branch 1 taken 2003557339 times.
✓ Branch 2 taken 115668567 times.
|
2119225906 | while (lbm_is_cons(curr)) { |
| 83 | 2003557339 | lbm_cons_t *cr = lbm_ref_cell(curr); | |
| 84 |
2/2✓ Branch 1 taken 2003557336 times.
✓ Branch 2 taken 3 times.
|
2003557339 | if (lbm_is_cons(cr->car)) { |
| 85 | 2003557336 | lbm_cons_t *pair = lbm_ref_cell(cr->car); | |
| 86 |
2/2✓ Branch 0 taken 541314259 times.
✓ Branch 1 taken 1462243077 times.
|
2003557336 | if ((pair->car == sym) |
| 87 |
2/2✓ Branch 0 taken 541314007 times.
✓ Branch 1 taken 252 times.
|
541314259 | && (pair->cdr != ENC_SYM_PLACEHOLDER)) { |
| 88 | 541314007 | *res = pair->cdr; | |
| 89 | 541314007 | return true; | |
| 90 | } | ||
| 91 | } | ||
| 92 | 1462243332 | curr = cr->cdr; | |
| 93 | } | ||
| 94 | 115668567 | 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 | 115667805 | bool lbm_global_env_lookup(lbm_value *res, lbm_value sym) { | |
| 101 | 115667805 | lbm_uint dec_sym = lbm_dec_sym(sym); | |
| 102 | 115667805 | lbm_uint ix = dec_sym & GLOBAL_ENV_MASK; | |
| 103 | 115667805 | lbm_value curr = env_global[ix]; | |
| 104 | |||
| 105 |
2/2✓ Branch 0 taken 115651332 times.
✓ Branch 1 taken 18319 times.
|
115669651 | while (curr) { // Uses the fact that nil is 0 and the assumption |
| 106 | // that global environments are structurally correct. | ||
| 107 | 115651332 | lbm_uint curr_ix = lbm_dec_ptr(curr); | |
| 108 | 115651332 | lbm_cons_t *curr_cell = &lbm_heaps[LBM_RAM_HEAP][curr_ix]; | |
| 109 | 115651332 | lbm_value c = curr_cell->car; | |
| 110 | 115651332 | lbm_uint c_ix = lbm_dec_ptr(c); // Assumes environment is correctly shaped. | |
| 111 | 115651332 | lbm_cons_t *c_cell = &lbm_heaps[LBM_RAM_HEAP][c_ix]; | |
| 112 |
2/2✓ Branch 0 taken 115649486 times.
✓ Branch 1 taken 1846 times.
|
115651332 | if (c_cell->car == sym) { |
| 113 | 115649486 | *res = c_cell->cdr; | |
| 114 | 115649486 | return true; | |
| 115 | } | ||
| 116 | 1846 | curr = curr_cell->cdr; | |
| 117 | } | ||
| 118 | 18319 | 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 | 74007415 | lbm_value lbm_env_set(lbm_value env, lbm_value key, lbm_value val) { | |
| 125 | |||
| 126 | 74007415 | lbm_value curr = env; | |
| 127 | lbm_value new_env; | ||
| 128 | lbm_value keyval; | ||
| 129 | |||
| 130 |
2/2✓ Branch 1 taken 93463802 times.
✓ Branch 2 taken 6919332 times.
|
100383134 | while(lbm_is_cons_rw(curr)) { |
| 131 | 93463802 | lbm_cons_t *cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr)]; | |
| 132 | 93463802 | lbm_value car_val = cell->car; | |
| 133 |
1/2✓ Branch 1 taken 93463802 times.
✗ Branch 2 not taken.
|
93463802 | if (lbm_is_cons_rw(car_val)) { // else corrupt environment. |
| 134 | 93463802 | lbm_cons_t *car_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(car_val)]; | |
| 135 |
2/2✓ Branch 0 taken 67088083 times.
✓ Branch 1 taken 26375719 times.
|
93463802 | if (car_cell->car == key) { |
| 136 | 67088083 | car_cell->cdr = val; | |
| 137 | 67088083 | return env; | |
| 138 | } | ||
| 139 | } // Possibly add a else here to handle corrupt environment. | ||
| 140 | 26375719 | curr = cell->cdr; | |
| 141 | } | ||
| 142 | |||
| 143 | 6919332 | keyval = lbm_cons(key,val); | |
| 144 |
2/2✓ Branch 1 taken 2730 times.
✓ Branch 2 taken 6916602 times.
|
6919332 | if (lbm_is_symbol(keyval)){ |
| 145 | 2730 | return keyval; | |
| 146 | } | ||
| 147 | |||
| 148 | 6916602 | new_env = lbm_cons(keyval, env); | |
| 149 | 6916602 | return new_env; | |
| 150 | } | ||
| 151 | |||
| 152 | 74472682 | lbm_value lbm_env_modify_binding(lbm_value env, lbm_value key, lbm_value val) { | |
| 153 | |||
| 154 | 74472682 | lbm_value curr = env; | |
| 155 | |||
| 156 |
2/2✓ Branch 1 taken 128659728 times.
✓ Branch 2 taken 3896288 times.
|
132556016 | while (lbm_is_cons_rw(curr)) { |
| 157 | 128659728 | lbm_cons_t *curr_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr)]; | |
| 158 | 128659728 | lbm_value car_val = curr_cell->car; | |
| 159 |
1/2✓ Branch 1 taken 128659728 times.
✗ Branch 2 not taken.
|
128659728 | if (lbm_is_cons_rw(car_val)) { |
| 160 | 128659728 | lbm_cons_t *car_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(car_val)]; | |
| 161 |
2/2✓ Branch 0 taken 70576394 times.
✓ Branch 1 taken 58083334 times.
|
128659728 | if (car_cell->car == key) { |
| 162 | 70576394 | car_cell->cdr = val; | |
| 163 | 70576394 | return env; | |
| 164 | } | ||
| 165 | } // Else environment is invalid. | ||
| 166 | 58083334 | curr = curr_cell->cdr; | |
| 167 | } | ||
| 168 | 3896288 | 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 | 5040852 | lbm_value lbm_env_drop_binding(lbm_value env, lbm_value key) { | |
| 179 | |||
| 180 |
2/2✓ Branch 1 taken 5040596 times.
✓ Branch 2 taken 256 times.
|
5040852 | if (lbm_is_cons_rw(env)) { |
| 181 | 5040596 | 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 5040593 times.
✓ Branch 2 taken 3 times.
|
5040596 | if (lbm_car(cell->car) == key) { |
| 185 | 5040593 | return cell->cdr; // New head of env | |
| 186 | } | ||
| 187 | |||
| 188 | 3 | lbm_cons_t *prev = cell; | |
| 189 | 3 | lbm_value curr = cell->cdr; | |
| 190 | |||
| 191 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
|
5 | while (lbm_is_cons_rw(curr)) { |
| 192 | 4 | cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr)]; | |
| 193 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | if (lbm_is_cons_rw(cell->car)) { |
| 194 | 4 | lbm_cons_t *car_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(cell->car)]; | |
| 195 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (car_cell->car == key) { |
| 196 | 2 | prev->cdr = cell->cdr; // removes "cell" from list | |
| 197 | 2 | return env; | |
| 198 | } | ||
| 199 | } // the unhandled else here would be an invalid environment. | ||
| 200 | 2 | prev = cell; | |
| 201 | 2 | curr = cell->cdr; | |
| 202 | } | ||
| 203 | } | ||
| 204 | 257 | return ENC_SYM_NOT_FOUND; | |
| 205 | } | ||
| 206 |