Skip to content

Instantly share code, notes, and snippets.

View Papierkorb's full-sized avatar

Stefan Merettig Papierkorb

View GitHub Profile
@Papierkorb
Papierkorb / amd64.cpp
Last active April 14, 2018 14:19
The humble beginnings of an AMD64 JIT (Without any libraries)
/* Compile: $ g++ -std=c++11 -o amd64 amd64.cpp */
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <sys/mman.h>
// Helper enums so we don't have to poke in bytes directly.
// You can find the documentation of all of these in the *free* AMD64 specification:
@Papierkorb
Papierkorb / bmp.rb
Created January 25, 2018 20:11
Quick and simple BMP writer for Ruby
# This code is meant for debugging purposes. Its performance is probably bad,
# it's just meant to work for some hacky scripts you throw out tomorrow :)
# Create an instance of `Bitmap`, passing the file (IO or string), the width and height.
# Then push one (or more) 32bit pixels in ARGB format using `Bitmap#<<`.
# Pixel format is thus: 0xAARRGGBB (Alpha is highest byte, blue lowest).
# (Alpha is ignored by most programs however)
class Bitmap
MAGIC = "BM"
class Boleite::InputReceiver
def process(event : InputEvent)
execute_all(@actions, event) unless event.claimed?
execute_all(@persistent_actions, event)
end
private def execute_all(actions, event)
actions.each{|act| act.execute(event) if act.interested?(event)}
end
end
@Papierkorb
Papierkorb / arcane_lvar.cr
Created December 16, 2017 17:28
Demo of type inference of local variables through concurrently ran fibers
waiter = Channel(Nil).new
var = "Hello"
spawn do
waiter.receive
var = 5
end
puts "var is a #{typeof(var)}"
puts "var right now is a #{var.class}"
@Papierkorb
Papierkorb / arcane_ivar.cr
Last active December 16, 2017 17:14
Demo on why `case x = @x` is necessary
class Foo
@stuff : String | Int32
@waiter = Channel(Nil).new
def initialize(@stuff)
spawn do
@waiter.receive # Simulate waiting for some IO
@stuff = "Oh noes!" # And store the result of it
end
@Papierkorb
Papierkorb / llvm_jit.cr
Created October 31, 2017 15:22
LLVM JIT example in Crystal
require "llvm"
# Initialize LLVM. There's also `LLVM.init_arm` if that's what you use.
LLVM.init_x86
# The context of everything we'll do. Main book-keeping structure.
ctx = LLVM::Context.new
# Code can be separated in different modules. A module can't directly interact
# with another module. If you'd do that, you'd have to link them.
@Papierkorb
Papierkorb / test.cr
Last active October 1, 2017 10:25 — forked from Groogy/test.cr
# Papierkorb suggestion
class Foo
include CrystalClear
requires var > 5 # Pushes "var > 5" into SOME_ARRAY
ensures result.query? # Pushes "result.query?" into SOME_ARRAY
contract def foo # Expands to the def, processes SOME_ARRAY, and clears it out for the next method
# Code
end
@Papierkorb
Papierkorb / 000_HOWTO.md
Last active March 9, 2024 17:59
Dynamic library in Crystal

Writing a dynamic library in Crystal, and calling it from C

  • Download all files.
  • Build the Crystal part:
crystal build --release --cross-compile --prelude ./no_main library.cr
              ^ You can leave this out
                        ^ We want to link ourselves!
 ^ Use our custom prelude!
# Requiring stuff from Crystals standard library
require "macros"
require "object"
require "comparable"
require "exception"
require "iterable"
require "iterator"
require "indexable"
require "primitives"
@Papierkorb
Papierkorb / set-file-association.rb
Created January 14, 2017 00:04
Set KDE/XDG file associations in bulk
#!/usr/bin/env ruby
# A script to bulk change file associations. Intended for KDE, but might work
# with other XDG conforming software. Run it without arguments to see how to
# use it. Created by me out of frustration, so the code just works ;)
require "inifile" # gem install inifile
ASSOC_FILE = "#{ENV['HOME']}/.config/mimeapps.list"
ASSOC_FILE_BAK = "#{ASSOC_FILE}.bak"