mirror of
https://github.com/Estom/notes.git
synced 2026-02-09 13:27:09 +08:00
go知识重新整理
This commit is contained in:
100
Go/DesignPattern/interpreter/interpreter.go
Normal file
100
Go/DesignPattern/interpreter/interpreter.go
Normal file
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* @Author:zhoutao
|
||||
* @Date:2020/12/12 下午3:05
|
||||
* @Desc:
|
||||
*/
|
||||
|
||||
package interpreter
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//解释器模式:定义了一套语言文法,并设计该语言的解释器。使用户能使用特定文法-》控制解释器的行为
|
||||
//解释器模式的意义在于,它分离多种复杂功能的实现,每个功能只需要关注自身的解释
|
||||
//对于调用者不同关心内部的解释器的具体工作,只需要用简单的方式组合命令就可以了。
|
||||
|
||||
type Node interface {
|
||||
Interpret() int
|
||||
}
|
||||
|
||||
type ValNode struct {
|
||||
val int
|
||||
}
|
||||
|
||||
func (v *ValNode) Interpret() int {
|
||||
return v.val
|
||||
}
|
||||
|
||||
//add
|
||||
type AddNode struct {
|
||||
left, right Node
|
||||
}
|
||||
|
||||
func (a *AddNode) Interpret() int {
|
||||
return a.left.Interpret() + a.right.Interpret()
|
||||
}
|
||||
|
||||
//min
|
||||
type MinNode struct {
|
||||
left, right Node
|
||||
}
|
||||
|
||||
func (m *MinNode) Interpret() int {
|
||||
return m.left.Interpret() - m.right.Interpret()
|
||||
}
|
||||
|
||||
//解析器
|
||||
type Parser struct {
|
||||
exp []string
|
||||
index int
|
||||
prev Node
|
||||
}
|
||||
|
||||
func (p *Parser) Parse(exp string) {
|
||||
p.exp = strings.Split(exp, " ")
|
||||
|
||||
for {
|
||||
if p.index >= len(p.exp) {
|
||||
return
|
||||
}
|
||||
|
||||
switch p.exp[p.index] {
|
||||
case "+":
|
||||
p.prev = p.newAddNode()
|
||||
case "_":
|
||||
p.prev = p.newMinNode()
|
||||
default:
|
||||
p.prev = p.newValNode()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Parser) newAddNode() Node {
|
||||
p.index++
|
||||
return &AddNode{
|
||||
left: p.prev,
|
||||
right: p.newValNode(),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Parser) newMinNode() Node {
|
||||
p.index++
|
||||
return &MinNode{
|
||||
left: p.prev,
|
||||
right: p.newValNode(),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Parser) newValNode() Node {
|
||||
v, _ := strconv.Atoi(p.exp[p.index])
|
||||
p.index++
|
||||
return &ValNode{
|
||||
val: v,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Parser) Result() Node {
|
||||
return p.prev
|
||||
}
|
||||
19
Go/DesignPattern/interpreter/interpreter_test.go
Normal file
19
Go/DesignPattern/interpreter/interpreter_test.go
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* @Author:zhoutao
|
||||
* @Date:2020/12/12 下午3:35
|
||||
* @Desc:
|
||||
*/
|
||||
|
||||
package interpreter
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestInterpreter(t *testing.T) {
|
||||
p := Parser{}
|
||||
p.Parse("1 + 2 + 3 - 4 + 5 - 6")
|
||||
res := p.Result().Interpret()
|
||||
expect := 1
|
||||
if res != expect {
|
||||
t.Fatalf("expect %d got %d", expect, res)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user