Skip to content

Commit dacd9c4

Browse files
author
James Moriarty
committed
rubocop auto correct.
1 parent 35b0e91 commit dacd9c4

File tree

4 files changed

+31
-33
lines changed

4 files changed

+31
-33
lines changed

lib/lisp.rb

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
1-
$:.unshift File.dirname(__FILE__)
1+
$LOAD_PATH.unshift File.dirname(__FILE__)
22

3-
require "lisp/version"
4-
require "lisp/repl"
3+
require 'lisp/version'
4+
require 'lisp/repl'
55

66
module Lisp
7-
def self.eval string
7+
def self.eval(string)
88
execute parse tokenize string
99
end
1010

11-
def self.tokenize string
12-
string.gsub("("," ( ").gsub(")"," ) ").split
11+
def self.tokenize(string)
12+
string.gsub('(', ' ( ').gsub(')', ' ) ').split
1313
end
1414

15-
def self.parse tokens, tree = []
16-
raise "unexpected: eof" if tokens.size.zero?
15+
def self.parse(tokens, tree = [])
16+
raise 'unexpected: eof' if tokens.size.zero?
1717

1818
case token = tokens.shift
19-
when "("
20-
while tokens[0] != ")" do
21-
tree.push parse tokens
22-
end
19+
when '('
20+
tree.push parse tokens while tokens[0] != ')'
2321
tokens.shift
2422
tree
25-
when ")"
26-
raise "unexpected: )"
23+
when ')'
24+
raise 'unexpected: )'
2725
else
2826
atom token
2927
end
3028
end
3129

32-
def self.atom token
30+
def self.atom(token)
3331
case token
3432
when /\d/
3533
token.to_f % 1 > 0 ? token.to_f : token.to_i
@@ -38,7 +36,7 @@ def self.atom token
3836
end
3937
end
4038

41-
def self.execute expression, scope = global
39+
def self.execute(expression, scope = global)
4240
return scope.fetch(expression) { |var| raise "#{var} is undefined" } if expression.is_a? Symbol
4341
return expression unless expression.is_a? Array
4442

@@ -48,14 +46,14 @@ def self.execute expression, scope = global
4846
scope[var] = execute expression, scope
4947
when :lambda
5048
_, params, expression = expression
51-
lambda { |*args| execute expression, scope.merge(Hash[params.zip(args)]) }
49+
->(*args) { execute expression, scope.merge(Hash[params.zip(args)]) }
5250
when :if
5351
_, test, consequent, alternative = expression
54-
expression = if execute test, scope then consequent else alternative end
52+
expression = execute test, scope ? consequent : alternative
5553
execute expression, scope
5654
when :set!
57-
_, var, expression = expression
58-
if scope.has_key?(var) then scope[var] = execute expression, scope else raise "#{var} is undefined" end
55+
_, var, expression = expression
56+
scope.key?(var) ? (scope[var] = execute expression, scope) : (raise "#{var} is undefined")
5957
when :begin
6058
_, *expression = expression
6159
expression.map { |expression| execute expression, scope }.last
@@ -69,7 +67,7 @@ def self.global
6967
@scope ||= begin
7068
operators = [:==, :"!=", :"<", :"<=", :">", :">=", :+, :-, :*, :/]
7169
operators.inject({}) do |scope, operator|
72-
scope.merge operator => lambda { |*args| args.inject &operator }
70+
scope.merge operator => ->(*args) { args.inject &operator }
7371
end
7472
end
7573
end

lib/lisp/ext/proc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Proc
22
def to_s
3-
"(lambda (...) (...))"
3+
'(lambda (...) (...))'
44
end
55

66
def inspect

lib/lisp/repl.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env ruby
22

3-
require "coolline"
4-
require "coderay"
3+
require 'coolline'
4+
require 'coderay'
55

6-
require "lisp/ext/proc"
6+
require 'lisp/ext/proc'
77

88
module Lisp
99
class REPL
@@ -20,9 +20,9 @@ def initialize
2020
end
2121

2222
def run
23-
trap("SIGINT") { throw :exit }
23+
trap('SIGINT') { throw :exit }
2424

25-
puts "ctrl-c to exit"
25+
puts 'ctrl-c to exit'
2626

2727
catch(:exit) do
2828
loop do
@@ -34,7 +34,7 @@ def run
3434

3535
reset_input!
3636
end
37-
rescue Exception => e
37+
rescue StandardError => e
3838
puts e.message
3939

4040
reset_input!
@@ -47,20 +47,20 @@ def run
4747

4848
def prompt
4949
count = open_count - close_count
50-
char = count > 0 ? ?( : ?)
50+
char = count > 0 ? '(' : ')'
5151
"#{char * count}> "
5252
end
5353

5454
def reset_input!
55-
@input = String.new
55+
@input = ''
5656
end
5757

5858
def open_count
59-
tokens.count(?()
59+
tokens.count('(')
6060
end
6161

6262
def close_count
63-
tokens.count(?))
63+
tokens.count(')')
6464
end
6565

6666
def tokens

lib/lisp/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Lisp
2-
VERSION = "1.4.0"
2+
VERSION = '1.4.0'.freeze
33
end

0 commit comments

Comments
 (0)