Skip to content

Instantly share code, notes, and snippets.

@latk
latk / solar_game.pl
Created April 25, 2013 08:08
A refactoring for sharksfan98's “Solar Game”. It now compiles with `use strict; use warnings`. Much of the repetitive code is offloaded into subs. The `ask_question` function takes a question string, the number of the correct answer, and an array reference (`[...]`) containing possible answers. The next refactoring step would put the questions t…
#########################################
# Solar Game #
# #
# (C) 2013 Donovan Roudabush #
# GPU/Creative Commons #
# #
# [email protected] #
# github.com/sharksfan98/solargame #
#########################################
@latk
latk / gist:5854616
Created June 24, 2013 23:19
Transforming Syntax – Marpa parsing example
use strict; use warnings; use 5.010;
use Data::Dumper;
my $data = <<'END';
If ((Myvalue.xyz == 1) Or (Frmae_1.signal_1 == 1)) Then a = 1
Else a = 0;
END
my $ast = Parser->parse(\$data);
@latk
latk / gist:5866025
Last active December 19, 2015 00:00
Transforming Syntax – Tree Cloning example
use strict; use warnings; use 5.010;
use Data::Dumper;
my $ast = bless( [
[
bless( [
bless( [
bless( [
bless( ['Myvalue.xyz'], 'Ast::Var' ),
'==',
@latk
latk / gist:5866146
Created June 26, 2013 09:38
Transforming Syntax – prettyprinting example
use strict; use warnings; use 5.010;
my $ast = bless( [
[
bless( [
bless( [
bless( [
bless( ['Myvalue.xyz'], 'Ast::Var' ),
'==',
bless( ['1'], 'Ast::Literal' )
@latk
latk / gist:5866793
Last active December 19, 2015 00:09
Transforming Syntax – optimizer example
use strict; use warnings; use 5.010;
my $ast = bless( [
[
bless( [
bless( [
bless( [
bless( ['Myvalue.xyz'], 'Ast::Var' ),
'==',
bless( ['1'], 'Ast::Literal' )
@latk
latk / test.pl
Last active December 23, 2015 01:09
Benchmarking 3rd element removal from large arrays
use 5.018; use strict; use warnings; use Benchmark ':all';
cmpthese $ARGV[0] => {
grep => \&with_grep,
grep_light => \&with_grep_light,
splice => \&with_splice,
slice => \&with_slice,
copy => \&with_copy,
};
use strict; use warnings;
use feature 'say';
use autodie;
use File::Slurp;
use Mojo;
use List::MoreUtils 'uniq';
use Data::Dump qw/dd pp/;
my %urls = (
@latk
latk / MarpaX::Interface::Inline Specification.md
Created January 10, 2014 20:25
MarpaX::Interface::Inline Specification

MarpaX::Interface::Inline Specification

The MarpaX::Interface::Inline module provides the Inline Rule Interface (IRIF) to write grammars for the [Marpa parser generator][Marpa::R2]. While Marpa already provides the Scanless Interface (SLIF), the IRIF tries to provide a more productive frontend:

  • Longest-Acceptable-Token Matching: The builtin lexer is smart and only tries to find tokens that can actually be used at the current position. This makes it easier to nest languages.
  • Inline Rules and Quantifiers: A regex like syntax allows you to specify rules where you need them – and not every rule has to be named. Quantifiers include support for Perl6's separation operator %.
  • Inline Actions: Each production is associated with an action that builds the AST. With the IRIF, these are specified inline, directly after the rule which they operate on.

Certain languages like Python use leading indentation to denote blocks. Wouldn't it be nice if a Marpa frontend like the IRIF would have support for this? Well, the problem is that implementing the Off-side Rule is similarly difficult to implementing here-docs: both are non-context free constructs, but are easy to implement by maintaining state in the scanner.

Consider this piece of Python code:

def foo(x, y):
    return x + (2*
y)

foo(1, 2) #=&gt; 5
@latk
latk / GetoptSubset.pm
Created September 19, 2017 15:17
Getopt::Long alternative prototype
package GetoptSubset;
# Copyright 2017 Lukas Atkinson
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software