help-octave
[Top][All Lists]
Advanced

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

Walking AST


From: Muthiah Annamalai
Subject: Walking AST
Date: Sun, 30 Dec 2007 18:03:01 -0600

Hello,

I have a problem trying to grab hold of a parse-tree parsed structure.

My code works for m-files that start with a major statement or so,
but a bunch of statements donot get printed or AST walked. Where
am I making a mistake?

This is the code for oct2pprint() which runs the          tree_print_code
tree walker on any parsed file given as input to user.

This function works on files like

**** hello.m ***
if ( sigma(0) + !alpha(0)  )
if ( beta(0) || rand(2) ) #must change #-comment to %, and || to |
  disp("hello world"); %must change the " quote to '
endif %change to if

if ( 0 && 1 )
  % all that goo must be changed on rhs of the fprintf statement
  fprintf('hello world %d', SEEK_CUR, __error_text__, SEEK_END, SEEK_SET);
end

x =  0 && 1 %must be 0 & 1
z = (and || they)  && 'and forever lived'
a = ! died %must be ~
end
**********************
But files like the following bunch of statements dont get walked,
except the first
statement. What am I doing wrong here?

****************** hello2.m *********

1 && 2
if ( 1 & 2 | 3 )
   z = (and || they)  && 'and forever lived'
   a = ! died %must be ~

end
*****************************************

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_matlab_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();
}

*****************************************

Please help,
Muthu


reply via email to

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