Browse Source

Tweak object displays in REPL

master
Dylan Baker 5 years ago
parent
commit
b3ce44f30a
2 changed files with 20 additions and 5 deletions
  1. 0
    4
      lib/chervil/ast/function.rb
  2. 20
    1
      lib/chervil/repl.rb

+ 0
- 4
lib/chervil/ast/function.rb View File

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

+ 20
- 1
lib/chervil/repl.rb View File

@@ -4,6 +4,25 @@ module Chervil
4 4
       @env = Env.new
5 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 26
     def run
8 27
       begin
9 28
         while line = Readline.readline("> ", true)
@@ -17,7 +36,7 @@ module Chervil
17 36
           end
18 37
 
19 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 40
         end
22 41
       rescue Interrupt
23 42
         puts

Loading…
Cancel
Save