This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sexpistol' | |
class ConfigParser | |
def parse(str) | |
Sexpistol.parse(str).inject({}){|s, exp| eval(exp, :global, s); s} | |
end | |
private | |
def eval(exp, group_name, scope) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def eval(exp, env) | |
case exp | |
in [:+, l, r] | |
eval(l, env) + eval(r, env) | |
in [:*, l, r] | |
eval(l, env) * eval(r, env) | |
in [:/, l, r] | |
eval(l, env) / eval(r, env) | |
in [:-, l, r] | |
eval(l, env) - eval(r, env) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule HashServer do | |
def start(port) do | |
{:ok, socket} = :gen_tcp.listen(port, [:binary, active: false, reuseaddr: true]) | |
IO.puts("Server started on port #{port}") | |
accept_connections(socket) | |
end | |
defp accept_connections(socket) do | |
{:ok, client} = :gen_tcp.accept(socket) | |
IO.puts("Accepted connection from #{inspect client}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Typed | |
def type(*args) | |
self.arg_type_list = args | |
self.prevent_nested_definition = false | |
init! | |
end | |
private | |
attr_accessor :arg_type_list, :prevent_nested_definition | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solution to the CodeWars "Tiny Three-Pass Compiler" kata | |
# https://www.codewars.com/kata/5265b0885fda8eac5900093b/train/ruby | |
class Compiler | |
def compile(program) | |
pass3(pass2(pass1(program))) | |
end | |
def tokenize(program) | |
program.scan(%r'[-+*/()\[\]]|[A-Za-z]+|\d+') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solution to the CodeWars "Simple Interactive Interpreter" kata | |
# https://www.codewars.com/kata/52ffcfa4aff455b3c2000750/train/ruby | |
require 'singleton' | |
class Interpreter | |
include Singleton | |
def initialize | |
@env = { funcs: Hash.new, vars: Hash.new } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
TemplateFuck is a brainfuck interpreter, written as a C++ template metaprogram. | |
https://en.wikipedia.org/wiki/Brainfuck | |
Compile with -ftemplate-depth set to at least 1000: | |
clang++ -O3 TemplateFuck.cpp -o prog -ftemplate-depth=1000 | |
Input for the interpreter is not implemented but it is easy to add. | |
To transform brainfuck code into a C++ type, the following Haskell program can be used: |