int a2_libsystem(struct a2_state* state) { int args = a2_top(state); if(args==0 || a2_type(state, 0)!=TSTRING) a2_err(state, "the arg must string type."); const char* str = a2_tostring(state, 0); system(str); return 0; }
int a2_librequire(struct a2_state* state){ int args = a2_top(state); if(args == 0 || a2_type(state, 0)!=TSTRING) a2_err(state, "the arg must string type."); a2_pushvalue(state, 0); // set key a2_require(state); return 1; }
// del int a2_libdel(struct a2_state* state){ int args = a2_top(state); if(args==2 && a2_type(state, 0)==TMAP) a2_delmap(state); else a2_err(state, "exp: del(map, key)"); return 0; }
int a2_libdofile(struct a2_state* state){ int args = a2_top(state); if(args==0 || a2_type(state, 0)!=TSTRING) a2_err(state, "the arg must string type."); const char* filename = a2_tostring(state, 0); int top = a2_top(state); a2_loadfile(state, filename); return a2_top(state) - top; }
int a2_libdostring(struct a2_state* state){ int args = a2_top(state); if(args==0 || a2_type(state, 0)!=TSTRING) a2_err(state, "the arg must string type."); const char* str = a2_tostring(state, 0); int top = a2_top(state); a2_dostring(state, str, strlen(str)); return a2_top(state) - top; }
int a2_libdostring(struct a2_state* state){ int args = a2_top(state); if(args==0 || a2_type(state, 0)!=TSTRING) a2_err(state, "the arg must string type."); const char* str = a2_tostring(state, 0); if(a2_dostring(state, str, strlen(str))){ printf("%s\n", a2_tostring(state, a2_top(state)-1)); } return 0; }
// add int a2_libadd(struct a2_state* state){ int args = a2_top(state); if(args<2) a2_err(state, "the number of args is error."); switch(a2_type(state, 0)){ case TARRAY: if(args!=2) a2_err(state, "the number of args is error at array add.[exp: add(array, value)]"); a2_addarray(state); break; case TMAP: if(args!=3) a2_err(state, "the number of args is error at map set.[exp: add(map, key value)]"); a2_setmap(state); break; default: a2_err(state, "the varable is not container."); } return 0; }
// print int a2_libprint(struct a2_state* state){ int args = a2_top(state); int i; for(i=0; i<args; i++){ switch(a2_type(state, i)){ case TNIL: printf("nil "); break; case TNUMBER: printf("%.14g ", a2_tonumber(state, i)); break; case TSTRING: printf("%s ", a2_tostring(state, i)); break; case TBOOL: printf("%s ", (a2_tobool(state, i))?("true"):("false")); break; case TCFUNCTION: printf("cfunc:%p ", a2_tocfunction(state, i)); break; case TCLOSURE: printf("closure:%p ", a2_topoint(state, i)); break; case TARRAY: printf("array:%p ", a2_topoint(state, i)); break; case TMAP: printf("map:%p ", a2_topoint(state, i)); break; default: assert(0); } } printf("\n"); return 0; }
A2_API inline const char* a2_typeinfo(struct a2_state* state, int idx){ return a2_type2string(a2_type(state, idx)); }