diff --git a/modules/std/syntax/functors/attribs.wx b/modules/std/syntax/functors/attribs.wx new file mode 100644 index 00000000..7b024af1 --- /dev/null +++ b/modules/std/syntax/functors/attribs.wx @@ -0,0 +1,75 @@ + +Namespace std.syntax + +#rem +@author iDkP from GaragePixel +@since 2025-07-04 +@example + + Function myFunc(attribs:VAttribs[]) + Local vattribs:=Bind(attribs) + Local height:=Int(vattribs.Get("height")) + Local width:=Float(vattribs.Get("width")) + + Print width+height + End + + Function myfn(attribs:VAttribs[]) + Local vattribs:=Bind(attribs) + Local height:=Int(vattribs.Get("height")) + Local width:=Float(vattribs.Get("width")) + + 'Print width+height + End + + Function Main() + + myFunc(Attribs(" title: ~qmyTitle~q, + height: 10, + width: 10.5" )) + + + myfn(New VAttribs[]( "title" , "myTitle", + "height", 10, + "width" , 10.5 )) + End +#end + +'#Import "../../types/types" +'#Import "../../system/reflection/reflection" +'#Import "../primitives/variants/variantcasts" + +Using std.types.. +Using std.reflection +Using std.syntax.variantcasts + +Function Attribs:VAttribs[]( pair:String ) + Return New TAttribs(pair).Bind() +End + +Class TAttribs + + Method New( attribs:String ) + _pair=attribs + End + + Method Bind:VAttribs[]() + + Local e:=_pair.Split(",") + Local tkn:String[] + Local data:=New Stack + + For Local i:=Eachin e + tkn = i.Replace("~n","").Split(":") + tkn[0] = Unquote(tkn[0]).Trim() + tkn[1] = Unquote(tkn[1]).Trim() + + data.Add(Cast(tkn[0])) + data.Add(Cast(tkn[1])) + End + + Return data.ToArray() + End + + Field _pair:String +End diff --git a/modules/std/syntax/functors/vattribs.wx b/modules/std/syntax/functors/vattribs.wx new file mode 100644 index 00000000..c257e3c2 --- /dev/null +++ b/modules/std/syntax/functors/vattribs.wx @@ -0,0 +1,110 @@ + +Namespace std.syntax + +#rem @pro Attributes Minilibrary +@author iDkP from GaragePixel +@since 2025-07-04 +@example + + Function myfn(attribs:VAttribs[]) + Local vars:=Bind(args) + Local height:=Int(vars.Get("height")) + Local width:=Float(vars.Get("width")) + + Print width+height + End + + Function Main() + + myfn(New VAttribs[]( "title" , "myTitle", + "height", 10, + "width" , 10.5 )) + End +#end + +'#Import "../../types/types" +'#Import "../../system/reflection/reflection" +'#Import "../primitives/variants/variantcasts" + +Using std.types.. +Using std.reflection +Using std.syntax.variantcasts + +Alias VAttribs:Variant 'Conveniant alias alternative name, usually used in conjonction with a container [] + 'It should be remplaced by Var, who was reserved by Mark for this usage. + +Alias VarAttribs:cVarAttribs + +Function Bind:VarAttribs(v:Variant[]) 'VarAttribs is an array of variant, interleaved as string,variant,string,variant... + Return New VarAttribs(v,True) 'Then VarAttribs is then used to make a Map called VarAttribs +End + +Class cVarAttribs Extends Map Where K=String And V=TVar + + 'Should be remplaced by Var, who was reserved by Mark for this usage. + 'The generic types are locked by the VarAttribs alias. We don't have to try calling + 'cVarAttribs from another type declaration or alias. Only the VarAttribs call is valid. + + Method New( attrs:Variant[], t:Bool=True ) Override + Assert((attrs.Length Mod 2) = 0,"Pair missing: array length must be even.") + Local len:=attrs.Length/2 + + For Local i:=0 Until len+2 Step 2 + local tvar:=New TVar() + tvar.Set(attrs[i+1]) + tvar.Type=stdlib.reflection.GetType(attrs[i+1]) + Add(Cast(attrs[i]),tvar) + End + End + + Method New( attrs:Stack, t:Bool=True ) Override + Assert((attrs.Length Mod 2) = 0,"Pair missing: array length must be even.") + Local len:=attrs.Length/2 + + For Local i:=0 Until len+2 Step 2 + local tvar:=New TVar() + tvar.Set(attrs[i+1]) + tvar.Type=stdlib.reflection.GetType(attrs[i+1]) + Add(Cast(attrs[i]),tvar) + End + End + + Method GetT:T( key:K ) + Local node:=FindNode( key ) + If node Return node.Value + Return Null + End + + Method GetBool:Bool( key:K ) + Return Bool(Get(key)) + End + + Method GetInt:Int( key:K ) + Return Int(Get(key)) + End + + Method GetFloat:Int( key:K ) + Return Float(Get(key)) + End + + Method GetString:String( key:K ) + Return String(Get(key)) + End + + Method GetB:Bool( key:K ) + Return GetBool(key) + End + + Method GetI:Int( key:K ) + Return GetInt(key) + End + + Method GetF:Float( key:K ) + Return GetFloat(key) + End + + Method GetS:String( key:K ) + Return GetString(key) + End + +End