Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use(require("core")) | ||
use(require("define")) | ||
use(require("patterns")) | ||
|
||
require("atomy/bootstrap") | ||
|
||
let-pattern = class: | ||
def(initialize(@body, @patterns)) {} | ||
|
||
def(bytecode(gen, mod)): | ||
new = Atomy Module new: use(mod) | ||
|
||
@patterns each [`(~pat = ~exp)]: | ||
definer = Atomy Bootstrap pattern-definer( | ||
Atomy Grammar AST QuasiQuote new(pat) | ||
exp | ||
) | ||
|
||
new evaluate(definer, new compile-context) | ||
|
||
new compile(gen, @body) | ||
|
||
macro(let-pattern(~*ms): ~*body): | ||
let-pattern new(`(do: ~*body), ms) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require "spec_helper" | ||
|
||
require "atomy/codeloader" | ||
|
||
describe "let-pattern kernel" do | ||
subject { Atomy::Module.new { use(require("let-pattern")) } } | ||
|
||
it "defines the given pattern for the duration of the body" do | ||
expect(subject.evaluate(seq(<<EOF))).to eq(5) | ||
let-pattern(Foo = pattern('5)): | ||
Foo = 5 | ||
EOF | ||
|
||
expect { subject.evaluate(seq(<<EOF)) }.to raise_error(Atomy::PatternMismatch) | ||
let-pattern(Foo = pattern('5)): | ||
Foo = 6 | ||
EOF | ||
end | ||
|
||
it "has the pattern in-scope of each other" do | ||
expect(subject.evaluate(seq(<<EOF))).to eq(5) | ||
let-pattern(Foo = pattern('5), Bar = pattern(`(baz & Foo))): | ||
Bar = 5 | ||
baz | ||
EOF | ||
end | ||
end |