Skip to content

Instantly share code, notes, and snippets.

View dwarring's full-sized avatar

David Warring dwarring

  • The C Peoples
  • New Zealand
View GitHub Profile
grammar Picker {
rule combination($rx, $narity) {
:my %*PICKED;
<choose($rx)> ** $narity
}
rule choose($rx) {
$<choice>=$rx <?{
if %*PICKED{ ~$<choice> } {
use v6;
sub G($s) {gather $s()}
sub T($s) {take $s}
say qq{{
<html>
<body>
<h1>Green Bottles...</h1>
{G { my $n = 10;
while $n > 0 {my $m = $n--; T qq{
@dwarring
dwarring / gist:6847212
Last active December 24, 2015 18:59
nqp / rakudo taylor approx
# naive approximation: pi = 1 - /1/3 + 1/5 ...
# language: perl6 / nqp
my $limit := 20_000;
my $n := 1;
my $pi_over_4 := 0.0;
while $n < $limit {
## my $m := 4.0*$n - 1.0; # this was making $m a Rat
# In the following Ruby code `xx -5` is treated as a method call,
# `yy -5` as an arithmetic expression.
#
def xx(n) ; 37 - n; end
puts xx -5 # output is 42
yy = 37
puts yy -5 # output is 32
@dwarring
dwarring / gist:6127599
Last active December 20, 2015 11:59
perl6 brainf*ck program
use v6;
grammar Brainfuck::Grammar {
rule TOP {^ [<cmd> || .+? ]* $}
proto token cmd { <...> }
token cmd:sym<ptr-inc> {\>}
token cmd:sym<ptr-dec> {\<}
token cmd:sym<inc> {\+}
token cmd:sym<dec> {\-}
token cmd:sym<input> {\,}
@dwarring
dwarring / gist:5992874
Last active December 19, 2015 17:38
Perl6 Case insensitive use of <sym> ?
use v6;
use Test;
grammar G {
## use rule :ignorecase; # nyi in rakudo. not what we want anyway
## use sym :ignorecase #??
rule TOP { <prop> + }
rule num {[\d|'.']+}
proto rule prop {<...>}
rule prop:sym<line-width> {:i <sym> ':' <num> }
rule prop:sym<line-height>:i { <sym> ':' <num> }