Click here to Skip to main content
11,641,209 members (64,079 online)
Click here to Skip to main content
Add your own
alternative version

A Calculation Engine for .NET

, 1 Sep 2013 CPOL 176.7K 8.2K 394
A calculation engine that is small, fast, and extensible.
Friend Class Logical

    ' Methods
    Public Shared Sub Register(ByVal ce As CalcEngine)
        ce.RegisterFunction("AND", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Logical.And))
        ce.RegisterFunction("OR", 1, Integer.MaxValue, New CalcEngineFunction(AddressOf Logical.Or))
        ce.RegisterFunction("NOT", 1, New CalcEngineFunction(AddressOf Logical.Not))
        ce.RegisterFunction("IF", 3, New CalcEngineFunction(AddressOf Logical.If))
        ce.RegisterFunction("TRUE", 0, New CalcEngineFunction(AddressOf Logical.True))
        ce.RegisterFunction("FALSE", 0, New CalcEngineFunction(AddressOf Logical.False))
    End Sub

    Private Shared Function [And](ByVal p As List(Of Expression)) As Object
        Dim b As Boolean = True
        Dim v As Expression
        For Each v In p
            b = (b AndAlso CBool(v))
        Next
        Return b
    End Function

    Private Shared Function [False](ByVal p As List(Of Expression)) As Object
        Return False
    End Function

    Private Shared Function [If](ByVal p As List(Of Expression)) As Object
        If p.Item(0) Is Nothing Then
            Return p.Item(2).Evaluate
        Else
            If CBool(p.Item(0)) Then
                Return p.Item(1).Evaluate
            Else
                Return p.Item(2).Evaluate
            End If
        End If
    End Function

    Private Shared Function [Not](ByVal p As List(Of Expression)) As Object
        Return Not CBool(p.Item(0))
    End Function

    Private Shared Function [Or](ByVal p As List(Of Expression)) As Object
        Dim b As Boolean = False
        Dim v As Expression
        For Each v In p
            b = (b OrElse CBool(v))
        Next
        Return b
    End Function

    Private Shared Function [True](ByVal p As List(Of Expression)) As Object
        Return True
    End Function

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Bernardo Castilho
Chief Technology Officer ComponentOne
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 1 Sep 2013
Article Copyright 2011 by Bernardo Castilho
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid