/* main.c by Michael Thorpe 2017-05-28 */ #include #include #include "lisp.h" extern char **environ; static int main_init(int argc,char **argv) { obj *c,*o; int i; c=INCREF(&nilobj); for(i=argc-1;i>0;i--) { o=new_string(argv[i],strlen(argv[i])); if(!o) return(1); c=cons(o,c); if(!c) return(1); } if(store_object("#argv",c)) return(1); return(0); } static int env_init() { obj *c,*key,*o,*value; int i; char *s; c=INCREF(&nilobj); for(i=0;environ[i];i++) { s=strchr(environ[i],'='); if(s) { value=new_string(s+1,strlen(s+1)); } else { s=environ[i]+strlen(environ[i]); value=INCREF(&nilobj); } if(!value) return(1); key=new_word(environ[i],s-environ[i]); if(!key) return(1); o=cons(key,value); if(!o) return(1); c=cons(o,c); if(!c) return(1); } if(store_object("#environment",c)) return(1); return(0); } int main(int argc,char **argv) { int i; if(register_objtype(&bytefunc_objtype) #ifdef USE_BIGINT || register_objtype(&bigint_objtype) #endif || register_objtype(&cons_objtype) #ifdef USE_FLOAT || register_objtype(&float_objtype) #endif || register_objtype(&integer_objtype) || register_objtype(&nil_objtype) || register_objtype("e_objtype) || register_objtype(&rawfunc_objtype) || register_objtype(&stream_objtype) || register_objtype(&string_objtype) || register_objtype(&word_objtype)) { return(1); } /* Initialize variables needed for throwoom() etc.: */ if(throw_init() /* Set up #environment list: */ || env_init() /* Set up #argv list: */ || main_init(argc,argv) /* Initialize #vm-opcodes etc.: */ || vm_init() /* Load .lisps that have been translated to C: */ || preload_init()) { fprintf(stderr,"got bad return value from an init!\n"); return(1); } /* Actually run the #init function (i.e., main loop): */ i=run_vm(); /* Generic object cleanup functions: */ throw_exit(); vm_exit(); /* Free all remaining objects: */ if(clean_up()) i=1; if(i) fprintf(stderr,"Failed to run correctly\n"); return(i); }