Class: Shift::Generator::JavaScriptGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/shift-lang/generator/javascript_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJavaScriptGenerator

Returns a new instance of JavaScriptGenerator.



12
13
14
15
16
17
18
# File 'lib/shift-lang/generator/javascript_generator.rb', line 12

def initialize()
	@blocks = []
	@defined_hashes = []
	@functions = []
	@has_models = false
	@builder = Shift::ShiftBuilder.new
end

Instance Attribute Details

#blocksObject

Returns the value of attribute blocks.



10
11
12
# File 'lib/shift-lang/generator/javascript_generator.rb', line 10

def blocks
  @blocks
end

#builderObject

Returns the value of attribute builder.



10
11
12
# File 'lib/shift-lang/generator/javascript_generator.rb', line 10

def builder
  @builder
end

#defined_hashesObject

Returns the value of attribute defined_hashes.



10
11
12
# File 'lib/shift-lang/generator/javascript_generator.rb', line 10

def defined_hashes
  @defined_hashes
end

#functionsObject

Returns the value of attribute functions.



10
11
12
# File 'lib/shift-lang/generator/javascript_generator.rb', line 10

def functions
  @functions
end

#has_modelsObject

Returns the value of attribute has_models.



10
11
12
# File 'lib/shift-lang/generator/javascript_generator.rb', line 10

def has_models
  @has_models
end

#url_variablesObject

Returns the value of attribute url_variables.



10
11
12
# File 'lib/shift-lang/generator/javascript_generator.rb', line 10

def url_variables
  @url_variables
end

Instance Method Details

#apply_transforms(statement, num_tabs) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/shift-lang/generator/javascript_generator.rb', line 299

def apply_transforms(statement, num_tabs)
	tabs = ""
	if (num_tabs > 0)
		num_tabs = num_tabs - 1
	end

	result = ""

	if(@blocks.length > 0)
		if(@blocks[@blocks.length - 1] == num_tabs)
			t = ""
			@blocks.pop.times { t += "\t" }
			result += t + "}\n"
		end
	end		

	num_tabs.times { tabs += "\t" }

	if(statement.is_a?(Hash))
		key = statement.keys[0]
		case key
		when :assignment_statement
			result += expression_transform(statement, num_tabs) + "\n"
		when :write_statement
			result += write_function_transform(statement, num_tabs) + "\n"	
		when :write_file_statement
			result += write_file_function_transform(statement, num_tabs) + "\n"
		when :read_statement
			result += read_statement_transform(statement, num_tabs)
		when :redirect_statement
			result += redirect_statement_transform(statement, num_tabs) + "\n"
		when :arithmetic_expression
			result += value_transform(statement, num_tabs)
		when :boolean_expression
			result += value_transform(statement, num_tabs)
		when :if_statement
			result += control_statement_transform(statement, num_tabs) + "\n"
		when :else_statement
			@blocks.push (num_tabs)
			result += tabs + value_transform(statement, num_tabs) + " {\n"
		when :for_statement
			statement = statement[:for_statement]
			tokens = statement.to_s.split(" ")
			@blocks.push (num_tabs)
			result += tabs + "for(var k in " + tokens[3] + ") {\n" 						
			result += tabs + "\t" + tokens[1] + " = " + tokens[3] + "[k];\n"
		when :while_statement
			result += control_statement_transform(statement, num_tabs) + "\n"								
		when :query
			result += query_transform(statement, num_tabs)
		when :int
			integer_casted_expression_transform(statement, num_tabs)
		else
			result += token_transform(statement, num_tabs)	
		end
	else
		if statement.is_a?(Array)
			concatenated_string_expression_transform(statement, num_tabs)
		else
			result += statement.to_s
		end
	end
end

#concatenated_string_expression_transform(statement, num_tabs) ⇒ Object



286
287
288
289
290
291
292
# File 'lib/shift-lang/generator/javascript_generator.rb', line 286

def concatenated_string_expression_transform(statement, num_tabs)
	statement = statement.map do | token |
		apply_transforms(token, 0)
	end

	statement.join(" + ")
end

#control_statement_transform(statement, num_tabs) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/shift-lang/generator/javascript_generator.rb', line 266

def control_statement_transform(statement, num_tabs)
	tabs = ""
	num_tabs.times { tabs += "\t" }

	key = statement.keys[0]

	result = ""

	@blocks.push (num_tabs)

	case key
	when :if_statement
		result = tabs + "if " + apply_transforms(statement[:if_statement][:condition], 0) + " {"
	when :while_statement
		result = tabs + "while " + apply_transforms(statement[:while_statement][:condition], 0) + " {"			
	end			

	result
end

#expression_transform(statement, num_tabs) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/shift-lang/generator/javascript_generator.rb', line 93

def expression_transform(statement, num_tabs)
	tabs = ""
	num_tabs.times { tabs += "\t" }
	key = statement.keys[0]
	case key
	when :assignment_statement
		extra = ""
		if(statement[:assignment_statement][:left].keys[0] == :hash_value)
			val = statement[:assignment_statement][:left][:hash_value].to_s
			hash_var = val[0..(val.index('[') - 1)]
			if !@defined_hashes.include?(hash_var)
				@defined_hashes.push hash_var
				extra += (tabs + "if (typeof " + hash_var + " == 'undefined')\n")
				extra += (tabs + "\t" + hash_var + " = {}\n")
			end
		end
		
		left = apply_transforms(statement[:assignment_statement][:left], 0) 
		right = apply_transforms(statement[:assignment_statement][:right], 0) 

		if(statement[:assignment_statement][:left].keys[0] == :object_value)
			value_name = statement[:assignment_statement][:left][:object_value].to_s
			value_name = value_name[(value_name.index('.') + 1)..(value_name.length - 1)]
		end

		if(statement[:assignment_statement][:right].is_a?(Hash) && statement[:assignment_statement][:right].keys[0] == :query)
			case statement[:assignment_statement][:right][:query][:operation]
			when "all"
				@functions.push (tabs + "});")
				tabs + right.to_s + left.to_s + ") {"
			when "new"
				tabs + "var " + left.to_s + " = " + right.to_s
			when "find"
				@functions.push (tabs + "});")
				tabs + right.to_s + left.to_s + ") {"
			when "find_by_id"
				@functions.push (tabs + "});")
				tabs + right.to_s + left.to_s + ") {"
			end
		else 
			extra + tabs + left.to_s + " = " + right.to_s + ";"
		end			
	else
		""
	end
end

#get_data(builder) ⇒ Object



20
21
22
# File 'lib/shift-lang/generator/javascript_generator.rb', line 20

def get_data(builder)
	@builder = builder
end

#get_data_from_file(file_name) ⇒ Object



24
25
26
# File 'lib/shift-lang/generator/javascript_generator.rb', line 24

def get_data_from_file(file_name)
	@builder.get_data_from_file file_name
end

#get_import_statementsObject



28
29
30
31
32
33
34
# File 'lib/shift-lang/generator/javascript_generator.rb', line 28

def get_import_statements()
	Shift::Generator::JavaScriptTemplates::IMPORT_JQNODE_STATEMENT + 
	Shift::Generator::JavaScriptTemplates::IMPORT_UTIL_STATEMENT + 
	Shift::Generator::JavaScriptTemplates::IMPORT_MU_STATEMENT + 
	(@has_models ? Shift::Generator::JavaScriptTemplates::IMPORT_DB_STATEMENT : "") + 
	Shift::Generator::JavaScriptTemplates::UTILITY_FUNCTIONS + "\n"
end

#get_model_dataObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/shift-lang/generator/javascript_generator.rb', line 36

def get_model_data()
	result = ""

	model_name_template = ERB.new(Shift::Generator::JavaScriptTemplates::MODEL_NAME_TEMPLATE)

	@builder.models.each do | model |
		model_name = model.name
		result += model_name_template.result(binding) + "\n"

		first = true

		# TODO: Test if comma syntax is valid

		model.attributes.each do | attribute |
			if (attribute[:type] == "string")
				if (first == true)
					first = false
				else
					result += ","
				end
				result += ("\t" + attribute[:name] + " : " + "String" + "\n") 
			else
				if (attribute[:type] == "integer")
					if (first == true)
						first = false
					else
						result += ","
					end						
					result += ("\t" + attribute[:name] + " : " + "Number" + "\n")	
				end
			end
		end

		result += "});\n\n"
	end

	result
end

#integer_casted_expression_transform(statement, num_tabs) ⇒ Object



294
295
296
297
# File 'lib/shift-lang/generator/javascript_generator.rb', line 294

def integer_casted_expression_transform(statement, num_tabs)
	statement.delete(:int)
	"Number(" + apply_transforms(statement, 0) + ")"
end

#query_transform(statement, num_tabs) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/shift-lang/generator/javascript_generator.rb', line 196

def query_transform(statement, num_tabs)
	tabs = ""
	num_tabs.times { tabs += "\t" }
	statement = statement[:query]
	case statement[:operation].to_s
	when "all"		
		statement[:model_name].to_s + ".all(function (err, "
	when "new"
		(statement[:model_name].to_s + "()")
	when "find"
		result = statement[:model_name].to_s + ".all({ where : "
		condition = statement[:condition]				
		
		condition_attribute = apply_transforms(condition[:value], 0)

		condition_expression = ""

		case statement[:condition][:operator].to_s.gsub!(" ", "")
		when "<="
			condition_expression = "{lte : " + condition_attribute + "}"
		when ">="
			condition_expression = "{gte : " + condition_attribute + "}"
		when "<"
			condition_expression = "{lt : " + condition_attribute + "}"
		when ">"
			condition_expression = "{gt : " + condition_attribute + "}"
		when "!="
			condition_expression = "{not : " + condition_attribute + "}"						
		else
			condition_expression = condition_attribute
		end
		
		result += "{ " + condition[:attribute] + " : " + condition_expression + " }"
		result += " }, function (err, "
	when "find_by_id"
		result = statement[:model_name].to_s + ".find("
		result += apply_transforms(statement[:item_id], 0)
		result += ", function(err, "
	when "update"
		@functions.push (tabs + "});")
		result = tabs + statement[:model_name].to_s + ".updateAttributes({}, function() {"				
	when "delete"
		@functions.push (tabs + "});")
		result = tabs + statement[:model_name].to_s + ".destroy(function() {"				
	end
end

#read_statement_transform(statement, num_tabs) ⇒ Object



243
244
245
246
# File 'lib/shift-lang/generator/javascript_generator.rb', line 243

def read_statement_transform(statement, num_tabs)
	param = apply_transforms(statement[:read_statement][:read_statement_param], 0)
	"data[" + param + "]"
end

#redirect_statement_transform(statement, num_tabs) ⇒ Object



248
249
250
251
252
253
254
255
# File 'lib/shift-lang/generator/javascript_generator.rb', line 248

def redirect_statement_transform(statement, num_tabs)
	tabs = ""
	num_tabs.times { tabs += "\t" }

	redirect_statement_template = ERB.new(Shift::Generator::JavaScriptTemplates::REDIRECT_STATEMENT_TEMPLATE)
	url = apply_transforms(statement[:redirect_statement][:redirect_statement_param], 0)
	"\n" + tabs + redirect_statement_template.result(binding)
end

#token_transform(statement, num_tabs) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/shift-lang/generator/javascript_generator.rb', line 140

def token_transform(statement, num_tabs)
	tabs = ""
	num_tabs.times { tabs += "\t" }

		key = statement.keys[0]

	case key
	when :identifier
		result = statement[:identifier].to_s 
		@url_variables.include?(result) ? "urlData['" + result + "']" : result
	when :object_value 
		if (statement[:object_value].to_s.split(".")[1] == "save")
			tabs + statement[:object_value].to_s.split(".")[0] + ".save();" + "\n"
		else
			tabs + statement[:object_value].to_s
		end
	when :hash_value
		statement[:hash_value].to_s
	when :string
		statement[:string].to_s
	else
		""
	end

end

#url_transform(url) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/shift-lang/generator/javascript_generator.rb', line 75

def url_transform(url) 
	if(url.is_a?(Array))
		result_url = ""
		
		url.each do | token |
			case token.keys[0]
			when :url_token
				result_url += "/" + token[:url_token].to_s
			when :url_variable
				result_url += "/:" + token[:url_variable].to_s
			end			
		end
		result_url
	else
		url.to_s
	end
end

#value_transform(statement, num_tabs) ⇒ Object



257
258
259
260
261
262
263
264
# File 'lib/shift-lang/generator/javascript_generator.rb', line 257

def value_transform(statement, num_tabs)
	tabs = ""
	num_tabs.times { tabs += "\t" }

	key = statement.keys[0]

	statement[key].to_s
end

#write_dataObject



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/shift-lang/generator/javascript_generator.rb', line 363

def write_data()
	if(@builder.models.length != 0)
		@has_models = true
	end

	result = get_import_statements + get_model_data

	@builder.handlers.each do | handlers |
		url = url_transform(handlers.url)
		method = handlers.method

		@url_variables = []
		current_url = handlers.url
		if(current_url.is_a?(Array))
			current_url.each do | token |
				case token.keys[0]
				when :url_variable
					@url_variables.push token[:url_variable]
				end
			end
		end

		url_handler_template = ERB.new(Shift::Generator::JavaScriptTemplates::URL_HANDLER_DEFINITION)
		result += "\n" + url_handler_template.result(binding) + "\n"

		@defined_hashes = []

		handlers.handler.each do | statement |
			result += apply_transforms(statement[:statement], statement[:num_tabs] + functions.length) 
		end

		i = @functions.length - 2

		blocks.each do | t |
			tabs = ""
			t.times { tabs += "\t" }
			result += tabs + "}" + "\n"
		end

		functions.reverse.each do | closing_block |
			tabs = ""
			i.times { tabs += "\t" }
			result += tabs + closing_block + "\n"
		end

		@functions = []
		@blocks = []

		result += "});\n"
	end

	result += "\n\n" + Shift::Generator::JavaScriptTemplates::APP_START_STATEMENT
end

#write_file_function_transform(statement, num_tabs) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/shift-lang/generator/javascript_generator.rb', line 177

def write_file_function_transform(statement, num_tabs)
	tabs = ""
	num_tabs.times { tabs += "\t" }
	
	write_file_template = ERB.new(Shift::Generator::JavaScriptTemplates::WRITE_FILE_TEMPLATE)
	
	if(statement[:write_file_statement].is_a?(Array))
		file_name = apply_transforms(statement[:write_file_statement][0][:file_name], 0)
		template_values = apply_transforms(statement[:write_file_statement][1][:template_values], 0)
		args = file_name + ', ' + template_values + ", response"
	else
		file_name = apply_transforms(statement[:write_file_statement][:file_name], 0)
		args = file_name + ', false'
	end		

	tabs + write_file_template.result(binding)
end

#write_function_transform(statement, num_tabs) ⇒ Object



166
167
168
169
170
171
172
173
174
175
# File 'lib/shift-lang/generator/javascript_generator.rb', line 166

def write_function_transform(statement, num_tabs)
	tabs = ""
	num_tabs.times { tabs += "\t" }

	write_data_template = ERB.new(Shift::Generator::JavaScriptTemplates::WRITE_DATA_TEMPLATE)

	write_data = apply_transforms(statement[:write_statement][:write_statement_param], 0)

	tabs + write_data_template.result(binding)
end