-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #373 from AlexDarigan/double_default_arguments
Automatically Double Function Keywords, Engine-Defined Default Arguments & User-Defined Default Arguments
- Loading branch information
Showing
7 changed files
with
312 additions
and
393 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,128 @@ | ||
extends Node | ||
|
||
signal custom | ||
var a = [5, 2, 10] | ||
var b = [5, 2, 10] | ||
var methods: Dictionary = {} # name: {} | ||
var is_built_in = false | ||
var inner_klass = "" | ||
#var klass = "res://N.gd" | ||
var klass = "res://OldExamples/Scripts/user.gd" | ||
var username | ||
|
||
func _init(_username: String = "Hello"): | ||
username = _username | ||
|
||
|
||
|
||
|
||
func _ready(): | ||
print(a == b) | ||
do() | ||
# parse_for_methods() | ||
# var n = NotBase.new("") | ||
|
||
master func do(): | ||
print("done") | ||
|
||
func append_function(function: Dictionary) -> void: | ||
if methods.has(function["name"]): | ||
return # already parsed in a subclasses | ||
methods[function["name"]] = function | ||
|
||
func parse_for_methods() -> void: | ||
var script: GDScript | ||
var engine_methods: Array = [] | ||
if not is_built_in and inner_klass == "": | ||
script = load(klass) | ||
engine_methods = ClassDB.class_get_method_list(script.get_instance_base_type()) | ||
while script != null: | ||
parse_script(script.source_code) | ||
script = script.get_base_script() | ||
else: | ||
engine_methods = ClassDB.class_get_method_list(klass) | ||
for method in methods: | ||
print(methods[method]) | ||
parse_builtins(engine_methods) | ||
|
||
|
||
func parse_builtins(engine_methods: Array) -> void: | ||
for method in engine_methods: | ||
var function = { | ||
"keyword": "", | ||
"name": method.name, | ||
"args": "", | ||
"return_type": "", | ||
} | ||
|
||
function["args"] = parse_engine_method_arguments(method.args, method.default_args) | ||
append_function(function) | ||
|
||
func parse_engine_method_arguments(args: Array, default_args: Array) -> String: | ||
var stringArgs = "" | ||
var non_defaults: Array = args.slice(0, args.size() - default_args.size()) | ||
var defaults: Array = args.slice(args.size() - default_args.size(), args.size()) | ||
for arg in non_defaults: | ||
stringArgs += "%s, " % arg.name | ||
for idx in default_args.size(): | ||
stringArgs += "%s = %s, " % [defaults[idx].name, default_args[idx]] | ||
stringArgs = stringArgs.rstrip(", ") | ||
return stringArgs | ||
|
||
func parse_script(source_code: String): | ||
var function: Dictionary = {} | ||
for line in source_code.split("\n"): | ||
if line.begins_with("func"): | ||
append_function(parse_function(line)) | ||
elif line.begins_with("static func"): | ||
append_function(parse_function(line, "static ")) | ||
elif line.begins_with("remote func"): | ||
append_function(parse_function(line, "remote ")) | ||
elif line.begins_with("master func"): | ||
append_function(parse_function(line, "master ")) | ||
elif line.begins_with("puppet func"): | ||
append_function(parse_function(line, "puppet ")) | ||
elif line.begins_with("slave func"): | ||
append_function(parse_function(line, "slave ")) | ||
|
||
func parse_function(line: String, keyword: String = "") -> Dictionary: | ||
var function = { | ||
"keyword": keyword, | ||
"name": "", | ||
"args": "", | ||
"return_type": "" | ||
} | ||
|
||
# Get Name | ||
var start: int = line.find("func") + 5 | ||
var length: int = line.find("(") - start | ||
function["name"] = line.substr(start, length) | ||
|
||
# Get Return Type | ||
var Rstart = line.find("-> ") | ||
var Rlength = line.find_last(":") - Rstart | ||
if "->" in line: | ||
function["return_type"] = line.substr(Rstart, Rlength) | ||
|
||
var argStart = line.find("(") + 1 | ||
var argLength = line.find(")") - argStart | ||
function["args"] = line.substr(argStart, argLength) | ||
|
||
return function | ||
|
||
func simple_func() -> void: | ||
pass | ||
|
||
func simple_func_args(a, b: int, c = "Hello", d: int = 100) -> void: | ||
pass | ||
|
||
static func static_func() -> void: | ||
pass | ||
|
||
remote func remote_func() -> void: | ||
pass | ||
|
||
master func master_func() -> void: | ||
pass | ||
|
||
puppet func puppet_func() -> void: | ||
pass | ||
|
||
slave func slave_func() -> void: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.