Skip to content

Instantly share code, notes, and snippets.

# Ruby, no bueno
a = 1
def f()
a=2
end
puts a # => 1
# Ruby, works
a = 1
__sym123 = binding
@simenge
simenge / .rb
Last active August 9, 2017 20:51
ylisp compiler stuff
Input:
(begin
(def (inc-by x) (+ x 1))
(inc-by 1)
('a' | gsub 'a' 'b' | gsub 'b' ('a' | upcase))
(if (eq? 1 1 (+ 0 (- 2 1))) (puts (+ 1 2 3)))
(-> (a 1 *b **c d: e: 1 &d) a)
(-> () (set! inc-by 1))
)
module Lisp
enum Tag
Str
Symbol
Int
Builtin
Lambda
List
True
False
module Test where
data Lisp = Integer Int
| Str String
| FloatT Float
toString :: Lisp -> String
toString x = case x of
Integer i -> show i
| Str s -> s
module Lisp
class List
property list : Array(Lisp::Object)
def initialize(@list = [] of Lisp::Object)
end
def to_s
"(" + @list.map(&.to_s).join(" ") + ")"
end
forward_missing_to list
simen@DESKTOP-G75FGNT:~$ curl -sSL https://dist.crystal-lang.org/apt/setup.sh | sudo bash
[sudo] password for simen:
Executing: /tmp/apt-key-gpghome.BpBCL6D3uk/gpg.1.sh --keyserver keys.gnupg.net --recv-keys 09617FD37CC06B54
gpg: connecting dirmngr at '/tmp/apt-key-gpghome.BpBCL6D3uk/S.dirmngr' failed: IPC connect call failed
gpg: keyserver receive failed: No dirmngr
Get:1 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:5 https://dist.crystal-lang.org/apt crystal InRelease [2478 B]
module Pat
macro match(&block)
case {{block.body.cond}}
{% for whens in block.body.whens %}
{% if whens.conds.first.is_a?(Cast) %}
when {{whens.conds.first.to}}
{{whens.conds.first.obj}} = {{block.body.cond}}
{{whens.body}}
{% elsif whens.conds.first.is_a?(ArrayLiteral) %}
when {{block.body.cond}}.is_a?(Array) && {{block.body.cond}}.as(Array).size == {{whens.conds.first.size}}
match {
case sexpr
when [sym("def"), params.as(Array(LispSymbol), rest(body.as(Array(LispObject))))]
# params is now bound to an Array(LispSymbol) and body to an array of LispObject
# consisting of sexpr[2..-1]
end
}
def annotate_tc(s : LispObject)
# Automatically annotating tail calls can be tricky, because we need to
# properly recurse while terminating at the right points. In particular:
# * The last expr in a function body is in tail position (TP)
# * If a function call is in TP, we need to annotate it
# * If an if expr is in TP, we need to annotate each branch
# * If a fn literal is in TP, we need to recursively call annotate_tc.
# However, we will not do this from within annotate_tc; rather,
# Interpreter#eval_fn, which evaluates a function literal and
# returns a Lisp::Lambda object, will call