0

For various toy projects I'd like to be able to embed object languages into the PolyML top level, like the backtick syntax for HOL, where expressions between backticks are parsed by a custom parser.

I don't mind the specific delimiting syntax: backticks `...`, guillemets <<...>>, or something like {|...|}. I just want to be able to write expressions at the top-level and have them parsed by a custom parser.

For example if I had a datatype like

datatype expression =
    Add of expression * expression
  | Int of int
  | Mul of expression * expression

I'd like to be able to type the following:

> `3 + 2 * 5`;
val it = Add (Int 3, Mul (Int 2, Int 5)): expression

Is this possible (in a simple way)?

fds
  • 1
  • 1

1 Answers1

0

For the case you have, you could approximate this with something like this

val op + = Add
val op * = Mul
val ` = Int
val it = `3 + `2 * `5

However, this isn't going to use a custom parser or anything, and will just rely on the existing parser.

If you wanted to use a custom parser the most straightforward way would simply be to write a function parse : string -> expression and apply it manually on the top level.

kopecs
  • 1,545
  • 10
  • 20