1 #Region "Microsoft.VisualBasic::592555dd2c6e5c688ee4eb66f89500f4, Microsoft.VisualBasic.Core\Language\Runtime.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class ArgumentReference
35     
36     '         Properties: Expression, Key
37     
38     '         FunctionToString
39     '         Operators: <>, =
40     
41     '     Class TypeSchema
42     
43     '         Properties: Type
44     
45     '         Constructor: (+1 OverloadsSub New
46     '         Function: Equals, ToString
47     '         Operators: (+2 OverloadsAnd, (+2 OverloadsOr
48     
49     '     Class Runtime
50     
51     '         FunctionToString
52     
53     
54     ' /********************************************************************************/
55
56 #End Region
57
58 Imports System.Runtime.CompilerServices
59 Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic
60 Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel.Repository
61 Imports Microsoft.VisualBasic.Emit.Delegates
62 Imports Microsoft.VisualBasic.Language.Default
63
64 Namespace Language
65
66     Public Class ArgumentReference : Implements INamedValue
67
68         Public name$, value
69
70         Private Property Key As String Implements IKeyedEntity(Of String).Key
71             Get
72                 Return name
73             End Get
74             Set(value As String)
75                 name = value
76             End Set
77         End Property
78
79         Public ReadOnly Property Expression(Optional null$ = "Nothing",
80                                             Optional stringEscaping As Func(Of StringString) = Nothing,
81                                             Optional isVar As Assert(Of String) = NothingAs String
82             Get
83                 Dim val$
84
85                 Static [isNot] As New DefaultValue(Of Assert(Of String))(Function(var) False)
86
87                 If value Is Nothing Then
88                     val = null
89                 ElseIf value.GetType Is GetType(String) Then
90                     If (isVar Or [isNot])(value) Then
91                         val = value
92                     Else
93                         val = $"""{(stringEscaping Or noEscaping)(value)}"""
94                     End If
95                 ElseIf value.GetType Is GetType(Char) Then
96                     val = $"""{value}"""
97                 Else
98                     val = value
99                 End If
100
101                 Return $"{name} = {val}"
102             End Get
103         End Property
104
105         Public Overrides Function ToString() As String
106             Return $"Dim {name} As Object = {Scripting.ToString(value, "null")}"
107         End Function
108
109         ''' <summary>
110         ''' Argument variable value assign
111         ''' </summary>
112         ''' <param name="var">The argument name</param>
113         ''' <param name="value">argument value</param>
114         ''' <returns></returns>
115         <MethodImpl(MethodImplOptions.AggressiveInlining)>
116         Public Shared Operator =(var As ArgumentReference, value As ObjectAs ArgumentReference
117             var.value = value
118             Return var
119         End Operator
120
121         Public Shared Operator <>(var As ArgumentReference, value As ObjectAs ArgumentReference
122             Throw New NotImplementedException
123         End Operator
124     End Class
125
126     Public Class TypeSchema
127
128         Public ReadOnly Property Type As Type
129
130         Sub New(type As Type)
131             Me.Type = type
132         End Sub
133
134         Public Overrides Function ToString() As String
135             Return Type.FullName
136         End Function
137
138         <MethodImpl(MethodImplOptions.AggressiveInlining)>
139         Public Overloads Shared Operator And(info As TypeSchema, types As Type()) As Boolean
140             Return types.All(Function(t) Equals(info.Type, base:=t))
141         End Operator
142
143         Private Overloads Shared Function Equals(info As Type, base As Type) As Boolean
144             If info.IsInheritsFrom(base) Then
145                 Return True
146             Else
147                 If base.IsInterface AndAlso info.ImplementInterface(base) Then
148                     Return True
149                 Else
150                     Return False
151                 End If
152             End If
153         End Function
154
155         <MethodImpl(MethodImplOptions.AggressiveInlining)>
156         Public Overloads Shared Operator Or(info As TypeSchema, types As Type()) As Boolean
157             Return types.Any(Function(t) Equals(info.Type, base:=t))
158         End Operator
159     End Class
160
161     ''' <summary>
162     ''' Runtime helper
163     ''' 
164     ''' ```vbnet
165     ''' Imports VB = Microsoft.VisualBasic.Language.Runtime
166     ''' 
167     ''' With New VB
168     '''     ' ...
169     ''' End With
170     ''' ```
171     ''' </summary>
172     Public Class Runtime
173
174         ''' <summary>
175         ''' Language syntax supports for argument list
176         ''' </summary>
177         ''' <param name="name$"></param>
178         ''' <returns></returns>
179         Default Public ReadOnly Property Argument(name$) As ArgumentReference
180             <MethodImpl(MethodImplOptions.AggressiveInlining)>
181             Get
182                 Return New ArgumentReference With {
183                     .name = name
184                 }
185             End Get
186         End Property
187
188         Public Overrides Function ToString() As String
189             Return "sciBASIC for VB.NET language runtime API"
190         End Function
191     End Class
192 End Namespace