MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / sh_assign

Function sh_assign

src/pipeline/pass_shell.c:2250–2303  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2248}
2249
2250static void sh_assign(sh_t *s, TSNode n, bool local) {
2251 TSNode name = ts_node_child_by_field_name(n, TS_FIELD("name"));
2252 TSNode value = ts_node_child_by_field_name(n, TS_FIELD("value"));
2253 if (!ts_node_is_null(value)) {
2254 sh_walk_substs(s, value);
2255 }
2256 if (ts_node_is_null(name)) {
2257 return;
2258 }
2259 if (sh_type(name, "subscript")) {
2260 TSNode base = ts_node_child_by_field_name(name, TS_FIELD("name"));
2261 if (!ts_node_is_null(base)) {
2262 sh_set(s, sh_text(s, base), &SH_UNKNOWN_VAL, local);
2263 }
2264 return;
2265 }
2266 const char *nm = sh_text(s, name);
2267 sh_val_t *v = cbm_arena_alloc(s->a, sizeof(*v));
2268 if (!nm || !v) {
2269 return;
2270 }
2271 *v = (sh_val_t){"", NULL, 0};
2272 if (sh_type(value, "array")) {
2273 uint32_t cnt = ts_node_named_child_count(value);
2274 const char **items = cbm_arena_alloc(s->a, (cnt ? cnt : SKIP_ONE) * sizeof(*items));
2275 if (!items) {
2276 return;
2277 }
2278 int k = 0;
2279 for (uint32_t i = 0; i < cnt; i++) {
2280 TSNode c = ts_node_named_child(value, i);
2281 if (!sh_type(c, "comment")) {
2282 items[k++] = sh_eval_word(s, c);
2283 }
2284 }
2285 *v = (sh_val_t){k > 0 ? items[0] : "", items, k};
2286 } else if (!ts_node_is_null(value)) {
2287 v->s = sh_eval(s, value);
2288 }
2289 bool append = false;
2290 for (uint32_t i = 0; i < ts_node_child_count(n); i++) {
2291 TSNode c = ts_node_child(n, i);
2292 append = append || (!ts_node_is_named(c) && strcmp(ts_node_type(c), "+=") == 0);
2293 }
2294 if (append) {
2295 bool found = false;
2296 const sh_val_t *prev = sh_lookup(s, nm, &found);
2297 if (found) {
2298 v->s = (prev->s && v->s) ? cbm_arena_sprintf(s->a, "%s%s", prev->s, v->s) : NULL;
2299 v->items = NULL;
2300 }
2301 }
2302 sh_set(s, nm, v, local);
2303}
2304
2305/* local / declare / typeset / export / readonly NAME[=VALUE] ... */
2306static void sh_declare(sh_t *s, TSNode n) {

Callers 2

sh_declareFunction · 0.85
sh_walkFunction · 0.85

Calls 9

sh_walk_substsFunction · 0.85
sh_typeFunction · 0.85
sh_setFunction · 0.85
sh_textFunction · 0.85
sh_eval_wordFunction · 0.85
sh_evalFunction · 0.85
sh_lookupFunction · 0.85
cbm_arena_allocFunction · 0.50
cbm_arena_sprintfFunction · 0.50

Tested by

no test coverage detected