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
@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> }
@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> {\,}
# 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: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
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{
grammar Picker {
rule combination($rx, $narity) {
:my %*PICKED;
<choose($rx)> ** $narity
}
rule choose($rx) {
$<choice>=$rx <?{
if %*PICKED{ ~$<choice> } {
@dwarring
dwarring / gist:7848868
Last active December 30, 2015 15:29
An (almost) Simple Grammar

An (almost) Simple Grammar

Today's example constructs a grammar for tracking playing cards in a single deal. We'll say it's poker with one or more players and that each player is being dealt a hand that contains exactly five cards.

The "almost" part is the need to track cards and detect duplicates. We want to check for repeated cards both within each card-hand and between hands.

To start with, here's the basic grammar (no duplicate checks yet):

grammar CardGame {
# from chr() in nqp/src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
if ((val >= 0xfdd0
&& (val <= 0xfdef // non character
|| ((val & 0xfffe) == 0xfffe) // non character
|| val > 0x10ffff) // out of range
@dwarring
dwarring / gist:9559508
Created March 14, 2014 23:54
Advent 2013 day 09 - final example
my $lisp-list = 1 => 2 => 3 => Nil; # it's nice that infix:<< => >> is right-associative
Pair.^add_fallback(
-> $, $name { $name ~~ /^c<[ad]>+r$/ }, # should we handle this? yes, if /^c<[ad]>+r$/
-> $, $name { # if it turned out to be our job, this is what we do
-> $p {
$name ~~ /^c(<[ad]>*)(<[ad]>)r$/; # split out last 'a' or 'd'
my $r = $1 eq 'a' ?? $p.key !! $p.value; # choose key or value
$0 ?? $r."c{$0}r"() !! $r; # maybe recurse
}
}
@dwarring
dwarring / gist:9821113
Created March 27, 2014 23:06
advent 2013 day 14 - config combiner
our %TestFiles = (
'config1.ini' => q:to"END1",