Still experimenting with MPlib

Nicola <[email protected]>
Newsgroups gmane.comp.tex.metapost
Message-ID <[email protected]>
Hi,
I'm still experimenting with MPlib, and there is something that I do not 
understand. I hope you can help me. I have tried the following program:

void imp_init_mp_instance()
{
   MP_options *opt = mp_options();
   opt->ini_version = 0;
   opt->mem_name = "mpost";
   opt->command_line = NULL;
   opt->noninteractive = 1;
   imp = mp_initialize(opt);
   if (!imp) { exit(EXIT_FAILURE); }
   mp_execute(imp, "\\", 1); // Flush MetaPost introductory text
}


int main(void)
{
   imp_init_mp_instance();

   char *line[] = {
     "if 1=0:",
     "message \"tsk\";",
     "fi;"
   };

   for (int i = 0; i < 3; i++) {
     mp_execute(imp, line[i], strlen(line[i]));
     mp_run_data *cmd_result = mp_rundata(imp);
     printf("%s", cmd_result->term_out.data);
     printf("Status: %d\n", mp_status(imp));
   }
}

This results in the MP instance being in an error state, and the error 
message is the following:

! Missing `:' has been inserted.
<inserted text>
                 :
<to be read again>
                    fi
<*> fi
       ;
! A statement can't begin with `:'.
<to be read again>
                    :
<to be read again>
                    fi
<*> fi
       ;
! Extra tokens will be flushed.
<to be read again>
                    :
<to be read again>
                    fi
<*> fi
       ;

Of course, this doesn't work as in mpost's prompt, as I had naïvely 
expected. So, should mp_execute() be fed with complete statements only?

It is not clear to me what happens internally when mp_execute() is 
passed an incomplete statement. For example, if I call:

mp_execute(imp, "x=1", 3); // Note the missing semi-colon
mp_execute(imp, "show x;", 7);

this acts as if x=1 was ignored (`show x` prints `>> x`). But the 
example above seems to indicate that this is not the case (if incomplete 
statements were ignored, I'd get an `! Extra fi.` error after the third 
invocation).



Nicola

--
http://tug.org/metapost/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.