Exemple #1
0
func parseDefine(node *ast.Tuple) ast.Node {
	fmt.Println("#parseDefine")
	nNode := len(node.Nodes)
	if nNode != 3 {
		panic("unexpeced define expression")
	}
	// implicit lambda expressions, e.g., (define (add x y) (+ x y))
	var vars ast.Node
	var expr ast.Node

	if node.Nodes[1].Type() == const_.TUPLE {
		t := node.Nodes[1].(*ast.Tuple)
		vars = t.Nodes[0]

		var args []ast.Node
		for _, node := range t.Nodes[1:] {
			args = append(args, node)
		}

		body := parseNode(node.Nodes[2])
		expr = ast.NewLambda(ast.NewTuple(args), body)
	} else {

		// 1. normal definition, e.g. (defien a 1)
		// 2. explicit lambda expressions, e.g. (define inc (lambda (x) (+ x 1)))
		vars = node.Nodes[1]
		expr = parseNode(node.Nodes[2])
	}

	return ast.NewDefine(vars, expr)
}
Exemple #2
0
func parseLambda(node *ast.Tuple) ast.Node {
	fmt.Println("#parseLambda")
	nNode := len(node.Nodes)
	if nNode != 3 {
		panic("unexpected lambda procedure")
	}

	formals := node.Nodes[1]
	body := parseNode(node.Nodes[2])

	return ast.NewLambda(formals, body)
}