Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: ALLUPPERCASE #75

Open
stefan-kral opened this issue May 10, 2021 · 1 comment
Open

Feature request: ALLUPPERCASE #75

stefan-kral opened this issue May 10, 2021 · 1 comment

Comments

@stefan-kral
Copy link

I want to use cppo for improving my ISO/IEC 13211-1:1995 (a.k.a "ISO-Prolog") compliant Prolog processor.

Specifically, I want to reduce the use of string constants in code blocks like the following one:

type plOpSpecifier = (* pre-fix *) FX|FY | (* in-fix *) XFX|XFY|YFX | (* post-fix *) XF|YF;;

let plOpSpecifier_of_string_opt = function
  |  "fx" -> Some  FX
  |  "fy" -> Some  FY
  | "xfx" -> Some XFX
  | "xfy" -> Some XFY
  | "yfx" -> Some YFX
  | "xf"  -> Some XF
  | "yf"  -> Some YF
  | _____ -> None;;

let plAtom_of_plOpSpecifier = function
  |  FX -> `Atom "fx"
  |  FY -> `Atom "fy"
  | XFX -> `Atom "xfx"
  | XFY -> `Atom "xfy"
  | YFX -> `Atom "yfx"
  | XF  -> `Atom "xf"
  | YF  -> `Atom "yf";;

So far, I have come up with the following:

#define X(x)  STRINGIFY(x) -> Some(CAPITALIZE(x))
  let plOpSpecifier_of_string_opt = function
    | X(fx) | X(fy) | X(xfx) | X(xfy) | X(yfx) | X(xf) | X(yf)
    | _ -> None;;
#undef X

#define X(x)  CAPITALIZE(x) -> `Atom(STRINGIFY(x))
  let plAtom_of_plOpSpecifier = function
    | X(fx) | X(fy) | X(xfx) | X(xfy) | X(yfx) | X(xf) | X(yf);;
#undef X

Alas, CAPITALIZE only touches the first character, so I get the following not-yet-perfect output:

$ cat sample.txt | cppo | ocamlformat --enable-outside-detected-project - --impl
let plOpSpecifier_of_string_opt = function
  | "fx" -> Some Fx
  | "fy" -> Some Fy
  | "xfx" -> Some Xfx
  | "xfy" -> Some Xfy
  | "yfx" -> Some Yfx
  | "xf" -> Some Xf
  | "yf" -> Some Yf
  | _ -> None

let plAtom_of_plOpSpecifier = function
  | Fx -> `Atom "fx"
  | Fy -> `Atom "fy"
  | Xfx -> `Atom "xfx"
  | Xfy -> `Atom "xfy"
  | Yfx -> `Atom "yfx"
  | Xf -> `Atom "xf"
  | Yf -> `Atom "yf"

I need all characters in uppercase. Having a ALLUPPERCASE builtin function in cppo would do it.

I wanted to start the discussion on this before filing a PR:) Comments?

@pmetzger
Copy link
Member

Although such tricks certainly make it harder to make mistakes when writing code, I would suggest that a PPX is probably a better mechanism here than hacking in cppo. In particular, code refactoring tools and formaters are broken by preprocessors like cppo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants