aboutsummaryrefslogtreecommitdiff
path: root/example/math_expression/main.go
blob: 5cabaa64036596d2e0de4e5bb549a28b33a05a35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main

import (
	"fmt"

	"go.neonxp.ru/unilex"
)

const (
	LP unilex.LexType = iota
	RP
	NUMBER
	OPERATOR
)

func main() {

	l := unilex.New("10 * (20.0 + 30.0)")

	go l.Run(lexExpression) // Start lexer

	// Read infix expression lexems from lexer and convert them to RPN (reverse polish notation)
	rpn := infixToRPNotation(l)
	fmt.Println("RPN:", rpn)

	// Calculate RPN
	result := calculateRPN(rpn)
	fmt.Println("Result:", result)
}

func lexExpression(l *unilex.Lexer) unilex.StateFunc {
	l.AcceptWhile(" \t")
	l.Ignore() // Ignore whitespaces

	switch {
	case l.Accept("("):
		l.Emit(LP)
	case l.Accept(")"):
		l.Emit(RP)
	case unilex.ScanNumber(l):
		l.Emit(NUMBER)
	case l.Accept("+-*/^!"):
		l.Emit(OPERATOR)
	case l.Peek() == unilex.EOF:
		return nil
	default:
		return l.Errorf("Unexpected symbol")
	}

	return lexExpression
}