void setequal(t_env **env, char **cmd) { t_env *ptrmaillon; char **varvalue; varvalue = NULL; if (*(varvalue = ft_strsplit(cmd[1], '=')) == NULL) return ((void)setequal_error()); ptrmaillon = *env; while (ptrmaillon) { if (*varvalue && ptrmaillon->name &&\ ft_strcmp(ptrmaillon->name, varvalue[0]) == 0) { free(ptrmaillon->value); if (varvalue[1] != NULL) ptrmaillon->value = ft_strdup(varvalue[1]); else ptrmaillon->value = NULL; ft_freetab(varvalue); return ; } ptrmaillon = ptrmaillon->next; } env_new(env, varvalue[0], varvalue[1]); ft_freetab(varvalue); }
void add_env(t_env **env, char **cmd) { int i; if (*env == NULL) *env = initmaillon(); if (!cmd[1]) showenv(env); else if (ft_strcmp(cmd[1], "=") == 0 && !cmd[2]) ft_putendl("You need to add a value and a parameter"); else if (!(i = 0) && cmd[1] && !cmd[2]) { while (cmd[1][i] != '\0') { if (cmd[1][i++] == '=') return (setequal(env, cmd)); } setnoequal(env, cmd); } else if (cmd[1] && cmd[2]) { if (setenv_twocmd(env, cmd) == 1) env_new(env, cmd[1], cmd[2]); } }
BDNProgram *bd_alpha_convert(BDNProgram *prog) { BDNProgram *nprog = malloc(sizeof(BDNProgram)); bd_nprogram_init(nprog); Vector *vec; BDNExprDef *def; int i; Env *env = env_new(); // set primitive labels to env PrimSig *sig; for(i = 0; i < primsigs->length; i++){ sig = vector_get(primsigs, i); env_set(env, sig->name, sig->lbl); } // set toplevel names to env vec = prog->defs; for(i = 0; i < vec->length; i++){ def = vector_get(vec, i); env_set(env, def->ident->name, bd_generate_toplevel_lbl(def->ident->name)); } // convert defs vec = prog->defs; for(i = 0; i < vec->length; i++){ def = vector_get(vec, i); vector_add(nprog->defs, bd_nexpr_def( bd_expr_ident(find_alt_name(env, def->ident->name), bd_type_clone(def->ident->type)), bd_alpha(env, def->body) )); } // convert maindef def = prog->maindef; nprog->maindef = bd_nexpr_def( bd_expr_ident(bd_generate_toplevel_lbl(def->ident->name), bd_type_clone(def->ident->type)), bd_alpha(env, def->body) ); // destroy vec = prog->defs; for(i = 0; i < vec->length; i++){ def = vector_get(vec, i); free(env_get(env, def->ident->name)); } env_destroy(env); bd_nprogram_destroy(prog); return nprog; }
static void closure_setup(mrb_state *mrb, struct RProc *p, int nlocals) { struct REnv *e; if (!mrb->c->ci->env) { e = env_new(mrb, nlocals); mrb->c->ci->env = e; } else { e = mrb->c->ci->env; } p->env = e; }
int main(int argc, const char * argv[]) { PaStream *stream; PaError err; synth s = synth_new(); //synth_add_oscillator(s, osc_new(440.0f, OSC_TYPE_SINE)); //synth_add_oscillator(s, osc_new(880.0f, OSC_TYPE_SINE)); //synth_add_oscillator(s, osc_new(1760.0f, OSC_TYPE_SINE)); //synth_add_oscillator(s, osc_new(1108.7f, OSC_TYPE_SQUARE)); /* Test that you can't add more than 4 oscillators */ //synth_add_oscillator(s, osc_new(440.0f, OSC_TYPE_SINE)); synth_set_envelope(s, env_new(0.005, 0.625, 0.5, 0.5, ENVELOPE_TYPE_EXPONENTIAL)); midi_init(); /* Play every other note. Impossible for a human */ // for (i = 0; i < NUM_MIDI_NOTES / 2; ++i) { // synth_add_note(s, 2*i, 90); // } /* A440 100% volume */ /*synth_add_note(s, 69, 90); synth_add_note(s, 57, 90); synth_add_note(s, 81, 90); synth_add_note(s, 45, 90);*/ err = Pa_Initialize(); CHK(err); err = Pa_OpenDefaultStream(&stream, 0, 1, paFloat32, SR, 512, callback, (void*)s); CHK(err); err = Pa_StartStream(stream); CHK(err); midi_loop(s); err = Pa_StopStream(stream); CHK(err); err = Pa_CloseStream(stream); CHK(err); Pa_Terminate(); synth_destroy(s); midi_terminate(); return 0; }
int main() { int tx_port = 0; char *source = "test/test_defs.b"; sys_init(0); tx_server(source, "bin/state", &tx_port); vol_init(0, "bin/volume"); char *code = sys_load(source); env = env_new(source, code); mem_free(code); int len = 0; char **files = sys_list("test/data", &len); vars = vars_new(len); rvars = vars_new(len); for (int i = 0; i < len; ++i) { vars_add(rvars, files[i], 0, NULL); vars_add(vars, files[i], 0, NULL); } vars_add(vars, "___param", 0, NULL); test_vars(); test_load(); test_param(); test_clone(); test_eq(); test_store(); test_select(); test_rename(); test_extend(); test_join(); test_project(); test_semidiff(); test_summary(); test_union(); test_compound(); test_call(); tx_free(); env_free(env); mem_free(files); vars_free(vars); vars_free(rvars); return 0; }
void setnoequal(t_env **env, char **cmd) { t_env *ptrmaillon; ptrmaillon = *env; while (ptrmaillon) { if (ptrmaillon->name && ft_strcmp(ptrmaillon->name, cmd[1]) == 0) { setenv_error(cmd); return ; } ptrmaillon = ptrmaillon->next; } env_new(env, cmd[1], cmd[2]); }
PUBLIC struct comal_env *env_find(char *name) { struct env_list *walk = env_root; while (walk) { if (strcmp(walk->env->envname, name) == 0) break; walk = walk->next; } if (walk) return walk->env; my_printf(MSG_DIALOG, 1, "Creating new environment %s", name); return env_new(name); }
MRB_API struct RProc * mrb_proc_new_cfunc_with_env(mrb_state *mrb, mrb_func_t func, mrb_int argc, const mrb_value *argv) { struct RProc *p = mrb_proc_new_cfunc(mrb, func); struct REnv *e; int i; p->env = e = env_new(mrb, argc); MRB_ENV_UNSHARE_STACK(e); e->stack = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * argc); if (argv) { for (i = 0; i < argc; ++i) { e->stack[i] = argv[i]; } } else { for (i = 0; i < argc; ++i) { SET_NIL_VALUE(e->stack[i]); } } return p; }
static void processor(const char *tx_addr, int port) { sys_init(1); sys_log('E', "started port=%d, tx=%s\n", port, tx_addr); /* connect to the control thread */ char addr[MAX_ADDR]; sys_address(addr, port); IO *io = sys_connect(addr, IO_CHUNK); tx_attach(tx_addr); /* get env code from the tx */ char *code = tx_program(); char *res = mem_alloc(MAX_BLOCK); while (!io->stop) { sys_iready(io, -1); int status = -1; long long sid = 0LL, time = sys_millis(); Env *env = NULL; Arg *arg = NULL; Vars *v = vars_new(0), *r = NULL, *w = NULL; Http_Req *req = http_parse_req(io); if (io->stop) goto exit; if (req == NULL) { status = http_400(io); goto exit; } if (req->method == OPTIONS) { status = http_opts(io); goto exit; } env = env_new("net", code); if (str_idx(req->path, "/fn") == 0) { int idx = (req->path[3] == '/') ? 4 : 3; int i = 0, len = 1, cnt = 0; Func **fns = env_funcs(env, req->path + idx, &cnt); status = http_200(io); while (status == 200 && len) { len = pack_fn2csv(fns, cnt, res, MAX_BLOCK, &i); status = http_chunk(io, res, len); } mem_free(fns); goto exit; } /* compare the request with the function defintion */ Func *fn = env_func(env, req->path + 1); if (fn == NULL) { Error *err = error_new("unknown function '%s'", req->path + 1); status = http_404(io, err->msg); mem_free(err); goto exit; } if (fn->rp.name != NULL && req->method != POST) { status = http_405(io, POST); goto exit; } if (fn->rp.name == NULL && req->method == POST) { status = http_405(io, GET); goto exit; } /* TODO: think what to do with duplicate parameter values */ for (int i = 0; i < req->args->len; ++i) { char *name = req->args->names[i]; if (array_freq(req->args->names, req->args->len, name) > 1) { Error *err = error_new("duplicate parameter '%s' " "(not supported)", name); status = http_404(io, err->msg); mem_free(err); goto exit; } } if (fn->pp.len != req->args->len) { Error *err = error_new("expected %d primitive parameters, got %d", fn->pp.len, req->args->len); status = http_404(io, err->msg); mem_free(err); goto exit; } arg = mem_alloc(sizeof(Arg)); for (int i = 0; i < fn->pp.len; ++i) { char *name = fn->pp.names[i]; Type t = fn->pp.types[i]; int idx = array_scan(req->args->names, req->args->len, name); if (idx < 0) { Error *err = error_new("unknown parameter '%s'", name); status = http_404(io, err->msg); mem_free(err); goto exit; } char *val = req->args->vals[idx]; int error = 0; if (t == Int) { arg->vals[i].v_int = str_int(val, &error); } else if (t == Real) arg->vals[i].v_real = str_real(val, &error); else if (t == Long) arg->vals[i].v_long = str_long(val, &error); else if (t == String) { error = str_len(val) > MAX_STRING; if (!error) str_cpy(arg->vals[i].v_str, val); } if (error) { Error *err = error_new("value '%s' (parameter '%s') " "is not of type '%s'", val, name, type_to_str(t)); status = http_404(io, err->msg); mem_free(err); goto exit; } } if (fn->rp.name != NULL) { TBuf *body = NULL; if (req->len > 0) { Error *err = pack_csv2rel(req->body, fn->rp.head, &body); if (err != NULL) { status = http_404(io, err->msg); mem_free(err); goto exit; } } else { body = tbuf_new(); } vars_add(v, fn->rp.name, 0, body); /* project the parameter */ Rel *param = rel_project(rel_load(fn->rp.head, fn->rp.name), fn->rp.head->names, fn->rp.head->len); rel_eval(param, v, arg); /* clean the previous version */ tbuf_clean(body); tbuf_free(body); /* replace with the new body */ int vpos = array_scan(v->names, v->len, fn->rp.name); v->vals[vpos] = param->body; param->body = NULL; rel_free(param); } /* start a transaction */ r = vars_new(fn->r.len); w = vars_new(fn->w.len); for (int i = 0; i < fn->r.len; ++i) vars_add(r, fn->r.names[i], 0, NULL); for (int i = 0; i < fn->w.len; ++i) vars_add(w, fn->w.names[i], 0, NULL); sid = tx_enter(addr, r, w); /* prepare variables */ for (int i = 0; i < r->len; ++i) { TBuf *body = vol_read(r->vols[i], r->names[i], r->vers[i]); vars_add(v, r->names[i], 0, body); } for (int i = 0; i < w->len; ++i) { int pos = array_scan(v->names, v->len, w->names[i]); if (pos < 0) vars_add(v, w->names[i], 0, NULL); } for (int i = 0; i < fn->t.len; ++i) vars_add(v, fn->t.names[i], 0, NULL); /* evaluate the function body */ for (int i = 0; i < fn->slen; ++i) rel_eval(fn->stmts[i], v, arg); /* prepare the return value. note, the resulting relation is just a container for the body, so it is not freed */ Rel *ret = NULL; if (fn->ret != NULL) ret = fn->stmts[fn->slen - 1]; /* persist the global variables */ for (int i = 0; i < w->len; ++i) { int idx = array_scan(v->names, v->len, w->names[i]); if (idx < 0) { status = http_500(io); goto exit; } vol_write(w->vols[i], v->vals[idx], w->names[i], w->vers[i]); tbuf_free(v->vals[idx]); v->vals[idx] = NULL; } /* confirm a success and send the result back */ status = http_200(io); if (status != 200) goto exit; tx_commit(sid); /* N.B. there is no explicit revert as the transaction manager handles nested tx_enter and a connectivity failure as a rollback */ int len = 1, i = 0; while (status == 200 && len) { len = pack_rel2csv(ret, res, MAX_BLOCK, i++); status = http_chunk(io, res, len); } exit: if (status != -1) sys_log('E', "%016llX method %c, path %s, time %lldms - %3d\n", sid, (req == NULL) ? '?' : req->method, (req == NULL) ? "malformed" : req->path, sys_millis() - time, status); if (r != NULL) vars_free(r); if (w != NULL) vars_free(w); if (arg != NULL) mem_free(arg); if (req != NULL) http_free_req(req); if (env != NULL) env_free(env); for (int i = 0; i < v->len; ++i) if (v->vals[i] != NULL) { tbuf_clean(v->vals[i]); tbuf_free(v->vals[i]); } vars_free(v); sys_term(io); } mem_free(code); mem_free(res); tx_detach(); sys_close(io); }