goexpression

package module
v0.0.0-...-50dd7b6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2023 License: MIT Imports: 5 Imported by: 0

README

GO Expression Evaluator

GO MIT

A Simple Expression Evaluator for Arithmetic Operations written in Go

Introduction

This project is a simple expression evaluator written in Go. The evaluator can handle basic arithmetic operations (+, -, *, /), parentheses (()), and unary operators. The main components of the project are:

  1. Tokenizer: The tokenizer is responsible for converting the input expression into a stream of tokens. Each token represents a specific element, such as a number, operator, or parenthesis.

  2. AST (Abstract Syntax Tree): After tokenization, the tokens are used to construct an Abstract Syntax Tree (AST). The AST is a hierarchical representation of the expression that makes it easier to evaluate the expression later.

  3. DFS Evaluation: The evaluator uses a Depth-First Search (DFS) algorithm to traverse the AST and perform the expression evaluation. This process considers operator precedence and handles nested expressions efficiently.

I made this project to discover the world of expression evalution.

Usage

How to Use

You can utilize the expression evaluator by calling the Evaluate function with your expression as a string argument. The function will return the result of the expression evaluation as a float64.

package main

import (
	"fmt"
	evaluator "github.com/la-ref/go-expression-evaluator/goexpression"
)

func main() {
	expression := "(2 + 3) * 4 - 6 / 2"
	result, err := evaluator.Evaluate(expression)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println("Result:", result) // Result: 17
}

Examples

Here are some example expressions that the evaluator can handle:

  • Basic arithmetic: 2 + 3 * 4 - 6 / 2
  • Parentheses: (2 + 3) * 4
  • Unary operators: -3 + 5

Getting Started

To use the expression evaluator in your project, follow these steps:

  1. Clone this repository to your local machine:
    git clone https://github.com/la-ref/go-expression-evaluator
    cd go-expression-evaluator
    
    OR
    go get -u github.com/la-ref/goexpression
    
  2. Implement the evaluator package in your Go code. Use the provided Evaluate function to evaluate expressions.
  3. Customize the evaluator to fit your project requirements. You can add more functionalities or optimize the code as needed.

Contribution

Contributions are welcome! If you find any issues or want to add new features, feel free to submit a pull request. Please make sure to follow the Go coding conventions.

Support Me

Give me a ⭐ if this project was helpful in any way!

Licence

This project is licensed under the MIT License. You are free to use, modify, and distribute this code following the terms of the license

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Evaluate

func Evaluate(value string) (float64, error)

func GetTypeValue

func GetTypeValue(t TokenType) (string, error)

Types

type AST

type AST struct {
	T     ASTType `json:"type"`
	Value any     `json:"value"`
	Left  *AST    `json:"left"`
	Right *AST    `json:"right"`
}

func Parse

func Parse(value string) (*AST, error)

func (*AST) AddLeft

func (a *AST) AddLeft(l *AST)

func (*AST) AddRight

func (a *AST) AddRight(r *AST)

func (*AST) Print

func (a *AST) Print()

type ASTType

type ASTType int
const (
	OPERATOR ASTType = iota
	UNARY
	VALUE
)

func (ASTType) MarshalJSON

func (t ASTType) MarshalJSON() ([]byte, error)

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

func (*Parser) ParseExpression

func (p *Parser) ParseExpression() (*AST, error)

Parse addition and substraction

func (*Parser) ParseFactor

func (p *Parser) ParseFactor() (*AST, error)

func (*Parser) ParseTerms

func (p *Parser) ParseTerms() (*AST, error)

Parse Multiplication and division

func (*Parser) Pop

func (p *Parser) Pop()

type Token

type Token struct {
	// contains filtered or unexported fields
}

func NewToken

func NewToken(value string) (*Token, error)

Create a new token with its type and its value

type TokenType

type TokenType int
const (
	NUMBER TokenType = iota
	OPERATION_PLUS
	OPERATION_MINUS
	OPERATION_MULTIPLY
	OPERATION_DIVIDE
	PARENTHESIS_LEFT
	PARENTHESIS_RIGHT
)

Token types

func CheckType

func CheckType[T string | Char](value T) (TokenType, error)

type Tokenizer

type Tokenizer struct {
	// contains filtered or unexported fields
}

func NewTokenizer

func NewTokenizer(input string) (*Tokenizer, error)

func (*Tokenizer) AppendToken

func (p *Tokenizer) AppendToken(t TokenType, value string) error

Add a new token at the end of the slice

func (*Tokenizer) PopToken

func (p *Tokenizer) PopToken() *Token

Pop the first token of the slice and returns it

func (*Tokenizer) Print

func (p *Tokenizer) Print()

func (*Tokenizer) Tokenize

func (p *Tokenizer) Tokenize(input string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL