calc_ast

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 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:

["Integer","007"] <nil>
["Integer","007"] <nil>
["MulDivExpr",[["Integer","007"]]] <nil>
["AddSubExpr",[["MulDivExpr",[["Integer","007"]]]]] <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:

["AddSubExpr",[["MulDivExpr",[["Integer","1"]]],["AddSub","+"],["MulDivExpr",[["Integer","1"]]]]] <nil>
["AddSubExpr",[["MulDivExpr",[["Integer","1"]]],["AddSub","+"],["MulDivExpr",[["Integer","1"],["MulDiv","*"],["Integer","1"]]]]] <nil>
["AddSubExpr",[["MulDivExpr",[["Integer","1"]]],["AddSub","+"],["MulDivExpr",[["Integer","1"],["MulDiv","*"],["Integer","1"]]],["AddSub","+"],["MulDivExpr",[["Integer","1"]]]]] <nil>
["AddSubExpr",[["MulDivExpr",[["Integer","1"]]],["AddSub","+"],["MulDivExpr",[["Integer","1"],["MulDiv","*"],["AddSubExpr",[["MulDivExpr",[["Integer","1"]]],["AddSub","+"],["MulDivExpr",[["Integer","1"]]]]]]]]] <nil>
["AddSubExpr",[["MulDivExpr",[["AddSubExpr",[["MulDivExpr",[["Integer","1"]]],["AddSub","+"],["MulDivExpr",[["Integer","1"]]]]],["MulDiv","*"],["AddSubExpr",[["MulDivExpr",[["Integer","1"]]],["AddSub","+"],["MulDivExpr",[["Integer","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