Skip to content

Instantly share code, notes, and snippets.

@spiiin
Created January 14, 2023 20:35
Show Gist options
  • Save spiiin/961974938919cc9233bacf0bb5c71cd1 to your computer and use it in GitHub Desktop.
Save spiiin/961974938919cc9233bacf0bb5c71cd1 to your computer and use it in GitHub Desktop.
dascript_rtti_annotation_example.das
options rtti=true
require rtti
require strings
require daslib/strings_boost
class V2
x : float
y : float
class V3: V2
[[test]] z : float
def describeType(info)
var res : string
if info.basicType==Type tHandle
res = string(info.annotation.name)
elif info.basicType==Type tStructure
res = info.structType?.name ?? "structure"
elif info.basicType==Type tEnumeration || info.basicType==Type tEnumeration8 || info.basicType==Type tEnumeration16
res = info.enumType?.name ?? "enumeration"
elif info.basicType==Type tPointer
res = info.firstType!=null ? "{describeType(*(info.firstType))}?" : "void?"
elif info.basicType==Type tArray
res = info.firstType!=null ? "array<{describeType(*(info.firstType))}>" : "array"
elif info.basicType==Type tTable
if info.firstType!=null && info.secondType!=null
res = "table<{describeType(deref(info.firstType))};{describeType(deref(info.secondType))}>"
else
res = "table"
elif info.basicType==Type tTuple
if info.argTypes!=null
res = "tuple<" + join([{for argtype in arg_types(info); describeType(argtype)}],";") + ">"
else
res += "tuple"
elif info.basicType==Type tVariant
if info.argTypes!=null && info.argNames!=null
res = "variant<" + join([{for argname,argtype in arg_names(info),arg_types(info); "{argname}:{describeType(argtype)}"}],";") + ">"
else
res += "variant"
elif info.basicType==Type tFunction || info.basicType==Type tLambda || info.basicType==Type tBlock
res = "{get_das_type_name(info.basicType)}<"
if info.argTypes!=null
res += "(" + join([{for argtype in arg_types(info); describeType(argtype)}],";") + ")"
if info.firstType!=null
if info.argTypes!=null
res += ":"
res += "{describeType(*(info.firstType))}"
res += ">"
else
res = get_das_type_name(info.basicType)
res += join([{for d in each_dim(info); d!=-1 ? "[{d}]" : "[]"}],"")
if info.isConst
res += " const"
if info.isRef
res += " &"
if info.isTemp
res += "#"
if info.isImplicit
res += " implicit"
return res
def describeValue(value)
if value is nothing
return ""
elif value is tString
return "=\"{value}\""
else
return "={value}"
def describeVariable(glob;extra="")
print("{extra}{glob.name} : {describeType(glob)}{describeValue(get_variable_value(glob))}\n")
def describeStructure(sinfo)
var anyAnn = false
structure_for_each_annotation(sinfo) <| $(ann; annArgs)
let argT = join([{for arg in annArgs; "{arg.name}{describeValue(get_annotation_argument_value(arg))}"}],",")
print("[{ann.name}({argT})]\n")
print("struct {sinfo.name}\n")
for sfield in sinfo
if sfield.annotation_arguments != null
for arg in deref(sfield.annotation_arguments)
print("\t[[{arg.name}]] ")
describeVariable(sfield,"\t")
[export]
def main
var a = new V3()
print("class_info(a): {class_info(a)}\n")
describeStructure(*class_info(a))
//Output
//class_info(a): [[ 0x5; V3; ; (_class|heapGC); 0x20; 0x93b8d07b5cc8cee; 0xcf51414d2d20b41e]]
//struct V3
// __rtti : void?
// __finalize : function<(V2):void>
// x : float
// y : float
// [[test]] z : float
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment