finish parsing

This commit is contained in:
vonhyou 2021-04-12 11:31:07 +08:00
commit 4f7bbe8129
2 changed files with 39 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.idea

38
minlisp.rb Normal file
View file

@ -0,0 +1,38 @@
# It's a minimal lisp interpreter written in Ruby-lang
# Author: @vonhyou
# Start at: Apr. 10, 2021
def tokenize(program)
# Convert scripts to token lists
replacements = { '(' => ' ( ', ')' => ' ) ' }
program.gsub(Regexp.union(replacements.keys), replacements)
.split(' ')
end
def read_tokens(tokens)
# read expressions from token
raise SyntaxError, 'Unexpected EOF' if tokens.empty?
token = tokens.shift
if token == '('
lst = []
lst << read_tokens(tokens) while tokens[0] != ')'
tokens.shift
return lst
elsif token == ')'
raise SyntaxError, "Unexpected ')'"
else
return atom token
end
end
def isInteger?(atom) = atom.match?(/^-?\d+$/)
def isFloat?(atom) = atom.match?(/^(-?\d+)(\.\d+)?$/)
def atom(token)
# Analyse numbers and symbols
return Integer token if isInteger? token
return Float token if isFloat? token
token
end