alphalang 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 03e9f2d6c36766c890ac954b41a9bf02f2fe70e1f4ac4a3d40843ff03c228b28
4
- data.tar.gz: bd5b500b199a1e32964a311847a28f39ca3b6059c2f06e0e8fcbd608106a641a
3
+ metadata.gz: 3ab6d2758dc063b20d389b6933acbe56ab78fda4ba97f0520cc9cddb5b79701d
4
+ data.tar.gz: f14a27f54b3ab0885484ecc800bd64954bb0bce0c4740a4c1ee7113d47606a3a
5
5
  SHA512:
6
- metadata.gz: 9ad4f5de676ecdd7afc6d630c4a3934aba3d45623a8b45e19196574b7453641b077c5c5ac3a2d1c4edad06cacfaa17b28d7abf778fd894c7c4494c01a9d56654
7
- data.tar.gz: 39f631c8dff1ecd6879786cee70656cb6c6b018db070d49d8c7e5cc6fccf8de018ba0ccf0608cdf11c00670f36d63eb2dcce5780c4cd2122b5b0a4cbc6e1ab9a
6
+ metadata.gz: ceddb38213e3e95d0156a12e0fdd5461cf7880d0c517603ca2117276f5f5db22630598afa6e8d0d66aa4865b3eacc947b09edc78f3660c20e9e2c07e4a633b5b
7
+ data.tar.gz: 347feeb1bc8ec3fa97f4b1bbd529bb071894f604e9bd534749cd4ab198e83c20da0c02c0f3d4ba016d5e249937ccc3f9ee33235d538262bf3ae0279a705c60b3
data/lib/alpha.rb CHANGED
@@ -36,31 +36,48 @@ class LangParser
36
36
  match(:stmt)
37
37
  end
38
38
 
39
+ ###################### Statement Rules
40
+
39
41
  rule :stmt do
40
42
  match(:if_comp_stmt)
43
+ match(:loop)
44
+ match(:declaration)
45
+ match(:builtin)
46
+ match(:expr_stmt)
47
+ end
48
+
49
+ rule :loop do
41
50
  match(:while_stmt)
51
+ end
52
+
53
+ rule :declaration do
42
54
  match(:func_dec)
43
55
  match(:ass_stmt)
56
+ end
57
+
58
+ rule :builtin do
44
59
  match(:pause_stmt)
45
- match(:expr_stmt)
60
+ match(:print_stmt)
46
61
  end
47
62
 
63
+ ###################### If Statement Rules
64
+
48
65
  rule :if_comp_stmt do
49
66
  match(:if_stmt, :comp_elseif, :else_stmt, :STOP) { |a, b, c| IfCompStmtNode.new(a, b, c) }
50
67
  match(:if_stmt, :else_stmt, :STOP) { |a, b| IfCompStmtNode.new(a, b) }
51
68
  match(:if_stmt, :STOP) { |a| IfCompStmtNode.new(a) }
52
69
  end
53
70
 
54
- rule :comp_elseif do
55
- match(:elseif_stmt, :comp_elseif) { |a, b| [a, b] }
56
- match(:elseif_stmt)
57
- end
58
-
59
71
  rule :if_stmt do
60
72
  match(:IF, :expr_stmt, :and, :expr_stmt, :comp_stmt) { |_, a, b, c, d| IfCompactNode.new(a, b, c, d) }
61
73
  match(:IF, :expr_stmt, :comp_stmt) { |_, a, b| IfNode.new(a, b) }
62
74
  end
63
75
 
76
+ rule :comp_elseif do
77
+ match(:elseif_stmt, :comp_elseif) { |a, b| [a, b] }
78
+ match(:elseif_stmt)
79
+ end
80
+
64
81
  rule :elseif_stmt do
65
82
  match(:ELSEIF, :expr_stmt, :comp_stmt) { |_, a, b| ElseifNode.new(a, b) }
66
83
  end
@@ -69,12 +86,16 @@ class LangParser
69
86
  match(:ELSE, :comp_stmt) { |_, b| ElseNode.new(b) }
70
87
  end
71
88
 
89
+ ###################### Loop Rules
90
+
72
91
  rule :while_stmt do
73
92
  match(:WHILE, :expr_stmt, :comp_stmt, :STOP) { |_, a, b| WhileLoopNode.new(a, b) }
74
93
  end
75
94
 
95
+ ###################### Variable and Function Declare Rules
96
+
76
97
  rule :func_dec do
77
- match(:DEF, :member, :comp_stmt, :STOP) { |_, name, value, _| FunctionDecNode.new(name, value) }
98
+ match(:DEF, :call_member, :comp_stmt, :STOP) { |_, name, value, _| FunctionDecNode.new(name, value) }
78
99
  end
79
100
 
80
101
  rule :ass_stmt do
@@ -82,10 +103,18 @@ class LangParser
82
103
  match(:member, '=', :array_stmt) { |var, _, value| VariableDecNode.new(var, value) }
83
104
  end
84
105
 
106
+ ###################### Built-In Function Rules
107
+
85
108
  rule :pause_stmt do
86
109
  match(:PAUSE, :expr) { |_, a| PauseNode.new(a) }
87
110
  end
88
111
 
112
+ rule :print_stmt do
113
+ match(:PRINT, :expr) { |_, a| PrintNode.new(a) }
114
+ end
115
+
116
+ ###################### Expression Rules
117
+
89
118
  rule :expr_stmt do
90
119
  match(:or_stmt)
91
120
  match(:and_stmt)
@@ -105,15 +134,6 @@ class LangParser
105
134
  match(:NOT, :expr_stmt) { |_, b| NotNode.new(b) }
106
135
  end
107
136
 
108
- rule :array_stmt do
109
- match('[', :arg_list, ']') { |_, array, _| array }
110
- end
111
-
112
- rule :arg_list do
113
- match(:expr, ',', :arg_list) { |a, _, b| ArrayNode.new(a, b) }
114
- match(:expr) { |a| ArrayNode.new(a, NilClass) }
115
- end
116
-
117
137
  rule :expr do
118
138
  match(:expr, /(<|>)/, :expr) { |a, op, b| CompareNode.new(a, op, b) }
119
139
  match(/(<|>)/, :expr) { |op, b| CompareNode.new(nil, op, b) }
@@ -131,25 +151,46 @@ class LangParser
131
151
  match(:atom)
132
152
  end
133
153
 
154
+ ###################### Data Types Rules
155
+
156
+ rule :array_stmt do
157
+ match('[', :arg_list, ']') { |_, array, _| array }
158
+ end
159
+
160
+ rule :arg_list do
161
+ match(:expr, ',', :arg_list) { |a, _, b| ArrayNode.new(a, b) }
162
+ match(:expr) { |a| ArrayNode.new(a, NilClass) }
163
+ end
164
+
134
165
  rule :atom do
135
- match('-', /\d+/, '.', /\d+/) { |neg, a, dot, b| NumberNode.new((neg+a+dot+b)) }
136
- match(/\d+/, '.', /\d+/) { |a, dot, b| NumberNode.new((a+dot+b)) }
137
- match('-', /\d+/) { |neg, a| NumberNode.new(neg + a) }
138
- match(/\d+/) { |a| NumberNode.new(a) }
166
+ match(:number)
167
+ match(:boolean)
168
+ match(:call_member)
169
+ match(:prio_stmt)
170
+ end
171
+
172
+ rule :boolean do
139
173
  match(ScopeManager.true_value) { |a| BoolNode.new(a) }
140
174
  match(ScopeManager.false_value) { |a| BoolNode.new(a) }
141
- match(:PRINT, :member) { |_, a| PrintNode.new(a) }
142
- match(:PRINT, :expr) { |_, a| PrintNode.new(a) }
143
- match(:member)
144
- match(:prio_stmt)
175
+ end
176
+
177
+ rule :number do
178
+ match('-', /\d+/, '.', /\d+/) { |neg, a, dot, b| NumberNode.new(neg + a + dot + b) }
179
+ match(/\d+/, '.', /\d+/) { |a, dot, b| NumberNode.new(a + dot + b) }
180
+ match('-', /\d+/) { |neg, a| NumberNode.new(neg + a) }
181
+ match(/\d+/) { |a| NumberNode.new(a) }
182
+ end
183
+
184
+ rule :call_member do
185
+ match(:member, '(', :arg_list, ')') { |var, _, args, _| FuncCallNode.new(var, args) }
186
+ match(:member, '(', ')') { |var, _, _| FuncCallNode.new(var, NilClass) }
187
+ match(:member, '[', '-', /\d+/, ']') { |var, _, neg, index, _| ArrayCallNode.new(var, (neg+index)) }
188
+ match(:member, '[', /\d+/, ']') { |var, _, index, _| ArrayCallNode.new(var, index) }
189
+ match(:member) { |var| VariableCallNode.new(var) }
145
190
  end
146
191
 
147
192
  rule :member do
148
- match(/[a-z]/, '(', :arg_list, ')') { |var, _, args, _| FuncCallNode.new(var, args) }
149
- match(/[a-z]/, '(', ')') { |var, _, _| FuncCallNode.new(var, NilClass) }
150
- match(/[a-z]/, '[', '-', /\d+/, ']') { |var, _, neg, index, _| ArrayCallNode.new(var, (neg+index)) }
151
- match(/[a-z]/, '[', /\d+/, ']') { |var, _, index, _| ArrayCallNode.new(var, index) }
152
- match(/[a-z]/) { |var| VariableCallNode.new(var) }
193
+ match(/[a-z]/)
153
194
  end
154
195
 
155
196
  rule :prio_stmt do
@@ -188,14 +188,7 @@ class PrintNode
188
188
  print "Array #{@value.name}: " unless TEST_UNIT_ACTIVE
189
189
  puts @value.evaluate.join(', ') unless TEST_UNIT_ACTIVE
190
190
  else
191
- if @value.evaluate == PrintNode
192
- if @value.value.is_a?(VariableCallNode)
193
- raise SyntaxError, "You have a duplicate 'print' statement! Printing #{@value.value.name} twice."
194
- else
195
- raise SyntaxError, "You have a duplicate 'print' statement! Printing #{@value.value} twice."
196
- end
197
- end
198
- puts @value.evaluate unless TEST_UNIT_ACTIVE
191
+ puts @value.evaluate unless TEST_UNIT_ACTIVE
199
192
  end
200
193
  self.class
201
194
  end
data/lib/rdparse.rb CHANGED
@@ -187,81 +187,56 @@ class Parser
187
187
  end # until
188
188
  end
189
189
 
190
- def find_faulty_expression_within_block(block_start_line, block_end_line, problematic_token)
191
- if block_start_line == nil || block_end_line == nil
192
- raise ParseError, "Error with <#{problematic_token}> somewhere around line #{block_end_line}#{block_start_line}"
193
- end
194
- index = 0
195
- result = []
196
- @file_string.each_line do |line|
197
- if index > block_start_line and index < block_end_line
198
-
199
- if line.match?(/;;.*$/)
200
- line = line.split(';;')[0]
201
- end
190
+ def convert_regex_sensitive_token(token)
191
+ token = @token_list['end'] if token == :STOP # fulhack pga :END påstods inte funka för länge sen
192
+ token = '[(]' if token == '('
193
+ token = '[)]' if token == ')'
194
+ token = '[+]' if token == '+'
195
+ token = '[-]' if token == '-'
196
+ token = '[*]' if token == '*'
197
+ token = '[/]' if token == '/'
198
+ token = "#{@token_list['(not|!)'].split('|')[0][1..]}" if token == :NOT
199
+ token = "#{@token_list['(and|&&)'].split('|')[0][1..]}" if token == :AND
200
+ token = "#{@token_list['(or|\|\|)'].split('|')[0][1..]}" if token == :OR
201
+ token
202
+ end
202
203
 
203
- if line.match?(/#{problematic_token}\s+$/)
204
- result << "ParseError, #{line.strip}() is missing an expression on line #{index + 1}"
205
- end
206
- end
207
- index += 1
204
+ def translate_tokens_array(array)
205
+ result = []
206
+ array.each do |token|
207
+ token = convert_regex_sensitive_token(token)
208
+ result << token unless token.is_a?(Symbol)
209
+ result << @token_list[token.to_s.downcase] if token.is_a?(Symbol)
208
210
  end
209
- raise ParseError, result[-1]
211
+ result
210
212
  end
211
213
 
212
- def find_faulty_expression_within_ends
213
- puts 'TODO: Implement search for faulty token between two of the same guides'
214
- raise ParseError, "Most likely mismatched '#{@token_list['END'.downcase]}' token."
214
+ def find_surrounding_code(problem_pos)
215
+ tokens_before_problem = []
216
+ temp = problem_pos
217
+ while temp >= 0
218
+ tokens_before_problem << @tokens[temp]
219
+ temp -= 1
220
+ end
221
+ tokens_before_problem.reverse
215
222
  end
216
223
 
217
224
  def find_faulty_line
218
- if @tokens[@max_pos-0].is_a?(NilClass)
219
- if @max_pos >= 3
220
- puts "Problem is in the end of the file after:\n #{@tokens[@max_pos - 3]}#{@tokens[@max_pos - 2]}#{@tokens[@max_pos - 1]}"
221
- else
222
- puts "Problem is right in the beginning of the file."
223
- end
224
- raise ParseError
225
- end
226
-
227
- problematic_token = "#{@tokens[@max_pos - 1].downcase}"
228
- before_problem = "#{@tokens[@max_pos - 2].downcase}"
229
- after_problem = "#{@tokens[@max_pos - 0].downcase}"
230
-
231
- after_problem = "[" + after_problem + "]" if !after_problem.match?(/[a-z]/)
232
- before_problem = "[" + before_problem + "]" if !before_problem.match?(/[a-z]/)
225
+ tokens_before_problem = find_surrounding_code(@max_pos - 1)
226
+ file_as_array_without_whitespaces = translate_tokens_array(tokens_before_problem)
233
227
 
234
- translated_problematic_token = @token_list[problematic_token]
228
+ pattern = file_as_array_without_whitespaces.join('\s*')
229
+ regex = Regexp.new(pattern)
235
230
 
236
- if translated_problematic_token.is_a?(NilClass)
237
- puts "The problem is #{before_problem}#{problematic_token}#{after_problem}"
238
- raise ParseError, "Faulty expression, this is not completely implemented."
239
- end
231
+ cleaned_string = @file_string.gsub(/;;.*/, '')
240
232
 
241
- index = 0
242
- problem_match_hash = {}
243
- found_end = false
244
- @file_string.each_line do |line|
233
+ match_data = regex.match(cleaned_string)
234
+ num_lines = match_data[0].count("\n") + 1
245
235
 
246
- if line.match?(before_problem) && found_end == false
247
- problem_match_hash[before_problem] = index
248
- end
236
+ problem = @tokens[@max_pos]
237
+ problem = @token_list[problem.to_s.downcase] unless @token_list[problem.to_s.downcase].is_a?(NilClass)
249
238
 
250
- if line.match?(after_problem) && index > problem_match_hash[before_problem]
251
- problem_match_hash[after_problem] = index
252
- found_end = true
253
- end
254
- index += 1
255
- end
256
-
257
- non_empty_line_before_problem = problem_match_hash[before_problem]
258
- non_empty_line_after_problem = problem_match_hash[after_problem]
259
-
260
- unless after_problem == before_problem
261
- find_faulty_expression_within_block(non_empty_line_before_problem, non_empty_line_after_problem, problematic_token)
262
- else
263
- find_faulty_expression_within_ends
264
- end
239
+ raise ParseError, "There is a problem around line #{num_lines}. Found <#{problem}>"
265
240
  end
266
241
 
267
242
  def parse(string)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alphalang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - mattias, victor
@@ -74,8 +74,6 @@ files:
74
74
  - lib/nodes/scopemanager.rb
75
75
  - lib/nodes/stmtnodes.rb
76
76
  - lib/rdparse.rb
77
- - lib/tester/assign.alpha
78
- - lib/tester/assign.alpha~
79
77
  - lib/tester/demo_de.alpha
80
78
  - lib/tester/demo_emoji.alpha
81
79
  - lib/tester/demo_emoji.alpha~
@@ -1,14 +0,0 @@
1
- x = 1
2
-
3
- def foo(bar)
4
- y = 0
5
- while bar < 20
6
- print bar
7
- bar = bar + 1
8
- y = bar
9
- end
10
- print y
11
- y
12
- end
13
-
14
- foo(x)
@@ -1,5 +0,0 @@
1
- x = 1
2
-
3
- def foo(bar)
4
- y = bar
5
- end