| 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 | 66575 | bool lbm_init_env(void) { | |
| 30 |
2/2✓ Branch 0 taken 2130400 times.
✓ Branch 1 taken 66575 times.
|
2196975 | for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) { |
| 31 | 2130400 | env_global[i] = ENC_SYM_NIL; | |
| 32 | } | ||
| 33 | 66575 | 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 | 31820977 | lbm_value *lbm_get_global_env(void) { | |
| 49 | 31820977 | return env_global; | |
| 50 | } | ||
| 51 | |||
| 52 | // Copy the list structure of an environment. | ||
| 53 | 1899358 | lbm_value lbm_env_copy_spine(lbm_value env) { | |
| 54 | |||
| 55 | 1899358 | lbm_value r = ENC_SYM_MERROR; | |
| 56 | 1899358 | lbm_uint len = lbm_list_length(env); | |
| 57 | |||
| 58 | 1899358 | lbm_value new_env = lbm_heap_allocate_list(len); | |
| 59 |
2/2✓ Branch 0 taken 1897180 times.
✓ Branch 1 taken 2178 times.
|
1899358 | if (new_env != ENC_SYM_MERROR) { |
| 60 | 1897180 | lbm_value curr_tgt = new_env; | |
| 61 | 1897180 | lbm_value curr_src = env; | |
| 62 | // if src environment is invalid, so is tgt. | ||
| 63 |
3/4✓ Branch 1 taken 4659838 times.
✓ Branch 2 taken 1897180 times.
✓ Branch 4 taken 4659838 times.
✗ Branch 5 not taken.
|
6557018 | while (lbm_is_cons_rw(curr_tgt) && lbm_is_cons(curr_src)) { |
| 64 | 4659838 | lbm_cons_t *tgt_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr_tgt)]; | |
| 65 | 4659838 | lbm_cons_t *src_cell = lbm_ref_cell(curr_src); | |
| 66 | 4659838 | tgt_cell->car = src_cell->car; | |
| 67 | 4659838 | curr_tgt = tgt_cell->cdr; | |
| 68 | 4659838 | curr_src = src_cell->cdr; | |
| 69 | } | ||
| 70 | 1897180 | r = new_env; | |
| 71 | } | ||
| 72 | 1899358 | 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 | 656982369 | bool lbm_env_lookup_b(lbm_value *res, lbm_value sym, lbm_value env) { | |
| 78 | 656982369 | lbm_value curr = env; | |
| 79 | |||
| 80 |
2/2✓ Branch 1 taken 2003529124 times.
✓ Branch 2 taken 115673984 times.
|
2119203108 | while (lbm_is_cons(curr)) { |
| 81 | 2003529124 | lbm_cons_t *cr = lbm_ref_cell(curr); | |
| 82 |
2/2✓ Branch 1 taken 2003529121 times.
✓ Branch 2 taken 3 times.
|
2003529124 | if (lbm_is_cons(cr->car)) { |
| 83 | 2003529121 | lbm_cons_t *pair = lbm_ref_cell(cr->car); | |
| 84 |
2/2✓ Branch 0 taken 541308637 times.
✓ Branch 1 taken 1462220484 times.
|
2003529121 | if ((pair->car == sym) |
| 85 |
2/2✓ Branch 0 taken 541308385 times.
✓ Branch 1 taken 252 times.
|
541308637 | && (pair->cdr != ENC_SYM_PLACEHOLDER)) { |
| 86 | 541308385 | *res = pair->cdr; | |
| 87 | 541308385 | return true; | |
| 88 | } | ||
| 89 | } | ||
| 90 | 1462220739 | curr = cr->cdr; | |
| 91 | } | ||
| 92 | 115673984 | return false; | |
| 93 | } | ||
| 94 | |||
| 95 | // Global environment lookup | ||
| 96 | // Assumes that environment is structurally correct. | ||
| 97 | // Assumes that global environment structures are never constant. | ||
| 98 | 115673223 | bool lbm_global_env_lookup(lbm_value *res, lbm_value sym) { | |
| 99 | 115673223 | lbm_uint dec_sym = lbm_dec_sym(sym); | |
| 100 | 115673223 | lbm_uint ix = dec_sym & GLOBAL_ENV_MASK; | |
| 101 | 115673223 | lbm_value curr = env_global[ix]; | |
| 102 | |||
| 103 |
2/2✓ Branch 0 taken 115656760 times.
✓ Branch 1 taken 18309 times.
|
115675069 | while (curr) { // Uses the fact that nil is 0 and the assumption |
| 104 | // that global environments are structurally correct. | ||
| 105 | 115656760 | lbm_uint curr_ix = lbm_dec_ptr(curr); | |
| 106 | 115656760 | lbm_cons_t *curr_cell = &lbm_heaps[LBM_RAM_HEAP][curr_ix]; | |
| 107 | 115656760 | lbm_value c = curr_cell->car; | |
| 108 | 115656760 | lbm_uint c_ix = lbm_dec_ptr(c); // Assumes environment is correctly shaped. | |
| 109 | 115656760 | lbm_cons_t *c_cell = &lbm_heaps[LBM_RAM_HEAP][c_ix]; | |
| 110 |
2/2✓ Branch 0 taken 115654914 times.
✓ Branch 1 taken 1846 times.
|
115656760 | if (c_cell->car == sym) { |
| 111 | 115654914 | *res = c_cell->cdr; | |
| 112 | 115654914 | return true; | |
| 113 | } | ||
| 114 | 1846 | curr = curr_cell->cdr; | |
| 115 | } | ||
| 116 | 18309 | return false; | |
| 117 | } | ||
| 118 | |||
| 119 | // TODO: env set should ideally copy environment if it has to update | ||
| 120 | // in place. This has never come up as an issue, the rest of the code | ||
| 121 | // must be very well behaved. | ||
| 122 | 74007207 | lbm_value lbm_env_set(lbm_value env, lbm_value key, lbm_value val) { | |
| 123 | |||
| 124 | 74007207 | lbm_value curr = env; | |
| 125 | lbm_value new_env; | ||
| 126 | lbm_value keyval; | ||
| 127 | |||
| 128 |
2/2✓ Branch 1 taken 93463765 times.
✓ Branch 2 taken 6919157 times.
|
100382922 | while(lbm_is_cons_rw(curr)) { |
| 129 | 93463765 | lbm_cons_t *cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr)]; | |
| 130 | 93463765 | lbm_value car_val = cell->car; | |
| 131 |
1/2✓ Branch 1 taken 93463765 times.
✗ Branch 2 not taken.
|
93463765 | if (lbm_is_cons_rw(car_val)) { // else corrupt environment. |
| 132 | 93463765 | lbm_cons_t *car_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(car_val)]; | |
| 133 |
2/2✓ Branch 0 taken 67088050 times.
✓ Branch 1 taken 26375715 times.
|
93463765 | if (car_cell->car == key) { |
| 134 | 67088050 | car_cell->cdr = val; | |
| 135 | 67088050 | return env; | |
| 136 | } | ||
| 137 | } // Possibly add a else here to handle corrupt environment. | ||
| 138 | 26375715 | curr = cell->cdr; | |
| 139 | } | ||
| 140 | |||
| 141 | 6919157 | keyval = lbm_cons(key,val); | |
| 142 |
2/2✓ Branch 1 taken 2730 times.
✓ Branch 2 taken 6916427 times.
|
6919157 | if (lbm_is_symbol(keyval)){ |
| 143 | 2730 | return keyval; | |
| 144 | } | ||
| 145 | |||
| 146 | 6916427 | new_env = lbm_cons(keyval, env); | |
| 147 | 6916427 | return new_env; | |
| 148 | } | ||
| 149 | |||
| 150 | 74472127 | lbm_value lbm_env_modify_binding(lbm_value env, lbm_value key, lbm_value val) { | |
| 151 | |||
| 152 | 74472127 | lbm_value curr = env; | |
| 153 | |||
| 154 |
2/2✓ Branch 1 taken 128658862 times.
✓ Branch 2 taken 3896288 times.
|
132555150 | while (lbm_is_cons_rw(curr)) { |
| 155 | 128658862 | lbm_cons_t *curr_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr)]; | |
| 156 | 128658862 | lbm_value car_val = curr_cell->car; | |
| 157 |
1/2✓ Branch 1 taken 128658862 times.
✗ Branch 2 not taken.
|
128658862 | if (lbm_is_cons_rw(car_val)) { |
| 158 | 128658862 | lbm_cons_t *car_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(car_val)]; | |
| 159 |
2/2✓ Branch 0 taken 70575839 times.
✓ Branch 1 taken 58083023 times.
|
128658862 | if (car_cell->car == key) { |
| 160 | 70575839 | car_cell->cdr = val; | |
| 161 | 70575839 | return env; | |
| 162 | } | ||
| 163 | } // Else environment is invalid. | ||
| 164 | 58083023 | curr = curr_cell->cdr; | |
| 165 | } | ||
| 166 | 3896288 | return ENC_SYM_NOT_FOUND; | |
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | // TODO: Drop binding should really return a new environment | ||
| 171 | // where the drop key/val is missing. | ||
| 172 | // | ||
| 173 | // The internal use of drop_binding in fundamental undefine | ||
| 174 | // is probably fine as we do not generally treat environments | ||
| 175 | // as first order values. If we did, drop_binding is too destructive! | ||
| 176 | 5040852 | lbm_value lbm_env_drop_binding(lbm_value env, lbm_value key) { | |
| 177 | |||
| 178 |
2/2✓ Branch 1 taken 5040596 times.
✓ Branch 2 taken 256 times.
|
5040852 | if (lbm_is_cons_rw(env)) { |
| 179 | 5040596 | lbm_cons_t *cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(env)]; | |
| 180 | |||
| 181 | // If key is first in env | ||
| 182 |
2/2✓ Branch 1 taken 5040593 times.
✓ Branch 2 taken 3 times.
|
5040596 | if (lbm_car(cell->car) == key) { |
| 183 | 5040593 | return cell->cdr; // New head of env | |
| 184 | } | ||
| 185 | |||
| 186 | 3 | lbm_cons_t *prev = cell; | |
| 187 | 3 | lbm_value curr = cell->cdr; | |
| 188 | |||
| 189 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
|
5 | while (lbm_is_cons_rw(curr)) { |
| 190 | 4 | cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr)]; | |
| 191 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | if (lbm_is_cons_rw(cell->car)) { |
| 192 | 4 | lbm_cons_t *car_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(cell->car)]; | |
| 193 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (car_cell->car == key) { |
| 194 | 2 | prev->cdr = cell->cdr; // removes "cell" from list | |
| 195 | 2 | return env; | |
| 196 | } | ||
| 197 | } // the unhandled else here would be an invalid environment. | ||
| 198 | 2 | prev = cell; | |
| 199 | 2 | curr = cell->cdr; | |
| 200 | } | ||
| 201 | } | ||
| 202 | 257 | return ENC_SYM_NOT_FOUND; | |
| 203 | } | ||
| 204 |