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

42 lines
1.1 KiB
Racket

6 years ago
#lang br/quicklang
(require brag/support "grammar.rkt")
(provide (all-defined-out) #%module-begin)
(module+ reader
(provide read-syntax))
(define-lex-abbrev reserved-toks
(:or "fun" "(" ")" "=" "+" "*" "/" "-" ","))
6 years ago
(define tokenize
6 years ago
(lexer
[(:or (from/to "//" "\n") (from/to "/*" "*/")) (token 'COMMENT #:skip? #t)]
6 years ago
[whitespace (tokenize input-port)]
6 years ago
[reserved-toks lexeme]
[alphabetic (token 'ID (string->symbol lexeme))]
[(:+ (char-set "0123456789")) (token 'INT (string->number lexeme))]))
(define-macro top #'begin)
6 years ago
(define-macro (func-def VAR VARS EXPR)
#'(define (VAR . VARS) EXPR))
6 years ago
(define-macro-cases sum
[(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)]
[(_ LEFT "-" RIGHT) #'(- LEFT RIGHT)]
[(_ OTHER) #'OTHER])
(define-macro-cases product
[(_ LEFT OP-STR RIGHT)
(with-syntax ([OP (string->symbol (syntax->datum #'OP-STR))])
#'(OP LEFT RIGHT))]
[(_ OTHER) #'OTHER])
(define-macro func-app #'#%app)
(define (read-syntax src ip)
6 years ago
(define parse-tree (parse (λ () (tokenize ip))))
6 years ago
(strip-context
(with-syntax ([PT parse-tree])
#'(module mod-name precalc-demo
PT))))