Add the Def, and Fn special forms

This commit is contained in:
Sameer Rahmani 2020-11-19 19:14:06 +00:00
parent ad37f02742
commit 680017f0ce
12 changed files with 249 additions and 51 deletions

3
.gitignore vendored
View File

@ -7,4 +7,5 @@ target/
\#*#
.\#*
*~
*~
bootstrap/serene

View File

@ -27,6 +27,7 @@ const (
Symbol
Number
List
Fn
)
type ILocatable interface {

View File

@ -26,3 +26,9 @@ type ISeq interface {
type ICountable interface {
Count() int
}
type IColl interface {
ISeq
ICountable
ToSlice() []IExpr
}

View File

@ -24,16 +24,7 @@ import (
"serene-lang.org/bootstrap/pkg/ast"
)
/** TODO:
Make sure to implement INode for Def as well to be able to point
to the write place at the input stream for error messages.
*/
type def struct{}
var Def = def{}
func (d def) Apply(rt *Runtime, scope IScope, args *List) (IExpr, error) {
func Def(rt *Runtime, scope IScope, args *List) (IExpr, error) {
switch args.Count() {
case 2:
name := args.First()
@ -46,7 +37,7 @@ func (d def) Apply(rt *Runtime, scope IScope, args *List) (IExpr, error) {
//value = args.Rest().(*List).First()
valueExpr := args.Rest().First()
value, err := EvalForm(rt, scope, valueExpr)
value, err := EvalForms(rt, scope, valueExpr)
if err != nil {
return nil, err

View File

@ -25,19 +25,11 @@ import (
"serene-lang.org/bootstrap/pkg/ast"
)
var sFormsTable map[string]ICallable = map[string]ICallable{
"def": Def,
}
func GetBuiltIn(s *Symbol) (ICallable, bool) {
// Go can't differntiate between returning a tupe directly or indirectly
v, ok := sFormsTable[s.GetName()]
return v, ok
}
func EvalForm(rt *Runtime, scope IScope, form IExpr) (IExpr, error) {
switch form.GetType() {
case ast.Nil:
return form, nil
case ast.Number:
return form, nil
@ -51,7 +43,7 @@ func EvalForm(rt *Runtime, scope IScope, form IExpr) (IExpr, error) {
expr := scope.Lookup(symbolName)
if expr == nil {
return nil, fmt.Errorf("Can't resolve symbol '%s' in ns '%s'", symbolName, rt.CurrentNS().GetName())
return nil, fmt.Errorf("can't resolve symbol '%s' in ns '%s'", symbolName, rt.CurrentNS().GetName())
}
return expr.Value, nil
@ -62,25 +54,94 @@ func EvalForm(rt *Runtime, scope IScope, form IExpr) (IExpr, error) {
// first element is `ICallable` and it's not a macro or special form.
// * An empty list evaluates to itself.
case ast.List:
list := form.(*List)
if list.Count() == 0 {
return list, nil
}
first := form.(*List).First()
var result []IExpr
if first.GetType() == ast.Symbol {
sform, ok := GetBuiltIn(first.(*Symbol))
if ok {
return sform.Apply(rt, scope, list.Rest())
lst := form.(*List)
for {
if lst.Count() > 0 {
expr, err := EvalForms(rt, scope, lst.First())
if err != nil {
return nil, err
}
result = append(result, expr)
lst = lst.Rest().(*List)
} else {
break
}
}
fmt.Printf("no builtin %#v", first)
return MakeList(result), nil
}
// Default case
return nil, errors.New("not implemented")
}
func EvalForms(rt *Runtime, scope IScope, forms IExpr) (IExpr, error) {
for {
if forms.GetType() != ast.List {
return EvalForm(rt, scope, forms)
}
list := forms.(*List)
if list.Count() == 0 {
return &Nil, nil
}
fmt.Printf("EVAL: %s\n", list)
rawFirst := list.First()
sform := ""
// Handling special forms
if rawFirst.GetType() == ast.Symbol {
sform = rawFirst.(*Symbol).GetName()
}
fmt.Printf("sform: %s\n", sform)
switch sform {
case "def":
return Def(rt, scope, list.Rest().(*List))
case "fn":
return Fn(rt, scope, list.Rest().(*List))
default:
exprs, err := EvalForm(rt, scope, list)
if err != nil {
return nil, err
}
f := exprs.(*List).First()
switch f.GetType() {
case ast.Fn:
fn := f.(*Function)
// Since we're passing a List to evaluate the
// result has to be a list. ( Rest returns a List )
args, e := EvalForm(rt, scope, list.Rest().(*List))
if e != nil {
return nil, e
}
argList, _ := args.(*List)
scope, err = MakeFnScope(fn.GetScope(), fn.GetParams(), argList)
if err != nil {
return nil, err
}
forms = MakeList(fn.GetBody().ToSlice())
// case ast.InteropFn:
default:
lst := exprs.(*List)
return lst.ToSlice()[lst.Count()-1], nil
// TODO: Fix this ugly error msg
//return nil, fmt.Errorf("can't call anything beside functions yet")
}
}
}
}
func Eval(rt *Runtime, forms ASTree) (IExpr, error) {
@ -88,18 +149,11 @@ func Eval(rt *Runtime, forms ASTree) (IExpr, error) {
return &Nil, nil
}
var ret IExpr
v, err := EvalForm(rt, rt.CurrentNS().GetRootScope(), MakeList(forms))
for _, form := range forms {
// v is here to shut up the linter
v, err := EvalForm(rt, rt.CurrentNS().GetRootScope(), form)
if err != nil {
return nil, err
}
ret = v
if err != nil {
return nil, err
}
return ret, nil
return v.(*List).First(), nil
}

View File

@ -18,6 +18,82 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"errors"
"fmt"
"serene-lang.org/bootstrap/pkg/ast"
)
type ICallable interface {
Apply(rt *Runtime, scope IScope, args *List) (IExpr, error)
}
type Function struct {
Node
name string
scope IScope
params IColl
body IColl
}
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)
}
func (f *Function) GetBody() IColl {
return f.body
}
func MakeFunction(scope IScope, params IColl, body IColl) *Function {
return &Function{
scope: scope,
params: params,
body: body,
}
}
func MakeFnScope(parent IScope, bindings IColl, values IColl) (*Scope, error) {
fmt.Printf("%s %s\n", bindings, values)
scope := MakeScope(parent.(*Scope))
// TODO: Implement destructuring
if bindings.Count() > values.Count() {
return nil, errors.New("'binding' and 'valuse' size don't match")
}
binds := bindings.ToSlice()
exprs := values.ToSlice()
for i := 0; i < len(binds); i += 1 {
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
}

View File

@ -66,7 +66,7 @@ func (l *List) First() IExpr {
return l.exprs[0]
}
func (l *List) Rest() *List {
func (l *List) Rest() ISeq {
if l.Count() < 2 {
return MakeEmptyList()
}
@ -83,6 +83,14 @@ func (l *List) Count() int {
// END: ICountable ---
// Implementing IColl for List ---
func (l *List) ToSlice() []IExpr {
return l.exprs
}
// END: IColl ---
func MakeList(elements []IExpr) *List {
return &List{
exprs: elements,

View File

@ -48,9 +48,10 @@ func (n *Namespace) GetName() string {
}
func MakeNS(name string, source string) Namespace {
s := MakeScope(nil)
return Namespace{
name: name,
rootScope: MakeScope(nil),
rootScope: *s,
source: source,
externals: map[string]Namespace{},
}

View File

@ -50,8 +50,8 @@ func (s *Scope) Insert(k string, v IExpr, public bool) {
s.bindings[k] = Binding{Value: v, Public: public}
}
func MakeScope(parent *Scope) Scope {
return Scope{
func MakeScope(parent *Scope) *Scope {
return &Scope{
parent: parent,
bindings: map[string]Binding{},
}

View File

@ -0,0 +1,50 @@
/*
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/>.
*/
package core
import (
"errors"
"serene-lang.org/bootstrap/pkg/ast"
)
func Fn(rt *Runtime, scope IScope, args *List) (IExpr, error) {
if args.Count() < 1 {
return nil, errors.New("'fn' needs at least an arguments list")
}
var params IColl
body := MakeEmptyList()
arguments := args.First()
// TODO: Add vector in here
// Or any other icoll
if arguments.GetType() == ast.List {
params = arguments.(IColl)
}
if args.Count() > 1 {
body = args.Rest().(*List)
}
return MakeFunction(scope, params, body), nil
}

View File

@ -18,7 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package core
import "serene-lang.org/bootstrap/pkg/ast"
import (
"strings"
"serene-lang.org/bootstrap/pkg/ast"
)
type Symbol struct {
Node
@ -43,6 +47,11 @@ func (s *Symbol) ToDebugStr() string {
return s.name
}
func (s *Symbol) IsRestable() bool {
// Weird name ? I know :D
return strings.HasPrefix(s.name, "&")
}
func MakeSymbol(s string) *Symbol {
return &Symbol{
name: s,

View File

@ -14,6 +14,7 @@ Then here is the list or parsers that we have considered
* Resources
For a generic list of resources on compiler design take a look at https://tomassetti.me/resources-create-programming-languages/
https://www.reddit.com/r/ProgrammingLanguages/comments/8ggx2n/is_llvm_a_good_backend_for_functional_languages/
** Lisp
- Make a Lisp :: https://github.com/kanaka/mal/blob/master/process/guide.md
** Rust