mirror of
https://github.com/vonhyou/lisp-interpreter.git
synced 2025-06-08 02:02:01 +00:00
fix env and write a module for futuer update
This commit is contained in:
parent
1ea52e195f
commit
d0863d1636
1 changed files with 94 additions and 97 deletions
97
prol.rb
97
prol.rb
|
@ -9,37 +9,34 @@
|
|||
# :atom
|
||||
#####
|
||||
|
||||
def parse(program)
|
||||
read_tokens(tokenize(program))
|
||||
end
|
||||
module Lisp
|
||||
|
||||
def tokenize(program)
|
||||
def self.parse(program)
|
||||
read_tokens(tokenize(program))
|
||||
end
|
||||
|
||||
def self.tokenize(program)
|
||||
# Convert scripts to token lists
|
||||
program.gsub('(', ' ( ').gsub(')', ' ) ').split
|
||||
end
|
||||
end
|
||||
|
||||
def make_list(tokens)
|
||||
lst = []
|
||||
lst << read_tokens(tokens) while tokens[0] != ')'
|
||||
tokens.shift
|
||||
lst
|
||||
end
|
||||
|
||||
def read_tokens(tokens)
|
||||
def self.read_tokens(tokens, lst = [])
|
||||
# read expressions from token
|
||||
raise SyntaxError, 'Unexpected EOF' if tokens.empty?
|
||||
|
||||
case token = tokens.shift
|
||||
when '('
|
||||
make_list tokens
|
||||
lst << read_tokens(tokens) while tokens[0] != ')'
|
||||
tokens.shift
|
||||
lst
|
||||
when ')'
|
||||
raise SyntaxError, "Unexpected ')'"
|
||||
else
|
||||
atom token
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def atom(token)
|
||||
def self.atom(token)
|
||||
# Analyse numbers and symbols
|
||||
case token
|
||||
when /\d/
|
||||
|
@ -47,32 +44,31 @@ def atom(token)
|
|||
else
|
||||
token.to_sym
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
##### Environments
|
||||
##### Environments
|
||||
|
||||
$global_env = {
|
||||
'+': ->(args) { args.sum }, # args.inject(0, :+)
|
||||
'-': ->(*args) { eval args.join('-') },
|
||||
'*': ->(*args) { eval args.join('*') },
|
||||
'/': ->(*args) { eval args.join('/') },
|
||||
'>': ->(args) { args[0] > args[1] },
|
||||
'<': ->(args) { args[0] < args[1] },
|
||||
'=': ->(args) { args[0] == args[1] },
|
||||
'>=': ->(args) { args[0] >= args[1] },
|
||||
'<=': ->(args) { args[0] <= args[1] },
|
||||
'min': ->(*args) { args.min },
|
||||
'max': ->(*args) { args.max },
|
||||
'car': ->(arr) { arr[0] },
|
||||
'cdr': ->(arr) { arr[1..-1] },
|
||||
'cons': ->(arr) { arr },
|
||||
'quote': ->(arr) { arr },
|
||||
'print': ->(arg) { p arg },
|
||||
'begin': ->(*_args) { true }
|
||||
}
|
||||
def self.make_global
|
||||
@global_env ||= begin
|
||||
ops = %i[== != < <= > >= + - * /]
|
||||
ops.inject({}) do |scope, op|
|
||||
scope.merge op => ->(*args) { args.inject(&op) }
|
||||
end
|
||||
end
|
||||
# @global_env.merge {
|
||||
# 'min' : ->(*args) { args.min },
|
||||
# 'max' : ->(*args) { args.max },
|
||||
# 'car' : ->(arr) { arr[0] },
|
||||
# 'cdr' : ->(arr) { arr[1..-1] },
|
||||
# 'cons' : ->(arr) { arr },
|
||||
# 'quote' : ->(arr) { arr },
|
||||
# 'print' : ->(arg) { p arg },
|
||||
# 'begin' : ->(*_args) { true }
|
||||
# }
|
||||
end
|
||||
|
||||
##### Lisp Eval
|
||||
def lisp_eval(elem, env = $global_env)
|
||||
##### Lisp Eval
|
||||
def self.lisp_eval(elem, env = make_global)
|
||||
if elem.instance_of? Symbol
|
||||
env[elem]
|
||||
elsif elem.instance_of?(Integer) || elem.instance_of?(Float)
|
||||
|
@ -94,20 +90,20 @@ def lisp_eval(elem, env = $global_env)
|
|||
elsif elem[0] == :not
|
||||
!lisp_eval(elem[1], env)
|
||||
else
|
||||
args = []
|
||||
elem[1..-1].each { |arg| args << lisp_eval(arg, env) }
|
||||
p lisp_eval(elem[0], env)
|
||||
lisp_eval(elem[0], env).call args
|
||||
func, *args = elem.map { |e| lisp_eval e, env }
|
||||
func.call *args
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
$copyleft = "Copyleft (Ↄ) 2021 vonhyou@lenva.tech
|
||||
# (def fib (lambda (n) (if (<= n 2) 1 (+ (fib (- n 1)) (fib (- n 2))))))
|
||||
|
||||
$copyleft = "Copyleft (Ↄ) 2021 vonhyou@lenva.tech
|
||||
(PRO)cessor of (L)ist for Mathematical Calculation
|
||||
This is an open source software, you can view its source code on github:
|
||||
https://github.com/vonhyou/lisp-interpreter\n\n"
|
||||
|
||||
##### REPL
|
||||
def repl(prompt = 'prol ƛ>> ')
|
||||
##### REPL
|
||||
def self.repl(prompt = 'prol ƛ>> ')
|
||||
puts $copyleft
|
||||
loop do
|
||||
print prompt
|
||||
|
@ -115,10 +111,11 @@ def repl(prompt = 'prol ƛ>> ')
|
|||
|
||||
print_value val unless val.nil? || val.instance_of?(Proc)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def print_value(value)
|
||||
def self.print_value(value)
|
||||
puts ";Value: #{value}"
|
||||
end
|
||||
end
|
||||
|
||||
repl
|
||||
Lisp.repl
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue