Browse Source

Prettier

master
Dylan Baker 5 years ago
parent
commit
76bc66d472

+ 3
- 4
lib/ahem/ast/for_loop.rb View File

@@ -9,16 +9,15 @@ class AST::ForLoop
9 9
 
10 10
   def ==(other)
11 11
     other.is_a?(AST::ForLoop) && other.iterator == @iterator &&
12
-      other.iterable == @iterable && other.block == @block
12
+      other.iterable == @iterable &&
13
+      other.block == @block
13 14
   end
14 15
 
15 16
   def execute(env)
16 17
     @iterable.execute(env).each do |i|
17 18
       _env = Environment.new
18 19
       _env.set(@iterator.name, i)
19
-      @block.statements.each do |statement|
20
-        statement.execute(_env)
21
-      end
20
+      @block.statements.each { |statement| statement.execute(_env) }
22 21
     end
23 22
   end
24 23
 end

+ 2
- 6
lib/ahem/lexer.rb View File

@@ -183,18 +183,14 @@ class Lexer
183 183
 
184 184
   def scan_all
185 185
     tokens = Array.new
186
-    until at_end
187
-      tokens << get_token
188
-    end
186
+    tokens << get_token until at_end
189 187
     tokens << Token.new(TokenKinds::EOF)
190 188
   end
191 189
 
192 190
   private
193 191
 
194 192
   def skip_whitespace
195
-    while !at_end && @source[@position].match(/\A\s/)
196
-      @position += 1
197
-    end
193
+    @position += 1 while !at_end && @source[@position].match(/\A\s/)
198 194
   end
199 195
 
200 196
   def skip_comment

+ 9
- 6
lib/ahem/parser.rb View File

@@ -20,24 +20,24 @@ class Parser
20 20
     elsif @current_token.type == TokenKinds::FUNCTION
21 21
       next_token = @lexer.peek
22 22
       if next_token && next_token.type == TokenKinds::IDENTIFIER
23
-				function_definition
23
+        function_definition
24 24
       else
25
-				expression_statement
25
+        expression_statement
26 26
       end
27 27
     elsif @current_token.type == TokenKinds::CLASS
28 28
       class_definition
29 29
     elsif @current_token.type == TokenKinds::FOR
30 30
       for_loop
31 31
     else
32
-			expression_statement
32
+      expression_statement
33 33
     end
34 34
   end
35 35
 
36
-	def expression_statement
36
+  def expression_statement
37 37
     expr = expression
38 38
     eat(TokenKinds::SEMICOLON)
39 39
     return expr
40
-	end
40
+  end
41 41
 
42 42
   def variable_declaration
43 43
     eat(TokenKinds::LET)
@@ -171,7 +171,9 @@ class Parser
171 171
     expr = binary
172 172
 
173 173
     if @current_token.type == TokenKinds::EQUALS
174
-      raise 'Invalid left hand side of assignment' unless expr.is_a?(AST::Identifier)
174
+      unless expr.is_a?(AST::Identifier)
175
+        raise 'Invalid left hand side of assignment'
176
+      end
175 177
       eat(TokenKinds::EQUALS)
176 178
       value = expression
177 179
       AST::Assignment.new(expr, value)
@@ -370,6 +372,7 @@ class Parser
370 372
 
371 373
   def eat(type)
372 374
     token = @current_token
375
+
373 376
     if token.type == type
374 377
       advance
375 378
       token

+ 8
- 14
spec/ast/assignment_spec.rb View File

@@ -1,25 +1,19 @@
1 1
 RSpec.describe AST::Assignment do
2 2
   it 'reassigns an existing variable' do
3 3
     env = Environment.new
4
-    AST::VariableDeclaration.new(
5
-      AST::Identifier.new('x'),
6
-      AST::Number.new(5.0),
7
-    ).execute(env)
8
-    AST::Assignment.new(
9
-      AST::Identifier.new('x'),
10
-      AST::Number.new(6.0),
11
-    ).execute(env)
4
+    AST::VariableDeclaration.new(AST::Identifier.new('x'), AST::Number.new(5.0))
5
+      .execute(env)
6
+    AST::Assignment.new(AST::Identifier.new('x'), AST::Number.new(6.0)).execute(
7
+      env
8
+    )
12 9
     expect(env.get('x').execute(env)).to eq(6.0)
13 10
   end
14 11
 
15 12
   it 'raises for an undeclared variable' do
16 13
     env = Environment.new
17 14
     expect {
18
-      AST::Assignment.new(
19
-        AST::Identifier.new('x'),
20
-        AST::Number.new(6.0),
21
-      ).execute(env)
22
-    }.to raise_error("Undefined variable x")
23
-
15
+      AST::Assignment.new(AST::Identifier.new('x'), AST::Number.new(6.0))
16
+        .execute(env)
17
+    }.to raise_error('Undefined variable x')
24 18
   end
25 19
 end

+ 1
- 3
spec/ast/class_definition_spec.rb View File

@@ -1,9 +1,7 @@
1 1
 RSpec.describe AST::ClassDefinition do
2 2
   it 'defines a class' do
3 3
     env = Environment.new(env)
4
-    klass = AST::ClassDefinition.new(
5
-      AST::Identifier.new('Class'), [], []
6
-    )
4
+    klass = AST::ClassDefinition.new(AST::Identifier.new('Class'), [], [])
7 5
     klass.execute(env)
8 6
     expect(env.get('Class')).to eq(klass)
9 7
   end

+ 19
- 40
spec/ast/conditional_spec.rb View File

@@ -7,17 +7,14 @@ RSpec.describe AST::Conditional do
7 7
             AST::Binary.new(
8 8
               AST::Operators::GREATER_THAN,
9 9
               AST::Number.new(5.0),
10
-              AST::Number.new(4.0),
10
+              AST::Number.new(4.0)
11 11
             ),
12
-            AST::Block.new(
13
-              [
14
-                AST::String.new("5 is greater than 4")
15
-              ]
16
-            )
12
+            AST::Block.new([AST::String.new('5 is greater than 4')])
17 13
           )
18 14
         ]
19
-      ).execute(Environment.new)
20
-    ).to eq("5 is greater than 4")
15
+      )
16
+        .execute(Environment.new)
17
+    ).to eq('5 is greater than 4')
21 18
   end
22 19
 
23 20
   it 'evaluates an else statement' do
@@ -28,25 +25,18 @@ RSpec.describe AST::Conditional do
28 25
             AST::Binary.new(
29 26
               AST::Operators::GREATER_THAN,
30 27
               AST::Number.new(3.0),
31
-              AST::Number.new(4.0),
28
+              AST::Number.new(4.0)
32 29
             ),
33
-            AST::Block.new(
34
-              [
35
-                AST::String.new("3 is greater than 4")
36
-              ]
37
-            )
30
+            AST::Block.new([AST::String.new('3 is greater than 4')])
38 31
           ),
39 32
           AST::Branch.new(
40 33
             AST::Boolean.new(true),
41
-            AST::Block.new(
42
-              [
43
-                AST::String.new("3 is _not_ greater than 4")
44
-              ]
45
-            )
34
+            AST::Block.new([AST::String.new('3 is _not_ greater than 4')])
46 35
           )
47 36
         ]
48
-      ).execute(Environment.new)
49
-    ).to eq("3 is _not_ greater than 4")
37
+      )
38
+        .execute(Environment.new)
39
+    ).to eq('3 is _not_ greater than 4')
50 40
   end
51 41
 
52 42
   it 'evaluates an else if statement' do
@@ -57,36 +47,25 @@ RSpec.describe AST::Conditional do
57 47
             AST::Binary.new(
58 48
               AST::Operators::GREATER_THAN,
59 49
               AST::Number.new(3.0),
60
-              AST::Number.new(4.0),
50
+              AST::Number.new(4.0)
61 51
             ),
62
-            AST::Block.new(
63
-              [
64
-                AST::String.new("3 is greater than 4")
65
-              ]
66
-            )
52
+            AST::Block.new([AST::String.new('3 is greater than 4')])
67 53
           ),
68 54
           AST::Branch.new(
69 55
             AST::Binary.new(
70 56
               AST::Operators::GREATER_THAN,
71 57
               AST::Number.new(3.0),
72
-              AST::Number.new(2.0),
58
+              AST::Number.new(2.0)
73 59
             ),
74
-            AST::Block.new(
75
-              [
76
-                AST::String.new("3 is greater than 2")
77
-              ]
78
-            )
60
+            AST::Block.new([AST::String.new('3 is greater than 2')])
79 61
           ),
80 62
           AST::Branch.new(
81 63
             AST::Boolean.new(true),
82
-            AST::Block.new(
83
-              [
84
-                AST::String.new("3 is _not_ greater than 4")
85
-              ]
86
-            )
64
+            AST::Block.new([AST::String.new('3 is _not_ greater than 4')])
87 65
           )
88 66
         ]
89
-      ).execute(Environment.new)
90
-    ).to eq("3 is greater than 2")
67
+      )
68
+        .execute(Environment.new)
69
+    ).to eq('3 is greater than 2')
91 70
   end
92 71
 end

+ 4
- 9
spec/ast/for_loop_spec.rb View File

@@ -4,23 +4,18 @@ RSpec.describe AST::ForLoop do
4 4
       AST::ForLoop.new(
5 5
         AST::Identifier.new('x'),
6 6
         AST::Array.new(
7
-          [
8
-            AST::Number.new(1.0),
9
-            AST::Number.new(2.0),
10
-            AST::Number.new(3.0),
11
-          ]
7
+          [AST::Number.new(1.0), AST::Number.new(2.0), AST::Number.new(3.0)]
12 8
         ),
13 9
         AST::Block.new(
14 10
           [
15 11
             AST::FunctionCall.new(
16 12
               AST::Identifier.new('print'),
17
-              [
18
-                AST::Identifier.new('x')
19
-              ]
13
+              [AST::Identifier.new('x')]
20 14
             )
21 15
           ]
22 16
         )
23
-      ).execute(Environment.new)
17
+      )
18
+        .execute(Environment.new)
24 19
     }.to output("1.0\n2.0\n3.0\n").to_stdout
25 20
   end
26 21
 end

+ 83
- 82
spec/ast/function_call_spec.rb View File

@@ -4,7 +4,8 @@ RSpec.describe AST::FunctionCall do
4 4
       AST::FunctionCall.new(
5 5
         AST::Identifier.new('print'),
6 6
         [AST::String.new('hello world')]
7
-      ).execute(Environment.new)
7
+      )
8
+        .execute(Environment.new)
8 9
     }.to output("hello world\n").to_stdout
9 10
   end
10 11
 
@@ -13,19 +14,23 @@ RSpec.describe AST::FunctionCall do
13 14
     AST::FunctionDefinition.new(
14 15
       AST::Identifier.new('add_one'),
15 16
       [AST::Identifier.new('n')],
16
-      AST::Block.new([
17
-        AST::Binary.new(
18
-          AST::Operators::ADD,
19
-          AST::Identifier.new('n'),
20
-          AST::Number.new(1.0)
21
-        )
22
-      ])
23
-    ).execute(env)
17
+      AST::Block.new(
18
+        [
19
+          AST::Binary.new(
20
+            AST::Operators::ADD,
21
+            AST::Identifier.new('n'),
22
+            AST::Number.new(1.0)
23
+          )
24
+        ]
25
+      )
26
+    )
27
+      .execute(env)
24 28
     expect(
25 29
       AST::FunctionCall.new(
26 30
         AST::Identifier.new('add_one'),
27 31
         [AST::Number.new(5.0)]
28
-      ).execute(env)
32
+      )
33
+        .execute(env)
29 34
     ).to eq(6.0)
30 35
   end
31 36
 
@@ -43,47 +48,51 @@ RSpec.describe AST::FunctionCall do
43 48
     AST::FunctionDefinition.new(
44 49
       AST::Identifier.new('factorial'),
45 50
       [AST::Identifier.new('n')],
46
-      AST::Block.new([
47
-        AST::Conditional.new(
48
-          [
49
-            AST::Branch.new(
50
-              AST::Binary.new(
51
-                AST::Operators::DOUBLE_EQUALS,
52
-                AST::Identifier.new('n'),
53
-                AST::Number.new(0.0),
54
-              ),
55
-              AST::Block.new([
56
-                AST::Number.new(1.0),
57
-              ])
58
-            ),
59
-            AST::Branch.new(
60
-              AST::Boolean.new(true),
61
-              AST::Block.new([
51
+      AST::Block.new(
52
+        [
53
+          AST::Conditional.new(
54
+            [
55
+              AST::Branch.new(
62 56
                 AST::Binary.new(
63
-                  AST::Operators::MULTIPLY,
57
+                  AST::Operators::DOUBLE_EQUALS,
64 58
                   AST::Identifier.new('n'),
65
-                  AST::FunctionCall.new(
66
-                    AST::Identifier.new('factorial'),
67
-                    [
68
-                      AST::Binary.new(
69
-                        AST::Operators::SUBTRACT,
70
-                        AST::Identifier.new('n'),
71
-                        AST::Number.new(1.0),
59
+                  AST::Number.new(0.0)
60
+                ),
61
+                AST::Block.new([AST::Number.new(1.0)])
62
+              ),
63
+              AST::Branch.new(
64
+                AST::Boolean.new(true),
65
+                AST::Block.new(
66
+                  [
67
+                    AST::Binary.new(
68
+                      AST::Operators::MULTIPLY,
69
+                      AST::Identifier.new('n'),
70
+                      AST::FunctionCall.new(
71
+                        AST::Identifier.new('factorial'),
72
+                        [
73
+                          AST::Binary.new(
74
+                            AST::Operators::SUBTRACT,
75
+                            AST::Identifier.new('n'),
76
+                            AST::Number.new(1.0)
77
+                          )
78
+                        ]
72 79
                       )
73
-                    ]
74
-                  )
80
+                    )
81
+                  ]
75 82
                 )
76
-              ])
77
-            ),
78
-          ]
79
-        )
80
-      ])
81
-    ).execute(env)
83
+              )
84
+            ]
85
+          )
86
+        ]
87
+      )
88
+    )
89
+      .execute(env)
82 90
     expect(
83 91
       AST::FunctionCall.new(
84 92
         AST::Identifier.new('factorial'),
85 93
         [AST::Number.new(5.0)]
86
-      ).execute(env)
94
+      )
95
+        .execute(env)
87 96
     ).to eq(120.0)
88 97
   end
89 98
 
@@ -102,30 +111,28 @@ RSpec.describe AST::FunctionCall do
102 111
     AST::FunctionDefinition.new(
103 112
       AST::Identifier.new('add_one'),
104 113
       [AST::Identifier.new('n')],
105
-      AST::Block.new([
106
-        AST::Binary.new(
107
-          AST::Operators::ADD,
108
-          AST::Identifier.new('n'),
109
-          AST::Number.new(1.0)
110
-        )
111
-      ])
112
-    ).execute(env)
114
+      AST::Block.new(
115
+        [
116
+          AST::Binary.new(
117
+            AST::Operators::ADD,
118
+            AST::Identifier.new('n'),
119
+            AST::Number.new(1.0)
120
+          )
121
+        ]
122
+      )
123
+    )
124
+      .execute(env)
113 125
 
114
-    AST::FunctionCall.new(
115
-      AST::Identifier.new('add_one'),
116
-      [
117
-        AST::Number.new(5)
118
-      ]
119
-    ).execute(env)
126
+    AST::FunctionCall.new(AST::Identifier.new('add_one'), [AST::Number.new(5)])
127
+      .execute(env)
120 128
 
121
-    expect do
129
+    expect {
122 130
       AST::FunctionCall.new(
123 131
         AST::Identifier.new('print'),
124
-        [
125
-          AST::Identifier.new('n')
126
-        ]
127
-      ).execute(env)
128
-    end.to raise_error('Undefined variable n')
132
+        [AST::Identifier.new('n')]
133
+      )
134
+        .execute(env)
135
+    }.to raise_error('Undefined variable n')
129 136
   end
130 137
 
131 138
   # let add_one = function(n) { n + 1; };
@@ -137,9 +144,7 @@ RSpec.describe AST::FunctionCall do
137 144
       AST::Identifier.new('add_one'),
138 145
       AST::FunctionDefinition.new(
139 146
         nil,
140
-        [
141
-          AST::Identifier.new('n'),
142
-        ],
147
+        [AST::Identifier.new('n')],
143 148
         AST::Block.new(
144 149
           [
145 150
             AST::Binary.new(
@@ -150,36 +155,32 @@ RSpec.describe AST::FunctionCall do
150 155
           ]
151 156
         )
152 157
       )
153
-    ).execute(env)
158
+    )
159
+      .execute(env)
154 160
 
155 161
     AST::VariableDeclaration.new(
156 162
       AST::Identifier.new('do'),
157 163
       AST::FunctionDefinition.new(
158 164
         nil,
159
-        [
160
-          AST::Identifier.new('f'),
161
-          AST::Identifier.new('x'),
162
-        ],
165
+        [AST::Identifier.new('f'), AST::Identifier.new('x')],
163 166
         AST::Block.new(
164 167
           [
165 168
             AST::FunctionCall.new(
166 169
               AST::Identifier.new('f'),
167
-              [
168
-                AST::Identifier.new('x')
169
-              ]
170
+              [AST::Identifier.new('x')]
170 171
             )
171 172
           ]
172 173
         )
173 174
       )
174
-    ).execute(env)
175
+    )
176
+      .execute(env)
175 177
 
176
-    result = AST::FunctionCall.new(
177
-      AST::Identifier.new('do'),
178
-      [
179
-        AST::Identifier.new('add_one'),
180
-        AST::Number.new(5.0)
181
-      ]
182
-    ).execute(env)
178
+    result =
179
+      AST::FunctionCall.new(
180
+        AST::Identifier.new('do'),
181
+        [AST::Identifier.new('add_one'), AST::Number.new(5.0)]
182
+      )
183
+        .execute(env)
183 184
 
184 185
     expect(result).to eq(6)
185 186
   end

+ 45
- 46
spec/ast/index_spec.rb View File

@@ -2,13 +2,12 @@ RSpec.describe AST::Index do
2 2
   it 'evaluates array index' do
3 3
     expect(
4 4
       AST::Index.new(
5
-        AST::Array.new([
6
-          AST::Number.new(1.0),
7
-          AST::Number.new(2.0),
8
-          AST::Number.new(3.0),
9
-        ]),
5
+        AST::Array.new(
6
+          [AST::Number.new(1.0), AST::Number.new(2.0), AST::Number.new(3.0)]
7
+        ),
10 8
         AST::Number.new(2.0)
11
-      ).execute(Environment.new)
9
+      )
10
+        .execute(Environment.new)
12 11
     ).to eq(3.0)
13 12
   end
14 13
 
@@ -16,73 +15,73 @@ RSpec.describe AST::Index do
16 15
     expect(
17 16
       AST::Index.new(
18 17
         AST::Hash.new(
19
-          :a => AST::Number.new(1.0),
20
-          :b => AST::Number.new(2.0),
21
-          :c => AST::Number.new(3.0),
18
+          a: AST::Number.new(1.0),
19
+          b: AST::Number.new(2.0),
20
+          c: AST::Number.new(3.0)
22 21
         ),
23 22
         AST::Atom.new(:b)
24
-      ).execute(Environment.new)
23
+      )
24
+        .execute(Environment.new)
25 25
     ).to eq(2.0)
26 26
   end
27 27
 
28 28
   it 'raises for out of bounds array index' do
29 29
     expect {
30 30
       AST::Index.new(
31
-        AST::Array.new([
32
-          AST::Number.new(1.0),
33
-          AST::Number.new(2.0),
34
-          AST::Number.new(3.0),
35
-        ]),
31
+        AST::Array.new(
32
+          [AST::Number.new(1.0), AST::Number.new(2.0), AST::Number.new(3.0)]
33
+        ),
36 34
         AST::Number.new(3.0)
37
-      ).execute(Environment.new)
38
-    }.to raise_error("Array index out of bounds")
35
+      )
36
+        .execute(Environment.new)
37
+    }.to raise_error('Array index out of bounds')
39 38
   end
40 39
 
41 40
   it 'raises for non-integral key on array' do
42 41
     expect {
43 42
       AST::Index.new(
44
-        AST::Array.new([
45
-          AST::Number.new(1.0),
46
-          AST::Number.new(2.0),
47
-          AST::Number.new(3.0),
48
-        ]),
43
+        AST::Array.new(
44
+          [AST::Number.new(1.0), AST::Number.new(2.0), AST::Number.new(3.0)]
45
+        ),
49 46
         AST::Number.new(1.5)
50
-      ).execute(Environment.new)
51
-    }.to raise_error("Array index requires integer key")
47
+      )
48
+        .execute(Environment.new)
49
+    }.to raise_error('Array index requires integer key')
52 50
 
53 51
     expect {
54 52
       AST::Index.new(
55
-        AST::Array.new([
56
-          AST::Number.new(1.0),
57
-          AST::Number.new(2.0),
58
-          AST::Number.new(3.0),
59
-        ]),
60
-        AST::String.new("5")
61
-      ).execute(Environment.new)
62
-    }.to raise_error("Array index requires integer key")
53
+        AST::Array.new(
54
+          [AST::Number.new(1.0), AST::Number.new(2.0), AST::Number.new(3.0)]
55
+        ),
56
+        AST::String.new('5')
57
+      )
58
+        .execute(Environment.new)
59
+    }.to raise_error('Array index requires integer key')
63 60
 
64 61
     expect {
65 62
       AST::Index.new(
66
-        AST::Array.new([
67
-          AST::Number.new(1.0),
68
-          AST::Number.new(2.0),
69
-          AST::Number.new(3.0),
70
-        ]),
63
+        AST::Array.new(
64
+          [AST::Number.new(1.0), AST::Number.new(2.0), AST::Number.new(3.0)]
65
+        ),
71 66
         AST::Atom.new(:atom)
72
-      ).execute(Environment.new)
73
-    }.to raise_error("Array index requires integer key")
67
+      )
68
+        .execute(Environment.new)
69
+    }.to raise_error('Array index requires integer key')
74 70
   end
75 71
 
76 72
   it 'raises for missing hash key' do
77 73
     expect {
78 74
       AST::Index.new(
79
-        AST::Hash.new({
80
-          :a => AST::Number.new(1.0),
81
-          :b => AST::Number.new(2.0),
82
-          :c => AST::Number.new(3.0),
83
-        }),
75
+        AST::Hash.new(
76
+          {
77
+            a: AST::Number.new(1.0),
78
+            b: AST::Number.new(2.0),
79
+            c: AST::Number.new(3.0)
80
+          }
81
+        ),
84 82
         AST::Atom.new(:d)
85
-      ).execute(Environment.new)
86
-    }.to raise_error("Key d does not exist on object")
83
+      )
84
+        .execute(Environment.new)
85
+    }.to raise_error('Key d does not exist on object')
87 86
   end
88 87
 end

+ 7
- 10
spec/ast/variable_declaration_spec.rb View File

@@ -1,24 +1,21 @@
1 1
 RSpec.describe AST::VariableDeclaration do
2 2
   it 'defines a variable' do
3 3
     env = Environment.new
4
-    AST::VariableDeclaration.new(
5
-      AST::Identifier.new('x'),
6
-      AST::Number.new(5.0),
7
-    ).execute(env)
4
+    AST::VariableDeclaration.new(AST::Identifier.new('x'), AST::Number.new(5.0))
5
+      .execute(env)
8 6
     expect(env.get('x').execute(env)).to eq(5.0)
9 7
   end
10 8
 
11 9
   it 'raises if a variable is already defined' do
12 10
     env = Environment.new
13
-    AST::VariableDeclaration.new(
14
-      AST::Identifier.new('x'),
15
-      AST::Number.new(5.0),
16
-    ).execute(env)
11
+    AST::VariableDeclaration.new(AST::Identifier.new('x'), AST::Number.new(5.0))
12
+      .execute(env)
17 13
     expect {
18 14
       AST::VariableDeclaration.new(
19 15
         AST::Identifier.new('x'),
20
-        AST::Number.new(6.0),
21
-      ).execute(env)
16
+        AST::Number.new(6.0)
17
+      )
18
+        .execute(env)
22 19
     }.to raise_error('Invalid declaration of previously declared variable x')
23 20
   end
24 21
 end

+ 4
- 4
spec/lexer_spec.rb View File

@@ -143,7 +143,7 @@ RSpec.describe Lexer do
143 143
       [
144 144
         Token.new(TokenKinds::FOR),
145 145
         Token.new(TokenKinds::IN),
146
-        Token.new(TokenKinds::EOF),
146
+        Token.new(TokenKinds::EOF)
147 147
       ]
148 148
     )
149 149
   end
@@ -152,7 +152,7 @@ RSpec.describe Lexer do
152 152
     expect(Lexer.new('"hello \"world\""').scan_all).to eq(
153 153
       [
154 154
         Token.new(TokenKinds::STRING, 'hello "world"'),
155
-        Token.new(TokenKinds::EOF),
155
+        Token.new(TokenKinds::EOF)
156 156
       ]
157 157
     )
158 158
   end
@@ -162,7 +162,7 @@ RSpec.describe Lexer do
162 162
       [
163 163
         Token.new(TokenKinds::STRING, "hello\nworld"),
164 164
         Token.new(TokenKinds::STRING, "hello\tworld"),
165
-        Token.new(TokenKinds::EOF),
165
+        Token.new(TokenKinds::EOF)
166 166
       ]
167 167
     )
168 168
   end
@@ -174,7 +174,7 @@ RSpec.describe Lexer do
174 174
         Token.new(TokenKinds::LPAREN),
175 175
         Token.new(TokenKinds::NUMBER, 5),
176 176
         Token.new(TokenKinds::RPAREN),
177
-        Token.new(TokenKinds::EOF),
177
+        Token.new(TokenKinds::EOF)
178 178
       ]
179 179
     )
180 180
   end

+ 10
- 19
spec/parser_spec.rb View File

@@ -369,9 +369,7 @@ RSpec.describe Parser do
369 369
             [
370 370
               AST::FunctionCall.new(
371 371
                 AST::Identifier.new('print'),
372
-                [
373
-                  AST::Identifier.new('x')
374
-                ]
372
+                [AST::Identifier.new('x')]
375 373
               )
376 374
             ]
377 375
           )
@@ -393,7 +391,7 @@ RSpec.describe Parser do
393 391
                 AST::Binary.new(
394 392
                   AST::Operators::ADD,
395 393
                   AST::Identifier.new('x'),
396
-                  AST::Identifier.new('y'),
394
+                  AST::Identifier.new('y')
397 395
                 )
398 396
               ]
399 397
             )
@@ -404,7 +402,9 @@ RSpec.describe Parser do
404 402
   end
405 403
 
406 404
   it 'allows returning an anonymous function from another function' do
407
-    expect(parse('function outer() { function() { print("Hello world"); }; }')).to eq(
405
+    expect(
406
+      parse('function outer() { function() { print("Hello world"); }; }')
407
+    ).to eq(
408 408
       [
409 409
         AST::FunctionDefinition.new(
410 410
           AST::Identifier.new('outer'),
@@ -418,9 +418,7 @@ RSpec.describe Parser do
418 418
                   [
419 419
                     AST::FunctionCall.new(
420 420
                       AST::Identifier.new('print'),
421
-                      [
422
-                        AST::String.new('Hello world')
423
-                      ]
421
+                      [AST::String.new('Hello world')]
424 422
                     )
425 423
                   ]
426 424
                 )
@@ -432,7 +430,7 @@ RSpec.describe Parser do
432 430
     )
433 431
   end
434 432
 
435
-	it 'allows toplevel function expressions' do
433
+  it 'allows toplevel function expressions' do
436 434
     expect(parse('function() { print("Hello world"); };')).to eq(
437 435
       [
438 436
         AST::FunctionDefinition.new(
@@ -442,25 +440,18 @@ RSpec.describe Parser do
442 440
             [
443 441
               AST::FunctionCall.new(
444 442
                 AST::Identifier.new('print'),
445
-                [
446
-                  AST::String.new('Hello world')
447
-                ]
443
+                [AST::String.new('Hello world')]
448 444
               )
449 445
             ]
450 446
           )
451 447
         )
452 448
       ]
453 449
     )
454
-	end
450
+  end
455 451
 
456 452
   it 'parses assignments' do
457 453
     expect(parse('x = 5;')).to eq(
458
-      [
459
-        AST::Assignment.new(
460
-          AST::Identifier.new('x'),
461
-          AST::Number.new(5.0),
462
-        )
463
-      ]
454
+      [AST::Assignment.new(AST::Identifier.new('x'), AST::Number.new(5.0))]
464 455
     )
465 456
   end
466 457
 end

Loading…
Cancel
Save