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-12-16 18:57:54 +00:00
|
|
|
// Function implementations:
|
|
|
|
// * We have two different types of functions. User defined functions and
|
|
|
|
// native functions
|
|
|
|
// * User defined functions are represented via the `Function` struct.
|
|
|
|
// * Native functions are represented via the `NativeFunction` struct.
|
|
|
|
// * User defined functions gets evaluated by:
|
|
|
|
// - Creating a new scope a direct child of the scope that the function
|
|
|
|
// defined in
|
|
|
|
// - Creating bindings in the new scope to bind the passed values to their
|
|
|
|
// arguments names
|
|
|
|
// - Evaluate the body of the function in context of the new scope and return
|
|
|
|
// the result of the last expression
|
2020-12-25 22:03:21 +00:00
|
|
|
// * Native functions evaluates by calling the `Apply` method of the `IFn`
|
2020-12-16 18:57:54 +00:00
|
|
|
// interface which is quite simple.
|
|
|
|
//
|
|
|
|
// TODOs:
|
|
|
|
// * Support for multi-arity functions
|
|
|
|
// * Support for protocol functions
|
|
|
|
// * `IFn` protocol
|
|
|
|
|
2020-11-19 19:14:06 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"serene-lang.org/bootstrap/pkg/ast"
|
2020-12-20 18:09:06 +00:00
|
|
|
"serene-lang.org/bootstrap/pkg/hash"
|
2020-11-19 19:14:06 +00:00
|
|
|
)
|
|
|
|
|
2020-12-15 19:08:51 +00:00
|
|
|
type nativeFnHandler = func(rt *Runtime, scope IScope, n Node, args *List) (IExpr, IError)
|
|
|
|
|
2020-12-25 22:03:21 +00:00
|
|
|
type IFn interface {
|
|
|
|
ast.ILocatable
|
2020-12-15 19:08:51 +00:00
|
|
|
Apply(rt *Runtime, scope IScope, n Node, args *List) (IExpr, IError)
|
|
|
|
}
|
|
|
|
|
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-12-25 00:58:28 +00:00
|
|
|
ExecutionScope
|
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
|
2020-12-04 21:08:48 +00:00
|
|
|
body *Block
|
|
|
|
isMacro bool
|
2020-11-19 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 19:08:51 +00:00
|
|
|
type NativeFunction struct {
|
|
|
|
// Node struct holds the necessary functions to make
|
|
|
|
// Functions locatable
|
|
|
|
Node
|
2020-12-25 00:58:28 +00:00
|
|
|
ExecutionScope
|
2020-12-15 19:08:51 +00:00
|
|
|
name string
|
|
|
|
fn nativeFnHandler
|
|
|
|
}
|
|
|
|
|
2020-11-19 19:14:06 +00:00
|
|
|
func (f *Function) GetType() ast.NodeType {
|
|
|
|
return ast.Fn
|
|
|
|
}
|
|
|
|
|
2020-12-20 18:09:06 +00:00
|
|
|
func (f *Function) Hash() uint32 {
|
|
|
|
// TODO: Fix this function to return an appropriate hash for a function
|
|
|
|
return hash.HashOf([]byte(f.String()))
|
|
|
|
}
|
|
|
|
|
2020-12-04 21:08:48 +00:00
|
|
|
func (f *Function) IsMacro() bool {
|
|
|
|
return f.isMacro
|
|
|
|
}
|
|
|
|
|
2020-11-19 19:14:06 +00:00
|
|
|
func (f *Function) String() string {
|
2020-12-04 21:08:48 +00:00
|
|
|
if f.isMacro {
|
|
|
|
return fmt.Sprintf("<Macro: %s at %p>", f.name, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("<Fn: %s at %p>", f.name, f)
|
2020-11-19 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Function) GetName() string {
|
|
|
|
// TODO: Handle ns qualified symbols here
|
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
2020-12-24 16:28:12 +00:00
|
|
|
func (f *Function) SetName(name string) {
|
|
|
|
f.name = name
|
|
|
|
}
|
|
|
|
|
2020-11-19 19:14:06 +00:00
|
|
|
func (f *Function) GetScope() IScope {
|
|
|
|
return f.scope
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Function) GetParams() IColl {
|
|
|
|
return f.params
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Function) ToDebugStr() string {
|
2020-12-04 21:08:48 +00:00
|
|
|
if f.isMacro {
|
|
|
|
return fmt.Sprintf("<Macro: %s at %p>", f.name, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("<Fn: %s at %p>", f.name, f)
|
2020-11-19 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
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-12-24 16:28:12 +00:00
|
|
|
func (f *Function) Apply(rt *Runtime, scope IScope, n Node, args *List) (IExpr, IError) {
|
|
|
|
application := args.Cons(f)
|
|
|
|
return EvalForms(rt, scope, application)
|
|
|
|
}
|
|
|
|
|
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{
|
2020-12-04 21:08:48 +00:00
|
|
|
scope: scope,
|
|
|
|
params: params,
|
|
|
|
body: body,
|
|
|
|
isMacro: false,
|
2020-11-19 19:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-24 16:28:12 +00:00
|
|
|
binds := bindings.ToSlice()
|
|
|
|
exprs := values.ToSlice()
|
|
|
|
numberOfBindings := len(binds)
|
2020-11-20 16:43:07 +00:00
|
|
|
|
2020-12-25 00:58:28 +00:00
|
|
|
if len(binds) > 0 {
|
|
|
|
lastBinding := binds[len(binds)-1]
|
|
|
|
|
|
|
|
if lastBinding.GetType() == ast.Symbol && lastBinding.(*Symbol).IsRestable() {
|
|
|
|
numberOfBindings = len(binds) - 1
|
|
|
|
}
|
2020-11-19 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 16:28:12 +00:00
|
|
|
if numberOfBindings > len(exprs) {
|
|
|
|
if rt.IsDebugMode() {
|
|
|
|
fmt.Printf("[DEBUG] Mismatch on bindings and values: Bindings: %s, Values: %s\n", bindings, values)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, MakeError(rt,
|
2020-12-25 01:18:46 +00:00
|
|
|
fmt.Sprintf("expected '%d' arguments, got '%d'.", bindings.Count(), values.Count()))
|
2020-12-24 16:28:12 +00:00
|
|
|
}
|
2020-11-19 19:14:06 +00:00
|
|
|
|
|
|
|
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() {
|
2020-12-24 16:28:12 +00:00
|
|
|
|
|
|
|
if i != len(binds)-1 {
|
|
|
|
return nil, MakeErrorFor(rt, binds[i], "The function argument with '&' has to be the last argument.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the number of values are one less than the number of bindings
|
|
|
|
// but the last binding is a Restable (e.g &x) the the last bindings
|
|
|
|
// has to be an empty list. Note the check for number of vlaues comes
|
|
|
|
// next.
|
|
|
|
rest := MakeEmptyList()
|
|
|
|
|
|
|
|
if i == len(exprs)-1 {
|
|
|
|
// If the number of values matches the number of bindings
|
|
|
|
// or it is more than that create a list from them
|
|
|
|
// to pass it to the last argument that has to be Restable (e.g &x)
|
|
|
|
rest = MakeList(exprs[i:])
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.Insert(binds[i].(*Symbol).GetName()[1:], rest, false)
|
2020-11-19 19:14:06 +00:00
|
|
|
break
|
|
|
|
} else {
|
|
|
|
scope.Insert(binds[i].(*Symbol).GetName(), exprs[i], false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return scope, nil
|
|
|
|
}
|
2020-12-15 19:08:51 +00:00
|
|
|
|
|
|
|
func (f *NativeFunction) GetType() ast.NodeType {
|
|
|
|
return ast.NativeFn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NativeFunction) String() string {
|
2020-12-20 18:09:06 +00:00
|
|
|
return fmt.Sprintf("<NativeFn: %s at %p>", f.name, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NativeFunction) Hash() uint32 {
|
|
|
|
// TODO: Fix this function to return an appropriate hash for a function
|
|
|
|
return hash.HashOf([]byte(f.String()))
|
2020-12-15 19:08:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NativeFunction) ToDebugStr() string {
|
|
|
|
return fmt.Sprintf("<NativeFn: %s>", f.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NativeFunction) Apply(rt *Runtime, scope IScope, n Node, args *List) (IExpr, IError) {
|
|
|
|
return f.fn(rt, scope, n, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeNativeFn(name string, f nativeFnHandler) NativeFunction {
|
|
|
|
return NativeFunction{
|
|
|
|
name: name,
|
|
|
|
fn: f,
|
|
|
|
}
|
|
|
|
}
|