Pārlūkot izejas kodu

Tweak object displays in REPL

master
Dylan Baker 5 gadus atpakaļ
vecāks
revīzija
b3ce44f30a
2 mainītis faili ar 20 papildinājumiem un 5 dzēšanām
  1. 0
    4
      lib/chervil/ast/function.rb
  2. 20
    1
      lib/chervil/repl.rb

+ 0
- 4
lib/chervil/ast/function.rb Parādīt failu

36
 
36
 
37
       current_expr
37
       current_expr
38
     end
38
     end
39
-
40
-    def to_s
41
-      "<function: #{@name.name}(#{@params.map(&:name).join(' ')})>"
42
-    end
43
   end
39
   end
44
 end
40
 end

+ 20
- 1
lib/chervil/repl.rb Parādīt failu

4
       @env = Env.new
4
       @env = Env.new
5
     end
5
     end
6
 
6
 
7
+    def display(obj)
8
+      case obj
9
+      when true
10
+        '#t'
11
+      when false
12
+        '#f'
13
+      when AST::Function
14
+        "<function: #{obj.name.name}(#{obj.params.map(&:name).join(' ')})>"
15
+      when Float
16
+        if obj.floor.to_f == obj
17
+          obj.to_i
18
+        else
19
+          obj
20
+        end
21
+      else
22
+        obj
23
+      end
24
+    end
25
+
7
     def run
26
     def run
8
       begin
27
       begin
9
         while line = Readline.readline("> ", true)
28
         while line = Readline.readline("> ", true)
17
           end
36
           end
18
 
37
 
19
           results = Interpreter.new(tree, @env).interpret
38
           results = Interpreter.new(tree, @env).interpret
20
-          results.each { |result| puts result unless result.nil? }
39
+          results.each { |result| puts display(result) unless result.nil? }
21
         end
40
         end
22
       rescue Interrupt
41
       rescue Interrupt
23
         puts
42
         puts

Notiek ielāde…
Atcelt
Saglabāt