Skip to content

Math Expression Parser and Evaluator written in Golang.

License

Notifications You must be signed in to change notification settings

mhaxanali/go-math-eval

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mathematical Expression Parser / Evaluator in Go (Golang)

By mhaxanali, a remaster of math-evaluate in Go.

Flow of Program

┌──────────────────────────┐
│        START             │
└─────────────┬────────────┘
              │
              ▼
┌──────────────────────────┐
│ Take raw input string    │
│ (e.g. "3 + -2 * 5")      │
└─────────────┬────────────┘
              │
              ▼
┌──────────────────────────┐
│ Tokenize input           │
│ → numbers                │
│ → operators (+ - * /)    │
│ → parentheses            │
└─────────────┬────────────┘
              │
              ▼
┌──────────────────────────┐
│ Normalize tokens         │
│ (fix unary minus,        │
│ merge negative numbers)  │
└─────────────┬────────────┘
              │
              ▼
┌──────────────────────────┐
│ Validate syntax          │
│ - check characters       │
│ - check balanced ()      │
│ - check operator usage   │
└─────────────┬────────────┘
              │
              ▼
     ┌─────────────────┐
     │ parentheses?    │───Yes──────┐
     └───────┬─────────┘            │
             │No                    ▼
             ▼               ┌────────────────────────┐
┌──────────────────────────┐ │ Evaluate inner-most () │
│ Proceed to operator      │ │ recursively            │
│ precedence evaluation    │ └─────────────┬──────────┘
└─────────────┬────────────┘               │
              │                            ▼
              ▼                   replace (...) with result
┌──────────────────────────┐              │
│ Handle * and / first     │ <────────────┘
│ left → right             │
└─────────────┬────────────┘
              │
              ▼
┌──────────────────────────┐
│ Handle + and - next      │
│ left → right             │
└─────────────┬────────────┘
              │
              ▼
┌──────────────────────────┐
│ Single token left?       │
└───────┬────────┬─────────┘
        │Yes     │No (error)
        ▼        ▼
┌───────────────┐ ┌────────────────────────┐
│ Return result │ │ Throw "invalid expr"   │
└───────────────┘ └────────────────────────┘

Why?

I was learning Go and decided to remake one of my python projects in Go and this is just that.

Usage

  • Clone the repo
    git clone https://github.com/mhaxanali/go-math-eval
  • Navigate to the directory
    cd go-math-eval
  • Build the binary file
    go build math-eval/main.go
  • Run the program with expression as argument.
    ./main.exe <expression>

Notes

  • Use " " if providing spaces inside expression such as:
    ./main.exe "2 + 2"

About

Math Expression Parser and Evaluator written in Golang.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages