-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
303178c
commit 608cc47
Showing
4 changed files
with
116 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package evaluator | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/JunNishimura/go-lisp/ast" | ||
"github.com/JunNishimura/go-lisp/object" | ||
) | ||
|
||
var specialForms = map[string]*object.SpecialForm{ | ||
"lambda": { | ||
Fn: func(sexp ast.SExpression, env *object.Environment) object.Object { | ||
consCell, ok := sexp.(*ast.ConsCell) | ||
if !ok { | ||
return newError("cdr of lambda must be a cons cell, got %T", sexp) | ||
} | ||
|
||
params, err := evalLambdaParams(consCell.Car()) | ||
if err != nil { | ||
return newError(err.Error()) | ||
} | ||
|
||
cdr := consCell.Cdr() | ||
consCell, ok = cdr.(*ast.ConsCell) | ||
if !ok { | ||
return newError("cdr of lambda must be a cons cell, got %T", cdr) | ||
} | ||
body := consCell.Car() | ||
|
||
return &object.Function{Parameters: params, Body: body, Env: env} | ||
}, | ||
}, | ||
} | ||
|
||
func evalLambdaParams(sexp ast.SExpression) ([]*ast.Symbol, error) { | ||
params := []*ast.Symbol{} | ||
|
||
if _, ok := sexp.(*ast.Nil); ok { | ||
return params, nil | ||
} | ||
|
||
consCell, ok := sexp.(*ast.ConsCell) | ||
if !ok { | ||
return nil, fmt.Errorf("parameters must be a list, got %T", sexp) | ||
} | ||
|
||
for { | ||
symbol, ok := consCell.Car().(*ast.Symbol) | ||
if !ok { | ||
return nil, fmt.Errorf("parameter must be a symbol, got %T", consCell.Car()) | ||
} | ||
params = append(params, symbol) | ||
|
||
switch cdr := consCell.Cdr().(type) { | ||
case *ast.Nil: | ||
return params, nil | ||
case *ast.ConsCell: | ||
consCell = cdr | ||
default: | ||
return nil, fmt.Errorf("parameters must be a list, got %T", consCell.Cdr()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters