CLIP_DLLEXPORT int _clip_load_inits(ClipMachine * mp, ClipFile * file) { long funcOffs = GETLONG(M_OFFS(file->body, 7, 4)); char *modbeg = M_OFFS(file->body, 2, 0); int pubnum = GETLONG(M_OFFS(file->body, 4, 2)); int initnum = GETSHORT(M_OFFS(file->body, 8, 4)); int exitnum = GETSHORT(M_OFFS(file->body, 8, 5)); ClipBlock b; ClipCodeVar c; int i; long hash; c.u.block = &b; file->pos = (long *) (modbeg + funcOffs); file->pos += 2 * pubnum; /*file->end = file->pos + 2 * num; */ for (i = 0; i < initnum; ++i) { make_func(file, &c, &hash); file->pos += 2; _clip_main_code(mp, c.u.block, _clip_argc, _clip_argv, _clip_envp); } for (i = 0; i < exitnum; ++i) { make_func(file, &c, &hash); file->pos += 2; mp->exits = realloc(mp->exits, (mp->nexits + 1) * sizeof(ClipBlock)); mp->exits[mp->nexits] = *c.u.block; ++file->refCount; ++mp->nexits; } return 0; }
item primitive_procedures(){ item temp = list(10, list(2, make_item("car"), make_func(base_car)), list(2, make_item("cdr"), make_func(base_cdr)), list(2, make_item("cons"), make_func(base_cons)), list(2, make_item("list"), make_func(base_list)), list(2, make_item("set-car!"), make_func(base_set_car)), list(2, make_item("set-cdr!"), make_func(base_set_cdr)), list(2, make_item("+"), make_func(base_add)), list(2, make_item("-"), make_func(base_sub)), list(2, make_item("="), make_func(base_equal)), list(2, make_item("*"), make_func(base_multiply))); return temp; }
/* * Parse a term. */ static bool parse_term_op(context_t cxt, term_t *val, unsigned priority) { term_t lval; if (!parse_term_head(cxt, &lval)) return false; do { char *op_name = (char *)gc_malloc(TOKEN_MAXLEN+1); token_t tok = token_peek(cxt, NULL, op_name); unsigned op_priority; assoc_t op_assoc; if (!parse_maybe_op(tok) || !binop_lookup(cxt->opinfo, op_name, &op_assoc, &op_priority, NULL, NULL) || op_priority > priority) { *val = lval; gc_free(op_name); return true; } if (op_priority == priority) { switch (op_assoc) { case YFX: *val = lval; return true; case XFY: break; case XFX: parse_error(cxt, "operator `!y%s!d' associativity error", op_name); gc_free(op_name); return false; } } if (!token_expect(cxt, tok)) { gc_free(op_name); return false; } term_t rval; if (!parse_term_op(cxt, &rval, op_priority)) { gc_free(op_name); return false; } atom_t atom = make_atom(gc_strdup(op_name), 2); gc_free(op_name); func_t f = make_func(atom, lval, rval); lval = term_func(f); } while (true); }
Future<typename std::result_of<Fn()>::type> runAsync(Fn task) { Future<typename std::result_of<Fn()>::type> future; auto fn = make_func(future, task); addTask(fn); return future; }
Future<typename std::result_of<Fn()>::type> runAsync(Fn task, int priority = DEFAULT_PRIORITY) { Future<typename std::result_of<Fn()>::type> future; auto fn = make_func(future, task); addTask(fn, priority); return future; }
static void iterator(void *key, void *value, void *user_data) { char *name = (char *)key; OperatorFunc func = (OperatorFunc)value; Scheme *sc = (Scheme *)user_data; if (!is_internal_operator(name)) new_slot_in_env(sc, make_symbol(sc, name), make_func(sc, func)); }
int _clip_next_File(struct ClipFile *file, ClipCodeVar * dest, long *hash) { if (file->pos == file->end) return 0; make_func(file, dest, hash); file->pos += 2; return 1; }
CLIP_DLLEXPORT int _clip_load_inits(ClipMachine * ClipMachineMemory, ClipFile * file) { long funcOffs = GETLONG(M_OFFS(file->body_of_ClipFile, 7, 4)); char *modbeg = M_OFFS(file->body_of_ClipFile, 2, 0); int pubnum = GETLONG(M_OFFS(file->body_of_ClipFile, 4, 2)); int initnum = GETSHORT(M_OFFS(file->body_of_ClipFile, 8, 4)); int exitnum = GETSHORT(M_OFFS(file->body_of_ClipFile, 8, 5)); ClipBlock b; ClipCodeVar c; int i; long hash; c.union_of_ClipCodeVar.ClipBlock_block_of_ClipCodeVar = &b; file->pos_of_ClipFile = (long *) (modbeg + funcOffs); file->pos_of_ClipFile += 2 * pubnum; /*file->end = file->pos + 2 * num; */ for (i = 0; i < initnum; ++i) { make_func(file, &c, &hash); file->pos_of_ClipFile += 2; _clip_main_code(ClipMachineMemory, c.union_of_ClipCodeVar.ClipBlock_block_of_ClipCodeVar, _clip_argc, _clip_argv, _clip_envp); } for (i = 0; i < exitnum; ++i) { make_func(file, &c, &hash); file->pos_of_ClipFile += 2; ClipMachineMemory->exits = (ClipBlock *) realloc(ClipMachineMemory->exits, (ClipMachineMemory->nexits + 1) * sizeof(ClipBlock)); ClipMachineMemory->exits[ClipMachineMemory->nexits] = *c.union_of_ClipCodeVar.ClipBlock_block_of_ClipCodeVar; ++file->refCount_of_ClipFile; ++ClipMachineMemory->nexits; } return 0; }
STATIC mp_obj_t ffimod_func(size_t n_args, const mp_obj_t *args) { (void)n_args; // always 4 mp_obj_ffimod_t *self = MP_OBJ_TO_PTR(args[0]); const char *symname = mp_obj_str_get_str(args[2]); void *sym = dlsym(self->handle, symname); if (sym == NULL) { mp_raise_OSError(MP_ENOENT); } return make_func(args[1], sym, args[3]); }
STATIC mp_obj_t ffimod_func(size_t n_args, const mp_obj_t *args) { (void)n_args; // always 4 mp_obj_ffimod_t *self = MP_OBJ_TO_PTR(args[0]); const char *symname = mp_obj_str_get_str(args[2]); void *sym = dlsym(self->handle, symname); if (sym == NULL) { nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ENOENT))); } return make_func(args[1], sym, args[3]); }
int main() { int array[] = {42}; std::vector<int> array2; array2.push_back(42); // Base function func(0, array); double a = 3.; double b = 4.; func(1, array, &a); func(2, array,NULL, &b); func(3, array, &a, &b); // "Convenience" functor func_functor<> func2; func2(4, array2); func_functor<std::list<double> > func3; std::list<double> a_list; a_list.push_back(5.); func3(5, array2, &a_list); func_functor<std::vector<double>, std::list<double> > func4; std::vector<double> a_vec; a_vec.push_back(6.); func4(6, array2, &a_vec, &a_list); // Convenience functor-function! make_func(a_list, empty_parameter())(7, array2, &a_list); make_func(empty_parameter(), a_list)(8, array2, NULL, &a_list); make_func(a_vec, a_list)(9, array2, &a_vec, &a_list); return 0; }
/* * Convert a constraint to a term. */ extern term_t solver_convert_cons(cons_t c) { switch (c->sym->type) { case X_CMP_Y: { atom_t atom = (c->sym == EQ? ATOM_EQ: ATOM_GT); return term_func(make_func(atom, c->args[X], c->args[Y])); } case X_CMP_C: { atom_t atom = (c->sym == EQ_C? ATOM_EQ: ATOM_GT); return term_func(make_func(atom, c->args[X], c->args[Y])); } case X_EQ_Y_OP_Z: { atom_t atom = (c->sym == EQ_PLUS? ATOM_ADD: ATOM_MUL); term_t t = term_func(make_func(atom, c->args[Y], c->args[Z])); atom = ATOM_EQ; return term_func(make_func(atom, c->args[X], t)); } case X_EQ_Y_OP_C: { term_t t; if (c->sym == EQ_PLUS_C) t = term_func(make_func(ATOM_ADD, c->args[Y], c->args[Z])); else t = term_func(make_func(ATOM_MUL, c->args[Z], c->args[Y])); atom_t atom = ATOM_EQ; return term_func(make_func(atom, c->args[X], t)); } default: { atom_t atom = make_atom(c->sym->name, c->sym->arity); return term_func(make_func_a(atom, c->args)); } } }
int _clip_first_File(struct ClipFile *file, ClipCodeVar * dest, long *hash) { long funcOffs = GETLONG(M_OFFS(file->body, 7, 4)); char *modbeg = M_OFFS(file->body, 2, 0); int pubnum = GETLONG(M_OFFS(file->body, 4, 2)); /*int initnum = GETLONG( M_OFFS(file->body, 7, 5)); */ /*int exitnum = GETLONG( M_OFFS(file->body, 7, 6)); */ int num; num = pubnum; file->pos = (long *) (modbeg + funcOffs); file->end = file->pos + 2 * num; if (num == 0) return 0; make_func(file, dest, hash); file->pos += 2; return 1; }
STATIC mp_obj_t mod_ffi_func(mp_obj_t rettype, mp_obj_t addr_in, mp_obj_t argtypes) { void *addr = (void*)MP_OBJ_TO_PTR(mp_obj_int_get_truncated(addr_in)); return make_func(rettype, addr, argtypes); }
int Parser::expression(int pos, int status) { int isputs = 0; if(tok.skip("$")) { // global varibale if(is_asgmt()) asgmt(); } else if(tok.skip("require")) { make_require(); } else if(tok.skip("def")) { blocksCount++; make_func(); } else if(tok.skip("module")) { blocksCount++; module = tok.tok[tok.pos++].val; eval(0, NON); module = ""; } else if(funcs.inside == false && !tok.is("def", 1) && !tok.is("module", 1) && !tok.is("$", 1) && !tok.is(";", 1) && module == "") { // main func entry funcs.inside = true; funcs.now++; funcs.append("main", ntv.count, 0); // append funcs ntv.genas("push ebp"); ntv.genas("mov ebp esp"); uint32_t espBgn = ntv.count + 2; ntv.genas("sub esp 0"); ntv.gencode(0x8b); ntv.gencode(0x75); ntv.gencode(0x0c); // mov esi, 0xc(%ebp) eval(0, BLOCK_NORMAL); ntv.gencode(0x81); ntv.gencode(0xc4); ntv.gencode_int32(ADDR_SIZE * (var.focus().size() + 6)); // add %esp nn ntv.gencode(0xc9);// leave ntv.gencode(0xc3);// ret ntv.gencode_int32_insert(ADDR_SIZE * (var.focus().size() + 6), espBgn); funcs.inside = false; } else if(is_asgmt()) { asgmt(); } else if((isputs=tok.skip("puts")) || tok.skip("print")) { do { ExprType et = expr_entry(); ntv.genas("push eax"); if(et.is_type(T_STRING)) { ntv.gencode(0xff); ntv.gencode(0x56); ntv.gencode(4);// call *0x04(esi) putString } else { ntv.gencode(0xff); ntv.gencode(0x16); // call (esi) putNumber } ntv.genas("add esp 4"); } while(tok.skip(",")); // for new line if(isputs) { ntv.gencode(0xff); ntv.gencode(0x56); ntv.gencode(8);// call *0x08(esi) putLN } } else if(tok.skip("for")) { blocksCount++; asgmt(); if(!tok.skip(",")) error("error: %d: expected ','", tok.tok[tok.pos].nline); make_while(); } else if(tok.skip("while")) { blocksCount++; make_while(); } else if(tok.skip("return")) { make_return(); } else if(tok.skip("if")) { blocksCount++; make_if(); } else if(tok.skip("else")) { uint32_t end; ntv.gencode(0xe9); end = ntv.count; ntv.gencode_int32(0);// jmp while end ntv.gencode_int32_insert(ntv.count - pos - 4, pos); eval(end, BLOCK_NORMAL); return 1; } else if(tok.skip("elsif")) { uint32_t endif, end; ntv.gencode(0xe9); endif = ntv.count; ntv.gencode_int32(0);// jmp while end ntv.gencode_int32_insert(ntv.count - pos - 4, pos); expr_entry(); // if condition ntv.gencode(0x83); ntv.gencode(0xf8); ntv.gencode(0x00);// cmp eax, 0 ntv.gencode(0x75); ntv.gencode(0x05); // jne 5 tok.skip(";"); ntv.gencode(0xe9); end = ntv.count; ntv.gencode_int32(0);// jmp while end eval(end, BLOCK_NORMAL); ntv.gencode_int32_insert(ntv.count - endif - 4, endif); return 1; } else if(tok.skip("break")) { make_break(); } else if(tok.skip("end")) { blocksCount--; if(status == NON) return 1; if(status == BLOCK_NORMAL) { ntv.gencode_int32_insert(ntv.count - pos - 4, pos); } else if(status == BLOCK_FUNC) funcs.inside = false; return 1; } else if(!tok.skip(";")) { expr_entry(); } return 0; }
/* * Parse a term (no operators). */ static bool parse_term_head(context_t cxt, term_t *val) { char *tokstr = (char *)gc_malloc(TOKEN_MAXLEN+1); term_t tokval; token_t tok = token_get(cxt, &tokval, tokstr); unsigned priority; if (parse_maybe_op(tok) && unop_lookup(cxt->opinfo, tokstr, &priority, NULL)) { term_t lval; if (!parse_term_op(cxt, &lval, priority)) { gc_free(tokstr); return false; } atom_t atom = make_atom(gc_strdup(tokstr), 1); func_t f = make_func(atom, lval); tokval = term_func(f); *val = tokval; gc_free(tokstr); return true; } bool ok = true; term_t *args = NULL; switch ((int)tok) { case '(': if (!parse_term_op(cxt, val, UINT32_MAX) || !token_expect(cxt, ')')) ok = false; break; case TOKEN_NIL: case TOKEN_BOOLEAN: case TOKEN_ATOM: case TOKEN_STRING: case TOKEN_NUMBER: *val = tokval; break; case TOKEN_VARIABLE: { if (token_peek(cxt, NULL, NULL) != '(') { // Really is a variable: *val = tokval; break; } // Otherwise this is a functor: var_t x = var(tokval); atom_t atom = make_atom(x->name, 0); if (!token_expect(cxt, '(')) { ok = false; break; } args = (term_t *)gc_malloc(MAX_ARGS * sizeof(term_t)); uint_t a = 0; if (token_peek(cxt, NULL, NULL) == ')') token_get(cxt, NULL, NULL); else { while (true) { ok = parse_term_op(cxt, &tokval, UINT32_MAX); if (!ok) break; if (a >= MAX_ARGS) { parse_error(cxt, "too many arguments; maximum is %zu", MAX_ARGS); ok = false; break; } args[a++] = tokval; tok = token_get(cxt, NULL, tokstr); if (tok == ',') continue; if (tok == ')') break; parse_error(cxt, "expected token `,' or `)'; got token " "`%s'", tokstr); ok = false; break; } if (!ok) break; } atom = atom_set_arity(atom, a); func_t f = make_func_a(atom, args); tokval = term_func(f); *val = tokval; break; } default: if (tok != TOKEN_ERROR) parse_error(cxt, "unexpected token `%s'", tokstr); ok = false; break; } gc_free(tokstr); gc_free(args); return ok; }