help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

howto make a visitor on parse tree?


From: Muthiah Annamalai
Subject: howto make a visitor on parse tree?
Date: Tue, 06 Nov 2007 09:27:57 -0600

Hello there,

I have a question, regarding usage of a parse tree, on a visitor class.
The visitor is a subclass of tree-walker.

Specifically, my code to instantiate the the tree walker fails, after
correct parsing. I cannot wrap my head around this.

<CODE>

DEFUN(oct2pprint,args,,
       "converts the given file to pretty-printing the  script.\n
Useage: oct2pprint(filename)")
{
  /* (copy from parse.y, parse_and_execute_file() function )
    1. Invoke the parser.
    2. Check status, and report errors. Reset parser.
    3. Give AST (global_command) to the tree_print_code.
    4. Cleanup.
  */
  
  if ( args.length() < 1 ) {
    print_usage();
    error("usage: oct2pprint(filename);");
    return octave_value();
  }

  std::string fname = args(0).string_value();
  
  unwind_protect::begin_frame ("oct2pprint");

  unwind_protect_bool (reading_script_file);
  unwind_protect_str (curr_fcn_file_full_name);

  reading_script_file = true;
  curr_fcn_file_full_name = fname;

  FILE *f = get_input_from_file (fname, 0);
  
  if ( !f )
    error("cannot open file", fname.c_str());
  

  unwind_protect::begin_frame ("oct2pprint_parse");

  unwind_protect_ptr (global_command);

  YY_BUFFER_STATE old_buf = current_buffer ();
  YY_BUFFER_STATE new_buf = create_buffer (f);

  unwind_protect::add (restore_input_buffer, old_buf);
  unwind_protect::add (delete_input_buffer, new_buf);

  switch_to_buffer (new_buf);

  unwind_protect_bool (line_editing);
  unwind_protect_bool (get_input_from_eval_string);
  unwind_protect_bool (parser_end_of_input);

  line_editing = false;
  get_input_from_eval_string = false;
  parser_end_of_input = false;

  unwind_protect_ptr (curr_sym_tab);

  int retval;
  do
    {
      reset_parser ();

      retval = octave_parse ();

      if (retval == 0)
        {
  tree_print_code pcode(octave_stdout);

  //handle the user script
  if ( global_command )
    {
      global_command->accept(pcode);
      octave_stdout<<"handling user script \n";
    }
  else
    octave_stdout<<"parsed something strange... \n";
}
    }
  while ( 0 );

  unwind_protect::run_frame ("oct2pprint_parse");

  unwind_protect::run_frame ("oct2pprint");

  //successfully executed the functions.
  return octave_value();
}

</CODE>

The code can only instantiate the walker for m-files in the form of one
single statement. It cannot walk a m-file with several statements, or 
even a single function, irrespective of them being parsed correctly.

What am I missing here? Please help.

Thanks,
-Muthu




reply via email to

[Prev in Thread] Current Thread [Next in Thread]