You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
beautiful-racket/beautiful-racket-demo/precalc-demo/main.rkt

44 lines
1.2 KiB
Racket

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

#lang br/quicklang
(require brag/support "grammar.rkt")
(provide top fun app s-or-d p-or-q)
(module+ reader
(provide read-syntax))
(define-lex-abbrev reserved-toks
(:or "fun" "(" ")" "=" "+" "*" "/" "-" ","))
(define-lex-abbrev digits (char-set "0123456789"))
(define tokenize-1
(lexer-srcloc
[whitespace (token lexeme #:skip? #t)]
[(:or (from/stop-before "#" "\n")
(from/to "/*" "*/")) (token 'COMMENT #:skip? #t)]
[reserved-toks lexeme]
[(:seq alphabetic (:* (:or alphabetic) digits)) (token 'ID (string->symbol lexeme))]
[(:seq (:? "-") (:+ digits)) (token 'INT (string->number lexeme))]))
(define-macro top #'#%module-begin)
(define-macro (fun VAR (ARGVAR ...) EXPR)
#'(define (VAR ARGVAR ...) EXPR))
(define-macro-cases s-or-d
[(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)]
[(_ LEFT "-" RIGHT) #'(- LEFT RIGHT)]
[(_ OTHER) #'OTHER])
(define-macro-cases p-or-q
[(_ LEFT "*" RIGHT) #'(* LEFT RIGHT)]
[(_ LEFT "/" RIGHT) #'(/ LEFT RIGHT)]
[(_ OTHER) #'OTHER])
(define-macro app #'#%app)
(define (read-syntax src ip)
(define parse-tree (parse src (λ () (tokenize-1 ip))))
(strip-context
(with-syntax ([PT parse-tree])
#'(module mod-name precalc-demo
PT))))