/* Serene --- Yet an other Lisp Copyright (c) 2020 Sameer Rahmani This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package core import ( "fmt" "serene-lang.org/bootstrap/pkg/ast" ) // Function struct represent a user defined function. type Function struct { // Node struct holds the necessary functions to make // Functions locatable Node // Name of the function, it can be empty and it has to be // set via `def` name string // Parent scope of the function. The scope which the function // is defined in scope IScope // A collection of arguments. Why IColl? because we can use // Lists and Vectors for the argument lists. Maybe even // hashmaps in future. params IColl // A reference to the body block of the function body *Block } func (f *Function) GetType() ast.NodeType { return ast.Fn } func (f *Function) String() string { return fmt.Sprintf(" values.Count() { return nil, MakeError(rt, "'binding' and 'valuse' size don't match") } binds := bindings.ToSlice() exprs := values.ToSlice() for i := 0; i < len(binds); i += 1 { // If an argument started with char `&` use it to represent // rest of values. // // for example: `(fn (x y &z) ...)` if binds[i].GetType() == ast.Symbol && binds[i].(*Symbol).IsRestable() { scope.Insert(binds[i+1].(*Symbol).GetName(), MakeList(exprs[i:]), false) break } else { scope.Insert(binds[i].(*Symbol).GetName(), exprs[i], false) } } return scope, nil }