Skip to content

Commit 29226be

Browse files
author
James Moriarty
committed
Revert "rubocop auto correct."
This reverts commit dacd9c4.
1 parent e9c2e4a commit 29226be

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

lib/lisp.rb

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
1-
$LOAD_PATH.unshift File.dirname(__FILE__)
1+
$:.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-
tree.push parse tokens while tokens[0] != ')'
19+
when "("
20+
while tokens[0] != ")" do
21+
tree.push parse tokens
22+
end
2123
tokens.shift
2224
tree
23-
when ')'
24-
raise 'unexpected: )'
25+
when ")"
26+
raise "unexpected: )"
2527
else
2628
atom token
2729
end
2830
end
2931

30-
def self.atom(token)
32+
def self.atom token
3133
case token
3234
when /\d/
3335
token.to_f % 1 > 0 ? token.to_f : token.to_i
@@ -36,7 +38,7 @@ def self.atom(token)
3638
end
3739
end
3840

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

@@ -46,14 +48,14 @@ def self.execute(expression, scope = global)
4648
scope[var] = execute expression, scope
4749
when :lambda
4850
_, params, expression = expression
49-
->(*args) { execute expression, scope.merge(Hash[params.zip(args)]) }
51+
lambda { |*args| execute expression, scope.merge(Hash[params.zip(args)]) }
5052
when :if
5153
_, test, consequent, alternative = expression
52-
expression = execute test, scope ? consequent : alternative
54+
expression = if execute test, scope then consequent else alternative end
5355
execute expression, scope
5456
when :set!
55-
_, var, expression = expression
56-
scope.key?(var) ? (scope[var] = execute expression, scope) : (raise "#{var} is undefined")
57+
_, var, expression = expression
58+
if scope.has_key?(var) then scope[var] = execute expression, scope else raise "#{var} is undefined" end
5759
when :begin
5860
_, *expression = expression
5961
expression.map { |expression| execute expression, scope }.last
@@ -67,7 +69,7 @@ def self.global
6769
@scope ||= begin
6870
operators = [:==, :"!=", :"<", :"<=", :">", :">=", :+, :-, :*, :/]
6971
operators.inject({}) do |scope, operator|
70-
scope.merge operator => ->(*args) { args.inject &operator }
72+
scope.merge operator => lambda { |*args| args.inject &operator }
7173
end
7274
end
7375
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 StandardError => e
37+
rescue Exception => 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 = ''
55+
@input = String.new
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

0 commit comments

Comments
 (0)