Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vector macro patterns #272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions cogs/syntax-tests.scm
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,6 @@
(check-equal? "catch-all lists, 1 arg" (catchall-list (a b)) '(a))
(check-equal? "catch-all lists, 2 args" (catchall-list (a b) (c d)) '(a c))

(define-syntax improper-rest
(syntax-rules ()
[(_ (a . b)) 'b]))

(skip-compile (check-equal? "improper-rest, 0 args" (improper-rest 1) '()))
(skip-compile (check-equal? "improper-rest, 1 arg" (improper-rest 1 2) '(2)))
(skip-compile (check-equal? "improper-rest, 2 args" (improper-rest 1 a (b)) '(a (b))))

(define bound-x 3)

(define-syntax lexical-capture
Expand All @@ -162,6 +154,14 @@

(let ([bound-x 'inner]) (check-equal? "hygiene, lexical capture" (lexical-capture) 3))

(define-syntax improper-rest
(syntax-rules ()
[(_ (a . b)) 'b]))

(check-equal? "improper-rest, 0 args" (improper-rest (1)) '())
(check-equal? "improper-rest, 1 arg" (improper-rest (1 2)) '(2))
(check-equal? "improper-rest, 2 args" (improper-rest (1 a (b))) '(a (b)))

(check-equal? "improper lists in syntax"
;; equivalent to (let [(x 1)] x)
(let . ([(x . (1 . ()))
Expand Down Expand Up @@ -211,6 +211,12 @@
'(y . z))
(check-equal? "improper list pattern, tail arguments, constant in cdr" (improper-tail x . "y") "y")

(define-syntax improper-tail-const
(syntax-rules ()
[(_ a . #t) (quote a)]))

(check-equal? "improper list pattern, constant tail" (improper-tail-const x . #t) 'x)

(define-syntax improper-tail-nested
(syntax-rules ()
[(_ (a . b)) (quote b)]))
Expand Down Expand Up @@ -270,6 +276,34 @@

(check-equal? "macro expansion correctly works within another syntax rules" (t 10) 11)


(define-syntax with-u8
(syntax-rules ()
[(_ #u8(1) a) a]))

(check-equal? "bytevector patterns" (with-u8 #u8(1) #(a b)) #(a b))

(check-syntax-error? "bytevector patterns, unmatched constant"
'((define-syntax with-u8
(syntax-rules ()
[(_ #u8(1) a) a]))
(with-u8 #u8(3) 1))
"macro expansion unable to match case")

(define-syntax with-vec
(syntax-rules ()
[(_ #(a)) 'a]))

(check-equal? "vector pattern" (with-vec #((x y))) '(x y))

(define-syntax into-vec
(syntax-rules ()
[(_ a b) #(b)]))

(check-equal? "vector pattern replacement" (into-vec x y) #(y))

(check-equal? "vector quasiquoting" `#(,(list 'a)) #((a)))

;; -------------- Report ------------------

(define stats (get-test-stats))
Expand Down
2 changes: 2 additions & 0 deletions crates/steel-core/src/compiler/passes/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,8 @@ impl<'a> VisitorMutUnitRef<'a> for AnalysisPass<'a> {
// println!("Free identifier: {}", a);
}
}

fn visit_vector(&mut self, _: &'a steel_parser::ast::Vector) {}
}

impl<'a> VisitorMutUnitRef<'a> for Analysis {
Expand Down
21 changes: 5 additions & 16 deletions crates/steel-core/src/compiler/passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,7 @@
}

#[inline]
fn visit_vector(&mut self, mut v: Vector) -> ExprKind {
let args: Vec<_> = v.args.into_iter().map(|exp| self.visit(exp)).collect();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've revisited a few of the visitor traits, trying to ascertain whether it makes sense or not to descend into the vector contents (given that they're self-quoting).

I don't have a lot of confidence in the end result, given that different visitors are used at different "expansion phases", and my understanding of those is not great.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that they're self quoting, I think it is correct to not descend into the vector contents


v.args = args;

fn visit_vector(&mut self, v: Vector) -> ExprKind {
ExprKind::Vector(v)
}
}
Expand Down Expand Up @@ -225,12 +221,7 @@
#[inline]
fn visit_require(&mut self, _s: &Require) {}

#[inline]
fn visit_vector(&mut self, v: &Vector) {
for arg in &v.args {
self.visit(arg);
}
}
fn visit_vector(&mut self, _v: &Vector) {}
}

pub trait VisitorMutControlFlow {
Expand Down Expand Up @@ -545,9 +536,7 @@
#[inline]
fn visit_require(&mut self, _s: &mut Require) {}

fn visit_vector(&mut self, v: &mut Vector) {
for arg in &mut v.args {
self.visit(arg);
}
}
// vectors are constants, most analysis passes should ignore them
#[inline]
fn visit_vector(&mut self, v: &mut Vector) {}

Check warning on line 541 in crates/steel-core/src/compiler/passes/mod.rs

View workflow job for this annotation

GitHub Actions / Test Suite (sync)

unused variable: `v`

Check warning on line 541 in crates/steel-core/src/compiler/passes/mod.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `v`
}
8 changes: 0 additions & 8 deletions crates/steel-core/src/parser/expand_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,7 @@
Ok(())
}

fn visit_vector(&mut self, v: &mut super::ast::Vector) -> Self::Output {

Check warning on line 367 in crates/steel-core/src/parser/expand_visitor.rs

View workflow job for this annotation

GitHub Actions / Test Suite (sync)

unused variable: `v`

Check warning on line 367 in crates/steel-core/src/parser/expand_visitor.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `v`
for arg in &mut v.args {
self.visit(arg)?;
}

Ok(())
}
}
Expand Down Expand Up @@ -992,11 +988,7 @@
res
}

fn visit_vector(&mut self, v: &mut super::ast::Vector) -> Self::Output {

Check warning on line 991 in crates/steel-core/src/parser/expand_visitor.rs

View workflow job for this annotation

GitHub Actions / Test Suite (sync)

unused variable: `v`

Check warning on line 991 in crates/steel-core/src/parser/expand_visitor.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `v`
for arg in &mut v.args {
self.visit(arg)?;
}

Ok(())
}
}
Expand Down
Loading
Loading