parse operators
This commit is contained in:
parent
acb3bce1e7
commit
3e96a1e66e
|
@ -1,6 +1,7 @@
|
||||||
#lang racket/base
|
#lang racket/base
|
||||||
|
|
||||||
(require "macro2.rkt"
|
(require "macro2.rkt"
|
||||||
|
"operator.rkt"
|
||||||
(for-syntax syntax/parse
|
(for-syntax syntax/parse
|
||||||
"literals.rkt"
|
"literals.rkt"
|
||||||
"parse2.rkt"
|
"parse2.rkt"
|
||||||
|
@ -20,3 +21,10 @@
|
||||||
(parse #'(code ...)))])
|
(parse #'(code ...)))])
|
||||||
(do-parse)))
|
(do-parse)))
|
||||||
#'rest)])))
|
#'rest)])))
|
||||||
|
|
||||||
|
(provide honu-+)
|
||||||
|
(define-honu-operator/syntax honu-+ 1
|
||||||
|
(lambda (left right)
|
||||||
|
(with-syntax ([left left]
|
||||||
|
[right right])
|
||||||
|
#'(+ left right))))
|
||||||
|
|
11
collects/honu/core/private/operator.rkt
Normal file
11
collects/honu/core/private/operator.rkt
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#lang racket/base
|
||||||
|
|
||||||
|
(require (for-syntax racket/base
|
||||||
|
"transformer.rkt"
|
||||||
|
syntax/parse))
|
||||||
|
|
||||||
|
(provide define-honu-operator/syntax)
|
||||||
|
(define-syntax (define-honu-operator/syntax stx)
|
||||||
|
(syntax-parse stx
|
||||||
|
[(_ name precedence function)
|
||||||
|
#'(define-syntax name (make-honu-operator precedence function))]))
|
|
@ -42,6 +42,11 @@
|
||||||
(with-syntax ([check check])
|
(with-syntax ([check check])
|
||||||
#'(syntax-local-value check #'check (lambda () #f)))))
|
#'(syntax-local-value check #'check (lambda () #f)))))
|
||||||
|
|
||||||
|
(define (bound-to-operator? check)
|
||||||
|
(let ([value (get-value check)])
|
||||||
|
(debug "operator? ~a ~a\n" check value)
|
||||||
|
(transformer:honu-operator? value)))
|
||||||
|
|
||||||
(define (bound-to-macro? check)
|
(define (bound-to-macro? check)
|
||||||
(let ([value (get-value check)])
|
(let ([value (get-value check)])
|
||||||
(debug "macro? ~a ~a\n" check value)
|
(debug "macro? ~a ~a\n" check value)
|
||||||
|
@ -54,6 +59,10 @@
|
||||||
(and (identifier? something)
|
(and (identifier? something)
|
||||||
(bound-to-macro? something)))
|
(bound-to-macro? something)))
|
||||||
|
|
||||||
|
(define (honu-operator? something)
|
||||||
|
(and (identifier? something)
|
||||||
|
(bound-to-operator? something)))
|
||||||
|
|
||||||
(define (semicolon? what)
|
(define (semicolon? what)
|
||||||
(define-literal-set check (semicolon))
|
(define-literal-set check (semicolon))
|
||||||
(and (identifier? what)
|
(and (identifier? what)
|
||||||
|
@ -81,16 +90,32 @@
|
||||||
current))])
|
current))])
|
||||||
parsed
|
parsed
|
||||||
(more-parsing . rest)))))]
|
(more-parsing . rest)))))]
|
||||||
|
[(honu-operator? #'head)
|
||||||
|
(define new-precedence (transformer:honu-operator-ref (syntax-local-value #'head) 0))
|
||||||
|
(define operator-transformer (transformer:honu-operator-ref (syntax-local-value #'head) 1))
|
||||||
|
(define association 'left)
|
||||||
|
(define check
|
||||||
|
(case association
|
||||||
|
[(left) >]
|
||||||
|
[(right) >=]))
|
||||||
|
(printf "new precedence ~a\n" new-precedence)
|
||||||
|
(if (check new-precedence precedence)
|
||||||
|
(do-parse #'(rest ...) new-precedence
|
||||||
|
(lambda (stuff)
|
||||||
|
(operator-transformer left stuff))
|
||||||
|
current)
|
||||||
|
(left current))]
|
||||||
[(semicolon? #'head)
|
[(semicolon? #'head)
|
||||||
(with-syntax ([so-far left])
|
(with-syntax ([so-far left])
|
||||||
#'(splicing-let-syntax ([more (lambda (stx)
|
#'(splicing-let-syntax ([more (lambda (stx)
|
||||||
(parse #'(rest ...)))])
|
(parse #'(rest ...)))])
|
||||||
so-far (more)))]
|
so-far (more)))]
|
||||||
|
|
||||||
[(identifier? #'head)
|
[(identifier? #'head)
|
||||||
(do-parse #'(rest ...) precedence #'head current)]
|
(do-parse #'(rest ...) precedence #'head current)]
|
||||||
[else (syntax-parse #'head
|
[else (syntax-parse #'head
|
||||||
#:literal-sets (cruft)
|
#:literal-sets (cruft)
|
||||||
|
[x:number (do-parse #'(rest ...)
|
||||||
|
precedence (left #'x) current)]
|
||||||
[(#%parens args ...)
|
[(#%parens args ...)
|
||||||
(debug "function call ~a\n" left)
|
(debug "function call ~a\n" left)
|
||||||
(with-syntax ([left left])
|
(with-syntax ([left left])
|
||||||
|
@ -101,7 +126,7 @@
|
||||||
|
|
||||||
)]))
|
)]))
|
||||||
|
|
||||||
(do-parse input 0 #'(void) #'(void)))
|
(do-parse input 0 (lambda (x) x) #'(void)))
|
||||||
|
|
||||||
(define (parse2 forms)
|
(define (parse2 forms)
|
||||||
(debug "parse forms ~a\n" forms)
|
(debug "parse forms ~a\n" forms)
|
||||||
|
|
|
@ -2,8 +2,11 @@
|
||||||
|
|
||||||
(require (for-syntax racket/base))
|
(require (for-syntax racket/base))
|
||||||
|
|
||||||
|
#;
|
||||||
(provide (all-defined-out))
|
(provide (all-defined-out))
|
||||||
|
|
||||||
|
(provide honu-transformer? make-honu-transformer)
|
||||||
|
|
||||||
(define-values (prop:honu-transformer honu-transformer? honu-transformer-ref)
|
(define-values (prop:honu-transformer honu-transformer? honu-transformer-ref)
|
||||||
(make-struct-type-property 'honu-transformer))
|
(make-struct-type-property 'honu-transformer))
|
||||||
|
|
||||||
|
@ -20,3 +23,26 @@
|
||||||
"procedure (arity 2)"
|
"procedure (arity 2)"
|
||||||
proc))
|
proc))
|
||||||
(make-honu-trans proc))
|
(make-honu-trans proc))
|
||||||
|
|
||||||
|
(provide (rename-out [prop:honu-operator? honu-operator?])
|
||||||
|
make-honu-operator
|
||||||
|
(rename-out [-honu-operator-ref honu-operator-ref]))
|
||||||
|
(define-values (prop:honu-operator prop:honu-operator? prop:honu-operator-ref)
|
||||||
|
(make-struct-type-property 'honu-operator))
|
||||||
|
|
||||||
|
#;
|
||||||
|
(provide honu-operator?)
|
||||||
|
(define-values (struct:honu-operator -make-honu-operator honu-operator? -honu-operator-ref honu-operator-set!)
|
||||||
|
(make-struct-type 'honu-operator #f 2 0 #f
|
||||||
|
(list (list prop:honu-operator #t))
|
||||||
|
(current-inspector) 0))
|
||||||
|
|
||||||
|
(define (make-honu-operator precedence proc)
|
||||||
|
(unless (and (procedure? proc)
|
||||||
|
(procedure-arity-includes? proc 2))
|
||||||
|
(raise-type-error
|
||||||
|
'define-honu-operator/syntax
|
||||||
|
"procedure (arity 2)"
|
||||||
|
proc))
|
||||||
|
(-make-honu-operator precedence proc))
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,8 @@
|
||||||
(require
|
(require
|
||||||
(prefix-in macro_ honu/core/private/macro2)
|
(prefix-in macro_ honu/core/private/macro2)
|
||||||
(rename-in honu/core/private/honu2
|
(rename-in honu/core/private/honu2
|
||||||
[honu-function honu_function])
|
[honu-function honu_function]
|
||||||
|
[honu-+ honu_plus])
|
||||||
(rename-in honu/core/private/literals
|
(rename-in honu/core/private/literals
|
||||||
[honu-= =]
|
[honu-= =]
|
||||||
[semicolon |;|])
|
[semicolon |;|])
|
||||||
|
@ -41,3 +42,7 @@
|
||||||
print(x)
|
print(x)
|
||||||
}))
|
}))
|
||||||
(test 5))
|
(test 5))
|
||||||
|
|
||||||
|
|
||||||
|
(let ()
|
||||||
|
(fake-module-begin #hx(1 honu_plus 1)))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user