orangeduck/mpc

no user defined callbacks?

Closed this issue · 7 comments

how would i make it, so when for example in math.c, the expression if valid, it does a user defined callback to say a function that calculates then prints the result, for example

maths <<< "1+2"
calls function USER_ADD with arguments 1, 2
outputs 3

or something like that, idk

You can use mpc_apply or provide a custom fold function to perform what is essentially a callback on the currently parsed object.

how would one parse input like the AST/mpca_lang does, for example, parse the same input in multiple parsers but in a way that consumes characters

for example:

  mpc_result_t r;
  mpc_parser_t * EOL = mpc_or(3, mpc_char(';'), mpc_newline(), mpc_eoi);
  mpc_parser_t * Line = mpc_many(mpcf_strfold, mpc_or(2, EOL, mpc_any()));
  
  if (mpc_parse("test", "aaer;fg\n54hsers;a=$[h.5'4e[u.9i']]", Line, &r)) puts(r.output);
  mpc_cleanup(1,Line,NULL);

like do i just consume input using input += strlen(r.output); or is there a special function for consuming input

I'm not sure I understand the question but I'm not sure there is a way to do what you want outside of just combining many parses together and using folds and apply functions to convert the data structure you want as it is being built. This is actually how mpca_lang works too - it builds one large mpc_parser_t for each of the expressions you give it - it doesn't call mpc_parse internally.