Skip to content

Instantly share code, notes, and snippets.

View kddnewton's full-sized avatar
👶
Paternity leave

Kevin Newton kddnewton

👶
Paternity leave
View GitHub Profile
@kddnewton
kddnewton / mutation.rb
Created November 8, 2022 20:22
Mutation visitor
require "syntax_tree"
# Create a new visitor that is going to visit the parsed tree.
visitor = SyntaxTree::Visitor::MutationVisitor.new
# Define a mutation on the visitor that is going to search for the given pattern
# to find nodes that match it and then call the given block to mutate the node
# in place in the tree.
visitor.mutate("If[predicate: Assign | OpAssign]") do |node|
# Get the existing If's predicate node.
@kddnewton
kddnewton / linter.rb
Created November 9, 2022 20:17
Linting with Syntax Tree
# frozen_string_literal: true
module SyntaxTree
# This class is used to create a mutation that transforms syntax trees based
# on various linting rules.
class Linter
def mutation
SyntaxTree.mutation do |mutator|
# Lint/AssignmentInCondition
mutator.mutate("IfNode[predicate: Assign | OpAssign] | UnlessNode[predicate: Assign | OpAssign] | WhileNode[predicate: Assign | OpAssign] | UntilNode[predicate: Assign | OpAssign]") do |node|
@kddnewton
kddnewton / count.rb
Created November 28, 2022 21:50
Count the number of YARV instructions in source
def count(iseq)
iseq[12].select { _1 in [:ensure | :rescue, *] }.sum { count(_1[1]) } +
iseq[13].sum do
case _1
in Integer | Symbol
0
in [*, ["YARVInstructionSequence/SimpleDataFormat", *] => child, *]
1 + count(child)
in Array
1
@kddnewton
kddnewton / emoji.rb
Created December 1, 2022 15:11
Emoji encoding
def c2e(string) = string.chars.map { |char| [char.ord + 128000].pack("U") }.join
def e2c(string) = string.unpack("U*").pack("c*")
c2e("Hello, world!")
# => "👈👥👬👬👯🐬🐠👷👯👲👬👤🐡"
e2c("👈👥👬👬👯🐬🐠👷👯👲👬👤🐡")
# => "Hello, world!"
e2c(c2e("Hello, world!"))
@kddnewton
kddnewton / index.rb
Last active December 22, 2022 20:42
Indexing classes/modules/methods
#!/usr/bin/env ruby
# frozen_string_literal: true
VM_DEFINECLASS_TYPE_CLASS = 0x00
VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01
VM_DEFINECLASS_TYPE_MODULE = 0x02
VM_DEFINECLASS_FLAG_SCOPED = 0x08
VM_DEFINECLASS_FLAG_HAS_SUPERCLASS = 0x10
def log(line, event, message)
@kddnewton
kddnewton / json.rb
Last active August 7, 2023 19:54
Faster JSON parser in Ruby
# frozen_string_literal: true
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "json_pure"
gem "benchmark-ips"
end
@kddnewton
kddnewton / assembler.rb
Created March 17, 2023 20:03
Using YARV assembler in Syntax Tree
putspecialobject 3
putnil
defineclass :FooBar, 0
definemethod :foo
putobject "FooBar#foo"
leave
definemethod :bar
putobject "class variable"
setclassvariable :@@bar
@kddnewton
kddnewton / neon.c
Created August 28, 2023 17:50
Attempting to parse identifiers using NEON SIMD
#include <stdbool.h>
#include <stddef.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
// Each entry in this lookup table indicates whether a character is a valid
@kddnewton
kddnewton / yarp_ast.rb
Last active August 28, 2023 18:32
Fetch YARP AST for a method
require "yarp"
module YARPAST
def yarp_ast
ast =
begin
RubyVM::AbstractSyntaxTree.of(self, keep_script_lines: true)
rescue ArgumentError
end
@kddnewton
kddnewton / cached.rb
Created September 12, 2023 13:58
Cached lookup
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "activerecord", "~> 7.0.0"
gem "sqlite3"
end