diff --git a/LINQ/LINQ/Interpreter/Expressions/FuncEval.vb b/LINQ/LINQ/Interpreter/Expressions/FuncEval.vb new file mode 100644 index 0000000..63078ae --- /dev/null +++ b/LINQ/LINQ/Interpreter/Expressions/FuncEval.vb @@ -0,0 +1,34 @@ +Imports LINQ.Runtime + +Namespace Interpreter.Expressions + + Public Class FuncEval : Inherits Expression + + Public Property func As Expression + Public Property parameters As Expression() + + Public Overrides Function Exec(env As Environment) As Object + Dim invoke As Object = func.Exec(env) + + If invoke Is Nothing Then + Throw New NullReferenceException + ElseIf TypeOf invoke Is String Then + invoke = env.FindInvoke(invoke) + ElseIf TypeOf invoke Is SymbolReference Then + invoke = env.FindInvoke(DirectCast(invoke, SymbolReference).symbolName) + Else + Throw New NotImplementedException + End If + + Dim args As New List(Of Object) + + For Each item In parameters + args.Add(item.Exec(env)) + Next + + Dim result As Object = invoke(args.ToArray) + + Return result + End Function + End Class +End Namespace \ No newline at end of file diff --git a/LINQ/LINQ/LINQ.vbproj b/LINQ/LINQ/LINQ.vbproj index 1ef680f..32889ca 100644 --- a/LINQ/LINQ/LINQ.vbproj +++ b/LINQ/LINQ/LINQ.vbproj @@ -158,6 +158,7 @@ + @@ -196,6 +197,9 @@ Settings.settings True + + + diff --git a/LINQ/LINQ/Runtime/Callable.vb b/LINQ/LINQ/Runtime/Callable.vb new file mode 100644 index 0000000..b1ee5ff --- /dev/null +++ b/LINQ/LINQ/Runtime/Callable.vb @@ -0,0 +1,31 @@ +Imports System.Reflection + +Namespace Runtime + + Public Class Callable + + Dim method As MethodInfo + + Public ReadOnly Property name As String + Get + Return method.Name + End Get + End Property + + Sub New(method As MethodInfo) + Me.method = method + End Sub + + Sub New(math1 As Func(Of Double, Double)) + Call Me.New(math1.Method) + End Sub + + Sub New(math2 As Func(Of Double, Double, Double)) + Call Me.New(math2.Method) + End Sub + + Public Function Evaluate(params As Object()) As Object + + End Function + End Class +End Namespace \ No newline at end of file diff --git a/LINQ/LINQ/Runtime/Environment.vb b/LINQ/LINQ/Runtime/Environment.vb index 64c5abe..52e7f35 100644 --- a/LINQ/LINQ/Runtime/Environment.vb +++ b/LINQ/LINQ/Runtime/Environment.vb @@ -27,6 +27,10 @@ Namespace Runtime Me.parent = parent End Sub + Public Function FindInvoke(name As String) As Callable + Return InternalInvoke.FindInvoke(name) + End Function + Public Function HasSymbol(name As String) As Boolean If symbols.ContainsKey(name) Then Return True diff --git a/LINQ/LINQ/Runtime/InternalInvoke.vb b/LINQ/LINQ/Runtime/InternalInvoke.vb new file mode 100644 index 0000000..05cb077 --- /dev/null +++ b/LINQ/LINQ/Runtime/InternalInvoke.vb @@ -0,0 +1,30 @@ +Imports System.Reflection + +Namespace Runtime + + Public Class InternalInvoke + + Shared ReadOnly invokes As Dictionary(Of String, Callable) + + Shared Sub New() + Call loadInternal(Of Math)() + End Sub + + Private Shared Sub loadInternal(Of T As Class)() + Dim type As TypeInfo = GetType(T) + Dim fields As FieldInfo() = type.DeclaredFields _ + .Where(Function(m) + Return m.IsStatic AndAlso m.FieldType Is GetType(Callable) + End Function) _ + .ToArray + + For Each item As FieldInfo In fields + invokes(item.Name) = item.GetValue(Nothing) + Next + End Sub + + Public Shared Function FindInvoke(name As String) As Callable + Return invokes.TryGetValue(name) + End Function + End Class +End Namespace \ No newline at end of file diff --git a/LINQ/LINQ/Runtime/Math.vb b/LINQ/LINQ/Runtime/Math.vb new file mode 100644 index 0000000..92d1b32 --- /dev/null +++ b/LINQ/LINQ/Runtime/Math.vb @@ -0,0 +1,12 @@ +Imports stdnum = System.Math + +Namespace Runtime + + Friend Class Math + + Public Shared ReadOnly abs As New Callable(AddressOf stdnum.Abs) + Public Shared ReadOnly min As New Callable(AddressOf stdnum.Min) + Public Shared ReadOnly max As New Callable(AddressOf stdnum.Max) + + End Class +End Namespace \ No newline at end of file diff --git a/LINQ/test/query.linq b/LINQ/test/query.linq index ffaf472..054f084 100644 --- a/LINQ/test/query.linq +++ b/LINQ/test/query.linq @@ -9,5 +9,5 @@ SELECT x.LipidIon, x.Formula, x.PeakQuality, x."m-Score" -ORDER BY lipidName +ORDER BY pow("m-Score", PeakQuality) TAKE 15 \ No newline at end of file