Created
July 12, 2019 15:01
-
-
Save akirchner333/3838a573ae870076afca5a72473114cc to your computer and use it in GitHub Desktop.
Lisp Parser
This file contains 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 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