Skip to content

Instantly share code, notes, and snippets.

@dannypsnl
Last active January 23, 2025 05:02
Show Gist options
  • Select an option

  • Save dannypsnl/801798713c3df2e5a7dbb7a6445337f7 to your computer and use it in GitHub Desktop.

Select an option

Save dannypsnl/801798713c3df2e5a7dbb7a6445337f7 to your computer and use it in GitHub Desktop.
math convention
#lang racket
(require (for-syntax syntax/parse))
(define-syntax (math stx)
(syntax-parse stx
#:datum-literals (+ *)
[(_ (o ...) op o* ...)
#'(let ([k (math o ...)])
(math k op o* ...))]
[(_ (o ...)) #'(math o ...)]
[(_ a * b + o* ...) #'(+ (* a b) (math o* ...))]
[(_ a op o* ...) #'(op a (math o* ...))]
[(_ a op b) #'(op a b)]
[(_ a:id) #'a]
[(_ a:number) #'a]))
(math 1 + 2)
(math 1)
(math 1 + 2 + 3)
(define x 3)
(math 9 + 2 * x + 3)
(math (9 + 2) * x + 3)
(math (9 + 2) * (x + 3))
@dannypsnl
Copy link
Author

I thought a more proper way is still writing a parser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment