Exemplo n.º 1
0
Error GDCompiler::_parse_block(CodeGen& codegen,const GDParser::BlockNode *p_block,int p_stack_level,int p_break_addr,int p_continue_addr) {

	codegen.push_stack_identifiers();
	int new_identifiers=0;
	codegen.current_line=p_block->line;

	for(int i=0;i<p_block->statements.size();i++) {

		const GDParser::Node *s = p_block->statements[i];


		switch(s->type) {
			case GDParser::Node::TYPE_NEWLINE: {

				const GDParser::NewLineNode *nl = static_cast<const GDParser::NewLineNode*>(s);
				codegen.opcodes.push_back(GDFunction::OPCODE_LINE);
				codegen.opcodes.push_back(nl->line);
				codegen.current_line=nl->line;

			} break;
			case GDParser::Node::TYPE_CONTROL_FLOW: {
				// try subblocks

				const GDParser::ControlFlowNode *cf = static_cast<const GDParser::ControlFlowNode*>(s);

				switch(cf->cf_type) {


					case GDParser::ControlFlowNode::CF_IF: {

#ifdef DEBUG_ENABLED
						codegen.opcodes.push_back(GDFunction::OPCODE_LINE);
						codegen.opcodes.push_back(cf->line);
						codegen.current_line=cf->line;
#endif
						int ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
						if (ret<0)
							return ERR_PARSE_ERROR;

						codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
						codegen.opcodes.push_back(ret);
						int else_addr=codegen.opcodes.size();
						codegen.opcodes.push_back(0); //temporary

						Error err = _parse_block(codegen,cf->body,p_stack_level,p_break_addr,p_continue_addr);
						if (err)
							return err;

						if (cf->body_else) {

							codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
							int end_addr=codegen.opcodes.size();
							codegen.opcodes.push_back(0);
							codegen.opcodes[else_addr]=codegen.opcodes.size();

							Error err = _parse_block(codegen,cf->body_else,p_stack_level,p_break_addr,p_continue_addr);
							if (err)
								return err;

							codegen.opcodes[end_addr]=codegen.opcodes.size();
						} else {
							//end without else
							codegen.opcodes[else_addr]=codegen.opcodes.size();

						}

					} break;
					case GDParser::ControlFlowNode::CF_FOR: {



						int slevel=p_stack_level;
						int iter_stack_pos=slevel;
						int iterator_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
						int counter_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
						int container_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
						codegen.alloc_stack(slevel);

						    codegen.push_stack_identifiers();
						      codegen.add_stack_identifier(static_cast<const GDParser::IdentifierNode*>(cf->arguments[0])->name,iter_stack_pos);

						int ret = _parse_expression(codegen,cf->arguments[1],slevel,false);
						if (ret<0)
							return ERR_COMPILATION_FAILED;

						//assign container
						codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN);
						codegen.opcodes.push_back(container_pos);
						codegen.opcodes.push_back(ret);

						//begin loop
						codegen.opcodes.push_back(GDFunction::OPCODE_ITERATE_BEGIN);
						codegen.opcodes.push_back(counter_pos);
						codegen.opcodes.push_back(container_pos);
						codegen.opcodes.push_back(codegen.opcodes.size()+4);
						codegen.opcodes.push_back(iterator_pos);
						codegen.opcodes.push_back(GDFunction::OPCODE_JUMP); //skip code for next
						codegen.opcodes.push_back(codegen.opcodes.size()+8);
						//break loop
						int break_pos=codegen.opcodes.size();
						codegen.opcodes.push_back(GDFunction::OPCODE_JUMP); //skip code for next
						codegen.opcodes.push_back(0); //skip code for next
						//next loop
						int continue_pos=codegen.opcodes.size();
						codegen.opcodes.push_back(GDFunction::OPCODE_ITERATE);
						codegen.opcodes.push_back(counter_pos);
						codegen.opcodes.push_back(container_pos);
						codegen.opcodes.push_back(break_pos);
						codegen.opcodes.push_back(iterator_pos);


						Error err = _parse_block(codegen,cf->body,slevel,break_pos,continue_pos);
						if (err)
							return err;


						codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
						codegen.opcodes.push_back(continue_pos);
						codegen.opcodes[break_pos+1]=codegen.opcodes.size();


						codegen.pop_stack_identifiers();

					} break;
					case GDParser::ControlFlowNode::CF_WHILE: {

						codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
						codegen.opcodes.push_back(codegen.opcodes.size()+3);
						int break_addr=codegen.opcodes.size();
						codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
						codegen.opcodes.push_back(0);
						int continue_addr=codegen.opcodes.size();

						int ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
						if (ret<0)
							return ERR_PARSE_ERROR;
						codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
						codegen.opcodes.push_back(ret);
						codegen.opcodes.push_back(break_addr);
						Error err = _parse_block(codegen,cf->body,p_stack_level,break_addr,continue_addr);
						if (err)
							return err;
						codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
						codegen.opcodes.push_back(continue_addr);

						codegen.opcodes[break_addr+1]=codegen.opcodes.size();

					} break;
					case GDParser::ControlFlowNode::CF_SWITCH: {

					} break;
					case GDParser::ControlFlowNode::CF_BREAK: {

						if (p_break_addr<0) {

							_set_error("'break'' not within loop",cf);
							return ERR_COMPILATION_FAILED;
						}
						codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
						codegen.opcodes.push_back(p_break_addr);

					} break;
					case GDParser::ControlFlowNode::CF_CONTINUE: {

						if (p_continue_addr<0) {

							_set_error("'continue' not within loop",cf);
							return ERR_COMPILATION_FAILED;
						}

						codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
						codegen.opcodes.push_back(p_continue_addr);

					} break;
					case GDParser::ControlFlowNode::CF_RETURN: {

						int ret;

						if (cf->arguments.size()) {

							ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
							if (ret<0)
								return ERR_PARSE_ERROR;

						} else {

							ret=GDFunction::ADDR_TYPE_NIL << GDFunction::ADDR_BITS;
						}

						codegen.opcodes.push_back(GDFunction::OPCODE_RETURN);
						codegen.opcodes.push_back(ret);

					} break;

				}
			} break;
			case GDParser::Node::TYPE_ASSERT: {
				// try subblocks

				const GDParser::AssertNode *as = static_cast<const GDParser::AssertNode*>(s);

				int ret = _parse_expression(codegen,as->condition,p_stack_level,false);
				if (ret<0)
					return ERR_PARSE_ERROR;

				codegen.opcodes.push_back(GDFunction::OPCODE_ASSERT);
				codegen.opcodes.push_back(ret);
			} break;
			case GDParser::Node::TYPE_LOCAL_VAR: {


				const GDParser::LocalVarNode *lv = static_cast<const GDParser::LocalVarNode*>(s);

				codegen.add_stack_identifier(lv->name,p_stack_level++);
				codegen.alloc_stack(p_stack_level);
				new_identifiers++;

			} break;
			default: {
				//expression
				int ret = _parse_expression(codegen,s,p_stack_level,true);
				if (ret<0)
					return ERR_PARSE_ERROR;
			} break;

		}

	}
	codegen.pop_stack_identifiers();
	return OK;
}
Exemplo n.º 2
0
Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *p_class,const GDParser::FunctionNode *p_func) {

	Vector<int> bytecode;
	CodeGen codegen;

	codegen.class_node=p_class;
	codegen.script=p_script;
	codegen.function_node=p_func;
	codegen.stack_max=0;
	codegen.current_line=0;
	codegen.call_max=0;
	codegen.debug_stack=ScriptDebugger::get_singleton()!=NULL;

	int stack_level=0;

	if (p_func) {
		for(int i=0;i<p_func->arguments.size();i++) {
			int idx = i;
			codegen.add_stack_identifier(p_func->arguments[i],i);
		}
		stack_level=p_func->arguments.size();
	}

	codegen.alloc_stack(stack_level);

	/* Parse initializer -if applies- */

	bool is_initializer=false || !p_func;

	if (!p_func || String(p_func->name)=="_init") {
		//parse initializer for class members
		if (!p_func && p_class->extends_used && p_script->native.is_null()){

			//call implicit parent constructor
			codegen.opcodes.push_back(GDFunction::OPCODE_CALL_SELF_BASE);
			codegen.opcodes.push_back(codegen.get_name_map_pos("_init"));
			codegen.opcodes.push_back(0);
			codegen.opcodes.push_back((GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)|0);

		}
		Error err = _parse_block(codegen,p_class->initializer,stack_level);
		if (err)
			return err;
		is_initializer=true;

	}

	/* Parse default argument code -if applies- */

	Vector<int> defarg_addr;
	StringName func_name;

	if (p_func) {
		if (p_func->default_values.size()) {

			codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
			defarg_addr.push_back(codegen.opcodes.size());
			for(int i=0;i<p_func->default_values.size();i++) {

				_parse_expression(codegen,p_func->default_values[i],stack_level,true);
				defarg_addr.push_back(codegen.opcodes.size());
			}


			defarg_addr.invert();
		}



		Error err = _parse_block(codegen,p_func->body,stack_level);
		if (err)
			return err;

		func_name=p_func->name;
	} else {
		func_name="_init";
	}

	codegen.opcodes.push_back(GDFunction::OPCODE_END);

	GDFunction *gdfunc=NULL;

	//if (String(p_func->name)=="") { //initializer func
	//	gdfunc = &p_script->initializer;

	//} else { //regular func
		p_script->member_functions[func_name]=GDFunction();
		gdfunc = &p_script->member_functions[func_name];
	//}

	if (p_func)
		gdfunc->_static=p_func->_static;

	//constants
	if (codegen.constant_map.size()) {
		gdfunc->_constant_count=codegen.constant_map.size();
		gdfunc->constants.resize(codegen.constant_map.size());
		gdfunc->_constants_ptr=&gdfunc->constants[0];
		const Variant *K=NULL;
		while((K=codegen.constant_map.next(K))) {
			int idx = codegen.constant_map[*K];
			gdfunc->constants[idx]=*K;
		}
	} else {

		gdfunc->_constants_ptr=NULL;
		gdfunc->_constant_count=0;
	}
	//global names
	if (codegen.name_map.size()) {

		gdfunc->global_names.resize(codegen.name_map.size());
		gdfunc->_global_names_ptr = &gdfunc->global_names[0];
		for(Map<StringName,int>::Element *E=codegen.name_map.front();E;E=E->next()) {

			gdfunc->global_names[E->get()]=E->key();
		}
		gdfunc->_global_names_count=gdfunc->global_names.size();

	} else {
		gdfunc->_global_names_ptr = NULL;
		gdfunc->_global_names_count =0;
	}


	if (codegen.opcodes.size()) {

		gdfunc->code=codegen.opcodes;
		gdfunc->_code_ptr=&gdfunc->code[0];
		gdfunc->_code_size=codegen.opcodes.size();

	} else {

		gdfunc->_code_ptr=NULL;
		gdfunc->_code_size=0;
	}

	if (defarg_addr.size()) {

		gdfunc->default_arguments=defarg_addr;
		gdfunc->_default_arg_count=defarg_addr.size();
		gdfunc->_default_arg_ptr=&gdfunc->default_arguments[0];
	} else {
		gdfunc->_default_arg_count=0;
		gdfunc->_default_arg_ptr=NULL;
	}

	gdfunc->_argument_count=p_func ? p_func->arguments.size() : 0;
	gdfunc->_stack_size=codegen.stack_max;
	gdfunc->_call_size=codegen.call_max;
	gdfunc->name=func_name;
	gdfunc->_script=p_script;
	gdfunc->source=source;

#ifdef DEBUG_ENABLED

	{
		gdfunc->func_cname=(String(source)+" - "+String(func_name)).utf8();
		gdfunc->_func_cname=gdfunc->func_cname.get_data();

	}

#endif
	if (p_func) {
		gdfunc->_initial_line=p_func->line;
	} else {
		gdfunc->_initial_line=0;
	}

	if (codegen.debug_stack)
		gdfunc->stack_debug=codegen.stack_debug;

	if (is_initializer)
		p_script->initializer=gdfunc;


	return OK;
}