Browse Source

Test nested expressions

master
Dylan Baker 5 years ago
parent
commit
7dae1bac04
2 changed files with 44 additions and 0 deletions
  1. 20
    0
      spec/ast/application_spec.rb
  2. 24
    0
      spec/parser_spec.rb

+ 20
- 0
spec/ast/application_spec.rb View File

@@ -10,5 +10,25 @@ module Chervil
10 10
           .evaluate(env)
11 11
       ).to eq(3.0)
12 12
     end
13
+
14
+    it 'evaluates nested arithmetic' do
15
+      env = Env.new
16
+      expect(
17
+        AST::Application.new(
18
+          AST::Identifier.new('+'),
19
+          [
20
+            AST::Application.new(
21
+              AST::Identifier.new('+'),
22
+              [AST::Number.new(1.0), AST::Number.new(2.0)]
23
+            ),
24
+            AST::Application.new(
25
+              AST::Identifier.new('+'),
26
+              [AST::Number.new(3.0), AST::Number.new(4.0)]
27
+            )
28
+          ]
29
+        )
30
+          .evaluate(env)
31
+      ).to eq(10.0)
32
+    end
13 33
   end
14 34
 end

+ 24
- 0
spec/parser_spec.rb View File

@@ -32,5 +32,29 @@ module Chervil
32 32
         AST::Definition.new(AST::Identifier.new('x'), AST::Number.new(5.0))
33 33
       )
34 34
     end
35
+
36
+    it 'parses a nested expression' do
37
+      expect(parse('(+ (+ 1 2) (- 3 4))').first).to eq(
38
+        AST::Application.new(
39
+          AST::Identifier.new("+"),
40
+          [
41
+            AST::Application.new(
42
+              AST::Identifier.new("+"),
43
+              [
44
+                AST::Number.new(1.0),
45
+                AST::Number.new(2.0),
46
+              ]
47
+            ),
48
+            AST::Application.new(
49
+              AST::Identifier.new("-"),
50
+              [
51
+                AST::Number.new(3.0),
52
+                AST::Number.new(4.0),
53
+              ]
54
+            ),
55
+          ]
56
+        )
57
+      )
58
+    end
35 59
   end
36 60
 end

Loading…
Cancel
Save