| 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 | 67082 | bool lbm_init_env(void) { | |
| 30 |
2/2✓ Branch 0 taken 2146624 times.
✓ Branch 1 taken 67082 times.
|
2213706 | for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) { |
| 31 | 2146624 | env_global[i] = ENC_SYM_NIL; | |
| 32 | } | ||
| 33 | 67082 | 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 | 32725736 | lbm_value *lbm_get_global_env(void) { | |
| 49 | 32725736 | return env_global; | |
| 50 | } | ||
| 51 | |||
| 52 | // Copy the list structure of an environment. | ||
| 53 | 1901823 | lbm_value lbm_env_copy_spine(lbm_value env) { | |
| 54 | |||
| 55 | 1901823 | lbm_value r = ENC_SYM_MERROR; | |
| 56 | 1901823 | lbm_uint len = lbm_list_length(env); | |
| 57 | |||
| 58 | 1901823 | lbm_value new_env = lbm_heap_allocate_list(len); | |
| 59 |
2/2✓ Branch 0 taken 1899647 times.
✓ Branch 1 taken 2176 times.
|
1901823 | if (new_env != ENC_SYM_MERROR) { |
| 60 | 1899647 | lbm_value curr_tgt = new_env; | |
| 61 | 1899647 | lbm_value curr_src = env; | |
| 62 | // if src environment is invalid, so is tgt. | ||
| 63 |
3/4✓ Branch 1 taken 4664695 times.
✓ Branch 2 taken 1899647 times.
✓ Branch 4 taken 4664695 times.
✗ Branch 5 not taken.
|
6564342 | while (lbm_is_cons_rw(curr_tgt) && lbm_is_cons(curr_src)) { |
| 64 | 4664695 | lbm_cons_t *tgt_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr_tgt)]; | |
| 65 | 4664695 | lbm_cons_t *src_cell = lbm_ref_cell(curr_src); | |
| 66 | 4664695 | tgt_cell->car = src_cell->car; | |
| 67 | 4664695 | curr_tgt = tgt_cell->cdr; | |
| 68 | 4664695 | curr_src = src_cell->cdr; | |
| 69 | } | ||
| 70 | 1899647 | r = new_env; | |
| 71 | } | ||
| 72 | 1901823 | 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 | 661920705 | bool lbm_env_lookup_b(lbm_value *res, lbm_value sym, lbm_value env) { | |
| 80 | 661920705 | lbm_value curr = env; | |
| 81 | |||
| 82 |
2/2✓ Branch 1 taken 2009002327 times.
✓ Branch 2 taken 117036666 times.
|
2126038993 | while (lbm_is_cons(curr)) { |
| 83 | 2009002327 | lbm_cons_t *cr = lbm_ref_cell(curr); | |
| 84 |
2/2✓ Branch 1 taken 2009002318 times.
✓ Branch 2 taken 9 times.
|
2009002327 | if (lbm_is_cons(cr->car)) { |
| 85 | 2009002318 | lbm_cons_t *pair = lbm_ref_cell(cr->car); | |
| 86 |
2/2✓ Branch 0 taken 544884291 times.
✓ Branch 1 taken 1464118027 times.
|
2009002318 | if ((pair->car == sym) |
| 87 |
2/2✓ Branch 0 taken 544884039 times.
✓ Branch 1 taken 252 times.
|
544884291 | && (pair->cdr != ENC_SYM_PLACEHOLDER)) { |
| 88 | 544884039 | *res = pair->cdr; | |
| 89 | 544884039 | return true; | |
| 90 | } | ||
| 91 | } | ||
| 92 | 1464118288 | curr = cr->cdr; | |
| 93 | } | ||
| 94 | 117036666 | 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 | 117031849 | bool lbm_global_env_lookup(lbm_value *res, lbm_value sym) { | |
| 101 | 117031849 | lbm_uint dec_sym = lbm_dec_sym(sym); | |
| 102 | 117031849 | lbm_uint ix = dec_sym & GLOBAL_ENV_MASK; | |
| 103 | 117031849 | lbm_value curr = env_global[ix]; | |
| 104 | |||
| 105 |
2/2✓ Branch 0 taken 117017636 times.
✓ Branch 1 taken 18702 times.
|
117036338 | while (curr) { // Uses the fact that nil is 0 and the assumption |
| 106 | // that global environments are structurally correct. | ||
| 107 | 117017636 | lbm_uint curr_ix = lbm_dec_ptr(curr); | |
| 108 | 117017636 | lbm_cons_t *curr_cell = &lbm_heaps[LBM_RAM_HEAP][curr_ix]; | |
| 109 | 117017636 | lbm_value c = curr_cell->car; | |
| 110 | 117017636 | lbm_uint c_ix = lbm_dec_ptr(c); // Assumes environment is correctly shaped. | |
| 111 | 117017636 | lbm_cons_t *c_cell = &lbm_heaps[LBM_RAM_HEAP][c_ix]; | |
| 112 |
2/2✓ Branch 0 taken 117013147 times.
✓ Branch 1 taken 4489 times.
|
117017636 | if (c_cell->car == sym) { |
| 113 | 117013147 | *res = c_cell->cdr; | |
| 114 | 117013147 | return true; | |
| 115 | } | ||
| 116 | 4489 | curr = curr_cell->cdr; | |
| 117 | } | ||
| 118 | 18702 | 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 | 74075960 | lbm_value lbm_env_set(lbm_value env, lbm_value key, lbm_value val) { | |
| 125 | |||
| 126 | 74075960 | lbm_value curr = env; | |
| 127 | lbm_value new_env; | ||
| 128 | lbm_value keyval; | ||
| 129 | |||
| 130 |
2/2✓ Branch 1 taken 93528170 times.
✓ Branch 2 taken 6924436 times.
|
100452606 | while(lbm_is_cons_rw(curr)) { |
| 131 | 93528170 | lbm_cons_t *cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr)]; | |
| 132 | 93528170 | lbm_value car_val = cell->car; | |
| 133 |
1/2✓ Branch 1 taken 93528170 times.
✗ Branch 2 not taken.
|
93528170 | if (lbm_is_cons_rw(car_val)) { // else corrupt environment. |
| 134 | 93528170 | lbm_cons_t *car_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(car_val)]; | |
| 135 |
2/2✓ Branch 0 taken 67151524 times.
✓ Branch 1 taken 26376646 times.
|
93528170 | if (car_cell->car == key) { |
| 136 | 67151524 | car_cell->cdr = val; | |
| 137 | 67151524 | return env; | |
| 138 | } | ||
| 139 | } // Possibly add a else here to handle corrupt environment. | ||
| 140 | 26376646 | curr = cell->cdr; | |
| 141 | } | ||
| 142 | |||
| 143 | 6924436 | keyval = lbm_cons(key,val); | |
| 144 |
2/2✓ Branch 1 taken 2730 times.
✓ Branch 2 taken 6921706 times.
|
6924436 | if (lbm_is_symbol(keyval)){ |
| 145 | 2730 | return keyval; | |
| 146 | } | ||
| 147 | |||
| 148 | 6921706 | new_env = lbm_cons(keyval, env); | |
| 149 | 6921706 | return new_env; | |
| 150 | } | ||
| 151 | |||
| 152 | 76144657 | lbm_value lbm_env_modify_binding(lbm_value env, lbm_value key, lbm_value val) { | |
| 153 | |||
| 154 | 76144657 | lbm_value curr = env; | |
| 155 | |||
| 156 |
2/2✓ Branch 1 taken 130426850 times.
✓ Branch 2 taken 4726858 times.
|
135153708 | while (lbm_is_cons_rw(curr)) { |
| 157 | 130426850 | lbm_cons_t *curr_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr)]; | |
| 158 | 130426850 | lbm_value car_val = curr_cell->car; | |
| 159 |
1/2✓ Branch 1 taken 130426850 times.
✗ Branch 2 not taken.
|
130426850 | if (lbm_is_cons_rw(car_val)) { |
| 160 | 130426850 | lbm_cons_t *car_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(car_val)]; | |
| 161 |
2/2✓ Branch 0 taken 71417799 times.
✓ Branch 1 taken 59009051 times.
|
130426850 | if (car_cell->car == key) { |
| 162 | 71417799 | car_cell->cdr = val; | |
| 163 | 71417799 | return env; | |
| 164 | } | ||
| 165 | } // Else environment is invalid. | ||
| 166 | 59009051 | curr = curr_cell->cdr; | |
| 167 | } | ||
| 168 | 4726858 | 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 | 5040870 | lbm_value lbm_env_drop_binding(lbm_value env, lbm_value key) { | |
| 179 | |||
| 180 |
2/2✓ Branch 1 taken 5040608 times.
✓ Branch 2 taken 262 times.
|
5040870 | if (lbm_is_cons_rw(env)) { |
| 181 | 5040608 | 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 5040599 times.
✓ Branch 2 taken 9 times.
|
5040608 | if (lbm_car(cell->car) == key) { |
| 185 | 5040599 | 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 | 265 | return ENC_SYM_NOT_FOUND; | |
| 205 | } | ||
| 206 |