Skip to content

Commit eb4ca84

Browse files
authored
Merge pull request #86 from GaragePixel/develop
stdlib adds #22 - syntax - functors - attribs, vattribs
2 parents 22eae71 + d1a9321 commit eb4ca84

2 files changed

Lines changed: 185 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
Namespace std.syntax
3+
4+
#rem
5+
@author iDkP from GaragePixel
6+
@since 2025-07-04
7+
@example
8+
9+
Function myFunc(attribs:VAttribs[])
10+
Local vattribs:=Bind(attribs)
11+
Local height:=Int(vattribs.Get("height"))
12+
Local width:=Float(vattribs.Get("width"))
13+
14+
Print width+height
15+
End
16+
17+
Function myfn(attribs:VAttribs[])
18+
Local vattribs:=Bind(attribs)
19+
Local height:=Int(vattribs.Get("height"))
20+
Local width:=Float(vattribs.Get("width"))
21+
22+
'Print width+height
23+
End
24+
25+
Function Main()
26+
27+
myFunc(Attribs(" title: ~qmyTitle~q,
28+
height: 10,
29+
width: 10.5" ))
30+
31+
32+
myfn(New VAttribs[]( "title" , "myTitle",
33+
"height", 10,
34+
"width" , 10.5 ))
35+
End
36+
#end
37+
38+
'#Import "../../types/types"
39+
'#Import "../../system/reflection/reflection"
40+
'#Import "../primitives/variants/variantcasts"
41+
42+
Using std.types..
43+
Using std.reflection
44+
Using std.syntax.variantcasts
45+
46+
Function Attribs:VAttribs[]( pair:String )
47+
Return New TAttribs(pair).Bind()
48+
End
49+
50+
Class TAttribs
51+
52+
Method New( attribs:String )
53+
_pair=attribs
54+
End
55+
56+
Method Bind:VAttribs[]()
57+
58+
Local e:=_pair.Split(",")
59+
Local tkn:String[]
60+
Local data:=New Stack<Variant>
61+
62+
For Local i:=Eachin e
63+
tkn = i.Replace("~n","").Split(":")
64+
tkn[0] = Unquote(tkn[0]).Trim()
65+
tkn[1] = Unquote(tkn[1]).Trim()
66+
67+
data.Add(Cast<Variant>(tkn[0]))
68+
data.Add(Cast<Variant>(tkn[1]))
69+
End
70+
71+
Return data.ToArray()
72+
End
73+
74+
Field _pair:String
75+
End
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
Namespace std.syntax
3+
4+
#rem @pro Attributes Minilibrary
5+
@author iDkP from GaragePixel
6+
@since 2025-07-04
7+
@example
8+
9+
Function myfn(attribs:VAttribs[])
10+
Local vars:=Bind(args)
11+
Local height:=Int(vars.Get("height"))
12+
Local width:=Float(vars.Get("width"))
13+
14+
Print width+height
15+
End
16+
17+
Function Main()
18+
19+
myfn(New VAttribs[]( "title" , "myTitle",
20+
"height", 10,
21+
"width" , 10.5 ))
22+
End
23+
#end
24+
25+
'#Import "../../types/types"
26+
'#Import "../../system/reflection/reflection"
27+
'#Import "../primitives/variants/variantcasts"
28+
29+
Using std.types..
30+
Using std.reflection
31+
Using std.syntax.variantcasts
32+
33+
Alias VAttribs:Variant 'Conveniant alias alternative name, usually used in conjonction with a container []
34+
'It should be remplaced by Var, who was reserved by Mark for this usage.
35+
36+
Alias VarAttribs:cVarAttribs<String,TVar>
37+
38+
Function Bind:VarAttribs(v:Variant[]) 'VarAttribs is an array of variant, interleaved as string,variant,string,variant...
39+
Return New VarAttribs(v,True) 'Then VarAttribs is then used to make a Map<String,Variant> called VarAttribs
40+
End
41+
42+
Class cVarAttribs<K,V> Extends Map<K,V> Where K=String And V=TVar
43+
44+
'Should be remplaced by Var, who was reserved by Mark for this usage.
45+
'The generic types are locked by the VarAttribs alias. We don't have to try calling
46+
'cVarAttribs from another type declaration or alias. Only the VarAttribs call is valid.
47+
48+
Method New( attrs:Variant[], t:Bool=True ) Override
49+
Assert((attrs.Length Mod 2) = 0,"Pair missing: array length must be even.")
50+
Local len:=attrs.Length/2
51+
52+
For Local i:=0 Until len+2 Step 2
53+
local tvar:=New TVar()
54+
tvar.Set(attrs[i+1])
55+
tvar.Type=stdlib.reflection.GetType(attrs[i+1])
56+
Add(Cast<String>(attrs[i]),tvar)
57+
End
58+
End
59+
60+
Method New( attrs:Stack<Variant>, t:Bool=True ) Override
61+
Assert((attrs.Length Mod 2) = 0,"Pair missing: array length must be even.")
62+
Local len:=attrs.Length/2
63+
64+
For Local i:=0 Until len+2 Step 2
65+
local tvar:=New TVar()
66+
tvar.Set(attrs[i+1])
67+
tvar.Type=stdlib.reflection.GetType(attrs[i+1])
68+
Add(Cast<String>(attrs[i]),tvar)
69+
End
70+
End
71+
72+
Method GetT<T>:T( key:K )
73+
Local node:=FindNode( key )
74+
If node Return node.Value
75+
Return Null
76+
End
77+
78+
Method GetBool:Bool( key:K )
79+
Return Bool(Get(key))
80+
End
81+
82+
Method GetInt:Int( key:K )
83+
Return Int(Get(key))
84+
End
85+
86+
Method GetFloat:Int( key:K )
87+
Return Float(Get(key))
88+
End
89+
90+
Method GetString:String( key:K )
91+
Return String(Get(key))
92+
End
93+
94+
Method GetB:Bool( key:K )
95+
Return GetBool(key)
96+
End
97+
98+
Method GetI:Int( key:K )
99+
Return GetInt(key)
100+
End
101+
102+
Method GetF:Float( key:K )
103+
Return GetFloat(key)
104+
End
105+
106+
Method GetS:String( key:K )
107+
Return GetString(key)
108+
End
109+
110+
End

0 commit comments

Comments
 (0)