EC_API EC_OBJ EcSetInstanceVariable( EC_OBJ obj, EC_OBJ at_class, const char *name, EC_OBJ value ) { EcInt offs; if (EC_NULLP(at_class)) at_class = EC_OBJECTCLASS(obj); offs = search_variable( EC_CLASSNIVARS(at_class), EC_CLASSIVTABLE(at_class), EcInternSymbol( name ) ); /* ec_fprintf( stderr, "SetIV obj: %w at_class: %w name: `%s'\n", obj, at_class, name ); ec_fprintf( stderr, " offs: %d nivars: %d ioffs: %d\n", offs, EC_CLASSNIVARS(at_class), EC_CLASSIOFFSET(at_class) );*/ if (offs < 0) return Ec_ERROR; /* fprintf( stderr, "SetIV: `%s' offs: %d coffs: %d\n", name, offs, EC_CLASSIOFFSET(at_class) );*/ return (EC_OBJECTIVARS(obj)[EC_CLASSIOFFSET(at_class) + offs] = value); }
/** * @brief is_variable_assignment is called when the user command is of the form var = something * (provided var is not an internal or external command) * This function searches the var_list and if it finds exiting variable with the same name, * then the corresponding structure of the variable is updated; * else, a new variable structure is created and is added to varlist * * @param cmd The command being processed * * @return YES, if cmd is indeed of the form 'x = something'; else NO */ int is_variable_assignment(command_t cmd) { int len = command_length(cmd); if(len > 2) { if(!strcmp(cmd->next->string,"=")) { struct variable* v = search_variable(cmd->string); if(cmd->next->next->string[0] == '"') { if(v == NULL) { struct variable* v = malloc(sizeof(struct variable)); v->string = malloc(strlen(cmd->next->next->string) + 1); strcpy(v->string, cmd->next->next->string + 1); v->string[strlen(cmd->next->next->string) - 2] = '\0'; v->next = var_list; v->name = malloc(sizeof(strlen(cmd->string) + 1)); strcpy(v->name, cmd->string); var_list = v; return YES; } else { v->string = malloc(sizeof(strlen(cmd->next->next->string) + 1)); strcpy(v->string,cmd->next->next->string + 1); v->string[strlen(cmd->next->next->string) - 2] = '\0'; return YES; } } else { return NO; } } else { return NO; } } else { return NO; } }
/** * @brief this function is used to implement "export var" command * * @param cmd The command being processed * * @return YES, if cmd is of form 'export var'; else NO */ int is_export(command_t cmd) { if(!strcmp(cmd->string, "export")) { if(command_length(cmd) != 2) { printf("improper usage of export command\n"); return YES; } else { struct variable* v = search_variable(cmd->next->string); if(v == NULL) { printf("no such variable\n"); } else { if(!strcmp(v->string,"")) { printf("sorry, kavach can only export strings and not integers :( \n"); } else { setenv(v->name,v->string,1); } } } return YES; } else { return NO; } }
static void patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest) { size_t i; mrb_code c; int argc = 0; for (i = 0; i < irep->ilen; i++) { c = irep->iseq[i]; switch(GET_OPCODE(c)){ case OP_ENTER: { mrb_aspec ax = GETARG_Ax(c); /* extra 1 means a slot for block */ argc = MRB_ASPEC_REQ(ax)+MRB_ASPEC_OPT(ax)+MRB_ASPEC_REST(ax)+MRB_ASPEC_POST(ax)+1; } break; case OP_EPUSH: patch_irep(mrb, irep->reps[GETARG_Bx(c)], bnest + 1); break; case OP_LAMBDA: { int arg_c = GETARG_c(c); if (arg_c & OP_L_CAPTURE) { patch_irep(mrb, irep->reps[GETARG_b(c)], bnest + 1); } } break; case OP_SEND: if (GETARG_C(c) != 0) { break; } { mrb_code arg = search_variable(mrb, irep->syms[GETARG_B(c)], bnest); if (arg != 0) { /* must replace */ irep->iseq[i] = MKOPCODE(OP_GETUPVAR) | MKARG_A(GETARG_A(c)) | arg; } } break; case OP_MOVE: /* src part */ if (potential_upvar_p(irep->lv, GETARG_B(c), argc, irep->nlocals)) { mrb_code arg = search_variable(mrb, irep->lv[GETARG_B(c) - 1].name, bnest); if (arg != 0) { /* must replace */ irep->iseq[i] = MKOPCODE(OP_GETUPVAR) | MKARG_A(GETARG_A(c)) | arg; } } /* dst part */ if (potential_upvar_p(irep->lv, GETARG_A(c), argc, irep->nlocals)) { mrb_code arg = search_variable(mrb, irep->lv[GETARG_A(c) - 1].name, bnest); if (arg != 0) { /* must replace */ irep->iseq[i] = MKOPCODE(OP_SETUPVAR) | MKARG_A(GETARG_B(c)) | arg; } } break; } } }
static void patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest, mrb_irep *top) { int i; mrb_code c; int argc = irep_argc(irep); for (i = 0; i < irep->ilen; i++) { c = irep->iseq[i]; switch(GET_OPCODE(c)){ case OP_EPUSH: patch_irep(mrb, irep->reps[GETARG_Bx(c)], bnest + 1, top); break; case OP_LAMBDA: { int arg_c = GETARG_c(c); if (arg_c & OP_L_CAPTURE) { patch_irep(mrb, irep->reps[GETARG_b(c)], bnest + 1, top); } } break; case OP_SEND: if (GETARG_C(c) != 0) { break; } else { mrb_code arg = search_variable(mrb, irep->syms[GETARG_B(c)], bnest); if (arg != 0) { /* must replace */ irep->iseq[i] = MKOPCODE(OP_GETUPVAR) | MKARG_A(GETARG_A(c)) | arg; } } break; case OP_MOVE: /* src part */ if (potential_upvar_p(irep->lv, GETARG_B(c), argc, irep->nlocals)) { mrb_code arg = search_variable(mrb, irep->lv[GETARG_B(c) - 1].name, bnest); if (arg != 0) { /* must replace */ irep->iseq[i] = MKOPCODE(OP_GETUPVAR) | MKARG_A(GETARG_A(c)) | arg; } } /* dst part */ if (potential_upvar_p(irep->lv, GETARG_A(c), argc, irep->nlocals)) { mrb_code arg = search_variable(mrb, irep->lv[GETARG_A(c) - 1].name, bnest); if (arg != 0) { /* must replace */ irep->iseq[i] = MKOPCODE(OP_SETUPVAR) | MKARG_A(GETARG_B(c)) | arg; } } break; case OP_GETUPVAR: { int lev = GETARG_C(c)+1; mrb_irep *tmp = search_irep(top, bnest, lev, irep); if (potential_upvar_p(tmp->lv, GETARG_B(c), irep_argc(tmp), tmp->nlocals)) { mrb_code arg = search_variable(mrb, tmp->lv[GETARG_B(c)-1].name, bnest); if (arg != 0) { /* must replace */ irep->iseq[i] = MKOPCODE(OP_GETUPVAR) | MKARG_A(GETARG_A(c)) | arg; } } } break; case OP_SETUPVAR: { int lev = GETARG_C(c)+1; mrb_irep *tmp = search_irep(top, bnest, lev, irep); if (potential_upvar_p(tmp->lv, GETARG_B(c), irep_argc(tmp), tmp->nlocals)) { mrb_code arg = search_variable(mrb, tmp->lv[GETARG_B(c)-1].name, bnest); if (arg != 0) { /* must replace */ irep->iseq[i] = MKOPCODE(OP_SETUPVAR) | MKARG_A(GETARG_A(c)) | arg; } } } break; case OP_STOP: if (mrb->c->ci->acc >= 0) { irep->iseq[i] = MKOP_AB(OP_RETURN, irep->nlocals, OP_R_NORMAL); } break; } } }