From 6552d3f2cd0e81cea55b572cd3b72e4cbf7e4b72 Mon Sep 17 00:00:00 2001 From: Jon Rafkind Date: Fri, 13 Jan 2012 13:10:29 -0700 Subject: [PATCH] [honu] allow classes to accept 0 constructor parameters. add 'to' as a binary operator that creates a list of numbers --- collects/honu/core/main.rkt | 1 + collects/honu/core/private/class.rkt | 1 + collects/honu/core/private/honu2.rkt | 48 ++++++++++++++++----------- collects/honu/core/private/parse2.rkt | 2 +- collects/tests/honu/for.honu | 4 +-- 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/collects/honu/core/main.rkt b/collects/honu/core/main.rkt index ed16d51ba9..47a0090235 100644 --- a/collects/honu/core/main.rkt +++ b/collects/honu/core/main.rkt @@ -33,6 +33,7 @@ [honu-var var] [honu-val val] [honu-for for] + [honu-to to] [honu-if if] [honu-quote quote] [honu-quasiquote quasiquote] diff --git a/collects/honu/core/private/class.rkt b/collects/honu/core/private/class.rkt index b5bec6f767..a0eb233921 100644 --- a/collects/honu/core/private/class.rkt +++ b/collects/honu/core/private/class.rkt @@ -26,6 +26,7 @@ (define-honu-syntax honu-class (lambda (code context) (syntax-parse code #:literal-sets (cruft) + ;; FIXME: empty parenthesis for constructor arguments should be optional [(_ name (#%parens (~var constructor-argument (separate-ids (literal-syntax-class honu-comma) (literal-syntax-class honu-comma)))) (#%braces method:honu-class-thing ...) . rest) (define class diff --git a/collects/honu/core/private/honu2.rkt b/collects/honu/core/private/honu2.rkt index 7103a032ba..967d317a7e 100644 --- a/collects/honu/core/private/honu2.rkt +++ b/collects/honu/core/private/honu2.rkt @@ -54,25 +54,7 @@ #'rest #f)]))) -(provide honu-for) -(define-honu-syntax honu-for - (lambda (code context) - (syntax-parse code #:literal-sets (cruft) - #:literals (honu-equal honu-in) - [(_ iterator:id honu-equal start:honu-expression honu-to end:honu-expression - honu-do body:honu-expression . rest) - (values - #'(%racket (for/list ([iterator (in-range start.result - end.result)]) - body.result)) - #'rest - #t)] - [(_ iterator:id honu-in stuff:honu-expression - honu-do body:honu-expression . rest) - (values #'(%racket (for/list ([iterator stuff.result]) - body.result)) - #'rest - #t)]))) + (provide honu-if) (define-honu-syntax honu-if @@ -259,6 +241,10 @@ (define-binary-operator honu-string=? 1 'left string=?) (define-binary-operator honu-modulo 2 'left modulo) +(define-binary-operator honu-to 0.001 'left + (lambda (left right) + (for/list ([i (in-range left right)]) i))) + (define-unary-operator honu-not 0.7 'left not) (define-binary-operator honu-equal 1 'left equal?) @@ -372,7 +358,8 @@ [pattern (~seq (~var first (id-except separator end)) (~seq (~var between (id-must-be separator)) (~var next (id-except separator end))) ...) - #:with (id ...) #'(first.x next.x ...)])) + #:with (id ...) #'(first.x next.x ...)] + [pattern (~seq) #:with (id ...) '()])) (begin-for-syntax (provide honu-declaration) @@ -417,3 +404,24 @@ (provide true false) (define true #t) (define false #f) + +(provide honu-for) +(define-honu-syntax honu-for + (lambda (code context) + (syntax-parse code #:literal-sets (cruft) + #:literals (honu-equal honu-in) + #; + [(_ iterator:id honu-equal start:honu-expression honu-to end:honu-expression + honu-do body:honu-expression . rest) + (values + #'(%racket (for/list ([iterator (in-range start.result + end.result)]) + body.result)) + #'rest + #t)] + [(_ iterator:id honu-in stuff:honu-expression + honu-do body:honu-expression . rest) + (values #'(%racket (for/list ([iterator stuff.result]) + body.result)) + #'rest + #t)]))) diff --git a/collects/honu/core/private/parse2.rkt b/collects/honu/core/private/parse2.rkt index aba4c70930..a8fe645d0c 100644 --- a/collects/honu/core/private/parse2.rkt +++ b/collects/honu/core/private/parse2.rkt @@ -161,7 +161,7 @@ (debug "Parse more: ~a unparsed ~a\n" parsed unparsed) (define output (if parsed (honu->racket parsed) - #'(begin))) + #'(void))) (debug "Output ~a\n" output) (with-syntax ([output output] [(unparsed-out ...) unparsed] diff --git a/collects/tests/honu/for.honu b/collects/tests/honu/for.honu index 9ae936ca86..0f8e18fd4c 100644 --- a/collects/tests/honu/for.honu +++ b/collects/tests/honu/for.honu @@ -1,9 +1,9 @@ #lang honu -for x = 1 + 5 to 10 do +for x in 1 + 5 to 10 do printf("x is ~a\n" x) -for x = 1 to 10 do { +for x in 1 to 10 do { var y = x + 1; printf("x ~a y ~a\n", x, y) }