Documentation
¶
Index ¶
- func AddSub(p *ast.Parser) (*ast.Node, error)
- func Evaluate(s string) (int, error)
- func EvaluateNode(n *ast.Node) int
- func Factor(p *ast.Parser) (*ast.Node, error)
- func Integer(p *ast.Parser) (*ast.Node, error)
- func MulDiv(p *ast.Parser) (*ast.Node, error)
- func Parse(s string) (*ast.Node, error)
- func Space(p *ast.Parser) (*ast.Node, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Evaluate ¶
Example ¶
package main
import (
"fmt"
calc "github.com/di-wu/parser/examples/calculator/ast"
)
func main() {
fmt.Println(calc.Evaluate("1 + 1"))
fmt.Println(calc.Evaluate("1 + 1 * 1"))
fmt.Println(calc.Evaluate("1 + 1 * 1 + 1"))
fmt.Println(calc.Evaluate("1 + 1 * (1 + 1)"))
fmt.Println(calc.Evaluate("(1 + 1) * (1 + 1)"))
}
Output: 2 <nil> 2 <nil> 3 <nil> 3 <nil> 4 <nil>
func EvaluateNode ¶
func Integer ¶
Example ¶
package main
import (
"fmt"
"github.com/di-wu/parser/ast"
calc "github.com/di-wu/parser/examples/calculator/ast"
)
func main() {
p := func(s string) *ast.Parser {
p, _ := ast.New([]byte(s))
return p
}
fmt.Println(p("007").Expect(calc.Integer))
fmt.Println(p("007").Expect(calc.Factor))
fmt.Println(p("007").Expect(calc.MulDiv))
fmt.Println(p("007").Expect(calc.AddSub))
}
Output: [005] 7 <nil> [005] 7 <nil> [002] [[005] 7] <nil> [001] [[002] [[005] 7]] <nil>
func Parse ¶
Example ¶
package main
import (
"fmt"
calc "github.com/di-wu/parser/examples/calculator/ast"
)
func main() {
fmt.Println(calc.Parse("1 + 1"))
fmt.Println(calc.Parse("1 + 1 * 1"))
fmt.Println(calc.Parse("1 + 1 * 1 + 1"))
fmt.Println(calc.Parse("1 + 1 * (1 + 1)"))
fmt.Println(calc.Parse("(1 + 1) * (1 + 1)"))
}
Output: [001] [[002] [[005] 1], [003] 43, [002] [[005] 1]] <nil> [001] [[002] [[005] 1], [003] 43, [002] [[005] 1, [004] 42, [005] 1]] <nil> [001] [[002] [[005] 1], [003] 43, [002] [[005] 1, [004] 42, [005] 1], [003] 43, [002] [[005] 1]] <nil> [001] [[002] [[005] 1], [003] 43, [002] [[005] 1, [004] 42, [001] [[002] [[005] 1], [003] 43, [002] [[005] 1]]]] <nil> [001] [[002] [[001] [[002] [[005] 1], [003] 43, [002] [[005] 1]], [004] 42, [001] [[002] [[005] 1], [003] 43, [002] [[005] 1]]]] <nil>
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.