/* 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 isMacro bool } func (f *Function) GetType() ast.NodeType { return ast.Fn } func (f *Function) IsMacro() bool { return f.isMacro } func (f *Function) String() string { if f.isMacro { return fmt.Sprintf("", f.name, f) } return fmt.Sprintf("", f.name, f) } func (f *Function) GetName() string { // TODO: Handle ns qualified symbols here return f.name } func (f *Function) GetScope() IScope { return f.scope } func (f *Function) GetParams() IColl { return f.params } func (f *Function) ToDebugStr() string { if f.isMacro { return fmt.Sprintf("", f.name, f) } return fmt.Sprintf("", f.name, f) } func (f *Function) GetBody() *Block { return f.body } // MakeFunction Create a function with the given `params` and `body` in // the given `scope`. func MakeFunction(scope IScope, params IColl, body *Block) *Function { return &Function{ scope: scope, params: params, body: body, isMacro: false, } } // MakeFnScope a new scope for the body of a function. It binds the `bindings` // to the given `values`. func MakeFnScope(rt *Runtime, parent IScope, bindings IColl, values IColl) (*Scope, IError) { scope := MakeScope(parent.(*Scope)) // TODO: Implement destructuring if bindings.Count() > 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 }