Browse Source

Don't leave variables bound after function execution :(

master
Dylan Baker 5 years ago
parent
commit
2242484bd6
2 changed files with 9 additions and 0 deletions
  1. 2
    0
      lib/chervil/ast/function.rb
  2. 7
    0
      spec/interpreter_spec.rb

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

@@ -23,6 +23,8 @@ module Chervil::AST
23 23
         raise "Expected #{params.size} arguments but received #{args.size}"
24 24
       end
25 25
 
26
+      env = ::Chervil::Env.new(env)
27
+
26 28
       @params.zip(args).each do |key, value|
27 29
         env.set(key.name, value)
28 30
       end

+ 7
- 0
spec/interpreter_spec.rb View File

@@ -25,5 +25,12 @@ module Chervil
25 25
       interpret('(define (plus-one x) (+ x one))', env)
26 26
       expect(interpret('(plus-one 2)', env).first).to eq(3.0)
27 27
     end
28
+
29
+    it 'doesn\'t leave variables bound after function execution' do
30
+      env = Env.new
31
+      interpret('(define (plus-one x) (+ 1 x))', env)
32
+      interpret('(plus-one 5)', env)
33
+      expect(env.get('x')).to eq(nil)
34
+    end
28 35
   end
29 36
 end

Loading…
Cancel
Save