0533e21999-02-19Mirar (Pontus Hagland) #include "global.h"
7068e31999-06-12Mirar (Pontus Hagland) #include "stralloc.h" #include "global.h"
6dc2772000-07-28Fredrik Hübinette (Hubbe) RCSID("$Id: parser.c,v 1.8 2000/07/28 07:14:14 hubbe Exp $");
7068e31999-06-12Mirar (Pontus Hagland) #include "pike_macros.h" #include "interpret.h"
0533e21999-02-19Mirar (Pontus Hagland) #include "program.h"
6c4a1a1999-12-30Martin Stjernholm #include "program_id.h"
7068e31999-06-12Mirar (Pontus Hagland) #include "object.h" #include "operators.h"
0533e21999-02-19Mirar (Pontus Hagland)  #include "parser.h"
6dc2772000-07-28Fredrik Hübinette (Hubbe) /* must be included last */ #include "module_magic.h"
7068e31999-06-12Mirar (Pontus Hagland) #define PARSER_INITER
0533e21999-02-19Mirar (Pontus Hagland) 
7068e31999-06-12Mirar (Pontus Hagland) /*#define DEBUG*/
6c4a1a1999-12-30Martin Stjernholm #define PARSER_CLASS(name,init,exit,prog,id) \
7068e31999-06-12Mirar (Pontus Hagland)  void init(void); void exit(void); struct program *prog; #define PARSER_SUBMODULE(name,init,exit) \ void init(void); void exit(void); #define PARSER_SUBMODMAG(name,init,exit) \ void init(void); void exit(void); #define PARSER_FUNCTION(name,func,def0,def1) \ void func(INT32 args); #include "initstuff.h" static struct { char *name; void (*init)(void); void (*exit)(void); struct program **dest;
6c4a1a1999-12-30Martin Stjernholm  int id;
7068e31999-06-12Mirar (Pontus Hagland) } initclass[]= { #undef PARSER_CLASS #undef PARSER_SUBMODULE #undef PARSER_FUNCTION #undef PARSER_SUBMODMAG #define PARSER_SUBMODMAG(a,b,c) #define PARSER_FUNCTION(a,b,c,d)
6c4a1a1999-12-30Martin Stjernholm #define PARSER_CLASS(name,init,exit,prog,id) { name,init,exit,&prog,id },
7068e31999-06-12Mirar (Pontus Hagland) #define PARSER_SUBMODULE(a,b,c) #include "initstuff.h" }; static struct { char *name; void (*init)(void); void (*exit)(void); } initsubmodule[]= { #undef PARSER_CLASS #undef PARSER_SUBMODULE
6c4a1a1999-12-30Martin Stjernholm #define PARSER_CLASS(name,init,exit,prog,id)
7068e31999-06-12Mirar (Pontus Hagland) #define PARSER_SUBMODULE(name,init,exit) { name,init,exit }, #include "initstuff.h"
07228a1999-06-19Fredrik Hübinette (Hubbe)  {0,0,0 }
7068e31999-06-12Mirar (Pontus Hagland) };
0533e21999-02-19Mirar (Pontus Hagland) 
7068e31999-06-12Mirar (Pontus Hagland) static struct
0533e21999-02-19Mirar (Pontus Hagland) { char *name;
7068e31999-06-12Mirar (Pontus Hagland)  void (*init)(void); void (*exit)(void); struct pike_string *ps; struct object *o; } submagic[]= { #undef PARSER_SUBMODULE #undef PARSER_SUBMODMAG #define PARSER_SUBMODULE(a,b,c) #define PARSER_SUBMODMAG(name,init,exit) { name,init,exit,NULL,NULL }, #include "initstuff.h"
07228a1999-06-19Fredrik Hübinette (Hubbe)  { 0,0,0,0,0 }
0533e21999-02-19Mirar (Pontus Hagland) };
7068e31999-06-12Mirar (Pontus Hagland) #ifdef PIKE_DEBUG #define PARSER_CHECK_STACK(X) do { if (save_sp != sp) { fatal("%s:%d: %d droppings on stack! previous init: %s\n", __FILE__, __LINE__, sp - save_sp,X); } } while(0) #else #define PARSER_CHECK_STACK(X) #endif /* PIKE_DEBUG */ static void parser_magic_index(INT32 args)
0533e21999-02-19Mirar (Pontus Hagland) {
7068e31999-06-12Mirar (Pontus Hagland)  struct svalue tmp; int i; if (args!=1) error("Parser.`[]: Too few or too many arguments\n"); if (sp[-1].type!=T_STRING) error("Parser.`[]: Illegal type of argument\n");
07228a1999-06-19Fredrik Hübinette (Hubbe)  for (i=0; i<(int)NELEM(submagic)-1; i++)
7068e31999-06-12Mirar (Pontus Hagland)  if (sp[-1].u.string==submagic[i].ps) { #ifdef PIKE_DEBUG struct svalue *save_sp; #endif pop_stack(); #ifdef PIKE_DEBUG save_sp = sp; #endif if (!submagic[i].o) { struct program *p; start_new_program(); (submagic[i].init)(); PARSER_CHECK_STACK(submagic[i].name); p=end_program(); submagic[i].o=clone_object(p,0); free_program(p); } ref_push_object(submagic[i].o); return; } stack_dup(); ref_push_object(fp->current_object); stack_swap(); f_arrow(2); if (sp[-1].type==T_INT) { pop_stack(); stack_dup(); push_text("_Parser_"); stack_swap(); f_add(2); push_int(0); SAFE_APPLY_MASTER("resolv",2); } if (sp[-1].type==T_INT) { pop_stack(); stack_dup(); push_text("_Parser"); push_int(0); SAFE_APPLY_MASTER("resolv",2); stack_swap();
52c5792000-05-23Fredrik Hübinette (Hubbe)  if(sp[-2].type == T_INT) { pop_stack(); }else{ f_index(2); }
7068e31999-06-12Mirar (Pontus Hagland)  } stack_swap(); pop_stack();
0533e21999-02-19Mirar (Pontus Hagland) } void pike_module_init(void) {
7068e31999-06-12Mirar (Pontus Hagland) #ifdef PIKE_DEBUG struct svalue *save_sp = sp; #endif
0533e21999-02-19Mirar (Pontus Hagland)  int i;
7068e31999-06-12Mirar (Pontus Hagland)  for (i=0; i<(int)NELEM(initclass); i++) { start_new_program();
bad5162000-06-23Fredrik Hübinette (Hubbe)  if (initclass[i].id) Pike_compiler->new_program->id = initclass[i].id;
7068e31999-06-12Mirar (Pontus Hagland)  #ifdef DEBUG fprintf(stderr,"Parser: initiating class \"Parser.%s\"...\n", initclass[i].name); #endif (initclass[i].init)(); PARSER_CHECK_STACK(initclass[i].name); initclass[i].dest[0]=end_program(); add_program_constant(initclass[i].name,initclass[i].dest[0],0); }
07228a1999-06-19Fredrik Hübinette (Hubbe)  for (i=0; i<(int)NELEM(initsubmodule)-1; i++)
0533e21999-02-19Mirar (Pontus Hagland)  { struct program *p;
7068e31999-06-12Mirar (Pontus Hagland)  struct pike_string *s;
0533e21999-02-19Mirar (Pontus Hagland) 
7068e31999-06-12Mirar (Pontus Hagland) #ifdef DEBUG fprintf(stderr,"Parser: initiating submodule \"Parser.%s\"...\n", initsubmodule[i].name); #endif
0533e21999-02-19Mirar (Pontus Hagland)  start_new_program();
7068e31999-06-12Mirar (Pontus Hagland)  (initsubmodule[i].init)(); PARSER_CHECK_STACK(initsubmodule[i].name);
0533e21999-02-19Mirar (Pontus Hagland)  p=end_program();
7068e31999-06-12Mirar (Pontus Hagland)  push_object(clone_object(p,0)); s=make_shared_string(initsubmodule[i].name); add_constant(s,sp-1,0); free_string(s);
0533e21999-02-19Mirar (Pontus Hagland)  free_program(p);
7068e31999-06-12Mirar (Pontus Hagland)  pop_stack(); }
07228a1999-06-19Fredrik Hübinette (Hubbe)  for (i=0; i<(int)NELEM(submagic)-1; i++)
7068e31999-06-12Mirar (Pontus Hagland)  submagic[i].ps=make_shared_string(submagic[i].name); #undef PARSER_FUNCTION #define PARSER_FUNCTION(name,func,def0,def1) ADD_FUNCTION(name,func,def0,def1); #include "initstuff.h" ADD_FUNCTION("`[]",parser_magic_index, tFunc(tString,tMixed),0); } void pike_module_exit(void) { int i; for (i=0; i<(int)NELEM(initclass); i++) { (initclass[i].exit)(); free_program(initclass[i].dest[0]);
0533e21999-02-19Mirar (Pontus Hagland)  }
07228a1999-06-19Fredrik Hübinette (Hubbe)  for (i=0; i<(int)NELEM(initsubmodule)-1; i++)
7068e31999-06-12Mirar (Pontus Hagland)  (initsubmodule[i].exit)();
07228a1999-06-19Fredrik Hübinette (Hubbe)  for (i=0; i<(int)NELEM(submagic)-1; i++)
7068e31999-06-12Mirar (Pontus Hagland)  if (submagic[i].o) { (submagic[i].exit)(); free_object(submagic[i].o); }
0533e21999-02-19Mirar (Pontus Hagland) }