-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (54 loc) · 1.12 KB
/
main.go
File metadata and controls
66 lines (54 loc) · 1.12 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"infix_postfix/matcher"
"infix_postfix/model"
"unicode"
)
func main() {
infix := " 34 / 68 - 23 + 93 * ( 24 / 2 )-(4+ 38) "
expectedPostfix := "34 68 / 23 - 93 24 2 / * + 4 38 + -"
tokens := lex([]rune(infix))
ast := parse(tokens)
rpn := emit(ast)
for _, t := range tokens {
fmt.Println("t: " + string(t))
}
ast.Print(0)
fmt.Printf("%s\n%s\n%s\n", infix, rpn, expectedPostfix)
}
// Lexer
func lex(src []rune) (tokens []model.Lexeme) {
var lexeme model.Lexeme
for i := 0; i < len(src); i++ {
if src[i] == ' ' {
continue
}
lexeme = append(lexeme, src[i])
if i+1 == len(src) ||
!unicode.IsDigit(src[i]) ||
!unicode.IsDigit(src[i+1]) {
tokens = append(tokens, lexeme)
lexeme = nil
}
}
return
}
// Parser
func parse(tokens []model.Lexeme) *model.Node {
ctx := model.Context{
Tokens: tokens,
Index: 0,
}
return matcher.Additive(&ctx)
}
// RPN emitter
func emit(n *model.Node) (rpn string) {
if len(n.Children) >= 1 {
rpn += emit(n.Children[0])
}
if len(n.Children) >= 2 {
rpn += emit(n.Children[1])
}
return rpn + string(n.Content) + " "
}