├── .gitignore ├── examples ├── syndicate │ ├── README.md │ ├── bank-account.rkt │ ├── broadcast-messages.rkt │ ├── syndicate.rkt │ └── file-system-during.rkt ├── x.rkt ├── where.rkt ├── sh.rkt ├── t.rkt ├── json-echo.rkt ├── honuish.rkt ├── xexpr.rkt ├── very-complex-macro.rkt ├── if-then-else.rkt └── shape.rkt ├── src ├── info.rkt └── something │ ├── main.rkt │ ├── test │ ├── s2.rkt │ ├── sweet.rkt │ └── simple-tests.rkt │ ├── infix.rkt │ ├── for.rkt │ ├── TODO.md │ ├── lang │ └── reader.rkt │ ├── shell.rkt │ ├── shell2.rkt │ ├── pratt.rkt │ ├── reader.rkt │ └── base.rkt ├── Makefile ├── COPYING ├── sth8.el ├── README.md ├── lgpl.txt └── gpl.txt /.gitignore: -------------------------------------------------------------------------------- 1 | compiled/ 2 | -------------------------------------------------------------------------------- /examples/syndicate/README.md: -------------------------------------------------------------------------------- 1 | Examples using `something` macros for 2 | [`syndicate`](https://github.com/tonyg/syndicate) programs. 3 | -------------------------------------------------------------------------------- /src/info.rkt: -------------------------------------------------------------------------------- 1 | #lang setup/infotab 2 | (define collection 'multi) 3 | (define deps '("base" "tabular")) 4 | (define build-deps '("rackunit-lib")) 5 | -------------------------------------------------------------------------------- /examples/x.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | 3 | def-operator + 60 left + 4 | 123 + 234 5 | 6 | def call f: (f) 7 | 8 | call :: 9 | printf "hi\n" 10 | printf "there\n" 11 | "result" 12 | 13 | call { : printf "eek1\n" } 14 | call . { : printf "eek2\n" } 15 | -------------------------------------------------------------------------------- /src/something/main.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require (prefix-in base: racket/base)) 4 | (base:require "base.rkt" "infix.rkt" (only-in "lang/reader.rkt" read-toplevel-syntax)) 5 | (base:provide (base:all-from-out "base.rkt" "infix.rkt")) 6 | 7 | (current-read-interaction read-toplevel-syntax) 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PACKAGENAME=something 2 | COLLECTS=something 3 | 4 | all: setup 5 | 6 | clean: 7 | find . -name compiled -type d | xargs rm -rf 8 | 9 | setup: 10 | raco setup $(COLLECTS) 11 | 12 | link: 13 | raco pkg install --link -n $(PACKAGENAME) $$(pwd)/src 14 | 15 | unlink: 16 | raco pkg remove $(PACKAGENAME) 17 | 18 | test: setup testonly 19 | 20 | testonly: 21 | raco test -p $(PACKAGENAME) 22 | -------------------------------------------------------------------------------- /src/something/test/s2.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | 3 | provide 4 | rename-out (s2minus -) 5 | 6 | require 7 | something/infix 8 | 9 | def-operator + 10 | 11 | def plus: + 12 | 13 | def-operator + 60 left 14 | a b: 15 | log-info "plus-op ~v ~v" a b 16 | plus a b 17 | 18 | def-operator s2minus 60 left s2minus 19 | 20 | def s2minus a b 21 | log-info "s2minus ~v ~v" a b 22 | a - b 23 | -------------------------------------------------------------------------------- /examples/where.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | 3 | provide 4 | where 5 | 6 | require 7 | something/infix 8 | for-syntax something/base 9 | 10 | def-operator where #f statement-macro where 11 | def-syntax where stx 12 | syntax-case stx (block) 13 | _ (head ...) ((_where (block (defhead ... (block defbody ...)) ...))) 14 | syntax (let (block (def defhead ... (block defbody ...)) ... head ...)) 15 | 16 | module+ main 17 | displayln (x + y x) 18 | where 19 | x: 123 20 | y z: z * 2 21 | -------------------------------------------------------------------------------- /examples/sh.rkt: -------------------------------------------------------------------------------- 1 | #lang something/shell 2 | // Simple demos 3 | 4 | ls -la $HOME | grep "^d" | fgrep -v "." 5 | 6 | ls -la | wc -l | read-line |> string-split |> car |> string->number |> \ 7 | printf "There are ~a lines here." | sed -e "s: are : seem to be :" 8 | (newline) 9 | 10 | def ps-output 11 | pipeline 12 | ps -wwwax 13 | preserve-header 1 {: grep "racket" } 14 | space-separated-columns [string->number] 15 | |> csv-expr->table 16 | print ps-output 17 | 18 | def message-box text: 19 | whiptail --title "Testing" --ok-button "OK" --msgbox text 8 50 20 | 21 | message-box "This is pretty cool." 22 | -------------------------------------------------------------------------------- /examples/t.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | 3 | require 4 | something/infix 5 | 6 | log-info "Hello, world" 7 | log-info "This is cool ~v" \ 8 | "isn't it" 9 | 10 | def f x 11 | x + 1 12 | 13 | def g 14 | { x: x + 1 } 15 | 16 | [f 123; f 124; g 125] 17 | 18 | map* [f 123; f 124; g 125] f 19 | 20 | map* [f 123; f 124; g 125] 21 | 125: `one-two-five-times-two 22 | x: x * 2 23 | 24 | map* [f 123; f 124; g 125]: x: x * 2 25 | 26 | map* [f 123; f 124; g 125] { x: x * 2 } 27 | 28 | printf "Type a Racket term: " 29 | (flush-output) 30 | match (read) 31 | [x; y]: x + y 32 | `hi: "Hello" 33 | `bye: "Goodbye" 34 | x: format "Something else: ~a" x 35 | -------------------------------------------------------------------------------- /examples/syndicate/bank-account.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | // Requires the "syndicate" package to be installed. 3 | 4 | require 5 | something/infix 6 | "syndicate.rkt" 7 | 8 | struct account (balance) :prefab 9 | struct deposit (amount) :prefab 10 | 11 | run-ground 12 | spawn 13 | field balance: 0 14 | assert (account !balance) 15 | on message (deposit $amount) 16 | balance <- !balance + amount 17 | 18 | spawn 19 | on asserted (account $balance) 20 | printf "Balance changed to ~a\n" balance 21 | 22 | spawn* 23 | until asserted (observe (deposit _)) 24 | !! deposit +100 25 | !! deposit -30 26 | -------------------------------------------------------------------------------- /src/something/infix.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require something/base) 4 | 5 | (def-operator \|\| 20 right or) 6 | (def-operator && 30 right and) 7 | (def-operator == 40 nonassoc equal?) 8 | (def-operator = 40 nonassoc =) 9 | (def-operator < 40 nonassoc <) 10 | (def-operator <= 40 nonassoc <=) 11 | (def-operator > 40 nonassoc >) 12 | (def-operator >= 40 nonassoc >=) 13 | (def-operator + 60 left +) 14 | (def-operator - 60 left -) 15 | (def-operator * 70 left *) 16 | (def-operator / 70 left /) 17 | (def-operator ∘ 80 right compose) 18 | ;; (def-operator - 500 prefix -) 19 | ;; (def-operator + 500 prefix +) 20 | 21 | (def-operator |`| 1100 prefix quasiquote) 22 | (def-operator |,| 1100 prefix unquote) 23 | (def-operator |,@| 1100 prefix unquote-splicing) 24 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | racket-something: Indentation-based Racket Syntax. 2 | Copyright (C) 2016 Tony Garnock-Jones 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as 6 | published by the Free Software Foundation, either version 3 of the 7 | License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, but 10 | WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this program (see the files "lgpl.txt" and 16 | "gpl.txt"). If not, see . 17 | -------------------------------------------------------------------------------- /examples/json-echo.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | 3 | require 4 | json 5 | racket/tcp 6 | 7 | def socket->string p: 8 | def-values local-host local-port remote-host remote-port: tcp-addresses p #t 9 | format "~a:~a-~a:~a" local-host local-port remote-host remote-port 10 | 11 | def session i o: 12 | let loop 13 | match read-json i 14 | == eof: (void) 15 | blob: 16 | log-info "Received JSON object: ~v" blob 17 | write-json blob o; newline o; flush-output o 18 | (loop) 19 | 20 | module+ main 21 | def port-number: 45678 22 | def s: tcp-listen port-number 500 #t 23 | log-info "Listening on port ~a" port-number 24 | let loop 25 | def-values i o: tcp-accept s 26 | log-info "Accepted session ~a" (socket->string i) 27 | session i o 28 | log-info "Terminated session ~a" (socket->string i) 29 | close-input-port i 30 | close-output-port o 31 | (loop) 32 | -------------------------------------------------------------------------------- /examples/honuish.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | 3 | require 4 | something/for 5 | 6 | /////////////////////////////////////////////////////////////////////////// 7 | // http://docs.racket-lang.org/honu/Examples.html 8 | 9 | // A for loop that iterates between two bounds. 10 | for { x: 1 + 5 .. 10 } 11 | printf "x is ~a\n" x 12 | 13 | // Similar to above but shows a block of expressions in the body 14 | for { x: 1 .. 10 } 15 | def y: x + 1 16 | printf "x ~a y ~a\n" x y 17 | 18 | // A for loop that iterates over a list of numbers 19 | for { x: [1; 2; 3] } 20 | printf "x ~a\n" x 21 | 22 | /////////////////////////////////////////////////////////////////////////// 23 | // Some more examples 24 | 25 | (newline) 26 | for { x: .. 3; y: ["hello"; "there"; "racketeer"] } 27 | printf "~a ~a\n" x y 28 | 29 | (newline) 30 | for* { x: .. 3; y: ["hello"; "there"; "racketeer"] } 31 | printf "~a ~a\n" x y 32 | 33 | for/hash { x : .. 3 } 34 | values x (x * 2) 35 | -------------------------------------------------------------------------------- /src/something/for.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | 3 | provide 4 | for 5 | for* 6 | for/list 7 | for*/list 8 | for/hash 9 | for*/hash 10 | 11 | require 12 | something/infix 13 | for-syntax something/base 14 | prefix-in base_ racket/base 15 | 16 | begin-for-syntax 17 | def expand-for base-stx stx 18 | syntax-case stx (block) 19 | _ (block (v (block exp)) ...) (block body ...) 20 | (quasisyntax ((unsyntax base-stx) ((v exp) ...) ('#%rewrite-body' body ...))) 21 | 22 | def-syntax for stx: expand-for (syntax base_for) stx 23 | def-syntax for* stx: expand-for (syntax base_for*) stx 24 | def-syntax for/list stx: expand-for (syntax base_for/list) stx 25 | def-syntax for*/list stx: expand-for (syntax base_for*/list) stx 26 | def-syntax for/hash stx: expand-for (syntax base_for/hash) stx 27 | def-syntax for*/hash stx: expand-for (syntax base_for*/hash) stx 28 | 29 | def-operator .. 10 nonassoc in-range 30 | def-operator .. 10 prefix in-range 31 | -------------------------------------------------------------------------------- /examples/syndicate/broadcast-messages.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | // Demonstrate sending a message to multiple receivers. 3 | // Requires the "syndicate" package to be installed. 4 | 5 | require 6 | something/infix 7 | "syndicate.rkt" 8 | 9 | struct envelope (destination message) :prefab 10 | 11 | run-ground 12 | spawn: on message (envelope `alice $message) 13 | log-info "Alice received ~v" message 14 | 15 | spawn: on message (envelope `bob $message) 16 | log-info "Bob received ~v" message 17 | 18 | spawn* 19 | log-info "Waiting for Alice and Bob." 20 | until asserted (observe (envelope `alice _)) 21 | until asserted (observe (envelope `bob _)) 22 | 23 | log-info "Sending a few messages..." 24 | !! envelope `alice "For Alice's eyes only" 25 | !! envelope `bob "Dear Bob, how are you? Kind regards, etc." 26 | !! envelope ? "Important announcement!" 27 | 28 | log-info "Sent all the messages." 29 | -------------------------------------------------------------------------------- /examples/xexpr.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | 3 | require 4 | racket/pretty 5 | something/infix 6 | for-syntax something/base 7 | except-in xml document 8 | 9 | def-syntax single-xexpr stx 10 | syntax-case stx (= block) 11 | _ str 12 | string? (syntax-e . (syntax str)) 13 | syntax str 14 | _ (= expr) 15 | syntax expr 16 | _ (tag (attr attr-expr) ... (block xexpr ...)) 17 | syntax (list (quote tag) (list (list (quote attr) attr-expr) ...) (single-xexpr xexpr) ...) 18 | _ (tag (attr attr-expr) ...) 19 | syntax (list (quote tag) (list (list (quote attr) attr-expr) ...)) 20 | 21 | def-syntax xexpr stx 22 | syntax-case stx (block) 23 | _ (block xexpr) 24 | syntax (single-xexpr xexpr) 25 | 26 | def-operator ++ 50 left string-append 27 | def-operator = 10 prefix = 28 | 29 | def document 30 | xexpr 31 | html 32 | head 33 | meta (http-equiv "Content-Type") (content "text/html; charset=utf-8") 34 | title: "Test page" 35 | body 36 | h1: "Hello" 37 | p: "Hello, world" 38 | h2: "Testing" 39 | p 40 | = "Hello, " ++ number->string (3 + 4) 41 | "! This rules." 42 | p 43 | "Another way of putting it would be to say that 3 + 4 = " 44 | = (number->string (3 + 4)) 45 | "." 46 | 47 | pretty-print document 48 | printf "\n~a\n" (xexpr->string document) 49 | -------------------------------------------------------------------------------- /src/something/TODO.md: -------------------------------------------------------------------------------- 1 | # Lexical 2 | 3 | - byte vectors 4 | - arbitrarily-spelled symbols 5 | - arbitrarily-spelled keywords 6 | - bring symbol spelling more into line with Racket orthodoxy 7 | - character syntax 8 | - other #-prefixed values 9 | - quote 10 | - syntax 11 | - quasiquote, unquote, unquote-splicing 12 | - quasisyntax, unsyntax, unsyntax-splicing 13 | 14 | # Experiments 15 | 16 | Current: 17 | 18 | symbols 'any#thing' 19 | #%any#thing 20 | keywords :'any#thing' 21 | quote not supported 22 | quasiquote not supported 23 | bytestrings not supported 24 | characters not supported 25 | 26 | Smalltalk-ish: 27 | 28 | symbols #'any#thing' 29 | #%any#thing 30 | keywords :'any#thing' 31 | quote ' 32 | quasiquote ` 33 | bytestrings #"bytes" 34 | characters #\c #\space 35 | syntax #' 36 | quasisyntax #` 37 | unquote , 38 | unquote-splicing ,@ 39 | unsyntax #, 40 | unsyntax-splicing #,@ 41 | 42 | All of `quote`, `quasiquote`, `syntax` and `quasisyntax` can 43 | automatically adapt to being used in block style, since inline or 44 | block style is unambiguous for a single value, and they all expect 45 | just one argument. For example, `quote` will always appear as one of 46 | 47 | (quote atom) 48 | (quote (block atom)) 49 | (quote (compound ...)) 50 | (quote (block (compound ...))) 51 | 52 | Hmm. Lexical abbreviations for syntax and quasisyntax might not be 53 | required? 54 | -------------------------------------------------------------------------------- /examples/very-complex-macro.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | 3 | require 4 | for-syntax something/base 5 | something/infix 6 | rename-in (only-in racket/match match) (match base_match) 7 | 8 | def-operator my-match #f prefix-macro my-match 9 | def-syntax my-match stx (parse #f) 10 | syntax-case stx (block) 11 | _ subject ... (block (match-pat ... (block body ...)) ...) 12 | let 13 | log-info "my-match ~v" (syntax->datum . stx) 14 | with-syntax { parsed-subject: parse (syntax (subject ...)) 15 | (parsed-match-pat ...): 16 | map parse (syntax->list . syntax ((match-pat ...) ...)) } 17 | log-info "parsed-subject ~v" (syntax->datum . syntax parsed-subject) 18 | log-info "parsed-match-pats ~v" (syntax->datum . syntax (parsed-match-pat ...)) 19 | syntax (base_match parsed-subject (parsed-match-pat ('#%rewrite-body' body ...)) ...) 20 | 21 | def-operator slurp1 #f prefix-macro slurp1 22 | def-syntax slurp1 stx parse 23 | log-info "slurp1 invoked with stx ~v" (syntax->datum stx) 24 | syntax-case stx () 25 | _ it rest ... 26 | let 27 | def the-rest: syntax (99 98 97 rest ...) 28 | log-info "slurp1 returning the-rest ~v" (syntax->datum the-rest) 29 | values (syntax (list (quote slurped) (quote it))) the-rest 30 | 31 | (dump-operator-table!) 32 | 33 | list 1 slurp1 x 65 64 34 | list 1 slurp1 (x) 65 64 35 | list 1 slurp1 (x y z) 65 64 36 | 37 | { v:: w: ["outer"; v; w] } . (my-match car . list 597 598 599 { zot: ["foo"; zot]; baz: ["xyzzy"; baz] }) . list 3 4 38 | 39 | { v: ["match-result"; v] } my-match car . list 97 98 99 { x: x } 40 | (list): "an empty list" 41 | (list v ...): ["nonempty list"; v] 42 | ? odd? x: 43 | log-info "got odd! ~v" x 44 | ["odd"; x] 45 | 97: "ninety-seven" 46 | 98: "ninety-eight" 47 | _: "idk" 48 | -------------------------------------------------------------------------------- /examples/if-then-else.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | 3 | provide 4 | IF 5 | 6 | require 7 | something/infix 8 | prefix-in base_ racket/base 9 | for-syntax something/base 10 | 11 | def-operator IF #f statement-macro IF-statement 12 | def-syntax IF-statement stx 13 | syntax-case stx (ELSE block) 14 | _ (head ...) ((_IF test (block true-exps ...)) (ELSE (block false-exps ...)) more ...) 15 | syntax ('#%rewrite-body*' (head ... (base_cond (test ('#%rewrite-body' true-exps ...)) 16 | (else ('#%rewrite-body' false-exps ...)))) 17 | (more ...)) 18 | _ (head ...) ((_IF test (block true-exps ...)) more ...) 19 | syntax ('#%rewrite-body*' (head ... (base_when test ('#%rewrite-body' true-exps ...))) 20 | (more ...)) 21 | _ (head ...) ((_IF pieces ...) more ...) 22 | syntax ('#%rewrite-body*' (head ...) ((begin (IF pieces ...)) more ...)) 23 | 24 | def-syntax IF stx 25 | syntax-case stx (ELSE block) 26 | _ test (block true-exps ...) ELSE (block false-exps ...) 27 | syntax (base_if test ('#%rewrite-body' true-exps ...) ('#%rewrite-body' false-exps ...)) 28 | _ test (block true-exps ...) 29 | syntax (base_when test ('#%rewrite-body' true-exps ...)) 30 | 31 | module+ main 32 | 33 | printf "What's the magic word? " 34 | (flush-output) 35 | 36 | def word: (read-line) 37 | 38 | IF (word == "please") 39 | printf "You said the magic word!\n" 40 | printf "How polite.\n" 41 | ELSE 42 | printf "You didn't say the magic word.\n" 43 | 44 | def is_magic: word == "magic" 45 | 46 | IF is_magic 47 | printf "But you did say 'magic'!\n" 48 | 49 | IF is_magic { printf "✓\n" } ELSE { printf "✗\n" } 50 | displayln (IF (word == "1") { "✓" } ELSE { "✗" }) 51 | displayln (IF (word == "1") { "✓" }) 52 | -------------------------------------------------------------------------------- /src/something/lang/reader.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (provide (rename-out [read-something-syntax read-syntax]) 4 | read-toplevel-syntax) 5 | 6 | (require (only-in parser-tools/lex position-line position-col position-offset)) 7 | (require syntax/strip-context) 8 | (require racket/match) 9 | 10 | (require "../reader.rkt") 11 | 12 | (define (form->syntax src t) 13 | (define (->syntax v pos) 14 | (datum->syntax #f v (vector src 15 | (position-line pos) 16 | (position-col pos) 17 | (position-offset pos) 18 | #f))) 19 | (define (walk-form kids pos) 20 | (->syntax (map walk kids) pos)) 21 | (define (walk t) 22 | (match t 23 | [(list kid) 24 | (walk kid)] 25 | [(list kids ...) 26 | (walk-form kids (if (pair? kids) (token-pos (car kids)) #f))] 27 | [(token pos 'form kids) 28 | (walk-form kids pos)] 29 | [(token pos 'block kids) 30 | (->syntax (cons #'block (map walk kids)) pos)] 31 | [(token pos 'sequence kids) 32 | (->syntax (cons #'#%seq (map walk kids)) pos)] 33 | [(token pos _ (namespaced-name #f id)) 34 | (->syntax id pos)] 35 | [(token pos _ (namespaced-name ns id)) 36 | (->syntax (list #'in-module (->syntax ns pos) (->syntax id pos)) pos)] 37 | [(token pos 'keyword val) 38 | (->syntax (string->keyword (symbol->string val)) pos)] 39 | [(token pos (or 'number 'string 'literal) val) 40 | (->syntax val pos)])) 41 | (walk t)) 42 | 43 | (define (read-something-syntax src [p (current-input-port)] #:language [language #'something/base]) 44 | (define forms (read-something-forms p)) 45 | (strip-context 46 | #`(module something-module #,language 47 | (#%rewrite-body #,@(map (lambda (f) (form->syntax src f)) forms))))) 48 | 49 | (define (read-toplevel-syntax src [p (current-input-port)]) 50 | (define forms (read-something-toplevel p)) 51 | (if (null? forms) 52 | eof 53 | (strip-context 54 | #`(#%rewrite-body #,@(map (lambda (f) (form->syntax src f)) forms))))) 55 | -------------------------------------------------------------------------------- /examples/shape.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | 3 | require 4 | something/infix 5 | for-syntax something/base 6 | only-in racket/math pi 7 | racket/class 8 | racket/draw 9 | racket/gui/base 10 | 11 | def-operator -> 900 left { receiver message: message receiver } 12 | 13 | def-syntax def-protocol stx 14 | syntax-case stx (block) 15 | _ protocol-name (block (method arg ...) ...) 16 | syntax (begin (def method arg ... : { receiver : send receiver method arg ... }) ...) 17 | 18 | /////////////////////////////////////////////////////////////////////////// 19 | 20 | def-operator ->> #f prefix-macro ->> 21 | def-syntax ->> stx parse 22 | syntax-case stx (block) 23 | _ p ... (block body ...) 24 | syntax (let: 25 | def v: '#%rewrite-infix' p ... 26 | (begin (('#%rewrite-infix' body) v) ...) 27 | v) 28 | 29 | def-protocol window-management 30 | show show? 31 | 32 | def-protocol drawing 33 | draw-path path 34 | draw-bitmap source dest-x dest-y 35 | 36 | def-protocol path-construction 37 | arc left top width height start-angle end-angle 38 | move-to x y 39 | line-to x y 40 | 41 | def-protocol bitmap-control 42 | set-smoothing smoothing 43 | set-pen color width style 44 | set-brush color style 45 | 46 | def quarter-circle cx cy quarter radius 47 | (arc (cx - radius) 48 | (cy - radius) 49 | (radius * 2) 50 | (radius * 2) 51 | (pi * quarter * 0.5) 52 | (pi * (quarter + 1) * 0.5)) 53 | 54 | def draw-it dc 55 | def corner-radius: 20 56 | def top: 20 57 | def left: 20 58 | def bottom: 100 59 | def right: 200 60 | dc -> draw-path (->> new 'dc-path%': 61 | move-to left (top + corner-radius) 62 | line-to left bottom 63 | line-to right bottom 64 | quarter-circle (right - corner-radius) (top + corner-radius) 0 corner-radius 65 | line-to (left + corner-radius) top 66 | quarter-circle (left + corner-radius) (top + corner-radius) 1 corner-radius) 67 | 68 | def logo: let 69 | def bm: make-bitmap 300 300 70 | draw-it (->> new 'bitmap-dc%' (bitmap bm): 71 | set-smoothing `smoothed 72 | set-pen "black" 1 `solid 73 | set-brush "yellow" `solid) 74 | bm 75 | 76 | let 77 | def frame: new 'frame%' (label "Shape") (width 340) (height 340) 78 | def canvas: new 'canvas%' (parent frame) (paint-callback { _ dc: dc -> draw-bitmap logo 0 0 }) 79 | frame -> show #t 80 | -------------------------------------------------------------------------------- /examples/syndicate/syndicate.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | 3 | require 4 | for-syntax something/base 5 | only-in syndicate/actor \ 6 | assert \ 7 | message asserted retracted 8 | except-in syndicate \ 9 | assert run-ground 10 | prefix-in s_ syndicate/actor 11 | prefix-in s_ syndicate/ground 12 | 13 | provide 14 | for-syntax (all-defined-out) 15 | (all-defined-out) 16 | all-from-out syndicate 17 | all-from-out syndicate/actor 18 | 19 | def-syntax def-block-syntax stx 20 | syntax-case stx () 21 | _ outer-id inner-id 22 | syntax (def-syntax outer-id outer-stx: 23 | syntax-case outer-stx (block) 24 | _ (block body (... ...)) 25 | syntax (inner-id body (... ...))) 26 | 27 | def-block-syntax spawn s_spawn 28 | def-block-syntax spawn* s_spawn* 29 | def-block-syntax run-ground s_run-ground 30 | def-block-syntax forever s_forever 31 | def-block-syntax react s_react 32 | def-block-syntax begin/dataflow s_begin/dataflow 33 | def-block-syntax on-start s_on-start 34 | def-block-syntax on-stop s_on-stop 35 | 36 | def-syntax def-event-syntax stx 37 | syntax-case stx () 38 | _ outer-id inner-id 39 | syntax (begin (def-operator outer-id #f prefix-macro outer-id) 40 | (def-syntax outer-id outer-stx parse: 41 | syntax-case outer-stx (block): 42 | _ evt (... ...) (block body (... ...)) 43 | (quasisyntax/loc outer-stx 44 | (inner-id (unsyntax (parse (syntax (evt (... ...))))) 45 | (unsyntax-splicing 46 | (map parse (syntax->list (syntax (body (... ...)))))))) 47 | _ evt (... ...) 48 | (quasisyntax/loc outer-stx 49 | (inner-id (unsyntax (parse (syntax (evt (... ...))))))))) 50 | 51 | def-event-syntax until s_until 52 | def-event-syntax during s_during 53 | def-event-syntax during/spawn s_during/spawn 54 | def-event-syntax on s_on 55 | def-event-syntax stop-when s_stop-when 56 | 57 | def-syntax field stx 58 | syntax-case stx () 59 | _ id (block init) 60 | syntax (s_field (id init)) 61 | 62 | def-syntax def/dataflow stx 63 | syntax-case stx () 64 | _ id (block init) 65 | syntax (s_define/dataflow id init) 66 | 67 | def-operator ! 1100 prefix ! 68 | def ! f: (f) 69 | 70 | def-operator <- 10 nonassoc <- 71 | def <- f v: f v 72 | 73 | def-operator !! 10 prefix !! 74 | def !! v: s_send! v 75 | 76 | def-operator $ 1100 prefix $ 77 | -------------------------------------------------------------------------------- /src/something/test/sweet.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | // c.f. Sweet-expression examples 3 | // https://sourceforge.net/p/readable/wiki/Solution/#quick-examples 4 | 5 | require 6 | something/infix 7 | something/for 8 | 9 | def fibfast n 10 | if n < 2 11 | n 12 | fibup n 2 1 0 13 | 14 | def fibup max count n1 n2 15 | if max = count 16 | n1 + n2 17 | fibup max (count + 1) (n1 + n2) n1 18 | 19 | def factorial n 20 | if n <= 1 21 | 1 22 | n * factorial (n - 1) 23 | 24 | module+ test 25 | require: rackunit 26 | check-equal? (for/list { x: .. 8 }: fibfast x) [0; 1; 1; 2; 3; 5; 8; 13] 27 | check-equal? (for/list { x: .. 8 }: factorial x) [1; 1; 2; 6; 24; 120; 720; 5040] 28 | 29 | /////////////////////////////////////////////////////////////////////////// 30 | 31 | require 32 | for-syntax something/base 33 | 34 | def-syntax sweet-let* stx // like the example at https://sourceforge.net/p/readable/wiki/Examples/ 35 | // but altered for something-style blocks 36 | syntax-case stx (block) 37 | _ (block body ...) 38 | syntax (begin body ...) 39 | _ (var init) rest ... (block body ...) 40 | syntax (let (var init) (block (sweet-let* rest ... (block body ...)))) 41 | 42 | module+ test 43 | def (t) 44 | def x: 0 45 | sweet-let* (x (x + 1)) \ 46 | (x (x + 1)) \ 47 | (x (x + 1)) \ 48 | (x (x + 1)) 49 | x 50 | 51 | check-equal? (t) 4 52 | 53 | /////////////////////////////////////////////////////////////////////////// 54 | 55 | def add-if-all-numbers lst 56 | call/ec 57 | exit 58 | let loop (lst lst) 59 | if null? lst 60 | 0 61 | if not.number? (car lst) 62 | exit #f 63 | car lst + loop (cdr lst) 64 | 65 | def add-if-all-numbers2 lst 66 | call/ec: exit 67 | let loop (lst lst) 68 | if null? lst 69 | 0 70 | if not (number? (car lst)) 71 | exit #f 72 | car lst + loop (cdr lst) 73 | 74 | def add-if-all-numbers3 lst 75 | call/ec: exit 76 | let loop (lst lst) 77 | cond 78 | when null? lst : 0 79 | unless number? (car lst) : exit #f 80 | else : car lst + loop (cdr lst) 81 | 82 | def add-if-all-numbers/acc lst 83 | let loop (lst lst) (sum 0) 84 | if null? lst 85 | sum 86 | if not (number? (car lst)) 87 | #f 88 | loop (cdr lst) (sum + car lst) 89 | 90 | def add-if-all-numbers/acc2 lst 91 | let loop (lst lst) (sum 0) 92 | cond 93 | when null? lst : sum 94 | unless number? (car lst) : #f 95 | else : loop (cdr lst) (sum + car lst) 96 | 97 | module+ test 98 | def check-adder adder 99 | check-equal? (adder [1; 2; 3; 4]) 10 100 | check-equal? (adder [1; 2; "hello"; 4]) #f 101 | 102 | check-adder add-if-all-numbers 103 | check-adder add-if-all-numbers2 104 | check-adder add-if-all-numbers3 105 | check-adder add-if-all-numbers/acc 106 | check-adder add-if-all-numbers/acc2 107 | -------------------------------------------------------------------------------- /examples/syndicate/file-system-during.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | // Toy file system, based on the example in the ESOP2016 submission. 3 | // syndicate/actor implementation, using "during" instead of "on asserted/until retracted". 4 | // Requires the "syndicate" package to be installed. 5 | 6 | require 7 | something/infix 8 | "syndicate.rkt" 9 | syndicate/drivers/timer 10 | only-in racket/port read-bytes-line-evt 11 | only-in racket/string string-trim string-split 12 | 13 | struct file (name content) :prefab 14 | struct save (file) :prefab 15 | struct delete (name) :prefab 16 | 17 | def sleep sec 18 | def timer-id: gensym `sleep 19 | until message (timer-expired timer-id _) 20 | on-start: !! set-timer timer-id (sec * 1000.0) `relative 21 | 22 | run-ground 23 | (spawn-timer-driver) 24 | 25 | spawn 26 | field files: (hash) 27 | during observe (file $name _) 28 | on-start: printf "At least one reader exists for ~v\n" name 29 | on-stop: printf "No remaining readers exist for ~v\n" name 30 | field content: hash-ref !files name #f 31 | assert (file name !content) 32 | on message (save (file name $new-content)): content <- new-content 33 | on message (delete name): content <- #f 34 | on message (save (file $name $new-content)): files <- hash-set !files name new-content 35 | on message (delete $name): files <- hash-remove !files name 36 | 37 | // Shell 38 | spawn 39 | field reader-count: 0 40 | on-start: (print-prompt) 41 | 42 | def (print-prompt) 43 | printf "> " 44 | (flush-output) 45 | 46 | def e: read-bytes-line-evt (current-input-port) `any 47 | 48 | stop-when message (inbound (external-event e [? eof-object? _])) 49 | 50 | on message (inbound (external-event e [? bytes? $bs])) 51 | match string-split (string-trim (bytes->string/utf-8 bs)) 52 | 53 | ["open"; name]: 54 | def reader-id: !reader-count 55 | reader-count <- !reader-count + 1 56 | spawn 57 | on-start: printf "Reader ~a opening file ~v.\n" reader-id name 58 | stop-when message `(stop-watching ,name) 59 | on asserted (file name $contents) 60 | (printf "Reader ~a sees that ~v contains: ~v\n" 61 | reader-id 62 | name 63 | contents) 64 | on-stop: printf "Reader ~a closing file ~v.\n" reader-id name 65 | 66 | ["close"; name]: 67 | !! `(stop-watching ,name) 68 | 69 | ["write"; name; words; ...]: 70 | !! save . file name words 71 | 72 | ["delete"; name]: 73 | !! delete name 74 | 75 | _: 76 | printf "I'm afraid I didn't understand that.\n" 77 | printf "Try: open filename\n" 78 | printf " close filename\n" 79 | printf " write filename some text goes here\n" 80 | printf " delete filename\n" 81 | 82 | sleep 0.1 83 | (print-prompt) 84 | -------------------------------------------------------------------------------- /src/something/test/simple-tests.rkt: -------------------------------------------------------------------------------- 1 | #lang something // -*- sth -*- 2 | 3 | require 4 | something/infix 5 | rename-in (only-in racket/base -) (- base-minus) 6 | "s2.rkt" 7 | rackunit 8 | 9 | // (dump-operator-table!) 10 | 11 | def print-and-return x 12 | printf "print-and-return ~a\n" x 13 | x 14 | 15 | def check-output expected-output expected-logging f 16 | local-require (only-in racket/string string-split) 17 | local-require (only-in racket/logging with-logging-to-port) 18 | def-values actual-output actual-logging: 19 | def o: (open-output-string) 20 | def l: (open-output-string) 21 | parameterize {current-output-port: o}: with-logging-to-port l f `info 22 | values (get-output-string o) (get-output-string l) 23 | check-equal? (string-split actual-output "\n") expected-output 24 | check-equal? (string-split actual-logging "\n") expected-logging 25 | 26 | check-output (list "print-and-return #f" 27 | "print-and-return 2" 28 | "n is 0" 29 | "n is 1" 30 | "n is 2" 31 | "n is 3" 32 | "n is 4" 33 | "n is 5" 34 | "n is 6" 35 | "n is 7" 36 | "n is 8" 37 | "n is 9" 38 | "x in let is 123") \ 39 | (list "plus-op 1 6" 40 | "plus-op 3 4" 41 | "plus-op 7 5" 42 | "s2minus 3 4" 43 | "s2minus -1 5" 44 | "plus-op 1 2" 45 | "plus-op 0 1" 46 | "plus-op 1 1" 47 | "plus-op 2 1" 48 | "plus-op 3 1" 49 | "plus-op 4 1" 50 | "plus-op 5 1" 51 | "plus-op 6 1" 52 | "plus-op 7 1" 53 | "plus-op 8 1" 54 | "plus-op 9 1") \ 55 | :: 56 | check-equal? (1 + 2 * 3) 7 57 | check-equal? (+ 3 4 5) 12 58 | check-equal? (3 + 4 + 5) 12 59 | check-equal? (3 - 4 - 5 base-minus 2 * 2) -10 60 | check-equal? (car [+]) (values +) 61 | check-equal? [1; + ; 2; 1 + 2] (list 1 (values +) 2 3) 62 | check-equal? (2 > 1) #t 63 | 64 | check-equal? (print-and-return #f || print-and-return 2 || print-and-return 3) 2 65 | 66 | def let-test-result: 67 | let (x 123) (y 234) (z 345) 68 | [`inside-the-let; x; y; z] 69 | check-equal? let-test-result (list `inside-the-let 123 234 345) 70 | 71 | let loop (n 0) 72 | def still-running?: n < 10 73 | when still-running? 74 | printf "n is ~a\n" n 75 | loop (n + 1) 76 | 77 | def cond-result 78 | cond 79 | when #t 80 | #t 81 | else 82 | #f 83 | check-true cond-result 84 | 85 | def my-list 86 | { items ... : items } 87 | 88 | check-equal? (my-list 1 2 3 4) [1; 2; 3; 4] 89 | 90 | let 91 | def x: 123 92 | printf "x in let is ~a\n" x 93 | check-equal? x 123 94 | 95 | def curried x:: y:: z: 96 | [`curried; x; y; z] 97 | 98 | def ((curried2 x) y) z 99 | [`curried2; x; y; z] 100 | 101 | check-equal? (((curried 1) 2) 3) [`curried; 1; 2; 3] 102 | check-equal? (curried 1 . 2 . 3) [`curried; 1; 2; 3] 103 | check-equal? (((curried2 1) 2) 3) [`curried2; 1; 2; 3] 104 | check-equal? (curried2 1 . 2 . 3) [`curried2; 1; 2; 3] 105 | 106 | def-operator > 40 n-ary > 107 | check-equal? (4 > 3 > 2) #t 108 | -------------------------------------------------------------------------------- /sth8.el: -------------------------------------------------------------------------------- 1 | ;;; sth.el --- STH code editing commands for Emacs 2 | 3 | ;; Add code like the following to your .emacs to install: 4 | ;; (autoload 'sth-mode "...path.to.wherever.you.put.this.file.../sth.el" nil t) 5 | ;; (setq auto-mode-alist (cons '("\\.sth\\'" . sth-mode) 6 | ;; auto-mode-alist)) 7 | 8 | ;; Copyright (C) 1988,94,96,2000 Free Software Foundation, Inc. 9 | ;; Copyright (C) 2003, 2005, 2011, 2016 Tony Garnock-Jones 10 | 11 | ;; This is free software; you can redistribute it and/or modify 12 | ;; it under the terms of the GNU General Public License as published by 13 | ;; the Free Software Foundation; either version 2, or (at your option) 14 | ;; any later version. 15 | 16 | ;; This is distributed in the hope that it will be useful, 17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | ;; GNU General Public License for more details. 20 | 21 | ;; You should have received a copy of the GNU General Public License 22 | ;; along with this software; see the file COPYING. If not, write to the 23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, 24 | ;; Boston, MA 02111-1307, USA. 25 | 26 | (require 'comint) 27 | (require 'font-lock) 28 | (require 'rx) 29 | (require 'cl-lib) 30 | 31 | ;;--------------------------------------------------------------------------- 32 | ;; State 33 | 34 | (defvar sth-buffer) 35 | 36 | ;;--------------------------------------------------------------------------- 37 | ;; Customization 38 | 39 | (defcustom sth-program-name "./sth-main.rkt" 40 | "*Program invoked by the `run-sth' command." 41 | :type 'string 42 | :group 'sth) 43 | 44 | (defcustom inferior-sth-mode-hook nil 45 | "*Hook for customising inferior-sth mode." 46 | :type 'hook 47 | :group 'sth) 48 | 49 | ;;--------------------------------------------------------------------------- 50 | ;; REPL 51 | 52 | (define-derived-mode inferior-sth-mode comint-mode "Inferior Something" 53 | "Major mode for interacting with an inferior Something process." 54 | ;; Customise in inferior-sth-mode-hook 55 | (setq comint-prompt-regexp "^\"[^\"\n]*\" *") 56 | (sth-mode-variables) 57 | (setq mode-line-process '(":%s"))) 58 | 59 | ;;;###autoload 60 | (defun run-sth (cmd) 61 | "Run an inferior Something process, input and output via buffer *sth*. 62 | If there is a process already running in `*sth*', switch to that buffer. 63 | With argument, allows you to edit the command line (default is value 64 | of `sth-program-name'). Runs the hooks `inferior-sth-mode-hook' 65 | \(after the `comint-mode-hook' is run). 66 | \(Type \\[describe-mode] in the process buffer for a list of commands.)" 67 | (interactive (list (if current-prefix-arg 68 | (read-string "Run Something: " sth-program-name) 69 | sth-program-name))) 70 | (if (not (comint-check-proc "*sth*")) 71 | (let ((cmdlist (scheme-args-to-list cmd))) 72 | (set-buffer (apply 'make-comint "sth" (car cmdlist) 73 | nil (cdr cmdlist))) 74 | (inferior-sth-mode))) 75 | (setq sth-program-name cmd) 76 | (setq sth-buffer "*sth*") 77 | (pop-to-buffer "*sth*")) 78 | 79 | ;;--------------------------------------------------------------------------- 80 | ;; Font-lock, Indentation, and Mode 81 | 82 | (defvar sth-mode-syntax-table (make-syntax-table) 83 | "Syntax table in use in sth-mode buffers.") 84 | 85 | (modify-syntax-entry ?' "\"" sth-mode-syntax-table) 86 | (modify-syntax-entry ?_ "_" sth-mode-syntax-table) 87 | (modify-syntax-entry ?\n "> b" sth-mode-syntax-table) 88 | (modify-syntax-entry ?\r "> b" sth-mode-syntax-table) 89 | (modify-syntax-entry ?/ ". 12b" sth-mode-syntax-table) 90 | (mapcar #'(lambda (x) (modify-syntax-entry x "w" sth-mode-syntax-table)) 91 | '(?- ?_ ?$ ?! ?? ?* ?+)) 92 | 93 | (defvar sth-font-lock-keywords 94 | (list 95 | ;; '("%assemble\\>" . font-lock-warning-face) 96 | 97 | '("\\<[A-Z][^[:space:].\n]*\\>" . font-lock-type-face) 98 | 99 | '("\\(\\\\)?\\)\\s-*\\(\\<[^[:space:].\n]*\\>\\)" 100 | (1 font-lock-keyword-face) 101 | (3 font-lock-function-name-face)) 102 | 103 | '("\\\\)" . font-lock-keyword-face) 104 | 105 | '("\\(:\\<[^[:space:].\n]*\\>\\)" . font-lock-constant-face) 106 | 107 | ;; '("'\\([^\\]\\|\\\\.\\)*?'" . nil) 108 | 109 | (cons (regexp-opt 110 | '( 111 | "float" 112 | "double" 113 | "word32" 114 | "word64" 115 | "integer" 116 | "boolean" 117 | "text" 118 | "set" 119 | "list" 120 | "dict" 121 | "not" 122 | "ref" 123 | ) 124 | 'words) 125 | 'font-lock-builtin-face) 126 | 127 | (regexp-opt 128 | '( 129 | "import" 130 | "def" 131 | "method" 132 | "self" 133 | "let" 134 | "let*" 135 | "letrec" 136 | "binary" 137 | "cond" 138 | "when" 139 | "else" 140 | "match" 141 | "raise" 142 | "catch" 143 | "throw" 144 | "return" 145 | ) 146 | 'words) 147 | )) 148 | 149 | (defun sth-mode-variables () 150 | (make-local-variable 'comment-start) 151 | (make-local-variable 'comment-end) 152 | (make-local-variable 'comment-start-skip) 153 | (setq comment-start "//") 154 | (setq comment-end "") 155 | (setq comment-start-skip "// *") 156 | (make-local-variable 'font-lock-defaults) 157 | (setq font-lock-defaults '(sth-font-lock-keywords nil nil ()))) 158 | 159 | ;;;###autoload 160 | (define-derived-mode sth-mode prog-mode "STH" 161 | "Major mode for editing STH code." 162 | (sth-mode-variables)) 163 | 164 | ;;--------------------------------------------------------------------------- 165 | ;; Provides 166 | 167 | (provide 'sth-mode) 168 | 169 | ;;; sth.el ends here 170 | -------------------------------------------------------------------------------- /src/something/shell.rkt: -------------------------------------------------------------------------------- 1 | #lang something 2 | 3 | require 4 | rename-in something ('#%app' base-app) 5 | only-in something/lang/reader read-toplevel-syntax 6 | racket/system 7 | racket/format 8 | only-in racket/list flatten 9 | 10 | racket/port 11 | racket/file 12 | racket/string 13 | tabular 14 | 15 | for-syntax something 16 | for-syntax syntax/stx 17 | 18 | provide 19 | except-out (all-from-out something) '#%module-begin' 20 | rename-out ('#%plain-module-begin' '#%module-begin') 21 | 22 | all-from-out racket/port 23 | all-from-out racket/file 24 | all-from-out racket/string 25 | all-from-out tabular 26 | 27 | rename-out (shell-app '#%app') 28 | run-in-background 29 | pipeline 30 | pipe 31 | rev-apply 32 | rev-apply* 33 | wait 34 | getenv* 35 | 36 | read-lines 37 | read0 38 | discard 39 | preserve-header 40 | space-separated-columns 41 | 42 | def-syntax shell-app stx 43 | syntax-case stx () 44 | _ f arg ... 45 | identifier? (syntax f) && bound-at-phase-0? (syntax f) 46 | syntax (base-app f arg ...) 47 | _ f arg ... 48 | identifier? (syntax f) 49 | build-command (syntax f) (syntax (arg ...)) 50 | _ f arg ... 51 | syntax (base-app f arg ...) 52 | 53 | begin-for-syntax 54 | def bound-at-phase-0? id-stx 55 | identifier-binding id-stx || \ 56 | (procedure-arity-includes? identifier-binding 3 && identifier-binding id-stx 0 #t) 57 | 58 | def build-command id-stx args-stx 59 | with-syntax { command: symbol->string (syntax-e id-stx) 60 | (arg ...): args-stx } 61 | syntax (apply system*/exit-code (find-command (quote command)) 62 | (flatten (list (parse-shell-argument arg) ...))) 63 | 64 | def find-command command-name: 65 | or (find-executable-path command-name) \ 66 | (error `find-command "No such command: ~v" command-name) 67 | 68 | def-syntax parse-shell-argument stx 69 | syntax-case stx () 70 | _ value 71 | number? (syntax-e (syntax value)) 72 | datum->syntax stx (number->string (syntax-e (syntax value))) 73 | _ id 74 | identifier? (syntax id) && regexp-match "^-" (symbol->string (syntax-e (syntax id))) 75 | datum->syntax stx (symbol->string (syntax-e (syntax id))) 76 | _ expr 77 | (syntax (format-shell-argument expr)) 78 | 79 | def format-shell-argument a 80 | cond 81 | when string? a: a 82 | when symbol? a: symbol->string a 83 | when number? a: '~a' a 84 | when list? a: map format-shell-argument a 85 | else: error (quote format-shell-argument) "Cannot format ~v" a 86 | 87 | def-operator & 8 postfix run-in-background 88 | def-syntax run-in-background stx 89 | syntax-case stx (block) 90 | _ (block expr ...) 91 | (syntax (run-in-background (begin expr ...))) 92 | _ expr 93 | (syntax (shell-thread :: expr)) 94 | 95 | def shell-thread thunk 96 | def ch: (make-channel) 97 | thread :: 98 | with-handlers 99 | when values e: 100 | channel-put ch e 101 | channel-put ch (thunk) 102 | ch 103 | 104 | def-syntax ensure stx 105 | syntax-case stx (block) 106 | _ finally-expr (block body ...) 107 | syntax (with-handlers: 108 | when values e: 109 | finally-expr 110 | raise e 111 | (begin0 (begin body ...) finally-expr)) 112 | 113 | def-syntax pipeline stx 114 | syntax-case stx (block '|>' '|<') 115 | _ (block) 116 | (syntax (copy-port (current-input-port) (current-output-port))) 117 | _ (block final-stage) 118 | (syntax final-stage) 119 | _ (block stage ... ('|>' final-stage)) 120 | (syntax (rev-apply (pipeline (block stage ...)) final-stage)) 121 | _ (block stage ... ('|>' final-stage ...)) 122 | (syntax (rev-apply (pipeline (block stage ...)) (final-stage ...))) 123 | _ (block stage ... ('|<' final-stage)) 124 | (syntax (rev-apply* (pipeline (block stage ...)) final-stage)) 125 | _ (block stage ... ('|<' final-stage ...)) 126 | (syntax (rev-apply* (pipeline (block stage ...)) (final-stage ...))) 127 | _ (block stage ... final-stage) 128 | (syntax (pipe (pipeline (block stage ...)) final-stage)) 129 | 130 | def-operator | 10 left pipe 131 | def-syntax pipe stx 132 | syntax-case stx () 133 | _ lhs rhs 134 | (syntax (pipe* {: run-pipe-stage lhs} {: run-pipe-stage rhs})) 135 | 136 | def run-pipe-stage result: 137 | if procedure? result 138 | (result) 139 | result 140 | 141 | def pipe* lhs-thunk rhs-thunk: 142 | def-values i o: (make-pipe) 143 | def lhs-thread: 144 | parameterize {current-output-port: o} 145 | (ensure (close-output-port o): 146 | begin0 (lhs-thunk) (flush-output o)) & 147 | def rhs-thread: 148 | parameterize {current-input-port: i} 149 | (ensure (close-input-port i): 150 | (rhs-thunk)) & 151 | wait lhs-thread 152 | wait rhs-thread 153 | 154 | def wait ch: 155 | match channel-get ch 156 | ? exn? e: raise e 157 | v: v 158 | 159 | def-operator |> 10 left rev-apply 160 | def-operator |< 10 left rev-apply* 161 | 162 | def-syntax rev-apply stx 163 | syntax-case stx () 164 | _ v id 165 | identifier? (syntax id) 166 | syntax (shell-app id v) 167 | _ v (f arg ...) 168 | syntax (shell-app f arg ... v) 169 | 170 | def-syntax rev-apply* stx 171 | syntax-case stx () 172 | _ v id 173 | identifier? (syntax id) 174 | syntax (shell-app id v) 175 | _ v (f arg ...) 176 | syntax (shell-app f v arg ...) 177 | 178 | // Double-duty $id is an environment variable reference, and $(id) is an output capture 179 | def-operator $ 1100 prefix getenv* 180 | def-syntax getenv* stx 181 | syntax-case stx () 182 | _ exp 183 | stx-pair? (syntax exp) 184 | (quasisyntax (pipe exp (compose string-trim port->string))) 185 | _ id 186 | identifier? (syntax id) 187 | (quasisyntax (getenv (unsyntax (symbol->string (syntax-e (syntax id)))))) 188 | 189 | current-read-interaction read-toplevel-syntax 190 | 191 | module+ reader 192 | require 193 | something/lang/reader 194 | provide 195 | rename-out (read-shell-syntax read-syntax) 196 | read-toplevel-syntax 197 | 198 | def read-shell-syntax src p 199 | read-syntax src p :language (syntax something/shell) 200 | 201 | /////////////////////////////////////////////////////////////////////////// 202 | // Sketches of utilities 203 | 204 | def read-lines (p (current-input-port)) 205 | port->lines p :line-mode `any 206 | 207 | def read0 (p (current-input-port)) 208 | port->string p |< string-split "\0" 209 | 210 | def (discard) 211 | for ((line (in-lines))) (void) 212 | 213 | def ((preserve-header nlines thunk)) 214 | for ((n (in-range nlines))) (displayln (read-line)) 215 | (thunk) 216 | 217 | def space-separated-columns (converters []) 218 | local-require racket/string 219 | def header: map string->symbol (string-split (read-line)) 220 | def nsplits: length header - 1 221 | def split-once s: 222 | match s 223 | pregexp "^\\s*(\\S+)\\s+(\\S.*)?$" [_; h; t]: values h t 224 | _: values s #f 225 | def split-line line n converters: 226 | if zero? n 227 | match converters 228 | []: list line 229 | cons c _: list ((c || values) line) 230 | let 231 | def-values h t: split-once line 232 | match converters 233 | []: cons h (split-line t (n - 1) []) 234 | cons c r: cons ((c || values) h) (split-line t (n - 1) r ) 235 | cons header (for/list ((line (in-lines))) (split-line line nsplits converters)) 236 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Indentation-based Racket Syntax with Macros and Infix Operators 2 | 3 | Not [sweet-exps](http://readable.sourceforge.net/) (see Asumu's 4 | [racket implementation](https://github.com/takikawa/sweet-racket)). 5 | Not [srfi-49](http://srfi.schemers.org/srfi-49/srfi-49.html). More 6 | inspired by Python and Haskell. 7 | 8 | (The name "something" is temporary, and likely to be repurposed.) 9 | 10 | # Installation 11 | 12 | Check out the repository. Then, in the directory containing 13 | `Makefile`, 14 | 15 | make link 16 | 17 | or 18 | 19 | raco pkg install --link -n something `pwd`/src 20 | 21 | # The main idea 22 | 23 | S-expressions, but with usually-implicit parentheses. Indentation for 24 | grouping is explicitly represented in the S-expression returned from 25 | the reader. 26 | 27 | This program: 28 | 29 | #lang something 30 | for { x: 1 .. 10 } 31 | def y: x + 1 32 | printf "x ~a y ~a\n" x y 33 | 34 | ... reads as this S-expression: 35 | 36 | (module something-module something/base 37 | (#%rewrite-body 38 | (for (block (x (block (1 .. 10)))) 39 | (block (def y (block (x + 1))) 40 | (printf "x ~a y ~a\n" x y))))) 41 | 42 | The `#%rewrite-body` macro, together with its companion 43 | `#%rewrite-infix`, consults an operator table, extendable via the 44 | `def-operator` macro, to rewrite infix syntax into standard prefix 45 | S-expressions. 46 | 47 | The `block` syntax has many different interpretations. It has a macro 48 | binding that turns it into a Racket `match-lambda*`, and it is used as 49 | literal syntax as input to other macro definitions. 50 | 51 | For example, here's one possible implementation of that `for` syntax: 52 | 53 | #lang something 54 | 55 | provide 56 | for 57 | 58 | require 59 | for-syntax something/base 60 | prefix-in base_ racket/base 61 | 62 | def-syntax for stx 63 | syntax-case stx (block) 64 | _ (block (v (block exp)) ...) (block body ...) 65 | (syntax (base_for ((v exp) ...) body ...)) 66 | 67 | def-operator .. 10 nonassoc in-range 68 | 69 | Notice how the `block` S-expressions are rewritten into a normal 70 | S-expression compatible with the underlying `for` from `racket/base`. 71 | 72 | Generally, all of these forms are equivalent 73 | 74 | x y z x y z: x y z { a; b } 75 | a a 76 | b b 77 | 78 | and they are read as 79 | 80 | (x y z (block a b)) 81 | 82 | and are then made available to the normal macro-expansion process 83 | (which involves a new infix-rewriting semi-phase). 84 | 85 | Colons are optional to indicate a following suite at the end of an 86 | indentation-sensitive line. Indentation-sensitivity is disabled inside 87 | parentheses. If inside a parenthesised expression, 88 | indentation-sensitivity can be reenabled with a colon at the end of a 89 | line: 90 | 91 | a b (c d: 92 | e 93 | f) 94 | 95 | = (a b (c d (block e f))) 96 | 97 | a b (c d 98 | e 99 | f) 100 | 101 | = (a b (c d e f)) 102 | 103 | Conversely, long lines may be split up and logically continued over 104 | subsequent physical lines with a trailing `\`: 105 | 106 | a b c \ 107 | d \ 108 | e 109 | 110 | = (a b c d e) 111 | 112 | Semicolons may also appear in vertically-laid-out suites; these two 113 | are equivalent: 114 | 115 | x y z 116 | a 117 | b; c 118 | d 119 | 120 | x y z { a; b; c; d } 121 | 122 | Suites may begin on the same line as their colon. Any indented 123 | subsequent lines become children of the portion after the colon, 124 | rather than the portion before. 125 | 126 | This example: 127 | 128 | x y z: a b 129 | c d 130 | e 131 | 132 | reads as 133 | 134 | (x y z (block (a b (block (c d) e)))) 135 | 136 | Square brackets are syntactic sugar for a `#%seq` macro: 137 | 138 | [a; b; c; d e f] → (#%seq a b c (d e f)) 139 | 140 | [ → (#%seq a (b (block c)) (d e f)) 141 | a 142 | b 143 | c 144 | d e f 145 | ] 146 | 147 | Forms starting with `block` in expression context expand into 148 | `match-lambda*` like this: 149 | 150 | { 151 | pat1a pat1b 152 | exp1a 153 | exp1b 154 | pat2a 155 | exp2 156 | } 157 | 158 | → (match-lambda* 159 | [(list pat1a pat1b) exp1a exp1b] 160 | [(list pat2a) exp2]) 161 | 162 | The `map*` function exported from `something/lang/implicit` differs 163 | from `map` in `racket/base` in that it takes its arguments in the 164 | opposite order, permitting maps to be written 165 | 166 | map* [1; 2; 3; 4] 167 | item: 168 | item + 1 169 | 170 | map* [1; 2; 3; 4] 171 | item: item + 1 172 | 173 | map* [1; 2; 3; 4]: item: item + 1 174 | 175 | map* [1; 2; 3; 4] { item: item + 1 } 176 | 177 | A nice consequence of all of the above is that curried functions have 178 | an interesting appearance: 179 | 180 | def curried x:: y:: z: 181 | [x; y; z] 182 | 183 | require rackunit 184 | check-equal? (((curried 1) 2) 3) [1; 2; 3] 185 | 186 | # A larger example 187 | 188 | More examples can be found in the [examples](examples/) and 189 | [src/something/test](src/something/test/) directories. 190 | 191 | #lang something 192 | 193 | require 194 | racket/pretty 195 | something/infix 196 | for-syntax something/lang/implicit 197 | except-in xml document 198 | 199 | def-syntax single-xexpr stx 200 | syntax-case stx (= block) 201 | _ str 202 | string? (syntax-e . (syntax str)) 203 | syntax str 204 | _ (= expr) 205 | syntax expr 206 | _ (tag (attr attr-expr) ... (block xexpr ...)) 207 | syntax (list (quote tag) (list (list (quote attr) attr-expr) ...) (single-xexpr xexpr) ...) 208 | _ (tag (attr attr-expr) ...) 209 | syntax (list (quote tag) (list (list (quote attr) attr-expr) ...)) 210 | 211 | def-syntax xexpr stx 212 | syntax-case stx (block) 213 | _ (block xexpr) 214 | syntax (single-xexpr xexpr) 215 | 216 | def-operator ++ 50 left string-append 217 | def-operator = 10 prefix = 218 | 219 | def document 220 | xexpr 221 | html 222 | head 223 | meta (http-equiv "Content-Type") (content "text/html; charset=utf-8") 224 | title: "Test page" 225 | body 226 | h1: "Hello" 227 | p: "Hello, world" 228 | h2: "Testing" 229 | p 230 | = "Hello, " ++ number->string (3 + 4) 231 | "! This rules." 232 | p 233 | "Another way of putting it would be to say that 3 + 4 = " 234 | = (number->string (3 + 4)) 235 | "." 236 | 237 | pretty-print document 238 | printf "\n~a\n" (xexpr->string document) 239 | 240 | # A note on lexical syntax 241 | 242 | The lexical syntax of this reader is not exactly that of Racket. For 243 | example, comments start with `//` rather than `;`, and the set of 244 | allowable non-escaped identifiers is different (smaller). 245 | 246 | I will likely revise this decision to bring it to be much closer to 247 | Racket's lexical syntax. 248 | 249 | # Emacs mode 250 | 251 | See [sth8.el](sth8.el). 252 | 253 | # Licence 254 | 255 | Copyright (C) 2016–2019 Tony Garnock-Jones 256 | 257 | This program is free software: you can redistribute it and/or modify 258 | it under the terms of the GNU Lesser General Public License as 259 | published by the Free Software Foundation, either version 3 of the 260 | License, or (at your option) any later version. 261 | 262 | This program is distributed in the hope that it will be useful, but 263 | WITHOUT ANY WARRANTY; without even the implied warranty of 264 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 265 | Lesser General Public License for more details. 266 | 267 | You should have received a copy of the GNU Lesser General Public 268 | License along with this program (see the files "lgpl.txt" and 269 | "gpl.txt"). If not, see . 270 | -------------------------------------------------------------------------------- /lgpl.txt: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /src/something/shell2.rkt: -------------------------------------------------------------------------------- 1 | #lang something 2 | // This is a copy of shell.rkt using explicit {...} blocks instead of indentation/colon-based blocks 3 | 4 | require { 5 | rename-in something ('#%app' base-app) 6 | only-in something/lang/reader read-toplevel-syntax 7 | racket/system 8 | racket/format 9 | only-in racket/list flatten 10 | 11 | racket/port 12 | racket/file 13 | racket/string 14 | tabular 15 | 16 | for-syntax something 17 | for-syntax syntax/stx 18 | } 19 | 20 | provide { 21 | except-out (all-from-out something) '#%module-begin' 22 | rename-out ('#%plain-module-begin' '#%module-begin') 23 | 24 | all-from-out racket/port 25 | all-from-out racket/file 26 | all-from-out racket/string 27 | all-from-out tabular 28 | 29 | rename-out (shell-app '#%app') 30 | run-in-background 31 | pipeline 32 | pipe 33 | rev-apply 34 | rev-apply* 35 | wait 36 | getenv* 37 | 38 | read-lines 39 | read0 40 | discard 41 | preserve-header 42 | space-separated-columns 43 | } 44 | 45 | def-syntax shell-app stx { 46 | syntax-case stx () { 47 | _ f arg ... { 48 | identifier? (syntax f) && bound-at-phase-0? (syntax f) 49 | syntax (base-app f arg ...) 50 | } 51 | _ f arg ... { 52 | identifier? (syntax f) 53 | build-command (syntax f) (syntax (arg ...)) 54 | } 55 | _ f arg ... { 56 | syntax (base-app f arg ...) 57 | } 58 | } 59 | } 60 | 61 | begin-for-syntax { 62 | def bound-at-phase-0? id-stx { 63 | identifier-binding id-stx || \ 64 | (procedure-arity-includes? identifier-binding 3 && identifier-binding id-stx 0 #t) 65 | } 66 | 67 | def build-command id-stx args-stx { 68 | with-syntax { 69 | command { symbol->string (syntax-e id-stx) } 70 | (arg ...) { args-stx } 71 | } { 72 | syntax (apply system*/exit-code (find-command (quote command)) 73 | (flatten (list (parse-shell-argument arg) ...))) 74 | } 75 | } 76 | } 77 | 78 | def find-command command-name { 79 | or (find-executable-path command-name) \ 80 | (error `find-command "No such command: ~v" command-name) 81 | } 82 | 83 | def-syntax parse-shell-argument stx { 84 | syntax-case stx () { 85 | _ value { 86 | number? (syntax-e (syntax value)) 87 | datum->syntax stx (number->string (syntax-e (syntax value))) 88 | } 89 | _ id { 90 | identifier? (syntax id) && regexp-match "^-" (symbol->string (syntax-e (syntax id))) 91 | datum->syntax stx (symbol->string (syntax-e (syntax id))) 92 | } 93 | _ expr { 94 | syntax (format-shell-argument expr) 95 | } 96 | } 97 | } 98 | 99 | def format-shell-argument a { 100 | cond { 101 | when string? a { a } 102 | when symbol? a { symbol->string a } 103 | when number? a { '~a' a } 104 | when list? a { map format-shell-argument a } 105 | else { error (quote format-shell-argument) "Cannot format ~v" a } 106 | } 107 | } 108 | 109 | def-operator & 8 postfix run-in-background 110 | def-syntax run-in-background stx { 111 | syntax-case stx (block) { 112 | _ (block expr ...) { 113 | syntax (run-in-background (begin expr ...)) 114 | } 115 | _ expr { 116 | syntax (shell-thread {{ expr }}) 117 | } 118 | } 119 | } 120 | 121 | def shell-thread thunk { 122 | def ch { (make-channel) } 123 | thread {{ 124 | with-handlers { 125 | when values e { 126 | channel-put ch e 127 | } 128 | channel-put ch (thunk) 129 | } 130 | }} 131 | ch 132 | } 133 | 134 | def-syntax ensure stx { 135 | syntax-case stx (block) { 136 | _ finally-expr (block body ...) { 137 | syntax (with-handlers { 138 | when values e { 139 | finally-expr 140 | raise e 141 | } 142 | (begin0 (begin body ...) finally-expr) 143 | }) 144 | } 145 | } 146 | } 147 | 148 | def-syntax pipeline stx { 149 | syntax-case stx (block '|>' '|<') { 150 | _ (block) { 151 | syntax (copy-port (current-input-port) (current-output-port)) 152 | } 153 | _ (block final-stage) { 154 | syntax final-stage 155 | } 156 | _ (block stage ... ('|>' final-stage)) { 157 | syntax (rev-apply (pipeline (block stage ...)) final-stage) 158 | } 159 | _ (block stage ... ('|>' final-stage ...)) { 160 | syntax (rev-apply (pipeline (block stage ...)) (final-stage ...)) 161 | } 162 | _ (block stage ... ('|<' final-stage)) { 163 | syntax (rev-apply* (pipeline (block stage ...)) final-stage) 164 | } 165 | _ (block stage ... ('|<' final-stage ...)) { 166 | syntax (rev-apply* (pipeline (block stage ...)) (final-stage ...)) 167 | } 168 | _ (block stage ... final-stage) { 169 | syntax (pipe (pipeline (block stage ...)) final-stage) 170 | } 171 | } 172 | } 173 | 174 | def-operator | 10 left pipe 175 | def-syntax pipe stx { 176 | syntax-case stx () { 177 | _ lhs rhs { 178 | syntax (pipe* {: run-pipe-stage lhs} {: run-pipe-stage rhs}) 179 | } 180 | } 181 | } 182 | 183 | def run-pipe-stage result { 184 | cond { 185 | when procedure? result { (result) } 186 | else { result } 187 | } 188 | } 189 | 190 | def pipe* lhs-thunk rhs-thunk { 191 | def-values i o { (make-pipe) } 192 | def lhs-thread { 193 | parameterize { current-output-port { o } } { 194 | ensure (close-output-port o) { 195 | begin0 (lhs-thunk) (flush-output o) 196 | } & 197 | } 198 | } 199 | def rhs-thread { 200 | parameterize { current-input-port { i } } { 201 | ensure (close-input-port i) { 202 | (rhs-thunk) 203 | } & 204 | } 205 | } 206 | wait lhs-thread 207 | wait rhs-thread 208 | } 209 | 210 | def wait ch { 211 | match channel-get ch { 212 | ? exn? e { raise e } 213 | v { v } 214 | } 215 | } 216 | 217 | def-operator |> 10 left rev-apply 218 | def-operator |< 10 left rev-apply* 219 | 220 | def-syntax rev-apply stx { 221 | syntax-case stx () { 222 | _ v id { 223 | identifier? (syntax id) 224 | syntax (shell-app id v) 225 | } 226 | _ v (f arg ...) { 227 | syntax (shell-app f arg ... v) 228 | } 229 | } 230 | } 231 | 232 | def-syntax rev-apply* stx { 233 | syntax-case stx () { 234 | _ v id { 235 | identifier? (syntax id) 236 | syntax (shell-app id v) 237 | } 238 | _ v (f arg ...) { 239 | syntax (shell-app f v arg ...) 240 | } 241 | } 242 | } 243 | 244 | // Double-duty $id is an environment variable reference, and $(id) is an output capture 245 | def-operator $ 1100 prefix getenv* 246 | def-syntax getenv* stx { 247 | syntax-case stx () { 248 | _ exp { 249 | stx-pair? (syntax exp) 250 | quasisyntax (pipe exp (compose string-trim port->string)) 251 | } 252 | _ id { 253 | identifier? (syntax id) 254 | quasisyntax (getenv (unsyntax (symbol->string (syntax-e (syntax id))))) 255 | } 256 | } 257 | } 258 | 259 | current-read-interaction read-toplevel-syntax 260 | 261 | module+ reader { 262 | require { 263 | something/lang/reader 264 | } 265 | provide { 266 | rename-out (read-shell-syntax read-syntax) 267 | read-toplevel-syntax 268 | } 269 | 270 | def read-shell-syntax src p { 271 | read-syntax src p :language (syntax something/shell) 272 | } 273 | } 274 | 275 | /////////////////////////////////////////////////////////////////////////// 276 | // Sketches of utilities 277 | 278 | def read-lines (p (current-input-port)) { 279 | port->string p |< string-split "\0" 280 | } 281 | 282 | def read0 (p (current-input-port)) { 283 | port->string p |< string-split "\0" 284 | } 285 | 286 | def (discard) { 287 | for ((line (in-lines))) (void) 288 | } 289 | 290 | def ((preserve-header nlines thunk)) { 291 | for ((n (in-range nlines))) (displayln (read-line)) 292 | (thunk) 293 | } 294 | 295 | def space-separated-columns (converters []) { 296 | local-require racket/string 297 | def header { map string->symbol (string-split (read-line)) } 298 | def nsplits { length header - 1 } 299 | def split-once s { 300 | match s { 301 | pregexp "^\\s*(\\S+)\\s+(\\S.*)?$" [_; h; t] { values h t } 302 | _ { values s #f } 303 | } 304 | } 305 | def split-line line n converters { 306 | cond { 307 | when zero? n { 308 | match converters { 309 | [] { list line } 310 | cons c _ { list ((c || values) line) } 311 | } 312 | } 313 | else { 314 | let { 315 | def-values h t { split-once line } 316 | match converters { 317 | [] { cons h (split-line t (n - 1) []) } 318 | cons c r { cons ((c || values) h) (split-line t (n - 1) r ) } 319 | } 320 | } 321 | } 322 | } 323 | } 324 | cons header (for/list ((line (in-lines))) (split-line line nsplits converters)) 325 | } 326 | -------------------------------------------------------------------------------- /src/something/pratt.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | ;; Pratt parsing, with a slight extension for adjacency as a pseudo-operator. 3 | 4 | (provide raw-pratt-parse 5 | pratt-parse 6 | ensure-no-leftover-tokens 7 | (struct-out operator)) 8 | 9 | (require racket/match) 10 | (require (only-in racket/list splitf-at)) 11 | 12 | (struct operator (token binding-power associativity handler) #:transparent) 13 | 14 | ;; An Input is a (Listof Token). 15 | 16 | ;; A Parser is a (Input ;; inputs 17 | ;; Natural ;; "right binding power" 18 | ;; (Value (Listof Token) -> ParseResult) ;; continuation 19 | ;; -> ParseResult) 20 | 21 | ;; A ParseResult is a (Values Value ;; semantic value 22 | ;; Input) ;; remaining unconsumed inputs 23 | 24 | ;; An Operator is one of 25 | ;; - (operator Token Natural 'left (Token Value Value -> Value)) 26 | ;; - (operator Token Natural 'nonassoc (Token Value Value -> Value)) 27 | ;; - (operator Token Natural 'n-ary (Token Value Value -> Value)) 28 | ;; - (operator Token Natural 'right (Token Value Value -> Value)) 29 | ;; - (operator Token Natural 'prefix (Token Value -> Value)) 30 | ;; - (operator Token Natural 'prefix-macro (Token Parser Input -> ParseResult)) 31 | ;; - (operator Token Natural 'postfix (Token Value -> Value)) 32 | 33 | ;; An OperatorTable is a (Token -> (Option Operator)). 34 | 35 | ;; (Non-assocative and n-ary operators are treated almost identically 36 | ;; to left-associative operators, and so must be explicitly handled in 37 | ;; the semantic-function contained in each table rule. Adjacency in 38 | ;; the `test` module shows an example: n-ary function call is exactly 39 | ;; a non-associative operator.) 40 | 41 | ;; Input 42 | ;; (Parser Input -> ParseResult) 43 | ;; (Token -> Natural) 44 | ;; (Token Value Parser Input -> ParseResult) 45 | ;; [#:k (Value Input -> X)] 46 | ;; -> X 47 | ;; where X is usually ParseResult unless `k` is supplied. 48 | (define (raw-pratt-parse tokens nud lbp led #:k [k values]) 49 | (define (parse tokens rbp k) 50 | (define-values (first-left first-tokens) (nud parse tokens)) 51 | (let loop ((left first-left) (tokens first-tokens)) 52 | (match tokens 53 | [(cons t tokens) #:when (> (lbp t) rbp) 54 | (define-values (new-left new-tokens) (led t left parse tokens)) 55 | (loop new-left new-tokens)] 56 | [_ 57 | (k left tokens)]))) 58 | (parse tokens 0 k)) 59 | 60 | ;; (Value -> Value) -> (Value Input -> ParseResult) 61 | (define (build-ast builder) 62 | (lambda (right tokens) 63 | (values (builder right) tokens))) 64 | 65 | ;; Symbol -> (Value Input -> Value) 66 | ;; Useful as a #:k argument to pratt-parse and friends. 67 | (define (ensure-no-leftover-tokens who) 68 | (lambda (value remaining-tokens) 69 | (when (pair? remaining-tokens) (error who "Leftover tokens")) 70 | value)) 71 | 72 | ;; Input 73 | ;; OperatorTable 74 | ;; OperatorTable 75 | ;; OperatorTable 76 | ;; Natural 77 | ;; (Value Value -> Value) 78 | ;; (-> Value) 79 | ;; [#:k (Value Input -> X)] 80 | ;; -> X 81 | ;; where X is usually Value unless `k` is supplied. 82 | (define (pratt-parse tokens 83 | prefix-tab 84 | postfix-tab 85 | infix-tab 86 | adjacency-bp 87 | compound? 88 | handle-compound 89 | handle-adjacency 90 | handle-eof 91 | #:k [k (ensure-no-leftover-tokens 'pratt-parse)]) 92 | 93 | ;; Parser Input -> ParseResult 94 | (define (nud parse tokens) ;; "null denotation" 95 | (match tokens 96 | ['() 97 | (values (handle-eof) '())] 98 | [(cons t tokens) 99 | (if (compound? t) 100 | (let ((ts-val (handle-compound t (lambda (ts) 101 | (parse ts 102 | 0 103 | (ensure-no-leftover-tokens 'pratt-parse)))))) 104 | (values ts-val tokens)) 105 | (match (prefix-tab t) 106 | [(operator _ bp associativity ctor) 107 | (case associativity 108 | [(prefix) (parse tokens bp (build-ast (lambda (v) 109 | ;; (log-info "prefix ~v ~v" t v) 110 | (ctor t v))))] 111 | [(prefix-macro) 112 | ;; (log-info "prefix-macro ~v ~v" t tokens) 113 | (ctor t parse tokens)] 114 | [else (error 'pratt-parse "Prefix use of non-prefix, non-prefix-macro operator ~v" t)])] 115 | [#f 116 | (values t tokens)]))])) 117 | 118 | ;; Token -> Natural 119 | (define (lbp t) ;; "left binding power" 120 | (cond [(infix-tab t) => operator-binding-power] 121 | [(postfix-tab t) => operator-binding-power] 122 | [else adjacency-bp])) 123 | 124 | ;; Token Value Parser Input -> ParseResult 125 | (define (led t left parse tokens) ;; "left denotation" 126 | (match (infix-tab t) 127 | [(operator _ bp (or 'left 'nonassoc 'n-ary) ctor) 128 | (parse tokens bp (build-ast (lambda (right) 129 | ;; (log-info "left/nonassoc/n-ary ~v ~v ~v" t left right) 130 | (ctor t left right))))] 131 | [(operator _ bp 'right ctor) 132 | (parse tokens (- bp 1) (build-ast (lambda (right) 133 | ;; (log-info "right ~v ~v ~v" t left right) 134 | (ctor t left right))))] 135 | [#f 136 | (match (postfix-tab t) 137 | [(operator _ bp _ ctor) 138 | (values (ctor t left) tokens)] 139 | [#f 140 | (parse (cons t tokens) 141 | adjacency-bp 142 | (build-ast (lambda (right) 143 | ;; (log-info "adj ~v ~v" left right) 144 | (handle-adjacency left right))))])])) 145 | 146 | (raw-pratt-parse #:k k tokens nud lbp led)) 147 | 148 | (module+ test 149 | (require rackunit) 150 | 151 | (define (finalize-app x) 152 | (match x 153 | [`(partial-app ,x) `(app ,x)] 154 | [_ x])) 155 | 156 | (define (mkunary ctor) 157 | (lambda (_t left) `(,ctor ,(finalize-app left)))) 158 | 159 | (define (mkbinary ctor) 160 | (lambda (_t left right) `(,ctor ,(finalize-app left) ,(finalize-app right)))) 161 | 162 | (define (handle-adjacency left right) 163 | (match left 164 | [`(partial-app ,xs) `(partial-app (,@xs ,right))] 165 | [f `(partial-app (,f ,right))])) 166 | 167 | (define ((assq-lookup tab) token) 168 | (cond [(assq token tab) => (lambda (entry) (apply operator entry))] 169 | [else #f])) 170 | 171 | (define (p tokens) 172 | (finalize-app 173 | (pratt-parse tokens 174 | (assq-lookup 175 | `((- 100 prefix ,(mkunary 'unary-minus)) 176 | (slurp 300 prefix-macro ,(lambda (t parse tokens) (values `(,t ,@tokens) 177 | '(z)))) 178 | (~ 300 prefix ,(mkunary 'tight-unary-minus)))) 179 | (assq-lookup 180 | `((! 300 postfix ,(mkunary 'factorial)))) 181 | (assq-lookup 182 | `((+ 20 left ,(mkbinary '+)) 183 | (- 20 left ,(mkbinary '-)) 184 | (* 30 left ,(mkbinary '*)) 185 | (/ 30 left ,(mkbinary '/)) 186 | (: 5 right ,(mkbinary 'cons)))) 187 | 200 188 | list? 189 | (lambda (ts parse) (finalize-app (parse ts))) 190 | handle-adjacency 191 | (lambda () '())))) 192 | 193 | (check-equal? (p '(a b c + d e f * g h i)) 194 | '(+ (app (a b c)) (* (app (d e f)) (app (g h i))))) 195 | 196 | (check-equal? (p '(a b c - d e f * g h i)) 197 | '(- (app (a b c)) (* (app (d e f)) (app (g h i))))) 198 | 199 | (check-equal? (p '((a b c + d e f) * g h i)) 200 | '(* (+ (app (a b c)) (app (d e f))) (app (g h i)))) 201 | 202 | (check-equal? (p '(- a)) 203 | '(unary-minus a)) 204 | 205 | (check-equal? (p '(- a d)) 206 | '(unary-minus (app (a d)))) 207 | 208 | (check-equal? (p '(- a + b)) 209 | '(+ (unary-minus a) b)) 210 | 211 | (check-equal? (p '(b c * - a d)) 212 | '(* (app (b c)) (unary-minus (app (a d))))) 213 | 214 | (check-equal? (p '(d (- a) b)) 215 | '(app (d (unary-minus a) b))) 216 | 217 | (check-equal? (p '(- a ! + b)) 218 | '(+ (unary-minus (factorial a)) b)) 219 | 220 | (check-equal? (p '(- a + b !)) 221 | '(+ (unary-minus a) (factorial b))) 222 | 223 | (check-equal? (p '(a b c ! d e f g h)) 224 | '(app (a b (factorial c) d e f g h))) 225 | 226 | (check-equal? (p '(a b c ! d e f - g h)) 227 | '(- (app (a b (factorial c) d e f)) (app (g h)))) 228 | 229 | (check-equal? (p '(a b c ! d e f ~ g h)) 230 | '(app (a b (factorial c) d e f (tight-unary-minus g) h))) 231 | 232 | (check-equal? (p '(a b c ! d e f slurp g h * foo ~ bar)) 233 | '(app (a b (factorial c) d e f (slurp g h * foo ~ bar) z))) 234 | 235 | (check-equal? (p '(a)) 'a) 236 | (check-equal? (p '(a b)) '(app (a b))) 237 | (check-equal? (p '(a b c)) '(app (a b c))) 238 | (check-equal? (p '(a b c d)) '(app (a b c d))) 239 | 240 | (check-equal? (p '(1 : 2 : 3 : ())) 241 | `(cons 1 (cons 2 (cons 3 ())))) 242 | (check-equal? (p '(1 : 2 : 3 ! : ())) 243 | `(cons 1 (cons 2 (cons (factorial 3) ())))) 244 | (check-equal? (p '(a : f g h i : 3 ! : ())) 245 | `(cons a (cons (app (f g h i)) (cons (factorial 3) ())))) 246 | 247 | (check-equal? (p '(a (+) b)) 248 | `(app (a + b))) 249 | 250 | (check-equal? (p '(-(a b))) 251 | `(unary-minus (app (a b)))) 252 | (check-equal? (p '(x + -(a b))) 253 | `(+ x (unary-minus (app (a b))))) 254 | 255 | (check-equal? (p '((a b c) d e)) 256 | '(app ((app (a b c)) d e))) 257 | ) 258 | -------------------------------------------------------------------------------- /src/something/reader.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (provide (struct-out token) 4 | (struct-out namespaced-name) 5 | 6 | read-something-forms 7 | read-something-toplevel 8 | 9 | ;; For debugging: 10 | token->raw-sexp) 11 | 12 | (require racket/match) 13 | (require (prefix-in srfi-13: (only-in srfi/13 string-contains))) 14 | 15 | (require (except-in parser-tools/lex token? token-value)) 16 | (require (prefix-in : parser-tools/lex-sre)) 17 | 18 | (struct token (pos kind value) #:prefab) 19 | (struct namespaced-name (prefix id) #:prefab) 20 | 21 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 22 | ;; Tokenization 23 | 24 | (define-lex-abbrevs 25 | [c:hex-digit (:or numeric (:/ #\a #\f #\A #\F))] 26 | [c:punctuation (:or "!" "$" "%" "&" 27 | "*" "+" "-" "/" 28 | "<" "=" ">" "?" 29 | "^" "|" "~" "@" 30 | "`" "," 31 | "\\")] 32 | [c:string-escape (:or "\\\\" 33 | "\\\"" 34 | "\\\'" 35 | "\\b" 36 | "\\n" 37 | "\\r" 38 | "\\t" 39 | "\\0" 40 | (:: "\\u" c:hex-digit c:hex-digit c:hex-digit c:hex-digit) 41 | (:: "\\x" c:hex-digit c:hex-digit))] 42 | [c:symbol-start (:or alphabetic "_" "*" "+" "-")] ;; this is getting silly 43 | [c:symbol-inner (:or c:symbol-start numeric c:punctuation)] 44 | [c:symbol-end (:or c:symbol-start numeric "!" "?")] 45 | [c:symbol-normal (:: c:symbol-start (:? (:: (:* c:symbol-inner) c:symbol-end)))] 46 | [c:symbol-quoted (:: "\'" (:* (:or c:string-escape (:~ "\\" "\'"))) "\'")] 47 | [c:symbol (:or c:symbol-normal c:symbol-quoted)] 48 | [c:namespace (:: c:symbol "::")] 49 | [c:operator-char (:or c:punctuation ":" ".")] 50 | [c:space (:or c:line-continuation (:- whitespace c:line-terminator) c:comment)] 51 | [c:line-continuation (:: "\\" 52 | (:* (:- whitespace c:line-terminator)) 53 | (:or c:comment c:line-terminator))] 54 | [c:comment (:: "//" (:* (:~ c:line-terminator)))] 55 | [c:line-terminator (:or "\n" "\r" "\u2028" "\u2029")]) 56 | 57 | (define read-token 58 | (lexer 59 | [(:: (:* c:space) c:line-terminator) 'newline] 60 | [(:+ c:space) (read-token input-port)] 61 | [(:: "#lang " (:* (:~ c:line-terminator)) c:line-terminator) (read-token input-port)] 62 | [(:: "\"" (:* (:or c:string-escape (:~ "\\" "\""))) "\"") 63 | (token start-pos 64 | 'string 65 | (unescape-string-lexeme lexeme))] 66 | [(:: (:? (:or "-" "+")) (:or "0x" "0X") (:+ c:hex-digit)) 67 | (let ((without-0x (if (memv (string-ref lexeme 0) '(#\+ #\-)) 68 | (string-append (substring lexeme 0 1) (substring lexeme 3)) 69 | (substring lexeme 2)))) 70 | (token start-pos 71 | 'number 72 | (string->number without-0x 16)))] 73 | [(:: (:? (:or "-" "+")) (:+ numeric) (:? (:: "." (:+ numeric)))) 74 | (token start-pos 75 | 'number 76 | (string->number lexeme 10))] 77 | [(:: ":" c:symbol) 78 | (token start-pos 79 | 'keyword 80 | (lexeme->symbol (substring lexeme 1)))] 81 | [(:: c:namespace (:or (:+ c:operator-char) c:symbol)) 82 | (make-namespaced-name start-pos 83 | 'identifier 84 | lexeme)] 85 | [":" (simple-token start-pos 'colon)] 86 | [(:or (:: (:- c:operator-char ":") (:* c:operator-char)) c:symbol) 87 | (token start-pos 88 | 'identifier 89 | (namespaced-name #f (lexeme->symbol lexeme)))] 90 | ["#t" (token start-pos 'literal #t)] 91 | ["#f" (token start-pos 'literal #f)] 92 | ["(" (simple-token start-pos 'oparen)] 93 | [")" (simple-token start-pos 'cparen)] 94 | ["[" (simple-token start-pos 'obrack)] 95 | ["]" (simple-token start-pos 'cbrack)] 96 | ["{" (simple-token start-pos 'obrace)] 97 | ["}" (simple-token start-pos 'cbrace)] 98 | [";" (simple-token start-pos 'semicolon)] 99 | [(eof) eof])) 100 | 101 | (define (simple-token pos kind) 102 | (token pos kind kind)) 103 | 104 | (define (lexeme->symbol lexeme) 105 | (string->symbol 106 | (if (char=? (string-ref lexeme 0) #\') 107 | (unescape-string-lexeme lexeme) 108 | lexeme))) 109 | 110 | (define (make-namespaced-name pos kind lexeme) 111 | (define ::-index (srfi-13:string-contains lexeme "::")) 112 | (define prefix (substring lexeme 0 ::-index)) 113 | (define suffix (substring lexeme (+ ::-index 2))) 114 | (token pos kind (namespaced-name (string->symbol prefix) (lexeme->symbol suffix)))) 115 | 116 | (define (unescape-string-lexeme lexeme) 117 | (unescape-escaped-string (substring lexeme 1 (- (string-length lexeme) 1)))) 118 | 119 | (define (unescape-escaped-string s) 120 | (list->string 121 | (let loop ((cs (string->list s))) 122 | (match cs 123 | ['() '()] 124 | [(list* #\\ #\\ rest) (cons #\\ (loop rest))] 125 | [(list* #\\ #\b rest) (cons #\backspace (loop rest))] 126 | [(list* #\\ #\n rest) (cons #\newline (loop rest))] 127 | [(list* #\\ #\r rest) (cons #\return (loop rest))] 128 | [(list* #\\ #\t rest) (cons #\tab (loop rest))] 129 | [(list* #\\ #\0 rest) (cons #\nul (loop rest))] 130 | [(list* #\\ #\u x1 x2 x3 x4 rest) 131 | (cons (integer->char 132 | (string->number (string x1 x2 x3 x4) 16)) 133 | (loop rest))] 134 | [(list* #\\ #\x x1 x2 rest) 135 | (cons (integer->char (string->number (string x1 x2) 16)) 136 | (loop rest))] 137 | [(list* #\\ x rest) (cons x (loop rest))] 138 | [(list* x rest) (cons x (loop rest))])))) 139 | 140 | (define (read-something-line p #:blank-line-is-eof? [blank-line-is-eof? #f]) 141 | (let loop ((tokens-rev '())) 142 | (match (read-token p) 143 | ['newline 144 | (if (null? tokens-rev) 145 | (if blank-line-is-eof? 146 | eof 147 | (loop '())) ;; we skip whitespace-only lines 148 | (reverse tokens-rev))] 149 | [token 150 | (if (eof-object? token) 151 | (if (null? tokens-rev) 152 | token 153 | (reverse tokens-rev)) 154 | (loop (cons token tokens-rev)))]))) 155 | 156 | (define (read-something-lines p) 157 | (define line (read-something-line p)) 158 | (if (eof-object? line) 159 | '() 160 | (cons line (read-something-lines p)))) 161 | 162 | (define (read-something-lines-toplevel p) 163 | (let read-more-lines ((acc '())) 164 | (match (read-something-line p #:blank-line-is-eof? (pair? acc)) 165 | [(or (? eof-object?) 166 | (list (token _ 'semicolon 'semicolon))) 167 | (reverse acc)] 168 | [line 169 | (read-more-lines (cons line acc))]))) 170 | 171 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 172 | ;; Reading 173 | 174 | (define (closer? token) 175 | (memq (token-kind token) '(cparen cbrack cbrace semicolon))) 176 | 177 | (define (non-closer? token) 178 | (not (closer? token))) 179 | 180 | (define (unclosed-grouping left-pos expected-terminator) 181 | (flush-output) ;; TODO: remove 182 | (error 'something-syntax-error 183 | "Unclosed grouping starting at ~a: expecting ~v" 184 | left-pos 185 | expected-terminator)) 186 | 187 | (define (mismatched-close-token pos expected-terminator got) 188 | (flush-output) ;; TODO: remove 189 | (error 'something-syntax-error 190 | "Mismatched close-token at ~a: expecting ~v, got ~v" 191 | pos 192 | expected-terminator 193 | got)) 194 | 195 | (define (lines-after-semicolon tokens lines) 196 | (if (null? tokens) 197 | lines 198 | (cons tokens lines))) 199 | 200 | (define (gather-lines-indented-further-than left-pos lines) 201 | (let loop ((acc-rev '()) (lines lines)) 202 | (match lines 203 | [(cons (cons (? non-closer? t) _) _) 204 | #:when (> (position-col (token-pos t)) (position-col left-pos)) 205 | (define-values (line remaining-lines) (detach-line lines)) 206 | (loop (cons line acc-rev) remaining-lines)] 207 | [(cons (cons (token _ 'semicolon _) more-tokens) more-lines) 208 | (loop acc-rev (lines-after-semicolon more-tokens more-lines))] 209 | [_ (values (reverse acc-rev) lines)]))) 210 | 211 | (define (detach-line lines 212 | #:left-pos [left-pos0 #f]) 213 | (match-define (cons (and tokens (cons (token left-pos1 _ _) _)) remaining-lines) lines) 214 | (detach-form make-form/1 '() (or left-pos0 left-pos1) tokens remaining-lines #f)) 215 | 216 | (define (make-form pos contents) 217 | (token pos 'form contents)) 218 | 219 | (define (make-form/1 pos contents) 220 | (match contents 221 | [(list item) item] 222 | [_ (make-form pos contents)])) 223 | 224 | (define (make-block pos contents) 225 | (token pos 'block contents)) 226 | 227 | (define (make-sequence pos contents) 228 | (token pos 'sequence contents)) 229 | 230 | (define (closer-for opener) 231 | (match opener 232 | ['oparen 'cparen] 233 | ['obrack 'cbrack] 234 | ['obrace 'cbrace])) 235 | 236 | (define (detach-form finish-form acc-rev left-pos tokens remaining-lines grouping-terminator) 237 | (match tokens 238 | ['() #:when (not grouping-terminator) 239 | (match remaining-lines 240 | [(cons (cons (token _ 'semicolon _) _) _) 241 | (values (finish-form left-pos (reverse acc-rev)) remaining-lines)] 242 | [_ 243 | (define-values (block-lines final-lines) 244 | (gather-lines-indented-further-than left-pos remaining-lines)) 245 | (values (finish-form left-pos 246 | (reverse (if (null? block-lines) 247 | acc-rev 248 | (cons (make-block left-pos block-lines) acc-rev)))) 249 | final-lines)])] 250 | ['() #:when grouping-terminator 251 | (match remaining-lines 252 | ['() 253 | (unclosed-grouping left-pos grouping-terminator)] 254 | [(cons new-tokens remaining-lines) 255 | (detach-form finish-form acc-rev left-pos new-tokens remaining-lines grouping-terminator)])] 256 | 257 | [(cons (token pos 'colon _) '()) 258 | (define-values (block-lines final-lines) 259 | (gather-lines-indented-further-than left-pos remaining-lines)) 260 | (detach-form finish-form 261 | (cons (make-block pos block-lines) acc-rev) 262 | left-pos 263 | '() 264 | final-lines 265 | grouping-terminator)] 266 | [(cons (token pos 'colon _) more-tokens) #:when (pair? more-tokens) 267 | (define-values (line final-lines) 268 | (detach-line #:left-pos left-pos (cons more-tokens remaining-lines))) 269 | (detach-form finish-form 270 | (cons (make-block pos (list line)) acc-rev) 271 | left-pos 272 | '() 273 | final-lines 274 | grouping-terminator)] 275 | 276 | [(cons (token pos (and opener (or 'obrace 'obrack)) _) more-tokens) 277 | (define closer (closer-for opener)) 278 | (define inner-finish-form (match opener ['obrace make-block] ['obrack make-sequence])) 279 | (let loop ((block-acc-rev '()) 280 | (lines (if (null? more-tokens) 281 | remaining-lines 282 | (cons more-tokens remaining-lines)))) 283 | (match lines 284 | ['() (unclosed-grouping left-pos closer)] 285 | [(cons (cons (token _ (== closer) _) final-tokens) final-lines) 286 | (define new-acc (cons (inner-finish-form pos (reverse block-acc-rev)) acc-rev)) 287 | (detach-form finish-form new-acc left-pos final-tokens final-lines grouping-terminator)] 288 | [(cons (cons (token _ 'semicolon _) more-tokens) more-lines) 289 | (loop block-acc-rev (lines-after-semicolon more-tokens more-lines))] 290 | [(cons (cons (? closer? (token pos got _)) _) _) 291 | (mismatched-close-token pos closer got)] 292 | [_ 293 | (define-values (line remaining-lines) (detach-line lines)) 294 | (loop (cons line block-acc-rev) remaining-lines)]))] 295 | 296 | [(cons (token pos 'oparen _) more-tokens) 297 | (define-values (form final-lines) 298 | (detach-form make-form '() left-pos more-tokens remaining-lines 'cparen)) 299 | (match final-lines 300 | ['() (unclosed-grouping left-pos 'cparen)] 301 | [(cons (cons (token _ 'cparen _) final-tokens) final-lines) 302 | (define new-acc (cons form acc-rev)) 303 | (detach-form finish-form new-acc left-pos final-tokens final-lines grouping-terminator)] 304 | [(cons (cons (? closer? (token pos got _)) _) _) 305 | (mismatched-close-token pos 'cparen got)])] 306 | 307 | [(cons (? closer?) more-tokens) 308 | (values (finish-form left-pos (reverse acc-rev)) (cons tokens remaining-lines))] 309 | 310 | [(cons other more-tokens) 311 | (detach-form finish-form 312 | (cons other acc-rev) 313 | left-pos 314 | more-tokens 315 | remaining-lines 316 | grouping-terminator)])) 317 | 318 | (define (extract-forms lines) 319 | (match lines 320 | ['() '()] 321 | [(cons (cons (token _ 'semicolon _) more-tokens) more-lines) 322 | (extract-forms (lines-after-semicolon more-tokens more-lines))] 323 | [(cons (cons (? closer? (token pos got _)) more-tokens) more-lines) 324 | (error 'something-syntax-error 325 | "Unexpected close-token ~v at ~a" 326 | got 327 | pos)] 328 | [lines 329 | (define-values (line remaining-lines) (detach-line lines)) 330 | (cons line (extract-forms remaining-lines))])) 331 | 332 | (define (read-something-forms [p (current-input-port)]) 333 | (port-count-lines! p) 334 | (extract-forms (read-something-lines p))) 335 | 336 | (define (read-something-toplevel [p (current-input-port)]) 337 | (port-count-lines! p) 338 | (extract-forms (read-something-lines-toplevel p))) 339 | 340 | ;; Useful for debugging. 341 | (define (token->raw-sexp t) 342 | (match t 343 | [(token _ 'form kids) (map token->raw-sexp kids)] 344 | [(token _ 'block kids) (cons '#%block (map token->raw-sexp kids))] 345 | [(token _ 'sequence kids) (cons '#%seq (map token->raw-sexp kids))] 346 | [(token _ _ (namespaced-name #f id)) id] 347 | [(token _ _ (namespaced-name ns id)) `(#%ns ,ns ,id)] 348 | [(token _ 'keyword val) (string->keyword (symbol->string val))] 349 | [(token _ _ val) val])) 350 | 351 | (module+ main 352 | (local-require racket/pretty) 353 | (pretty-print 354 | (map 355 | token->raw-sexp 356 | (read-something-forms)))) 357 | -------------------------------------------------------------------------------- /src/something/base.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (provide (except-out (all-from-out racket/base) 4 | define 5 | define-values 6 | begin-for-syntax 7 | define-syntax 8 | syntax 9 | require 10 | provide 11 | struct 12 | syntax-case 13 | with-syntax 14 | let 15 | let* 16 | letrec 17 | when 18 | unless 19 | cond 20 | module+ 21 | if 22 | #%top-interaction 23 | with-handlers 24 | parameterize) 25 | (except-out (all-from-out racket/match) match) 26 | #%seq 27 | block 28 | |.| 29 | def-operator 30 | get-operator-table! 31 | dump-operator-table! 32 | #%rewrite-infix 33 | #%rewrite-body 34 | #%rewrite-body* 35 | #%no-infix 36 | rec 37 | (rename-out [something-define def] 38 | [something-define-values def-values] 39 | [something-begin-for-syntax begin-for-syntax] 40 | [something-define-syntax def-syntax] 41 | [something-require require] 42 | [something-provide provide] 43 | [something-match match] 44 | [something-struct struct] 45 | [something-syntax-case syntax-case] 46 | [something-with-syntax with-syntax] 47 | [something-syntax syntax] 48 | [something-map map*] 49 | [something-let let] 50 | [something-let* let*] 51 | [something-letrec letrec] 52 | [something-when when] 53 | [something-unless unless] 54 | [something-cond cond] 55 | [something-module+ module+] 56 | [something-if if] 57 | [something-top-interaction #%top-interaction] 58 | [something-with-handlers with-handlers] 59 | [something-parameterize parameterize])) 60 | 61 | (require racket/match) 62 | (require (only-in racket/list split-at-right)) 63 | 64 | (require (for-syntax racket)) 65 | (require (for-syntax syntax/stx)) 66 | (require (for-syntax syntax/id-table)) 67 | (require (for-syntax "pratt.rkt")) 68 | (require (for-syntax syntax/srcloc)) 69 | 70 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 71 | 72 | (define-for-syntax operators 73 | (begin ;; (log-info "creating fresh operator table") 74 | (make-free-id-table))) 75 | 76 | (define-for-syntax valid-associativities 77 | '(prefix prefix-macro postfix left right nonassoc n-ary statement-macro)) 78 | 79 | (define-for-syntax (macro? stx) 80 | (and (identifier? stx) 81 | (syntax-local-value stx (lambda () #f)))) 82 | 83 | (define-syntax (#%no-infix stx) 84 | (raise-syntax-error #f "Use of #%no-infix survived infixification" stx)) 85 | 86 | (define-syntax (partial-app stx) 87 | (raise-syntax-error #f "Internal error: partially-built application escaped #%rewrite-infix" stx)) 88 | 89 | (define-for-syntax (->syntax ctx sexp) 90 | (datum->syntax ctx sexp ctx)) 91 | 92 | (define-for-syntax (finalize-app stx) 93 | (syntax-case stx (partial-app) 94 | [(partial-app . vals) 95 | #'vals] 96 | [_ 97 | stx])) 98 | 99 | (define-for-syntax (rewrite-infix stx) 100 | ;; (log-info "APP --> ~v" (syntax->datum stx)) 101 | (define result 102 | (finalize-app 103 | (syntax-case stx (#%no-infix) 104 | [(#%no-infix term) 105 | (syntax/loc stx term)] 106 | [(special-id body ...) 107 | (and (identifier? #'special-id) 108 | (or (free-identifier=? #'special-id #'block) 109 | (free-identifier=? #'special-id #'#%seq))) 110 | (->syntax stx (cons #'special-id (map rewrite-infix (syntax->list #'(body ...)))))] 111 | [(piece) 112 | (->syntax stx (list (rewrite-infix #'piece)))] 113 | [pieces 114 | (stx-pair? #'pieces) 115 | (let ((result (pratt-parse (syntax->list #'pieces) 116 | (operator-table '(prefix prefix-macro)) 117 | (operator-table '(postfix)) 118 | (operator-table '(left right nonassoc n-ary)) 119 | 1000 120 | (lambda (stx) (pair? (syntax->list stx))) 121 | (lambda (stx parse) (rewrite-infix stx)) 122 | (lambda (left right) 123 | (syntax-case left (partial-app) 124 | [(partial-app . xs) 125 | (->syntax left `(,#'partial-app ,@(syntax->list #'xs) ,right))] 126 | [_ 127 | (->syntax left `(,#'partial-app ,left ,right))])) 128 | (lambda () eof)))) 129 | (if (eof-object? result) 130 | #''() 131 | result)) 132 | ] 133 | [piece 134 | #'piece]))) 135 | ;; (log-info "<-- APP ~v" (if (syntax? result) (syntax->datum result) result)) 136 | result) 137 | 138 | (define-for-syntax (same-binding-power? op-stx binding-power) 139 | (for/or [(op (in-set (free-id-table-ref operators op-stx set)))] 140 | (= (operator-binding-power op) binding-power))) 141 | 142 | (define-for-syntax (finalize-unary-subterm handler) 143 | (lambda (t v) (handler t (finalize-app v)))) 144 | (define-for-syntax (finalize-binary-subterms handler) 145 | (lambda (t l r) (handler t (finalize-app l) (finalize-app r) l))) 146 | 147 | (define-for-syntax (build-operator id-stx binding-power associativity handler-stx) 148 | (operator id-stx 149 | binding-power 150 | associativity 151 | (case associativity 152 | [(left right) 153 | (finalize-binary-subterms 154 | (lambda (op-stx left right _raw-left) 155 | (if (eof-object? right) 156 | (->syntax left (list left op-stx)) 157 | (->syntax handler-stx (list handler-stx left right)))))] 158 | [(nonassoc) 159 | (finalize-binary-subterms 160 | (lambda (op-stx left right raw-left) 161 | (if (eof-object? right) 162 | (->syntax left (list left op-stx)) 163 | (let ((result (->syntax left (list handler-stx left right)))) 164 | (syntax-case raw-left () 165 | [(op _ _) 166 | (same-binding-power? #'op binding-power) 167 | (raise-syntax-error 168 | #f 169 | (format "Cannot chain non-associative operators ~a and ~a" 170 | (syntax-e op-stx) 171 | (syntax-e #'op)) 172 | result)] 173 | [_ result])))))] 174 | [(n-ary) 175 | (finalize-binary-subterms 176 | (lambda (op-stx left right raw-left) 177 | (syntax-case raw-left () 178 | [(op args ...) 179 | (eq? #'op handler-stx) 180 | (if (eof-object? right) 181 | (raise-syntax-error #f 182 | (format "Missing n-ary argument to operator ~a" 183 | (syntax-e op-stx)) 184 | left) 185 | (->syntax left `(,handler-stx ,@(syntax->list #'(args ...)) ,right)))] 186 | [(op args ...) 187 | (same-binding-power? #'op binding-power) 188 | (raise-syntax-error 189 | #f 190 | (format "Cannot chain non-associative operators ~a and ~a" 191 | (syntax-e op-stx) 192 | (syntax-e #'op)) 193 | (->syntax left (list handler-stx left right)))] 194 | [_ 195 | (if (eof-object? right) 196 | (->syntax left (list left op-stx)) 197 | (->syntax left (list handler-stx left right)))])))] 198 | [(prefix-macro) 199 | (lambda (op-stx parse tokens) 200 | (define tokens-stx (->syntax op-stx (cons handler-stx tokens))) 201 | (when (not (macro? handler-stx)) 202 | (raise-syntax-error #f 203 | (format "parser-macro handler is not a macro ~a" 204 | (syntax-e op-stx)) 205 | tokens-stx)) 206 | (define (user-parse tokens-stx 207 | [rbp 0] 208 | [k (ensure-no-leftover-tokens 'prefix-macro-user-parse)]) 209 | (parse (syntax-case tokens-stx () 210 | [(token) (syntax->list #'(token))] 211 | [(token ...) (syntax->list #'((token ...)))]) 212 | rbp 213 | (lambda (v toks) 214 | (k (finalize-app v) toks)))) 215 | (call-with-values 216 | (lambda () ((syntax-local-value handler-stx) tokens-stx user-parse)) 217 | (case-lambda 218 | [(value tokens) (values value (if (list? tokens) tokens (syntax->list tokens)))] 219 | [(value) (values value '())])))] 220 | [(prefix postfix) 221 | (finalize-unary-subterm 222 | (lambda (op-stx v) 223 | (->syntax v (list handler-stx v))))] 224 | [(statement-macro) 225 | (lambda (stx) 226 | (->syntax stx (cons handler-stx stx)))]))) 227 | 228 | (define-for-syntax (define-operator! id-stx binding-power-stx associativity-stx handler-stx) 229 | (define binding-power (syntax-e binding-power-stx)) 230 | (define associativity (syntax-e associativity-stx)) 231 | (case associativity 232 | [(prefix-macro statement-macro) 233 | (when (not (eq? binding-power #f)) 234 | (raise-syntax-error #f 235 | (format "Binding power for prefix-macro must be #f; got ~v" binding-power) 236 | binding-power-stx))] 237 | [else 238 | (when (not (and (integer? binding-power) (positive? binding-power))) 239 | (raise-syntax-error #f 240 | (format "Binding power must be positive integer; got ~v" binding-power) 241 | binding-power-stx))]) 242 | (when (not (memq associativity valid-associativities)) 243 | (raise-syntax-error #f 244 | (format "Associativity must be one of ~a; got ~v" 245 | valid-associativities 246 | associativity) 247 | associativity-stx)) 248 | (define op (build-operator id-stx binding-power associativity handler-stx)) 249 | (define associativities-to-clear 250 | (case associativity 251 | [(prefix prefix-macro) '(prefix prefix-macro)] 252 | [(postfix) '(postfix)] 253 | [(left right nonassoc n-ary) '(left right nonassoc n-ary)] 254 | [(statement-macro) '(statement-macro)])) 255 | (define existing-set 256 | (for/set [(op (in-set (free-id-table-ref operators id-stx set))) 257 | #:when (not (memq (operator-associativity op) associativities-to-clear))] 258 | op)) 259 | ;; (log-info "defining operator ~v" id-stx) 260 | (free-id-table-set! operators 261 | id-stx 262 | (set-add existing-set op))) 263 | 264 | (define-for-syntax (undefine-operator! id-stx) 265 | ;; (log-info "undefining operator ~v" id-stx) 266 | (free-id-table-remove! operators id-stx)) 267 | 268 | (define-syntax-rule (get-operator-table! fn) 269 | (begin-for-syntax (fn operators))) 270 | 271 | (define-syntax (dump-operator-table! stx) 272 | (syntax-case stx () 273 | [(_) 274 | #`(get-operator-table! 275 | (lambda (table) 276 | (local-require racket/base) 277 | (local-require racket/pretty) 278 | (eprintf "~aOperator table:\n" #,(source-location->prefix stx)) 279 | (free-id-table-for-each 280 | table 281 | (lambda (id ops) 282 | (for [(op (in-set ops))] 283 | (match-define (operator _ bp associativity _) op) 284 | (eprintf " ~a\t~a\t~a\t~a\n" 285 | bp 286 | associativity 287 | (syntax-e id) 288 | (source-location->string id)))))))])) 289 | 290 | (define-for-syntax (operator-table associativities) 291 | (lambda (id-stx) 292 | ;; (log-info " - looking up ~v for ~v" id-stx associativities) 293 | (and (identifier? id-stx) 294 | (let ((ops (free-id-table-ref operators id-stx set))) 295 | (for/or [(op (in-set ops))] 296 | ;; (log-info " - comparing ~v with ~v" id-stx op) 297 | (and op (memq (operator-associativity op) associativities) op)))))) 298 | 299 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 300 | 301 | (define-syntax (#%rewrite-infix stx) 302 | ;; (log-info "#%rewrite-infix(~v) --> ~v" (syntax-local-phase-level) stx) 303 | (define result 304 | (syntax-case stx () 305 | [(_ token) (rewrite-infix #'token)] 306 | [(_ . tokens) (rewrite-infix #'tokens)])) 307 | ;; (log-info "<-- #%rewrite-infix(~v) ~v" (syntax-local-phase-level) (syntax->datum result)) 308 | result) 309 | 310 | (define-syntax (#%rewrite-body stx) 311 | (syntax-case stx () 312 | [(_ . mores) 313 | (quasisyntax/loc #'mores (#%rewrite-body* () mores))])) 314 | 315 | (define-syntax (#%rewrite-body* stx) 316 | (define statement-macro-table (operator-table '(statement-macro))) 317 | (syntax-case stx () 318 | [(_ heads ((id e ...) more ...)) 319 | (statement-macro-table #'id) 320 | (let ((op (statement-macro-table #'id))) ;; TODO: avoid redundant lookup 321 | ((operator-handler op) (quasisyntax/loc stx (heads ((id e ...) more ...)))))] 322 | [(_ heads ()) 323 | (quasisyntax/loc stx (begin . heads))] 324 | [(_ (head ...) (e . mores)) 325 | (quasisyntax/loc #'mores (#%rewrite-body* (head ... (#%rewrite-infix e)) mores))])) 326 | 327 | (define-syntax (def-operator stx [parse #f]) ;; optional parse -> can be used in both contexts 328 | (syntax-case stx () 329 | [(_ id binding-power associativity handler) 330 | #'(begin-for-syntax (define-operator! #'id #'binding-power #'associativity #'handler))] 331 | [(_ id) 332 | #'(begin-for-syntax (undefine-operator! #'id))])) 333 | (def-operator def-operator #f prefix-macro def-operator) 334 | 335 | (define-match-expander #%seq 336 | (syntax-id-rules () 337 | [(_ pat ...) (list pat ...)] 338 | [_ (list)]) 339 | (syntax-id-rules () 340 | [(_ exp ...) (#%app list exp ...)] 341 | [_ (#%app list)])) 342 | 343 | (define-syntax (block stx) 344 | (syntax-case stx () 345 | [(_ clause ...) 346 | (with-syntax ([(transformed-clause ...) 347 | (map (lambda (clause-stx) 348 | (syntax-case clause-stx (block) 349 | [(pat ... (block body ...)) 350 | #'[(list pat ...) (#%rewrite-body body ...)]] 351 | [(block body ...) 352 | #'[(list) (#%rewrite-body body ...)]])) 353 | (syntax->list #'(clause ...)))]) 354 | #'(match-lambda* transformed-clause ...))])) 355 | 356 | (def-operator |.| 900 left |.|) 357 | (define-syntax-rule (|.| f v) (f v)) 358 | 359 | (def-operator something-define #f prefix-macro something-define) 360 | (require (for-syntax syntax/strip-context)) 361 | (define-syntax (something-define stx [parse #f]) 362 | (syntax-case stx (block) 363 | [(_ (f v ...) (block body ...)) 364 | #'(define (f v ...) (#%rewrite-body body ...))] 365 | [(_ f v0 v ... (block body ...)) 366 | #'(define (f v0 v ...) (#%rewrite-body body ...))] 367 | [(_ v (block e)) 368 | #'(define v (#%rewrite-infix e))])) 369 | 370 | (define-syntax (something-define-values stx) 371 | (syntax-case stx (block) 372 | [(_ var ... (block body ...)) 373 | #'(define-values (var ...) (let () (#%rewrite-body body ...)))])) 374 | 375 | (def-operator something-begin-for-syntax #f prefix-macro something-begin-for-syntax) 376 | (define-syntax (something-begin-for-syntax stx [parse #f]) 377 | (syntax-case stx (block) 378 | [(_ (block body ...)) 379 | (quasisyntax/loc stx 380 | (begin-for-syntax (#,(syntax-shift-phase-level #'#%rewrite-body 1) body ...)))])) 381 | 382 | (def-operator something-define-syntax #f prefix-macro something-define-syntax) 383 | (define-syntax (something-define-syntax stx [parse #f]) 384 | (syntax-case stx (block) 385 | [(_ f v0 v ... (block body ...)) 386 | (quasisyntax/loc stx 387 | (define-syntax (f v0 v ...) (#,(syntax-shift-phase-level #'#%rewrite-body 1) body ...)))] 388 | [(_ v e) 389 | (quasisyntax/loc stx 390 | (define-syntax v 391 | (#,(syntax-shift-phase-level #'#%rewrite-infix 1) e)))])) 392 | 393 | (def-operator something-require #f prefix-macro something-require) 394 | (define-syntax (something-require stx [parse #f]) 395 | ;; optional "parse", making this a dual macro, e.g. for support for 396 | ;; simple .racketrc/interactive usage 397 | (syntax-case stx (block require) 398 | [(_ w ... (block v ...)) 399 | #'(require w ... v ...)] 400 | [(_ w ...) 401 | #'(require w ...)])) 402 | 403 | (def-operator something-provide #f prefix-macro something-provide) 404 | (define-syntax (something-provide stx [parse #f]) 405 | (syntax-case stx (block) 406 | [(_ w ... (block v ...)) 407 | #'(provide w ... v ...)] 408 | [(_ w ...) 409 | #'(provide w ...)])) 410 | 411 | (def-operator something-match #f prefix-macro something-match) 412 | (define-syntax (something-match stx parse) 413 | (syntax-case stx (block) 414 | [(_ e ... (block (pat-piece ... (block body ...)) ...)) 415 | #`(match (#%rewrite-infix e ...) 416 | #,@(map (lambda (clause-stx) 417 | (syntax-case clause-stx () 418 | [((p ...) (body ...)) 419 | #`[#,(parse #'(p ...)) 420 | (#%rewrite-body body ...)]])) 421 | (syntax->list #'([(pat-piece ...) (body ...)] ...))))])) 422 | 423 | (define-syntax (something-struct stx) 424 | (syntax-case stx (block) 425 | [(_ name super (field ...) (block rest ...)) 426 | #'(struct name super (field ...) rest ...)] 427 | [(_ name super (field ...) rest ...) 428 | #'(struct name super (field ...) rest ...)] 429 | [(_ name (field ...) (block rest ...)) 430 | #'(struct name (field ...) rest ...)] 431 | [(_ name (field ...) rest ...) 432 | #'(struct name (field ...) rest ...)])) 433 | 434 | (def-operator something-syntax-case #f prefix-macro something-syntax-case) 435 | (define-syntax (something-syntax-case stx parse) 436 | (syntax-case stx (block) 437 | [(_ s (lit ...) (block (pat ... (block body ...)) ...)) 438 | (quasisyntax/loc stx 439 | (syntax-case s (lit ...) 440 | #,@(map (lambda (clause-stx) 441 | (syntax-case clause-stx () 442 | [((pat ...) (body ...)) 443 | #`(#,(parse #'(pat ...)) #,@(map parse (syntax->list #'(body ...))))])) 444 | (syntax->list #'([(pat ...) (body ...)] ...)))))])) 445 | 446 | (def-operator something-with-syntax #f prefix-macro something-with-syntax) 447 | (define-syntax (something-with-syntax stx parse) 448 | (syntax-case stx (block) 449 | [(_ (block (pattern (block stx-expr)) ...) (block body ...)) 450 | #'(with-syntax ([pattern (#%rewrite-infix stx-expr)] ...) (#%rewrite-body body ...))])) 451 | 452 | (def-operator something-syntax #f prefix-macro something-syntax) 453 | (define-syntax (something-syntax stx parse) 454 | (syntax-case stx (block) 455 | [(_ template) 456 | (syntax/loc stx (syntax template))])) 457 | 458 | (define (something-map arg . args) 459 | (define-values (lists f-list) (split-at-right (cons arg args) 1)) 460 | (apply map (car f-list) lists)) 461 | 462 | (define-for-syntax (expand-let-like kind-stx expr-stx) 463 | (syntax-case expr-stx (block) 464 | [(_ loop (name init) ... (block . bodies)) 465 | (identifier? #'loop) 466 | #`(#,kind-stx loop ((name init) ...) (#%rewrite-body . bodies))] 467 | [(_ (name init) ... (block . bodies)) 468 | #`(#,kind-stx ((name init) ...) (#%rewrite-body . bodies))])) 469 | 470 | (define-syntax (something-let stx) (expand-let-like #'let stx)) 471 | (define-syntax (something-let* stx) (expand-let-like #'let* stx)) 472 | (define-syntax (something-letrec stx) (expand-let-like #'letrec stx)) 473 | 474 | (define-syntax (rec stx) 475 | (syntax-case stx (block) 476 | [(rec id (block body ...)) 477 | (syntax/loc stx (letrec ((id (block body ...))) id))])) 478 | 479 | (def-operator something-when #f prefix-macro something-when) 480 | (define-syntax (something-when stx parse) 481 | (syntax-case stx (block) 482 | [(something-when test ... (block body ...)) 483 | (syntax/loc stx (when (#%rewrite-infix test ...) (#%rewrite-body body ...)))])) 484 | 485 | (def-operator something-unless #f prefix-macro something-unless) 486 | (define-syntax (something-unless stx parse) 487 | (syntax-case stx (block) 488 | [(something-unless test ... (block body ...)) 489 | (syntax/loc stx (unless (#%rewrite-infix test ...) (#%rewrite-body body ...)))])) 490 | 491 | (def-operator something-cond #f prefix-macro something-cond) 492 | (define-syntax (something-cond stx parse) 493 | (syntax-case stx (block something-when something-unless else) 494 | [(_ (block (something-when test ... (block body ...)) clauses ...)) 495 | (syntax/loc stx (if (#%rewrite-infix test ...) 496 | (#%rewrite-body body ...) 497 | (#%rewrite-infix (something-cond (block clauses ...)))))] 498 | [(_ (block (something-unless test ... (block body ...)) clauses ...)) 499 | (syntax/loc stx (if (not (#%rewrite-infix test ...)) 500 | (#%rewrite-body body ...) 501 | (#%rewrite-infix (something-cond (block clauses ...)))))] 502 | [(_ (block (else (block body ...)))) 503 | (syntax/loc stx (#%rewrite-body body ...))] 504 | [(_ (block)) 505 | (raise-syntax-error #f "cond: no matching clause" stx)])) 506 | 507 | (define-syntax (something-module+ stx) 508 | (syntax-case stx (block) 509 | [(something-module+ modname (block . bodies)) 510 | (syntax/loc stx (module+ modname (#%rewrite-body . bodies)))])) 511 | 512 | (def-operator something-if #f prefix-macro something-if) 513 | (define-syntax (something-if stx parse) 514 | (syntax-case stx (block) 515 | [(something-if test ... (block then else)) 516 | (syntax/loc stx (if (#%rewrite-infix (test ...)) (#%rewrite-infix then) (#%rewrite-infix else)))])) 517 | 518 | (define-syntax (something-top-interaction stx) 519 | (syntax-case stx () 520 | [(_ . form) 521 | #'(#%rewrite-infix form)])) 522 | 523 | (def-operator something-with-handlers #f prefix-macro something-with-handlers) 524 | (define-syntax (something-with-handlers stx [parse #f]) 525 | (syntax-case stx (block something-when) 526 | [(_ (block (something-when test-proc id (block handler ...)) ... body)) 527 | (syntax/loc stx 528 | (with-handlers [((#%rewrite-infix test-proc) 529 | (lambda (id) (#%rewrite-body handler ...))) ...] 530 | (#%rewrite-infix body)))])) 531 | 532 | (define-syntax (something-parameterize stx) 533 | (syntax-case stx (block) 534 | [(_ (block (v (block e ...)) ...) (block body ...)) 535 | (quasisyntax/loc stx 536 | (parameterize ((v (begin e ...)) ...) (#%rewrite-body body ...)))])) 537 | -------------------------------------------------------------------------------- /gpl.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------