mirror of
https://github.com/vonhyou/lisp-interpreter.git
synced 2025-06-08 10:21:59 +00:00
finish parsing
This commit is contained in:
commit
4f7bbe8129
2 changed files with 39 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
.idea
|
38
minlisp.rb
Normal file
38
minlisp.rb
Normal 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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue