Esempio n. 1
0
File: parse.go Progetto: albrow/calc
// E'2 -> "(" E ")"
func ep2(buf *token.Buffer, tree ast.Node) (newTree ast.Node, err error) {
	origPos := buf.Pos()
	defer func() {
		if err != nil {
			buf.MustSeek(origPos)
		}
	}()
	newTree = tree.Copy()
	expr := ast.New()
	newTree.AddChild(expr)
	if err := termOpenParen(buf); err != nil {
		return nil, err
	}
	if buf.Pos() >= buf.Len() {
		return nil, io.EOF
	}
	eTree, err := e(buf, tree.Copy())
	if err != nil {
		return nil, err
	}
	expr.AddChildren(eTree.Children())
	if buf.Pos() >= buf.Len() {
		return nil, io.EOF
	}
	if err := termCloseParen(buf); err != nil {
		return nil, err
	}
	return newTree, nil
}
Esempio n. 2
0
File: eval.go Progetto: albrow/calc
func Eval(tree ast.Node) (*big.Rat, error) {
	switch node := tree.(type) {
	case *ast.Number:
		return parseNumNode(node)
	case *ast.BaseNode:
		return evalNodes(tree.Children())
	default:
		return nil, fmt.Errorf("Unkown node type: %T", tree)
	}
}
Esempio n. 3
0
File: parse.go Progetto: albrow/calc
// E'1 -> Number
func ep1(buf *token.Buffer, tree ast.Node) (newTree ast.Node, err error) {
	origPos := buf.Pos()
	defer func() {
		if err != nil {
			buf.MustSeek(origPos)
		}
	}()
	node, err := termNumber(buf)
	if err != nil {
		return nil, err
	}
	newTree = tree.Copy()
	newTree.AddChild(node)
	return newTree, nil
}
Esempio n. 4
0
File: parse.go Progetto: albrow/calc
// E1 -> E' Op E
func e1(buf *token.Buffer, tree ast.Node) (newTree ast.Node, err error) {
	origPos := buf.Pos()
	defer func() {
		if err != nil {
			buf.MustSeek(origPos)
		}
	}()
	newTree = tree.Copy()
	epTree, err := ep(buf, tree.Copy())
	if err != nil {
		return nil, err
	}
	newTree.AddChildren(epTree.Children())
	if buf.Pos() >= buf.Len() {
		return nil, io.EOF
	}
	opNode, err := termOp(buf)
	if err != nil {
		return nil, err
	}
	newTree.AddChild(opNode)
	if buf.Pos() >= buf.Len() {
		return nil, io.EOF
	}
	eTree, err := e(buf, tree.Copy())
	if err != nil {
		return nil, err
	}
	newTree.AddChildren(eTree.Children())
	return newTree, nil
}