Last active
January 23, 2025 05:02
-
-
Save dannypsnl/801798713c3df2e5a7dbb7a6445337f7 to your computer and use it in GitHub Desktop.
math convention
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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)) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I thought a more proper way is still writing a parser