Skip to content

Instantly share code, notes, and snippets.

@dyama
Created June 15, 2016 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyama/56cc59c25692ba8fbd51ddfc59b4acf7 to your computer and use it in GitHub Desktop.
Save dyama/56cc59c25692ba8fbd51ddfc59b4acf7 to your computer and use it in GitHub Desktop.
Indent nest syntax for Lisp
$indent = 2
class Array;
def nest; self[0] end
def nest=(val); self[0]=val end
def value; self[1] end
def value=(val); self[1]=val end
def comment; self[2] end
def comment=(val); self[2]=val end
end
def nestlv(s)
/^( *)/.match(s).captures.first.size / $indent
end
def exsplit(s)
s.scan(/("[^"]*")|('[^']*')|([^ ]+)/).flatten.select{|m|m}
end
s = STDIN.read.gsub(/\\\n/, '').split(/\r?\n/).map(&:chomp)
s.map!{|c| [nestlv(c), c.gsub(/ *;.*$/, "").strip, c.gsub(/^.*?( *;.*)?$/, '\1')] }
s.push [0, "()"]
s.each_cons(2) do |c, n|
if /^\s*$/ =~ c.value
printf "%s%s\n", c.value, c.comment
next
end
cont = c.nest < n.nest # continue to next line
if !/^'?(?<p>\((?:[^()]|\g<p>)*\))$/.match(c.value)
if exsplit(c.value).size > 1 && !cont
c.value = "(%s)" % c.value
end
end
printf " " * $indent * c.nest
if cont
printf("(" * (n.nest - c.nest))
end
printf "%s%s", c.value, c.comment
if c.nest > n.nest
printf(")" * (c.nest - n.nest))
end
printf "\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment