calc_ast

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddSub

func AddSub(p *ast.Parser) (*ast.Node, error)

func Evaluate

func Evaluate(s string) (int, error)
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 EvaluateNode(n *ast.Node) int

func Factor

func Factor(p *ast.Parser) (*ast.Node, error)

func Integer

func Integer(p *ast.Parser) (*ast.Node, error)
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 MulDiv

func MulDiv(p *ast.Parser) (*ast.Node, error)

func Parse

func Parse(s string) (*ast.Node, error)
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>

func Space

func Space(p *ast.Parser) (*ast.Node, error)

Space consumes all the spaces.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL