Example #1
0
File: eval.go Project: jbert/gol
func (e *Evaluator) evalProgn(np *gol.NodeProgn) (gol.Node, error) {
	// Value if no children
	var lastVal gol.Node
	lastVal = gol.NewNodeList()

	body := np.Rest()
	err := body.Foreach(func(child gol.Node) error {
		v, err := e.Eval(child)
		if err != nil {
			return err
		}
		lastVal = v
		return nil
	})
	if err != nil {
		return nil, err
	}

	// Return last
	return lastVal, nil
}
Example #2
0
File: compile.go Project: jbert/gol
func (gb *GolangBackend) compileProgn(progn *gol.NodeProgn) (string, error) {
	if progn.Len() == 0 {
		return "", nil
	}

	first := true

	golangRetType, err := golangStringForType(progn.Type())
	if err != nil {
		return "", err
	}
	lines := []string{fmt.Sprintf("func() %s {", golangRetType)}

	err = progn.ForeachLast(func(n gol.Node, last bool) error {
		if first {
			first = false
			return nil
		}
		s, err := gb.compile(n)
		if err != nil {
			return err
		}
		if last {
			s = "return " + s
		}
		lines = append(lines, s)
		return nil
	})
	if err != nil {
		return "", err
	}
	lines = append(lines, `
}()`)

	return strings.Join(lines, "\n"), nil
}