Skip to content

Instantly share code, notes, and snippets.

@akirchner333
Created July 12, 2019 15:01
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 akirchner333/3838a573ae870076afca5a72473114cc to your computer and use it in GitHub Desktop.
Save akirchner333/3838a573ae870076afca5a72473114cc to your computer and use it in GitHub Desktop.
Lisp Parser
defmodule Lisp do
@doc "Removes the wrapping parenthesis and breaks the expression up into individual arguments and sub-expressions, then parses each of those"
def parse("(" <> lisp) do
remove_parens = String.slice(lisp, 0..-2)
Regex.scan(~r/(?:\(.*\)|[^\s]+)/, remove_parens)
|> List.flatten
|> Enum.map(&parse/1)
end
@doc "parses individual operators and arguments. Currently all it does is convert numbers"
def parse(lisp) do
if lisp =~ ~r/\d+/ do
String.to_integer(lisp)
else
lisp
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment