Add the support for special forms
This commit is contained in:
parent
38baf577bb
commit
cba74e29af
|
@ -0,0 +1,64 @@
|
||||||
|
/*
|
||||||
|
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"
|
||||||
|
"serene-lang.org/bootstrap/pkg/runtime"
|
||||||
|
"serene-lang.org/bootstrap/pkg/scope"
|
||||||
|
"serene-lang.org/bootstrap/pkg/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
/** 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.Runtime, scope scope.IScope, args *types.List) (types.IExpr, error) {
|
||||||
|
switch args.Count() {
|
||||||
|
case 2:
|
||||||
|
name := args.First()
|
||||||
|
|
||||||
|
if name.GetType() != ast.Symbol {
|
||||||
|
return nil, errors.New("The first argument of 'def' has to be a symbol")
|
||||||
|
}
|
||||||
|
|
||||||
|
sym := name.(*types.Symbol)
|
||||||
|
|
||||||
|
//value = args.Rest().(*types.List).First()
|
||||||
|
valueExpr := args.Rest().First()
|
||||||
|
value, err := EvalForm(rt, scope, valueExpr)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ns := rt.CurrentNS()
|
||||||
|
ns.DefineGlobal(sym.GetName(), value, true)
|
||||||
|
return sym, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("'def' form need at least 2 arguments")
|
||||||
|
}
|
|
@ -28,13 +28,21 @@ import (
|
||||||
"serene-lang.org/bootstrap/pkg/types"
|
"serene-lang.org/bootstrap/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func evalForm(rt *runtime.Runtime, scope scope.IScope, form types.IExpr) (types.IExpr, error) {
|
var sFormsTable = map[string]types.ICallable{
|
||||||
|
"def": Def,
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetBuildIn(s *types.Symbol) (types.ICallable, bool) {
|
||||||
|
return sFormsTable[s.GetName()]
|
||||||
|
}
|
||||||
|
|
||||||
|
func EvalForm(rt *runtime.Runtime, scope scope.IScope, form types.IExpr) (types.IExpr, error) {
|
||||||
switch form.GetType() {
|
switch form.GetType() {
|
||||||
case ast.Nil:
|
case ast.Nil:
|
||||||
case ast.Number:
|
case ast.Number:
|
||||||
return form, nil
|
return form, nil
|
||||||
|
|
||||||
// Symbol Evaluation Rules:
|
// Symbol evaluation rules:
|
||||||
// * If it's a NS qualified symbol (NSQS), Look it up in the external symbol table of
|
// * If it's a NS qualified symbol (NSQS), Look it up in the external symbol table of
|
||||||
// the current namespace.
|
// the current namespace.
|
||||||
// * If it's not a NSQS Look up the name in the current scope.
|
// * If it's not a NSQS Look up the name in the current scope.
|
||||||
|
@ -49,6 +57,27 @@ func evalForm(rt *runtime.Runtime, scope scope.IScope, form types.IExpr) (types.
|
||||||
|
|
||||||
return expr.Value, nil
|
return expr.Value, nil
|
||||||
|
|
||||||
|
// List evaluation rules:
|
||||||
|
// * The first element of the list has to be an expression which implements `ICallable`
|
||||||
|
// * The rest o the elements have to be evaluated only after we have determind the the
|
||||||
|
// first element is `ICallable` and it's not a macro or special form.
|
||||||
|
// * An empty list evaluates to itself.
|
||||||
|
case ast.List:
|
||||||
|
list := form.(*types.List)
|
||||||
|
if list.Count() == 0 {
|
||||||
|
return list, nil
|
||||||
|
}
|
||||||
|
first := form.(*types.List).First()
|
||||||
|
|
||||||
|
if first.GetType() == ast.Symbol {
|
||||||
|
sform, ok := GetBuiltIn(first.(*types.Symbol))
|
||||||
|
if ok {
|
||||||
|
return sform.Apply(rt, scope, list.Rest())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("no builtin %#v", first)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default case
|
// Default case
|
||||||
|
@ -64,7 +93,7 @@ func Eval(rt *runtime.Runtime, forms types.ASTree) (types.IExpr, error) {
|
||||||
|
|
||||||
for _, form := range forms {
|
for _, form := range forms {
|
||||||
// v is here to shut up the linter
|
// v is here to shut up the linter
|
||||||
v, err := evalForm(rt, rt.CurrentNS().GetRootScope(), form)
|
v, err := EvalForm(rt, rt.CurrentNS().GetRootScope(), form)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -20,7 +20,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
// of it to be used as the basic blocks of Serene's namespaces.
|
// of it to be used as the basic blocks of Serene's namespaces.
|
||||||
package namespace
|
package namespace
|
||||||
|
|
||||||
import "serene-lang.org/bootstrap/pkg/scope"
|
import (
|
||||||
|
"serene-lang.org/bootstrap/pkg/scope"
|
||||||
|
"serene-lang.org/bootstrap/pkg/types"
|
||||||
|
)
|
||||||
|
|
||||||
type INamespace interface {
|
type INamespace interface {
|
||||||
DefineGlobal()
|
DefineGlobal()
|
||||||
|
@ -37,7 +40,9 @@ type Namespace struct {
|
||||||
externals map[string]Namespace
|
externals map[string]Namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Namespace) DefineGlobal() {}
|
func (n *Namespace) DefineGlobal(k string, v types.IExpr, public bool) {
|
||||||
|
n.rootScope.Insert(k, v, public)
|
||||||
|
}
|
||||||
|
|
||||||
func (n *Namespace) LookupGlobal() {}
|
func (n *Namespace) LookupGlobal() {}
|
||||||
|
|
||||||
|
|
|
@ -19,4 +19,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
package types
|
package types
|
||||||
|
|
||||||
type ISeq interface {
|
type ISeq interface {
|
||||||
|
First() IExpr
|
||||||
|
Rest() ISeq
|
||||||
|
}
|
||||||
|
|
||||||
|
type ICountable interface {
|
||||||
|
Count() int
|
||||||
}
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
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 types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"serene-lang.org/bootstrap/pkg/runtime"
|
||||||
|
"serene-lang.org/bootstrap/pkg/scope"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ICallable interface {
|
||||||
|
Apply(rt *runtime.Runtime, scope scope.IScope, args *List) (IExpr, error)
|
||||||
|
}
|
|
@ -25,20 +25,25 @@ import (
|
||||||
"serene-lang.org/bootstrap/pkg/ast"
|
"serene-lang.org/bootstrap/pkg/ast"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/** WARNING:
|
||||||
|
This List implementation may look simple and performant but since
|
||||||
|
we're using a slice here. But in fact it's not memory effecient at
|
||||||
|
all. We need to rewrite this later to be a immutable and persistent
|
||||||
|
link list of cons.
|
||||||
|
*/
|
||||||
|
|
||||||
type List struct {
|
type List struct {
|
||||||
Node
|
Node
|
||||||
exprs []IExpr
|
exprs []IExpr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l List) Eval() IExpr {
|
// Implementing IExpr for List ---
|
||||||
return &Nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l List) GetType() ast.NodeType {
|
func (l *List) GetType() ast.NodeType {
|
||||||
return ast.List
|
return ast.List
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l List) String() string {
|
func (l *List) String() string {
|
||||||
var strs []string
|
var strs []string
|
||||||
for _, e := range l.exprs {
|
for _, e := range l.exprs {
|
||||||
strs = append(strs, e.String())
|
strs = append(strs, e.String())
|
||||||
|
@ -46,12 +51,46 @@ func (l List) String() string {
|
||||||
return fmt.Sprintf("(%s)", strings.Join(strs, " "))
|
return fmt.Sprintf("(%s)", strings.Join(strs, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l List) ToDebugStr() string {
|
func (l *List) ToDebugStr() string {
|
||||||
return fmt.Sprintf("%#v", l)
|
return fmt.Sprintf("%#v", l)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// END: IExpr ---
|
||||||
|
|
||||||
|
// Implementing ISeq for List ---
|
||||||
|
|
||||||
|
func (l *List) First() IExpr {
|
||||||
|
if l.Count() == 0 {
|
||||||
|
return Nil
|
||||||
|
}
|
||||||
|
return l.exprs[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *List) Rest() *List {
|
||||||
|
if l.Count() < 2 {
|
||||||
|
return MakeEmptyList()
|
||||||
|
}
|
||||||
|
return MakeList(l.exprs[1:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// END: ISeq ---
|
||||||
|
|
||||||
|
// Implementing ICountable for List ---
|
||||||
|
|
||||||
|
func (l *List) Count() int {
|
||||||
|
return len(l.exprs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// END: ICountable ---
|
||||||
|
|
||||||
func MakeList(elements []IExpr) *List {
|
func MakeList(elements []IExpr) *List {
|
||||||
return &List{
|
return &List{
|
||||||
exprs: elements,
|
exprs: elements,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MakeEmptyList() *List {
|
||||||
|
return &List{
|
||||||
|
exprs: []IExpr{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue