2020-11-10 21:37:30 +00:00
|
|
|
/*
|
|
|
|
Serene --- Yet an other Lisp
|
|
|
|
|
|
|
|
Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-11-15 22:16:48 +00:00
|
|
|
package core
|
2020-11-15 21:38:09 +00:00
|
|
|
|
2020-11-19 19:14:06 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"serene-lang.org/bootstrap/pkg/ast"
|
|
|
|
)
|
|
|
|
|
2020-11-20 16:43:07 +00:00
|
|
|
// Function struct represent a user defined function.
|
2020-11-19 19:14:06 +00:00
|
|
|
type Function struct {
|
2020-11-20 16:43:07 +00:00
|
|
|
// Node struct holds the necessary functions to make
|
|
|
|
// Functions locatable
|
2020-11-19 19:14:06 +00:00
|
|
|
Node
|
2020-11-20 16:43:07 +00:00
|
|
|
|
|
|
|
// 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.
|
2020-11-19 19:14:06 +00:00
|
|
|
params IColl
|
2020-11-20 16:43:07 +00:00
|
|
|
|
|
|
|
// A reference to the body block of the function
|
|
|
|
body *Block
|
2020-11-19 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Function) GetType() ast.NodeType {
|
|
|
|
return ast.Fn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Function) String() string {
|
|
|
|
return fmt.Sprintf("<Fn: %s at %p", 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 {
|
|
|
|
return fmt.Sprintf("<Fn: %s at %p", f.name, f)
|
|
|
|
}
|
|
|
|
|
2020-11-19 22:17:50 +00:00
|
|
|
func (f *Function) GetBody() *Block {
|
2020-11-19 19:14:06 +00:00
|
|
|
return f.body
|
|
|
|
}
|
|
|
|
|
2020-11-20 16:43:07 +00:00
|
|
|
// MakeFunction Create a function with the given `params` and `body` in
|
|
|
|
// the given `scope`.
|
2020-11-19 22:17:50 +00:00
|
|
|
func MakeFunction(scope IScope, params IColl, body *Block) *Function {
|
2020-11-19 19:14:06 +00:00
|
|
|
return &Function{
|
|
|
|
scope: scope,
|
|
|
|
params: params,
|
|
|
|
body: body,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 16:43:07 +00:00
|
|
|
// MakeFnScope a new scope for the body of a function. It binds the `bindings`
|
|
|
|
// to the given `values`.
|
2020-11-24 18:27:48 +00:00
|
|
|
func MakeFnScope(rt *Runtime, parent IScope, bindings IColl, values IColl) (*Scope, IError) {
|
2020-11-19 19:14:06 +00:00
|
|
|
scope := MakeScope(parent.(*Scope))
|
|
|
|
|
|
|
|
// TODO: Implement destructuring
|
2020-11-20 16:43:07 +00:00
|
|
|
|
2020-11-19 19:14:06 +00:00
|
|
|
if bindings.Count() > values.Count() {
|
2020-11-24 18:27:48 +00:00
|
|
|
return nil, MakeError(rt, "'binding' and 'valuse' size don't match")
|
2020-11-19 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
binds := bindings.ToSlice()
|
|
|
|
exprs := values.ToSlice()
|
|
|
|
|
|
|
|
for i := 0; i < len(binds); i += 1 {
|
2020-11-20 16:43:07 +00:00
|
|
|
// If an argument started with char `&` use it to represent
|
|
|
|
// rest of values.
|
|
|
|
//
|
|
|
|
// for example: `(fn (x y &z) ...)`
|
2020-11-19 19:14:06 +00:00
|
|
|
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
|
|
|
|
}
|