Esempio n. 1
0
static void _parser_show_function(const GDScriptParser::FunctionNode *p_func, int p_indent, GDScriptParser::BlockNode *p_initializer = NULL) {

	String txt;
	if (p_func->_static)
		txt = "static ";
	txt += "func ";
	if (p_func->name == "") // initializer
		txt += "[built-in-initializer]";
	else
		txt += String(p_func->name);
	txt += "(";

	for (int i = 0; i < p_func->arguments.size(); i++) {

		if (i != 0)
			txt += ", ";
		txt += "var " + String(p_func->arguments[i]);
		if (i >= (p_func->arguments.size() - p_func->default_values.size())) {
			int defarg = i - (p_func->arguments.size() - p_func->default_values.size());
			txt += "=";
			txt += _parser_expr(p_func->default_values[defarg]);
		}
	}

	txt += ")";

	//todo constructor check!

	txt += ":";

	_print_indent(p_indent, txt);
	if (p_initializer)
		_parser_show_block(p_initializer, p_indent + 1);
	_parser_show_block(p_func->body, p_indent + 1);
}
Esempio n. 2
0
static void _parser_show_class(const GDScriptParser::ClassNode *p_class, int p_indent, const Vector<String> &p_code) {

	if (p_indent == 0 && (String(p_class->extends_file) != "" || p_class->extends_class.size())) {

		_print_indent(p_indent, _parser_extends(p_class));
		print_line("\n");
	}

	for (int i = 0; i < p_class->subclasses.size(); i++) {

		const GDScriptParser::ClassNode *subclass = p_class->subclasses[i];
		String line = "class " + subclass->name;
		if (String(subclass->extends_file) != "" || subclass->extends_class.size())
			line += " " + _parser_extends(subclass);
		line += ":";
		_print_indent(p_indent, line);
		_parser_show_class(subclass, p_indent + 1, p_code);
		print_line("\n");
	}

	for (int i = 0; i < p_class->constant_expressions.size(); i++) {

		const GDScriptParser::ClassNode::Constant &constant = p_class->constant_expressions[i];
		_print_indent(p_indent, "const " + String(constant.identifier) + "=" + _parser_expr(constant.expression));
	}

	for (int i = 0; i < p_class->variables.size(); i++) {

		const GDScriptParser::ClassNode::Member &m = p_class->variables[i];

		_print_indent(p_indent, "var " + String(m.identifier));
	}

	print_line("\n");

	for (int i = 0; i < p_class->static_functions.size(); i++) {

		_parser_show_function(p_class->static_functions[i], p_indent);
		print_line("\n");
	}

	for (int i = 0; i < p_class->functions.size(); i++) {

		if (String(p_class->functions[i]->name) == "_init") {
			_parser_show_function(p_class->functions[i], p_indent, p_class->initializer);
		} else
			_parser_show_function(p_class->functions[i], p_indent);
		print_line("\n");
	}
	//_parser_show_function(p_class->initializer,p_indent);
	print_line("\n");
}
Esempio n. 3
0
static String _parser_expr(const GDScriptParser::Node *p_expr) {

	String txt;
	switch (p_expr->type) {

		case GDScriptParser::Node::TYPE_IDENTIFIER: {

			const GDScriptParser::IdentifierNode *id_node = static_cast<const GDScriptParser::IdentifierNode *>(p_expr);
			txt = id_node->name;
		} break;
		case GDScriptParser::Node::TYPE_CONSTANT: {
			const GDScriptParser::ConstantNode *c_node = static_cast<const GDScriptParser::ConstantNode *>(p_expr);
			if (c_node->value.get_type() == Variant::STRING)
				txt = "\"" + String(c_node->value) + "\"";
			else
				txt = c_node->value;

		} break;
		case GDScriptParser::Node::TYPE_SELF: {
			txt = "self";
		} break;
		case GDScriptParser::Node::TYPE_ARRAY: {
			const GDScriptParser::ArrayNode *arr_node = static_cast<const GDScriptParser::ArrayNode *>(p_expr);
			txt += "[";
			for (int i = 0; i < arr_node->elements.size(); i++) {

				if (i > 0)
					txt += ", ";
				txt += _parser_expr(arr_node->elements[i]);
			}
			txt += "]";
		} break;
		case GDScriptParser::Node::TYPE_DICTIONARY: {
			const GDScriptParser::DictionaryNode *dict_node = static_cast<const GDScriptParser::DictionaryNode *>(p_expr);
			txt += "{";
			for (int i = 0; i < dict_node->elements.size(); i++) {

				if (i > 0)
					txt += ", ";

				const GDScriptParser::DictionaryNode::Pair &p = dict_node->elements[i];
				txt += _parser_expr(p.key);
				txt += ":";
				txt += _parser_expr(p.value);
			}
			txt += "}";
		} break;
		case GDScriptParser::Node::TYPE_OPERATOR: {

			const GDScriptParser::OperatorNode *c_node = static_cast<const GDScriptParser::OperatorNode *>(p_expr);
			switch (c_node->op) {

				case GDScriptParser::OperatorNode::OP_PARENT_CALL:
					txt += ".";
				case GDScriptParser::OperatorNode::OP_CALL: {

					ERR_FAIL_COND_V(c_node->arguments.size() < 1, "");
					String func_name;
					const GDScriptParser::Node *nfunc = c_node->arguments[0];
					int arg_ofs = 0;
					if (nfunc->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {

						const GDScriptParser::BuiltInFunctionNode *bif_node = static_cast<const GDScriptParser::BuiltInFunctionNode *>(nfunc);
						func_name = GDScriptFunctions::get_func_name(bif_node->function);
						arg_ofs = 1;
					} else if (nfunc->type == GDScriptParser::Node::TYPE_TYPE) {

						const GDScriptParser::TypeNode *t_node = static_cast<const GDScriptParser::TypeNode *>(nfunc);
						func_name = Variant::get_type_name(t_node->vtype);
						arg_ofs = 1;
					} else {

						ERR_FAIL_COND_V(c_node->arguments.size() < 2, "");
						nfunc = c_node->arguments[1];
						ERR_FAIL_COND_V(nfunc->type != GDScriptParser::Node::TYPE_IDENTIFIER, "");

						if (c_node->arguments[0]->type != GDScriptParser::Node::TYPE_SELF)
							func_name = _parser_expr(c_node->arguments[0]) + ".";

						func_name += _parser_expr(nfunc);
						arg_ofs = 2;
					}

					txt += func_name + "(";

					for (int i = arg_ofs; i < c_node->arguments.size(); i++) {

						const GDScriptParser::Node *arg = c_node->arguments[i];
						if (i > arg_ofs)
							txt += ", ";
						txt += _parser_expr(arg);
					}

					txt += ")";

				} break;
				case GDScriptParser::OperatorNode::OP_INDEX: {

					ERR_FAIL_COND_V(c_node->arguments.size() != 2, "");

					//index with []
					txt = _parser_expr(c_node->arguments[0]) + "[" + _parser_expr(c_node->arguments[1]) + "]";

				} break;
				case GDScriptParser::OperatorNode::OP_INDEX_NAMED: {

					ERR_FAIL_COND_V(c_node->arguments.size() != 2, "");

					txt = _parser_expr(c_node->arguments[0]) + "." + _parser_expr(c_node->arguments[1]);

				} break;
				case GDScriptParser::OperatorNode::OP_NEG: {
					txt = "-" + _parser_expr(c_node->arguments[0]);
				} break;
				case GDScriptParser::OperatorNode::OP_NOT: {
					txt = "not " + _parser_expr(c_node->arguments[0]);
				} break;
				case GDScriptParser::OperatorNode::OP_BIT_INVERT: {
					txt = "~" + _parser_expr(c_node->arguments[0]);
				} break;
				case GDScriptParser::OperatorNode::OP_IN: {
					txt = _parser_expr(c_node->arguments[0]) + " in " + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_EQUAL: {
					txt = _parser_expr(c_node->arguments[0]) + "==" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_NOT_EQUAL: {
					txt = _parser_expr(c_node->arguments[0]) + "!=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_LESS: {
					txt = _parser_expr(c_node->arguments[0]) + "<" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_LESS_EQUAL: {
					txt = _parser_expr(c_node->arguments[0]) + "<=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_GREATER: {
					txt = _parser_expr(c_node->arguments[0]) + ">" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_GREATER_EQUAL: {
					txt = _parser_expr(c_node->arguments[0]) + ">=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_AND: {
					txt = _parser_expr(c_node->arguments[0]) + " and " + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_OR: {
					txt = _parser_expr(c_node->arguments[0]) + " or " + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_ADD: {
					txt = _parser_expr(c_node->arguments[0]) + "+" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_SUB: {
					txt = _parser_expr(c_node->arguments[0]) + "-" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_MUL: {
					txt = _parser_expr(c_node->arguments[0]) + "*" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_DIV: {
					txt = _parser_expr(c_node->arguments[0]) + "/" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_MOD: {
					txt = _parser_expr(c_node->arguments[0]) + "%" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: {
					txt = _parser_expr(c_node->arguments[0]) + "<<" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: {
					txt = _parser_expr(c_node->arguments[0]) + ">>" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_ASSIGN: {
					txt = _parser_expr(c_node->arguments[0]) + "=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_ASSIGN_ADD: {
					txt = _parser_expr(c_node->arguments[0]) + "+=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_ASSIGN_SUB: {
					txt = _parser_expr(c_node->arguments[0]) + "-=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_ASSIGN_MUL: {
					txt = _parser_expr(c_node->arguments[0]) + "*=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_ASSIGN_DIV: {
					txt = _parser_expr(c_node->arguments[0]) + "/=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_ASSIGN_MOD: {
					txt = _parser_expr(c_node->arguments[0]) + "%=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT: {
					txt = _parser_expr(c_node->arguments[0]) + "<<=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT: {
					txt = _parser_expr(c_node->arguments[0]) + ">>=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_AND: {
					txt = _parser_expr(c_node->arguments[0]) + "&=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_OR: {
					txt = _parser_expr(c_node->arguments[0]) + "|=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_XOR: {
					txt = _parser_expr(c_node->arguments[0]) + "^=" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_BIT_AND: {
					txt = _parser_expr(c_node->arguments[0]) + "&" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_BIT_OR: {
					txt = _parser_expr(c_node->arguments[0]) + "|" + _parser_expr(c_node->arguments[1]);
				} break;
				case GDScriptParser::OperatorNode::OP_BIT_XOR: {
					txt = _parser_expr(c_node->arguments[0]) + "^" + _parser_expr(c_node->arguments[1]);
				} break;
				default: {}
			}

		} break;
		case GDScriptParser::Node::TYPE_NEWLINE: {

			//skippie
		} break;
		default: {

			String error = "Parser bug at " + itos(p_expr->line) + ", invalid expression type: " + itos(p_expr->type);
			ERR_EXPLAIN(error);
			ERR_FAIL_V("");
		}
	}

	return txt;
	//return "("+txt+")";
}
Esempio n. 4
0
static void _parser_show_block(const GDScriptParser::BlockNode *p_block, int p_indent) {

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

		const GDScriptParser::Node *statement = p_block->statements[i];

		switch (statement->type) {

			case GDScriptParser::Node::TYPE_CONTROL_FLOW: {

				const GDScriptParser::ControlFlowNode *cf_node = static_cast<const GDScriptParser::ControlFlowNode *>(statement);
				switch (cf_node->cf_type) {

					case GDScriptParser::ControlFlowNode::CF_IF: {

						ERR_FAIL_COND(cf_node->arguments.size() != 1);
						String txt;
						txt += "if ";
						txt += _parser_expr(cf_node->arguments[0]);
						txt += ":";
						_print_indent(p_indent, txt);
						ERR_FAIL_COND(!cf_node->body);
						_parser_show_block(cf_node->body, p_indent + 1);
						if (cf_node->body_else) {
							_print_indent(p_indent, "else:");
							_parser_show_block(cf_node->body_else, p_indent + 1);
						}

					} break;
					case GDScriptParser::ControlFlowNode::CF_FOR: {
						ERR_FAIL_COND(cf_node->arguments.size() != 2);
						String txt;
						txt += "for ";
						txt += _parser_expr(cf_node->arguments[0]);
						txt += " in ";
						txt += _parser_expr(cf_node->arguments[1]);
						txt += ":";
						_print_indent(p_indent, txt);
						ERR_FAIL_COND(!cf_node->body);
						_parser_show_block(cf_node->body, p_indent + 1);

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

						ERR_FAIL_COND(cf_node->arguments.size() != 1);
						String txt;
						txt += "while ";
						txt += _parser_expr(cf_node->arguments[0]);
						txt += ":";
						_print_indent(p_indent, txt);
						ERR_FAIL_COND(!cf_node->body);
						_parser_show_block(cf_node->body, p_indent + 1);

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

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

						_print_indent(p_indent, "continue");
					} break;
					case GDScriptParser::ControlFlowNode::CF_BREAK: {

						_print_indent(p_indent, "break");
					} break;
					case GDScriptParser::ControlFlowNode::CF_RETURN: {

						if (cf_node->arguments.size())
							_print_indent(p_indent, "return " + _parser_expr(cf_node->arguments[0]));
						else
							_print_indent(p_indent, "return ");
					} break;
				}

			} break;
			case GDScriptParser::Node::TYPE_LOCAL_VAR: {

				const GDScriptParser::LocalVarNode *lv_node = static_cast<const GDScriptParser::LocalVarNode *>(statement);
				_print_indent(p_indent, "var " + String(lv_node->name));
			} break;
			default: {
				//expression i guess
				_print_indent(p_indent, _parser_expr(statement));
			}
		}
	}
}