This commit is contained in:
vonhyou 2021-04-15 01:21:41 +08:00
parent 55c47b641e
commit ced73fc2f3

13
prol.rb
View file

@ -20,15 +20,20 @@ module Lisp
program.gsub('(', ' ( ').gsub(')', ' ) ').split
end
def self.read_tokens(tokens, lst = [])
def self.make_list(tokens)
lst = []
lst << read_tokens(tokens) while tokens[0] != ')'
tokens.shift
lst
end
def self.read_tokens(tokens)
# read expressions from token
raise SyntaxError, 'Unexpected EOF' if tokens.empty?
case token = tokens.shift
when '('
lst << read_tokens(tokens) while tokens[0] != ')'
tokens.shift
lst
make_list tokens
when ')'
raise SyntaxError, "Unexpected ')'"
else