Beispiel #1
0
def parse():
	ice9_parser_global.logger.debug("Entering parse")
	
	in_head = False
	in_body = False

	# Case for no input
	if not ice9_parser_global.tok:
		return

	type = ice9_parser_global.tok.type
	stmt_node_list = list()

	ice9_parser_global.setup_code_generation()
	while type != 'EOF':
		
		in_head = False

		if in_body == False:
			if type == 'VAR':
				in_head = True
				ice9_parser_global.consume()
				ice9_parser_global.logger.debug(ice9_parser_global.tok)
				ice9_parser_var.handle_var()

			elif type == 'TYPE':
				in_head = True
				ice9_parser_global.consume()
				ice9_parser_global.logger.debug(ice9_parser_global.tok)
				ice9_parser_type.handle_type()

			elif type == 'FORWARD':
				in_head = True
				ice9_parser_global.consume()
				ice9_parser_global.logger.debug(ice9_parser_global.tok)
				ice9_parser_forward.handle_forward()
	
			elif type =='PROC':
				in_head = True
				ice9_parser_global.consume()
				ice9_parser_global.logger.debug(ice9_parser_global.tok)
				ice9_parser_proc.handle_proc()

		if in_head == False:
			ice9_parser_global.setup_stmt_section(ice9_parser_global.lno)
			if ice9_parser_statements.in_first_of_statement() == True:
				in_body = True
				stmt_node_list += ice9_parser_statements.statements()
			else:
				print "Line", ice9_parser_global.tok.lineno, ": unexpected token -", ice9_parser_global.tok.value
				sys.exit()

		type = ice9_parser_global.tok.type

		# Generate Code
		for stmt in stmt_node_list:
			stmt.generate_code()

	ice9_parser_global.logger.debug("Exiting parse")
	return
def handle_if():
	ice9_parser_global.logger.debug("Entering handle_if")

	stmt_node = ice9_node.stmt_node()
	stmt_node.rule = 'IF'

	# The condition expression
	expr_node = ice9_parser_expressions.expr(None)

	if expr_node.return_data_type != 'bool':
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, \
			"if condition must return a 'bool'"
		sys.exit()
	if expr_node.dimension != 0:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, \
			"if condition cannot be a array variable, must return a 'bool'"
		sys.exit()

	stmt_node.condition = expr_node

	if ice9_parser_global.tok.type == 'ARROW':
		ice9_parser_global.consume()
		ice9_parser_global.logger.debug(ice9_parser_global.tok)
	else:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting '->'"
		sys.exit()

	if ice9_parser_statements.in_first_of_statement():
		stmt_node.pre_child = ice9_parser_statements.statements()
	else:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting a statement"
		sys.exit()

	if ice9_parser_global.tok.type == 'BOX':
		ice9_parser_global.consume()
		ice9_parser_global.logger.debug(ice9_parser_global.tok)
		stmt_node.post_child = elseif_or_else()
	
	if ice9_parser_global.tok.type == 'FI':
		ice9_parser_global.consume()
		ice9_parser_global.logger.debug(ice9_parser_global.tok)
	else:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting fi"
		sys.exit()

	ice9_parser_global.logger.debug("Exiting handle_if")
	return stmt_node
def do():
	ice9_parser_global.logger.debug("Entering do")
	
	ice9_parser_global.loop_depth += 1

	stmt_node = ice9_node.stmt_node()
	stmt_node.rule = 'DO'
	stmt_node.condition = ice9_parser_expressions.expr(None)

	if stmt_node.condition.return_data_type != 'bool':
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, \
			"if condition must return a 'bool'"
		sys.exit()

	if stmt_node.condition.dimension != 0:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, \
			"if condition cannot be a array variable, must return a 'bool'"
		sys.exit()

	if ice9_parser_global.tok.type == 'ARROW':
		ice9_parser_global.consume()
		ice9_parser_global.logger.debug(ice9_parser_global.tok)
	else:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting '->'"
		sys.exit()

	stmt_node.pre_child = ice9_parser_statements.statements()

	if ice9_parser_global.tok.type == 'OD':
		ice9_parser_global.consume()
		ice9_parser_global.logger.debug(ice9_parser_global.tok)
	else:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting od"
		sys.exit()
	
	ice9_parser_global.loop_depth -= 1

	ice9_parser_global.logger.debug("Exiting do")
	return stmt_node
def fa():
	ice9_parser_global.logger.debug("Entering fa")

	stmt_node = ice9_node.stmt_node()
	stmt_node.rule = 'FA'
	
	ice9_parser_global.loop_depth += 1
	
	if ice9_parser_global.tok.type == 'ID':
		fa_loop_id = ice9_parser_global.tok.value
		ice9_parser_global.consume()
		ice9_parser_global.logger.debug(ice9_parser_global.tok)
	else:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting id"
		sys.exit()

	loop_id_node = ice9_node.expr_node()
	loop_id_node.operation = 'LOOP_ID'
	loop_id_node.value = fa_loop_id

	if ice9_parser_global.tok.type == 'ASSIGN':
		ice9_parser_global.consume()
		ice9_parser_global.logger.debug(ice9_parser_global.tok)
	else:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting ':='"
		sys.exit()

	loop_id_node.pre_child = ice9_parser_expressions.expr(None)	

	if loop_id_node.pre_child.return_data_type != 'int':
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, \
			"fa loop variable must be a 'int'"
		sys.exit()

	if loop_id_node.pre_child.dimension != 0:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, \
			"fa loop variable cannot be a array, must be a 'int'"
		sys.exit()

	if ice9_parser_global.tok.type == 'TO':
		ice9_parser_global.consume()
		ice9_parser_global.logger.debug(ice9_parser_global.tok)
	else:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting to"
		sys.exit()

	loop_id_node.post_child = ice9_parser_expressions.expr(None)
	
	if loop_id_node.post_child.return_data_type != 'int':
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, \
			"fa loop variable must be a 'int'"
		sys.exit()

	if loop_id_node.post_child.dimension != 0:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, \
			"fa loop expression cannot be a array , must be a 'int'"
		sys.exit()

	ice9_parser_global.fa_loop_variable.append(fa_loop_id)

	'''
	Might be un-necessary
	# Obtain the location to store the fa loop variable
	if ice9_parser_global.scope[-1] == 'GLOBAL':
		address = ice9_parser_global.get_next_global_address()
	else:
		address = ice9_st_proc.st_proc[ice9_parser_global.scope[-1]].parameter_count + 2 + \
			ice9_st_proc.st_proc[ice9_parser_global.scope[-1]].local_variable_count
	
	st_var_entry = ice9_st_var.st_var_entry('int', 0, address)
	'''

	st_var_entry = ice9_st_var.st_var_entry('int', 0, 1, 1)
	st_var_entry.basic_data_type = 'int'

	# Bypass the checks in st_var.add_entry_to_st_var
	# If variable not previously used, make an entry for it
	if fa_loop_id not in ice9_st_var.st_var:
		fa_loop_id_stack = list()
		ice9_st_var.st_var[fa_loop_id] = fa_loop_id_stack

	ice9_st_var.st_var[fa_loop_id].append(st_var_entry)
	
	if ice9_parser_global.tok.type == 'ARROW':
		ice9_parser_global.consume()
		ice9_parser_global.logger.debug(ice9_parser_global.tok)
	else:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting '->'"
		sys.exit()

	stmt_node.pre_child = loop_id_node
	stmt_node.post_child = ice9_parser_statements.statements()

	if ice9_parser_global.tok.type == 'AF':
		ice9_parser_global.consume()
		ice9_parser_global.logger.debug(ice9_parser_global.tok)
	else:
		print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting af"
		sys.exit()

	# Clean up
	ice9_parser_global.fa_loop_variable.pop()

	ice9_parser_global.loop_depth -= 1
	ice9_st_var.st_var[fa_loop_id].pop()
	
	if len(ice9_st_var.st_var[fa_loop_id]) == 0:
		del ice9_st_var.st_var[fa_loop_id]

	ice9_parser_global.logger.debug("Exiting fa")
	return stmt_node
def elseif_or_else():
	ice9_parser_global.logger.debug("Entering elseif_or_else")
	
	stmt_node = ice9_node.stmt_node()
	stmt_node.rule = 'IF'

	if ice9_parser_global.tok.type == 'ELSE':
		ice9_parser_global.consume()
		ice9_parser_global.logger.debug(ice9_parser_global.tok)

		# Create dummy expression that always evaluates to true
		expr_node = ice9_node.expr_node()
		expr_node.operation = 'TRUE'
		expr_node.return_data_type = 'bool'
		
		stmt_node.condition = expr_node
	
		if ice9_parser_global.tok.type == 'ARROW':
			ice9_parser_global.consume()
			ice9_parser_global.logger.debug(ice9_parser_global.tok)
		else:
			print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting '->'"
			sys.exit()

		if ice9_parser_statements.in_first_of_statement():
			stmt_node.pre_child = ice9_parser_statements.statements()
		else:
			print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting a statement"
			sys.exit()
			
	else:
			
		# Condition expression
		expr_node = ice9_parser_expressions.expr(None)

		if expr_node.return_data_type != 'bool':
			print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, \
				"if condition must return a 'bool'"
			sys.exit()

		if expr_node.dimension != 0:
			print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, \
				"if condition cannot be a array variable, must return a 'bool'"
			sys.exit()

		stmt_node.condition = expr_node		

		if ice9_parser_global.tok.type == 'ARROW':
			ice9_parser_global.consume()
			ice9_parser_global.logger.debug(ice9_parser_global.tok)
		else:
			print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting ';'"
			sys.exit()
	
		if ice9_parser_statements.in_first_of_statement():
			stmt_node.pre_child = ice9_parser_statements.statements()
		else:
			print "Line", ice9_parser_global.tok.lineno, ": on token", ice9_parser_global.tok.value, "expecting a statement"
			sys.exit()			
	
		if ice9_parser_global.tok.type == 'BOX':
			ice9_parser_global.consume()
			ice9_parser_global.logger.debug(ice9_parser_global.tok)
			stmt_node.post_child = elseif_or_else()

	ice9_parser_global.logger.debug("Exiting elseif_or_else")
	return stmt_node