demonstrating non-() stx
This commit is contained in:
parent
72d1c9d58b
commit
619fbb9217
|
@ -1,4 +1,6 @@
|
||||||
TODO @ everywhere
|
DONE @ everywhere
|
||||||
|
|
||||||
|
DONE demonstrate how @ everywhere enables non-() macros like datalog
|
||||||
|
|
||||||
DONE {} particular => (#%braces)
|
DONE {} particular => (#%braces)
|
||||||
|
|
||||||
|
|
24
remix/datalog0.rkt
Normal file
24
remix/datalog0.rkt
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
#lang racket/base
|
||||||
|
(require datalog/runtime
|
||||||
|
(prefix-in stx: datalog/stx)
|
||||||
|
(for-syntax racket/base
|
||||||
|
remix/raw-stx0
|
||||||
|
datalog/private/compiler
|
||||||
|
datalog/parse
|
||||||
|
syntax/parse))
|
||||||
|
|
||||||
|
(define-syntax (datalog stx)
|
||||||
|
(syntax-parse stx
|
||||||
|
[(_ thy:expr s:str ...)
|
||||||
|
(with-syntax
|
||||||
|
([(stmt ...)
|
||||||
|
(compile-program
|
||||||
|
(parse-program
|
||||||
|
(syntax-strings->input-port
|
||||||
|
(syntax-source stx)
|
||||||
|
(syntax->list #'(s ...)))))])
|
||||||
|
(syntax/loc stx
|
||||||
|
(stx:datalog thy stmt ...)))]))
|
||||||
|
|
||||||
|
(provide make-theory
|
||||||
|
datalog)
|
45
remix/raw-stx0.rkt
Normal file
45
remix/raw-stx0.rkt
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
#lang racket/base
|
||||||
|
(require racket/match)
|
||||||
|
|
||||||
|
(define (syntax-strings->input-port name ss)
|
||||||
|
(define line 1)
|
||||||
|
(define col 0)
|
||||||
|
(define pos 1)
|
||||||
|
(define current-idx #f)
|
||||||
|
(define current-bs #f)
|
||||||
|
(define next-ss ss)
|
||||||
|
|
||||||
|
(define (read-in bs)
|
||||||
|
(cond
|
||||||
|
[(not current-bs)
|
||||||
|
(match next-ss
|
||||||
|
['() eof]
|
||||||
|
[(cons ss more-ss)
|
||||||
|
(set! line (syntax-line ss))
|
||||||
|
(set! col (syntax-column ss))
|
||||||
|
(set! pos (syntax-position ss))
|
||||||
|
(set! current-bs (string->bytes/utf-8 (syntax->datum ss)))
|
||||||
|
(set! current-idx 0)
|
||||||
|
(set! next-ss more-ss)
|
||||||
|
(read-in bs)])]
|
||||||
|
[(< current-idx (bytes-length current-bs))
|
||||||
|
(define how-many
|
||||||
|
(min (bytes-length bs)
|
||||||
|
(- (bytes-length current-bs)
|
||||||
|
current-idx)))
|
||||||
|
(define end (+ current-idx how-many))
|
||||||
|
(bytes-copy! bs 0 current-bs current-idx end)
|
||||||
|
(set! current-idx end)
|
||||||
|
(set! col (+ col how-many))
|
||||||
|
(set! pos (+ pos how-many))
|
||||||
|
how-many]
|
||||||
|
[else
|
||||||
|
(set! current-bs #f)
|
||||||
|
(read-in bs)]))
|
||||||
|
(define (get-location)
|
||||||
|
(values line col pos))
|
||||||
|
|
||||||
|
(make-input-port name read-in #f void #f #f
|
||||||
|
get-location void #f #f))
|
||||||
|
|
||||||
|
(provide syntax-strings->input-port)
|
|
@ -6,9 +6,13 @@
|
||||||
(def a 40)
|
(def a 40)
|
||||||
(def b 2)
|
(def b 2)
|
||||||
(+ a b))
|
(+ a b))
|
||||||
|
(module+ test
|
||||||
|
x)
|
||||||
|
|
||||||
(def (f x y)
|
(def (f x y)
|
||||||
(+ [(def z (+ x x)) z] y))
|
(+ [(def z (+ x x)) z] y))
|
||||||
|
(module+ test
|
||||||
|
(f x x))
|
||||||
|
|
||||||
(def (g x)
|
(def (g x)
|
||||||
(cond
|
(cond
|
||||||
|
@ -16,10 +20,18 @@
|
||||||
(def z (/ x 2))
|
(def z (/ x 2))
|
||||||
[(< z 100) "div 100"]
|
[(< z 100) "div 100"]
|
||||||
[#:else "other"]))
|
[#:else "other"]))
|
||||||
|
|
||||||
(module+ test
|
(module+ test
|
||||||
x
|
|
||||||
(f x x)
|
|
||||||
(g 50)
|
(g 50)
|
||||||
(g 199)
|
(g 199)
|
||||||
(g 200))
|
(g 200))
|
||||||
|
|
||||||
|
(require remix/datalog0)
|
||||||
|
(def graph (make-theory))
|
||||||
|
@datalog[graph]{
|
||||||
|
edge(a, b). edge(b, c). edge(c, d). edge(d, a).
|
||||||
|
path(X, Y) :- edge(X, Y).
|
||||||
|
path(X, Y) :- edge(X, Z), path(Z, Y).
|
||||||
|
path(X, Y)?
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user