calcgo icon indicating copy to clipboard operation
calcgo copied to clipboard

Interpreter for Numeric Expressions

Calcgo

Build Status codecov Godoc Go Report Card

This is an experimental learning project, to better understand the process of lexing and parsing.

Description

Calcgo exposes a lexer, parser and interpreter to get tokens, an ast and the result of a basic mathematical calculation. All three functions accept a language L(G) defined here.

The calculations follow basic math rules, like "multiplication and division first, then addition and subtraction" rule. To break this rule it is possible to use brackets. There needs to be at least one whitespace character between an operator an a number. All other whitespace character get ignored by the lexer.

Lexer:

lexer.Lex("(1 + 2) * 3")

Parser:

parser.Parse("(1 + 2) * 3")

Interpreter:

interpreter.Interpret("1 + 2 * 3")   // Result: 7
interpreter.Interpret("(1 + 2) * 3") // Result: 9

Interpreter with variable:

Calcgo supports variables. An instantiation of all variables has to be supplied before interpreting.

i := interpreter.NewInterpreter("1 + a")
i.SetVar("a", 1.0)
i.GetResult() // Result: 2
i.SetVar("a", 2.0)
i.GetResult() // Result: 3

Example

package main

import (
	"fmt"

	"github.com/relnod/calcgo"
)

func main() {
	number, _ := calcgo.Calc("1 + 1")

	fmt.Println(number)
}

Tests and Benchmarks

Running Tests

Run tests with go test -v -race ./... or with ginkgo -r -v.

Running Benchmarks

Benchmarks can be tun with go test -run=^$ -bench=. ./...

To see the differences between current branch and master run ./scripts/benchcmp.sh -n 5

License

This project is licensed under the MIT License. See the LICENSE file for details.