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

48 lines
1.2 KiB
Racket

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