mirror of
https://github.com/vonhyou/lisp-interpreter.git
synced 2025-06-08 02:02:01 +00:00
.
This commit is contained in:
parent
55c47b641e
commit
ced73fc2f3
1 changed files with 9 additions and 4 deletions
13
prol.rb
13
prol.rb
|
@ -20,15 +20,20 @@ module Lisp
|
||||||
program.gsub('(', ' ( ').gsub(')', ' ) ').split
|
program.gsub('(', ' ( ').gsub(')', ' ) ').split
|
||||||
end
|
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
|
# read expressions from token
|
||||||
raise SyntaxError, 'Unexpected EOF' if tokens.empty?
|
raise SyntaxError, 'Unexpected EOF' if tokens.empty?
|
||||||
|
|
||||||
case token = tokens.shift
|
case token = tokens.shift
|
||||||
when '('
|
when '('
|
||||||
lst << read_tokens(tokens) while tokens[0] != ')'
|
make_list tokens
|
||||||
tokens.shift
|
|
||||||
lst
|
|
||||||
when ')'
|
when ')'
|
||||||
raise SyntaxError, "Unexpected ')'"
|
raise SyntaxError, "Unexpected ')'"
|
||||||
else
|
else
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue