GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/env.c
Date: 2026-07-11 23:13:58
Exec Total Coverage
Lines: 101 101 100.0%
Functions: 9 9 100.0%
Branches: 44 48 91.7%

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 67033 bool lbm_init_env(void) {
30
2/2
✓ Branch 0 taken 2145056 times.
✓ Branch 1 taken 67033 times.
2212089 for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) {
31 2145056 env_global[i] = ENC_SYM_NIL;
32 }
33 67033 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 0 taken 6 times.
✓ Branch 1 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 32722927 lbm_value *lbm_get_global_env(void) {
49 32722927 return env_global;
50 }
51
52 // Copy the list structure of an environment.
53 1901790 lbm_value lbm_env_copy_spine(lbm_value env) {
54
55 1901790 lbm_value r = ENC_SYM_MERROR;
56 1901790 lbm_uint len = lbm_list_length(env);
57
58 1901790 lbm_value new_env = lbm_heap_allocate_list(len);
59
2/2
✓ Branch 0 taken 1899616 times.
✓ Branch 1 taken 2174 times.
1901790 if (new_env != ENC_SYM_MERROR) {
60 1899616 lbm_value curr_tgt = new_env;
61 1899616 lbm_value curr_src = env;
62 // if src environment is invalid, so is tgt.
63
3/4
✓ Branch 0 taken 4664664 times.
✓ Branch 1 taken 1899616 times.
✓ Branch 2 taken 4664664 times.
✗ Branch 3 not taken.
6564280 while (lbm_is_cons_rw(curr_tgt) && lbm_is_cons(curr_src)) {
64 4664664 lbm_cons_t *tgt_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr_tgt)];
65 4664664 lbm_cons_t *src_cell = lbm_ref_cell(curr_src);
66 4664664 tgt_cell->car = src_cell->car;
67 4664664 curr_tgt = tgt_cell->cdr;
68 4664664 curr_src = src_cell->cdr;
69 }
70 1899616 r = new_env;
71 }
72 1901790 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 661130304 bool lbm_env_lookup_b(lbm_value *res, lbm_value sym, lbm_value env) {
80 661130304 lbm_value curr = env;
81
82
2/2
✓ Branch 0 taken 2010190560 times.
✓ Branch 1 taken 116340296 times.
2126530855 while (lbm_is_cons(curr)) {
83 2010190560 lbm_cons_t *cr = lbm_ref_cell(curr);
84
2/2
✓ Branch 0 taken 2010190551 times.
✓ Branch 1 taken 9 times.
2010190560 if (lbm_is_cons(cr->car)) {
85 2010190551 lbm_cons_t *pair = lbm_ref_cell(cr->car);
86
2/2
✓ Branch 0 taken 544790261 times.
✓ Branch 1 taken 1465400290 times.
2010190551 if ((pair->car == sym)
87
2/2
✓ Branch 0 taken 544790009 times.
✓ Branch 1 taken 252 times.
544790261 && (pair->cdr != ENC_SYM_PLACEHOLDER)) {
88 544790009 *res = pair->cdr;
89 544790009 return true;
90 }
91 }
92 1465400551 curr = cr->cdr;
93 }
94 116340296 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 116335477 bool lbm_global_env_lookup(lbm_value *res, lbm_value sym) {
101 116335477 lbm_uint dec_sym = lbm_dec_sym(sym);
102 116335477 lbm_uint ix = dec_sym & GLOBAL_ENV_MASK;
103 116335477 lbm_value curr = env_global[ix];
104
105
2/2
✓ Branch 0 taken 116321501 times.
✓ Branch 1 taken 18672 times.
116340172 while (curr) { // Uses the fact that nil is 0 and the assumption
106 // that global environments are structurally correct.
107 116321501 lbm_uint curr_ix = lbm_dec_ptr(curr);
108 116321500 lbm_cons_t *curr_cell = &lbm_heaps[LBM_RAM_HEAP][curr_ix];
109 116321500 lbm_value c = curr_cell->car;
110 116321500 lbm_uint c_ix = lbm_dec_ptr(c); // Assumes environment is correctly shaped.
111 116321501 lbm_cons_t *c_cell = &lbm_heaps[LBM_RAM_HEAP][c_ix];
112
2/2
✓ Branch 0 taken 116316806 times.
✓ Branch 1 taken 4695 times.
116321501 if (c_cell->car == sym) {
113 116316806 *res = c_cell->cdr;
114 116316806 return true;
115 }
116 4695 curr = curr_cell->cdr;
117 }
118 18672 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 74074621 lbm_value lbm_env_set(lbm_value env, lbm_value key, lbm_value val) {
125
126 74074621 lbm_value curr = env;
127 lbm_value new_env;
128 lbm_value keyval;
129
130
2/2
✓ Branch 0 taken 93527260 times.
✓ Branch 1 taken 6923994 times.
100451254 while(lbm_is_cons_rw(curr)) {
131 93527260 lbm_cons_t *cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr)];
132 93527260 lbm_value car_val = cell->car;
133
1/2
✓ Branch 0 taken 93527260 times.
✗ Branch 1 not taken.
93527260 if (lbm_is_cons_rw(car_val)) { // else corrupt environment.
134 93527260 lbm_cons_t *car_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(car_val)];
135
2/2
✓ Branch 0 taken 67150627 times.
✓ Branch 1 taken 26376633 times.
93527260 if (car_cell->car == key) {
136 67150627 car_cell->cdr = val;
137 67150627 return env;
138 }
139 } // Possibly add a else here to handle corrupt environment.
140 26376633 curr = cell->cdr;
141 }
142
143 6923994 keyval = lbm_cons(key,val);
144
2/2
✓ Branch 0 taken 2730 times.
✓ Branch 1 taken 6921264 times.
6923994 if (lbm_is_symbol(keyval)){
145 2730 return keyval;
146 }
147
148 6921264 new_env = lbm_cons(keyval, env);
149 6921264 return new_env;
150 }
151
152 76143555 lbm_value lbm_env_modify_binding(lbm_value env, lbm_value key, lbm_value val) {
153
154 76143555 lbm_value curr = env;
155
156
2/2
✓ Branch 0 taken 130425727 times.
✓ Branch 1 taken 4726858 times.
135152585 while (lbm_is_cons_rw(curr)) {
157 130425727 lbm_cons_t *curr_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(curr)];
158 130425727 lbm_value car_val = curr_cell->car;
159
1/2
✓ Branch 0 taken 130425727 times.
✗ Branch 1 not taken.
130425727 if (lbm_is_cons_rw(car_val)) {
160 130425727 lbm_cons_t *car_cell = &lbm_heaps[LBM_RAM_HEAP][lbm_dec_ptr(car_val)];
161
2/2
✓ Branch 0 taken 71416697 times.
✓ Branch 1 taken 59009030 times.
130425727 if (car_cell->car == key) {
162 71416697 car_cell->cdr = val;
163 71416697 return env;
164 }
165 } // Else environment is invalid.
166 59009030 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 0 taken 5040608 times.
✓ Branch 1 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 0 taken 5040599 times.
✓ Branch 1 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 0 taken 12 times.
✓ Branch 1 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 0 taken 12 times.
✗ Branch 1 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