Eval for Swift - Easily evaluate simple expressions on the go

Overview

Eval for Swift

Easily evaluate simple expressions on the go...

This is a port of the BigEval.js/Eval.net library

Features:

  • Evaluate basic math operators (5 * (4 / 3))
  • Use constants (x * 27 / 4)
  • Support for pre-defined function calls (30 * pow(24, 6) / cos(20))
  • Support for custom function calls
  • Support for logic operators (26 * 3 < 100 - returns a bool value)
  • Support for bitwise operators ((1 << 2) == 4)
  • Support for string values ("test" + 5 == "test5")
  • Customize the type that is used for numeric values in the expression.
  • Customize the code behind the execution of any of the operators.
  • Support for compiling an expression and running multiple times while supplying different constants

Installation

Just add the repo in Swift Package Manager

Usage

import Eval
        
let config = DoubleEvalConfiguration()

let result1 = try Evaluator.execute("12+45*10", config) as? Double
let result2 = try Evaluator.execute("30 * pow(24, 6) / cos(20)", config) as? Double

let compiled = try Evaluator.compile("5 * n", config.clone())

compiled.setConstant(8, forName: "n")
let result3 = try compiled.execute() as? Double

compiled.setConstant(9, forName: "n")
let result4 = try compiled.execute() as? Double

Operators

The operators currently supported in order of precedence are -

[
    ['!'],  // Factorial
    ['**'],  // power
    ['/', '*', '%'],
    ['+', '-'],
    ['<<', '>>'],  // bit shifts
    ['<', '<=', '>', '>='],
    ['==', '=', '!='],   // equality comparisons
    ['&'], ['^'], ['|'],   // bitwise operations
    ['&&'], ['||']   // logical operations
]

Me

Help

If you want to buy me a beer, you are very welcome to Donate Thanks :-)

License

This library is under the Apache License 2.0.

This library is free and can be used in commercial applications without royalty.

You might also like...
Simple Background Fetch Tasks

Simple Background Fetch Tasks

A Simple way help you drop or drag your source (like UIImage) between different App.

A Simple way help you drop or drag your source (like UIImage) between different App.

A simple protocol package that does nothing

HasResult A simple protocol package that does nothing. The HasResult protocol has a simple result property and a ResultType associated type. This is m

A simple Hacker News mobile client

A simple Hacker News mobile client. Overview This app was built with the Hacker News API This is one of my first apps outside of a tut

A simple, reliable and scalable delivery API for transactional push notifications for websites and applications
A simple, reliable and scalable delivery API for transactional push notifications for websites and applications

Catapush is a simple, reliable and scalable delivery API for transactional push notifications for websites and applications. Ideal for sending data-dr

A simple To Do application written in React Native

Example To Do List application in react-native Requirements, install as needed: React Native: $ npm i -g react-native-cli watchman: $ brew install wa

Tofu - A simple Todo app built with SwiftUI, a REST API, and a local Realm cache
Tofu - A simple Todo app built with SwiftUI, a REST API, and a local Realm cache

Tofu A simple Todo app built with SwiftUI, a REST API, and a local Realm cache.

MicrofrontendGenerator - Script for creating micro frontends for Mobile in a simple and easy way

Introdução Template para a criação de SDK iOS. Existem duas opções de template:

ZakatFatoora - A simple way to implement e-invoicing (FATOORA) for iOS
ZakatFatoora - A simple way to implement e-invoicing (FATOORA) for iOS

ZakatFatoora - A simple way to implement e-invoicing (FATOORA) for iOS

Owner
Daniel Cohen Gindi
Daniel Cohen Gindi
Set `Open using Rosetta` option on Xcode easily

xcode-arch A utility to switch running architecture of Xcode on M1 mac. Motivation Currently, there is no way to toggle Open using Rosetta option othe

Takuhiro Muta 7 Aug 8, 2022
This is a Swift Package bundling different Train APIs into one simple Swift interface.

This is a Swift Package bundling different Train APIs into one simple Swift interface.

ICE Buddy 8 Jul 5, 2022
A simple Swift sample code to reads ISO 10303-21 exchange structure (STEP P21) file for AP242 schema.

simpleP21ReadSample A simple sample code to reads ISO 10303-21 exchange structure (STEP P21) file for AP242 schema. by Tsutomu Yoshida, Minokamo Japan

Tsutomu Yoshida 1 Nov 23, 2021
RMSUI - A Simple Swift MVVM architectured Rick & Morty UI app in order to practice SwiftUI & GraphQL

RMSUI A Simple Swift MVVM architectured "Rick & Morty UI" app ?? in order to pra

null 0 Jan 24, 2022
TTipBoxView is a simple and flexible UI component fully written in Swift

TTipBoxView is a simple and flexible UI component fully written in Swift. It is developed to help you create a hint/prompt view quickly, saving your time and avoiding having to write many lines of codes.

Nguyen Duc Thinh 3 Aug 18, 2022
A simple project which shows how to pull off custom view controller transitions.

Custom View Controller Transitions This project explains and shows how to make custom view controller transitions in the most simple way possible. Eac

Jordan Morgan 91 Oct 23, 2022
A simple deamon that restores your window positions on external monitors.

Memmon Memmon remembers what your Mac forgets – A simple deamon that restores your window positions on external monitors. FAQ Why‽ I am frustrated! Wh

Helena Schobs 55 Dec 1, 2022
A simple project that shows a list of local coffee shop reviews

Project This is a simple project that shows a list of local coffee shop reviews Details Please use git and commit often with meaningful commit message

null 0 Oct 16, 2021
This is a simple mobile app which is connect to the Twitter API

Project 3 - My Twitter My Twitter is a basic twitter app to read your tweets. Time spent on two parts: 8.5 hours spent in total Twitter - Part II This

Alem 1 Dec 14, 2022
A simple self-development challenge application that aimed to learn

Movie List Challenge A simple self-development challenge application that aimed to learn "The Composable Architecture" basics and build a reactive str

Nicat Muzaffarli 1 Oct 30, 2021