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/scriptish-demo/main.rkt

43 lines
1.4 KiB
Racket

6 years ago
#lang br/quicklang
5 years ago
(require "grammar.rkt" brag/support)
6 years ago
(module+ reader
(provide read-syntax))
5 years ago
(define-lex-abbrev reserved-toks
5 years ago
(:or "var" "=" ";" "{" "}" "//" "/*" "*/"
"+" "*" "/" "-"
"'" "\""
":" "," "(" ")"
"if" "else" "while" "?"
"==" "!=" "<=" "<" ">=" ">" "&&" "||"
"function"
"return" "++" "--" "+=" "-="))
6 years ago
5 years ago
(define-lex-abbrev digits (:+ (char-set "0123456789")))
5 years ago
(define tokenize-1
(lexer-srcloc
6 years ago
[(:or (from/stop-before "//" "\n")
(from/to "/*" "*/")) (token 'COMMENT #:skip? #t)]
5 years ago
[reserved-toks lexeme]
5 years ago
[(:seq (:? "-") (:or (:seq (:? digits) "." digits)
(:seq digits (:? "."))))
(token 'NUMBER (string->number lexeme))]
5 years ago
[(:seq (:+ (:- (:or alphabetic punctuation digits) reserved-toks)))
(if (string-contains? lexeme ".")
(token 'DEREF (map string->symbol (string-split lexeme ".")))
(token 'ID (string->symbol lexeme)))]
6 years ago
[(:or (from/to "\"" "\"") (from/to "'" "'"))
5 years ago
(token 'STRING (string-trim lexeme (substring lexeme 0 1)))]
6 years ago
[whitespace (token 'WHITE #:skip? #t)]
[any-char lexeme]))
(define (read-syntax src ip)
5 years ago
(port-count-lines! ip)
(lexer-file-path ip)
(define parse-tree (parse src (λ () (tokenize-1 ip))))
(strip-bindings
6 years ago
(with-syntax ([PT parse-tree])
#'(module scriptish-mod scriptish-demo/expander
6 years ago
PT))))