#include #include #include #include #include static const char *code = \ "# test program for libc.nsi\n" "dup print \"\n\" print\n" "\"food\"\n" "ns_pop\n" "\"one\" \"one\" strcmp $x\n" "\"one\" \"two\" strcmp $y\n" "\"strcmp one-one: \" print x print \"\n\" print\n" "\"strcmp one-two: \" print y print \"\n\" print\n" "\"ls\" system\n" "# \"echo 'hello world'\" system\n" "{ \"true\n\" print } $t\n" "\"false\" t ns_pop\n" "# print\n" "exit\n" ; static void myns_system() { struct ns_obj obj; struct ns_obj arg_0 = ns_pop(); int ret = system(arg_0.u.s->arr); obj.type = TY_INT; obj.u.i = ret; ns_push(obj); } static void myns_strcmp() { struct ns_obj obj; struct ns_obj arg_0 = ns_pop(); struct ns_obj arg_1 = ns_pop(); int ret = strcmp(arg_0.u.s->arr, arg_1.u.s->arr); obj.type = TY_INT; obj.u.i = ret; ns_push(obj); } static void myns_ns_pop() { ns_pop(); } static struct ns_namemap my_ns_funcmap[] = { { "system", { TY_FUNC, { .f = myns_system } } }, { "strcmp", { TY_FUNC, { .f = myns_strcmp } } }, { "ns_pop", { TY_FUNC, { .f = myns_ns_pop } } }, { 0 } }; static void my_ns_init() { struct ns_namemap *curr = my_ns_funcmap; for(;curr->key;++curr) trie_add(ns_functrie, curr->key, curr->obj); } int main() { ns_init(); my_ns_init(); ns_interpret(code); return 0; }