Skip to content

Commit

Permalink
👷 Evaluate function declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
ChmielewskiKamil committed Dec 7, 2024
1 parent da702e9 commit 8d47346
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ func Eval(node ast.Node, env *object.Environment) object.Object {

func evalFunctionDeclaration(fn *ast.FunctionDeclaration, env *object.Environment) object.Object {
function := &object.Function{
Name: fn.Name,
Body: fn.Body,
Env: env,
Name: fn.Name,
Params: fn.Params,
Body: fn.Body,
Env: env,
}

return env.Set(function.Name.Value, function)
Expand Down
8 changes: 8 additions & 0 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ func Test_Eval_FunctionDeclaration(t *testing.T) {
if fn.Name.String() != "add" {
t.Fatalf("Function's name is not add. got=%s", fn.Name.String())
}

if len(fn.Params.List) != 2 {
t.Fatalf("Function has wrong parameterst. got=%+v", fn.Params.List)
}

if fn.Params.List[0].Name.String() != "a" {
t.Fatalf("Function param incorrect. got=%s", fn.Params.List[0].Name.String())
}
}

/*~*~*~*~*~*~*~*~*~*~*~*~* Helper Functions ~*~*~*~*~*~*~*~*~*~*~*~*~*/
Expand Down

0 comments on commit 8d47346

Please sign in to comment.