Click here to Skip to main content
15,888,610 members
Articles / Programming Languages / C#

A Calculation Engine for .NET

Rate me:
Please Sign up or sign in to vote.
4.92/5 (183 votes)
1 Sep 2013Public Domain15 min read 658.9K   11.4K   421  
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 A Public Domain dedication


Written By
Software Developer
Brazil Brazil
Software Architect/Developer with several years experience creating and delivering software.

Full-stack Web development (including React, Firebase, TypeScript, HTML, CSS), Entity Framework, C#, MS SQL Server.

Passionate about new technologies and always keen to learn new things as well as improve on existing skills.

Comments and Discussions