Browse Source

Prettier

master
Dylan Baker 5 years ago
parent
commit
76bc66d472

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

9
 
9
 
10
   def ==(other)
10
   def ==(other)
11
     other.is_a?(AST::ForLoop) && other.iterator == @iterator &&
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
   end
14
   end
14
 
15
 
15
   def execute(env)
16
   def execute(env)
16
     @iterable.execute(env).each do |i|
17
     @iterable.execute(env).each do |i|
17
       _env = Environment.new
18
       _env = Environment.new
18
       _env.set(@iterator.name, i)
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
     end
21
     end
23
   end
22
   end
24
 end
23
 end

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

183
 
183
 
184
   def scan_all
184
   def scan_all
185
     tokens = Array.new
185
     tokens = Array.new
186
-    until at_end
187
-      tokens << get_token
188
-    end
186
+    tokens << get_token until at_end
189
     tokens << Token.new(TokenKinds::EOF)
187
     tokens << Token.new(TokenKinds::EOF)
190
   end
188
   end
191
 
189
 
192
   private
190
   private
193
 
191
 
194
   def skip_whitespace
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
   end
194
   end
199
 
195
 
200
   def skip_comment
196
   def skip_comment

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

20
     elsif @current_token.type == TokenKinds::FUNCTION
20
     elsif @current_token.type == TokenKinds::FUNCTION
21
       next_token = @lexer.peek
21
       next_token = @lexer.peek
22
       if next_token && next_token.type == TokenKinds::IDENTIFIER
22
       if next_token && next_token.type == TokenKinds::IDENTIFIER
23
-				function_definition
23
+        function_definition
24
       else
24
       else
25
-				expression_statement
25
+        expression_statement
26
       end
26
       end
27
     elsif @current_token.type == TokenKinds::CLASS
27
     elsif @current_token.type == TokenKinds::CLASS
28
       class_definition
28
       class_definition
29
     elsif @current_token.type == TokenKinds::FOR
29
     elsif @current_token.type == TokenKinds::FOR
30
       for_loop
30
       for_loop
31
     else
31
     else
32
-			expression_statement
32
+      expression_statement
33
     end
33
     end
34
   end
34
   end
35
 
35
 
36
-	def expression_statement
36
+  def expression_statement
37
     expr = expression
37
     expr = expression
38
     eat(TokenKinds::SEMICOLON)
38
     eat(TokenKinds::SEMICOLON)
39
     return expr
39
     return expr
40
-	end
40
+  end
41
 
41
 
42
   def variable_declaration
42
   def variable_declaration
43
     eat(TokenKinds::LET)
43
     eat(TokenKinds::LET)
171
     expr = binary
171
     expr = binary
172
 
172
 
173
     if @current_token.type == TokenKinds::EQUALS
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
       eat(TokenKinds::EQUALS)
177
       eat(TokenKinds::EQUALS)
176
       value = expression
178
       value = expression
177
       AST::Assignment.new(expr, value)
179
       AST::Assignment.new(expr, value)
370
 
372
 
371
   def eat(type)
373
   def eat(type)
372
     token = @current_token
374
     token = @current_token
375
+
373
     if token.type == type
376
     if token.type == type
374
       advance
377
       advance
375
       token
378
       token

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

1
 RSpec.describe AST::Assignment do
1
 RSpec.describe AST::Assignment do
2
   it 'reassigns an existing variable' do
2
   it 'reassigns an existing variable' do
3
     env = Environment.new
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
     expect(env.get('x').execute(env)).to eq(6.0)
9
     expect(env.get('x').execute(env)).to eq(6.0)
13
   end
10
   end
14
 
11
 
15
   it 'raises for an undeclared variable' do
12
   it 'raises for an undeclared variable' do
16
     env = Environment.new
13
     env = Environment.new
17
     expect {
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
   end
18
   end
25
 end
19
 end

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

1
 RSpec.describe AST::ClassDefinition do
1
 RSpec.describe AST::ClassDefinition do
2
   it 'defines a class' do
2
   it 'defines a class' do
3
     env = Environment.new(env)
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
     klass.execute(env)
5
     klass.execute(env)
8
     expect(env.get('Class')).to eq(klass)
6
     expect(env.get('Class')).to eq(klass)
9
   end
7
   end

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

7
             AST::Binary.new(
7
             AST::Binary.new(
8
               AST::Operators::GREATER_THAN,
8
               AST::Operators::GREATER_THAN,
9
               AST::Number.new(5.0),
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
   end
18
   end
22
 
19
 
23
   it 'evaluates an else statement' do
20
   it 'evaluates an else statement' do
28
             AST::Binary.new(
25
             AST::Binary.new(
29
               AST::Operators::GREATER_THAN,
26
               AST::Operators::GREATER_THAN,
30
               AST::Number.new(3.0),
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
           AST::Branch.new(
32
           AST::Branch.new(
40
             AST::Boolean.new(true),
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
   end
40
   end
51
 
41
 
52
   it 'evaluates an else if statement' do
42
   it 'evaluates an else if statement' do
57
             AST::Binary.new(
47
             AST::Binary.new(
58
               AST::Operators::GREATER_THAN,
48
               AST::Operators::GREATER_THAN,
59
               AST::Number.new(3.0),
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
           AST::Branch.new(
54
           AST::Branch.new(
69
             AST::Binary.new(
55
             AST::Binary.new(
70
               AST::Operators::GREATER_THAN,
56
               AST::Operators::GREATER_THAN,
71
               AST::Number.new(3.0),
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
           AST::Branch.new(
62
           AST::Branch.new(
81
             AST::Boolean.new(true),
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
   end
70
   end
92
 end
71
 end

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

4
       AST::ForLoop.new(
4
       AST::ForLoop.new(
5
         AST::Identifier.new('x'),
5
         AST::Identifier.new('x'),
6
         AST::Array.new(
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
         AST::Block.new(
9
         AST::Block.new(
14
           [
10
           [
15
             AST::FunctionCall.new(
11
             AST::FunctionCall.new(
16
               AST::Identifier.new('print'),
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
     }.to output("1.0\n2.0\n3.0\n").to_stdout
19
     }.to output("1.0\n2.0\n3.0\n").to_stdout
25
   end
20
   end
26
 end
21
 end

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

4
       AST::FunctionCall.new(
4
       AST::FunctionCall.new(
5
         AST::Identifier.new('print'),
5
         AST::Identifier.new('print'),
6
         [AST::String.new('hello world')]
6
         [AST::String.new('hello world')]
7
-      ).execute(Environment.new)
7
+      )
8
+        .execute(Environment.new)
8
     }.to output("hello world\n").to_stdout
9
     }.to output("hello world\n").to_stdout
9
   end
10
   end
10
 
11
 
13
     AST::FunctionDefinition.new(
14
     AST::FunctionDefinition.new(
14
       AST::Identifier.new('add_one'),
15
       AST::Identifier.new('add_one'),
15
       [AST::Identifier.new('n')],
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
     expect(
28
     expect(
25
       AST::FunctionCall.new(
29
       AST::FunctionCall.new(
26
         AST::Identifier.new('add_one'),
30
         AST::Identifier.new('add_one'),
27
         [AST::Number.new(5.0)]
31
         [AST::Number.new(5.0)]
28
-      ).execute(env)
32
+      )
33
+        .execute(env)
29
     ).to eq(6.0)
34
     ).to eq(6.0)
30
   end
35
   end
31
 
36
 
43
     AST::FunctionDefinition.new(
48
     AST::FunctionDefinition.new(
44
       AST::Identifier.new('factorial'),
49
       AST::Identifier.new('factorial'),
45
       [AST::Identifier.new('n')],
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
                 AST::Binary.new(
56
                 AST::Binary.new(
63
-                  AST::Operators::MULTIPLY,
57
+                  AST::Operators::DOUBLE_EQUALS,
64
                   AST::Identifier.new('n'),
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
     expect(
90
     expect(
83
       AST::FunctionCall.new(
91
       AST::FunctionCall.new(
84
         AST::Identifier.new('factorial'),
92
         AST::Identifier.new('factorial'),
85
         [AST::Number.new(5.0)]
93
         [AST::Number.new(5.0)]
86
-      ).execute(env)
94
+      )
95
+        .execute(env)
87
     ).to eq(120.0)
96
     ).to eq(120.0)
88
   end
97
   end
89
 
98
 
102
     AST::FunctionDefinition.new(
111
     AST::FunctionDefinition.new(
103
       AST::Identifier.new('add_one'),
112
       AST::Identifier.new('add_one'),
104
       [AST::Identifier.new('n')],
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
       AST::FunctionCall.new(
130
       AST::FunctionCall.new(
123
         AST::Identifier.new('print'),
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
   end
136
   end
130
 
137
 
131
   # let add_one = function(n) { n + 1; };
138
   # let add_one = function(n) { n + 1; };
137
       AST::Identifier.new('add_one'),
144
       AST::Identifier.new('add_one'),
138
       AST::FunctionDefinition.new(
145
       AST::FunctionDefinition.new(
139
         nil,
146
         nil,
140
-        [
141
-          AST::Identifier.new('n'),
142
-        ],
147
+        [AST::Identifier.new('n')],
143
         AST::Block.new(
148
         AST::Block.new(
144
           [
149
           [
145
             AST::Binary.new(
150
             AST::Binary.new(
150
           ]
155
           ]
151
         )
156
         )
152
       )
157
       )
153
-    ).execute(env)
158
+    )
159
+      .execute(env)
154
 
160
 
155
     AST::VariableDeclaration.new(
161
     AST::VariableDeclaration.new(
156
       AST::Identifier.new('do'),
162
       AST::Identifier.new('do'),
157
       AST::FunctionDefinition.new(
163
       AST::FunctionDefinition.new(
158
         nil,
164
         nil,
159
-        [
160
-          AST::Identifier.new('f'),
161
-          AST::Identifier.new('x'),
162
-        ],
165
+        [AST::Identifier.new('f'), AST::Identifier.new('x')],
163
         AST::Block.new(
166
         AST::Block.new(
164
           [
167
           [
165
             AST::FunctionCall.new(
168
             AST::FunctionCall.new(
166
               AST::Identifier.new('f'),
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
     expect(result).to eq(6)
185
     expect(result).to eq(6)
185
   end
186
   end

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

2
   it 'evaluates array index' do
2
   it 'evaluates array index' do
3
     expect(
3
     expect(
4
       AST::Index.new(
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
         AST::Number.new(2.0)
8
         AST::Number.new(2.0)
11
-      ).execute(Environment.new)
9
+      )
10
+        .execute(Environment.new)
12
     ).to eq(3.0)
11
     ).to eq(3.0)
13
   end
12
   end
14
 
13
 
16
     expect(
15
     expect(
17
       AST::Index.new(
16
       AST::Index.new(
18
         AST::Hash.new(
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
         AST::Atom.new(:b)
22
         AST::Atom.new(:b)
24
-      ).execute(Environment.new)
23
+      )
24
+        .execute(Environment.new)
25
     ).to eq(2.0)
25
     ).to eq(2.0)
26
   end
26
   end
27
 
27
 
28
   it 'raises for out of bounds array index' do
28
   it 'raises for out of bounds array index' do
29
     expect {
29
     expect {
30
       AST::Index.new(
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
         AST::Number.new(3.0)
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
   end
38
   end
40
 
39
 
41
   it 'raises for non-integral key on array' do
40
   it 'raises for non-integral key on array' do
42
     expect {
41
     expect {
43
       AST::Index.new(
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
         AST::Number.new(1.5)
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
     expect {
51
     expect {
54
       AST::Index.new(
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
     expect {
61
     expect {
65
       AST::Index.new(
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
         AST::Atom.new(:atom)
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
   end
70
   end
75
 
71
 
76
   it 'raises for missing hash key' do
72
   it 'raises for missing hash key' do
77
     expect {
73
     expect {
78
       AST::Index.new(
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
         AST::Atom.new(:d)
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
   end
86
   end
88
 end
87
 end

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

1
 RSpec.describe AST::VariableDeclaration do
1
 RSpec.describe AST::VariableDeclaration do
2
   it 'defines a variable' do
2
   it 'defines a variable' do
3
     env = Environment.new
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
     expect(env.get('x').execute(env)).to eq(5.0)
6
     expect(env.get('x').execute(env)).to eq(5.0)
9
   end
7
   end
10
 
8
 
11
   it 'raises if a variable is already defined' do
9
   it 'raises if a variable is already defined' do
12
     env = Environment.new
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
     expect {
13
     expect {
18
       AST::VariableDeclaration.new(
14
       AST::VariableDeclaration.new(
19
         AST::Identifier.new('x'),
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
     }.to raise_error('Invalid declaration of previously declared variable x')
19
     }.to raise_error('Invalid declaration of previously declared variable x')
23
   end
20
   end
24
 end
21
 end

+ 4
- 4
spec/lexer_spec.rb View File

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

+ 10
- 19
spec/parser_spec.rb View File

369
             [
369
             [
370
               AST::FunctionCall.new(
370
               AST::FunctionCall.new(
371
                 AST::Identifier.new('print'),
371
                 AST::Identifier.new('print'),
372
-                [
373
-                  AST::Identifier.new('x')
374
-                ]
372
+                [AST::Identifier.new('x')]
375
               )
373
               )
376
             ]
374
             ]
377
           )
375
           )
393
                 AST::Binary.new(
391
                 AST::Binary.new(
394
                   AST::Operators::ADD,
392
                   AST::Operators::ADD,
395
                   AST::Identifier.new('x'),
393
                   AST::Identifier.new('x'),
396
-                  AST::Identifier.new('y'),
394
+                  AST::Identifier.new('y')
397
                 )
395
                 )
398
               ]
396
               ]
399
             )
397
             )
404
   end
402
   end
405
 
403
 
406
   it 'allows returning an anonymous function from another function' do
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
         AST::FunctionDefinition.new(
409
         AST::FunctionDefinition.new(
410
           AST::Identifier.new('outer'),
410
           AST::Identifier.new('outer'),
418
                   [
418
                   [
419
                     AST::FunctionCall.new(
419
                     AST::FunctionCall.new(
420
                       AST::Identifier.new('print'),
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
     )
430
     )
433
   end
431
   end
434
 
432
 
435
-	it 'allows toplevel function expressions' do
433
+  it 'allows toplevel function expressions' do
436
     expect(parse('function() { print("Hello world"); };')).to eq(
434
     expect(parse('function() { print("Hello world"); };')).to eq(
437
       [
435
       [
438
         AST::FunctionDefinition.new(
436
         AST::FunctionDefinition.new(
442
             [
440
             [
443
               AST::FunctionCall.new(
441
               AST::FunctionCall.new(
444
                 AST::Identifier.new('print'),
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
   it 'parses assignments' do
452
   it 'parses assignments' do
457
     expect(parse('x = 5;')).to eq(
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
   end
456
   end
466
 end
457
 end

Loading…
Cancel
Save