SemVal *p_execute(SemVal *arr) { register size_t count; SemVal tmp, *argp, /* pointer to args */ e; count = arr->type; /* get argument count */ if (count < 6) /* to few arguments */ { util_semantic(gp_illegalArgCount, "execute"); return (arr); /* dummy args return */ } argp = codestruc(arr, 0); /* point to first arg */ e = *(argp + 2); /* cmd head info at e */ p_callRss(&e, f_cmd_head); /* code for cmd_head at e */ p_callRss(argp + 3, f_arg_head); /* code for arg_head */ p_catCode(&e, argp + 3); /* code appended to e*/ p_callRss(&argp[count - 2], f_arg_tail); /* code for arg_tail */ p_catCode(&e, &argp[count - 2]); /* code appended to e*/ p_callRss(&argp[count - 1], f_cmd_tail); /* code for cmd_tail */ p_catCode(&e, &argp[count - 1]); /* code appended to e*/ /* keep variable # of args */ memmove(argp + 2, argp + 4, (count - 2) * sizeof(SemVal)); arr->type -= 4; /* remove 4 arguments */ p_catCode(&e, p_specials(f_exec, arr)); /* catenate call-code */ free(gp_stringbuf); /* make sure empty string */ gp_stringbuf = rss_strdup(""); /* is pushed */ tmp = *p_stackFrame(e_str | e_const); /* empty string argument */ p_expr2stack(&tmp); p_catCode(&e, &tmp); /* empty string on the stack */ p_generateCode(&e, op_call_rss, f_cmd_tail); /* used with cmd_tail..cmd_head */ p_generateCode(&e, op_call_rss, f_arg_tail); p_generateCode(&e, op_call_rss, f_arg_head); p_callRss(&e, f_cmd_head); *arr = e; return arr; }
SemVal *p_ternary(SemVal *cond, SemVal *ifTrue, SemVal *ifFalse) { if ((ifTrue->type & ifFalse->type & e_typeMask) == 0) { util_semantic(gp_typeConflict, "?:"); p_clearOperands(ifTrue, ifFalse); return cond; } if (test_type(cond, e_const)) /* constant: true or false */ { p_discard(cond); if (cond->evalue) { p_discard(ifFalse); p_expr2stack(ifTrue); return ifTrue; } p_discard(ifTrue); p_expr2stack(ifFalse); return ifFalse; } p_expr2stack(ifTrue); /* convert the expressions to code */ p_expr2stack(ifFalse); p_generateCode(cond, op_jmp_false, j_falselist); /* jmp around the ifTrue code */ p_patchupTrue(cond, 1); /* destination for the ifTrue code */ p_catCode(cond, ifTrue); /* cond = cond + ifTrue */ p_generateCode(cond, op_jmp, j_truelist); /* jmp around the ifFalse code */ p_patchupFalse(cond, 1); /* destination of the false alternative */ p_catCode(cond, ifFalse); /* cond = cond + ifTrue + jmp + ifFalse */ p_patchupTrue(cond, 1); /* jump from ifTrue to the end of expr. */ return cond; /* ?: return */ }
SemVal *p_catStmnts(SemVal *lval, SemVal *rval) { p_patchupFalse(lval, 1); msg("lval length: %u, rval length: %u", lval->codelen, rval->codelen); if (gp_nestLevel == 0) { util_out(gp_bin, lval->code, lval->codelen); p_discard(lval); return rval; } return p_catCode(lval, rval); }
void p_catArgs(SemVal *arr) { register unsigned count; SemVal *ep; SemVal e; if (!(count = arr->type)) return; /* no arguments */ ep = (SemVal *)arr->code; /* local pointer to SemVals */ e = ep[--count]; /* e: code of last argument */ while (count--) p_catCode(&e, &ep[count]); /* catenate next argument(s) */ free(arr->code); /* memory of array is free again */ *arr = e; /* arguments changed to code */ }
SemVal *p_indexOp(SemVal *larg, SemVal *rarg) { register int ok; ExprType type = f_element; SemVal *tmp; p_expr2stack(larg); /* arg to stack */ p_expr2stack(rarg); /* arg to stack */ /* This follows the code of `p_twoArgs.c' to compute a list/string */ /* element */ /* first arg must be int */ if (!test_type(larg, e_int)) /* first expression is no int */ { tmp = larg; /* then swap */ larg = rarg; rarg = tmp; } if ( (ok = test_type(larg, e_int)) ) /* right arg must be int */ { /* second arg == list: ok */ if (!(ok = test_type(rarg, e_list))) { /* second arg == string: ok */ ok = test_type(rarg, e_str); type = f_str_el; /* string element requested */ } } if (ok) { p_catCode(rarg, larg); /* make one code vector */ p_callRss(rarg, type); } else { util_semantic(gp_typeConflict, gp_funstring[type]); p_discard(larg); } return (rarg); }
/* opstr is '=', or "/=", etc. */ SemVal *p_assignment(SemVal *lval, SemVal *rval, char *opstr) { SemVal *tmp; unsigned type; unsigned value; if (!test_type(lval, e_var)) { util_semantic(gp_lvalueNeeded, opstr); p_discard(rval); return lval; } p_trySIconvert(lval, rval); p_expr2stack(rval); /* convert rval to code */ /* same types */ if (lval->type & rval->type & (e_int | e_str | e_list)) { type = lval->type; /* save type/idx for return */ value = lval->evalue; p_generateCode(lval, op_copy_var, lval->evalue); tmp = p_catCode(rval, lval); /* catenate p_assignment code */ tmp->evalue = value; /* set lvalue type and idx */ tmp->type = type; return tmp; } util_semantic(gp_typeConflict, opstr); p_discard(rval); return lval; }
SemVal *p_orBool(SemVal *lexp, SemVal *rexp) { if (lexp->type & rexp->type & e_const) /* two constants: compute result */ { lexp->evalue = (test_type(lexp, e_str) || lexp->evalue) || (test_type(rexp, e_str) || rexp->evalue); set_type(lexp, e_const | e_int); } else /* at least one code-part */ { p_forceExpr2Bool(lexp); /* boolean code */ p_forceExpr2Bool(rexp); lexp->codelen -= p_rmJmpZero(lexp->codelen, lexp->falselist, lexp->falselen); p_patchupFalse(lexp, 1); lexp = p_catCode(lexp, rexp); set_type(lexp, e_bool | e_stack); } return (lexp); }
SemVal *p_twoArgs(ExprType type, SemVal *larg, SemVal *rarg) { register int ok; msg("start"); p_expr2stack(larg); /* arg to stack */ p_expr2stack(rarg); /* arg to stack */ switch ((FunNr)type) { case f_fgets: ok = test_type(larg, e_str) && test_type(rarg, e_list); break; case f_element: /* f_element */ /* first arg must be int */ if ( (ok = test_type(larg, e_int)) ) { /* second arg == list: ok */ if (!(ok = test_type(rarg, e_list))) { /* second arg == string: ok */ ok = test_type(rarg, e_str); type = f_str_el; /* string element requested */ } } break; case f_resize: ok = test_type(larg, e_str) && test_type(rarg, e_int); break; case f_listfind: ok = test_type(larg, e_list) && test_type(rarg, e_str); break; case f_listunion: ok = test_type(larg, e_list) && test_type(rarg, e_str | e_list); break; default: /* case f_strchr: case f_strtok: case f_c_ext: case f_c_base: case f_c_path: case f_strfind: */ ok = larg->type & rarg->type & e_str; } msg("types test %d, funstring: %x", ok, type); if (ok) { p_catCode(rarg, larg); /* make one code vector */ p_callRss(rarg, type); } else { util_semantic(gp_typeConflict, gp_funstring[type]); p_discard(larg); rarg = p_stackFrame(e_null); } msg("leaving"); return rarg; }