Skip to content

Commit

Permalink
replace fn identifier with def (#40)
Browse files Browse the repository at this point in the history
Signed-off-by: Flipez <code@brauser.io>
  • Loading branch information
Flipez authored Jan 15, 2022
1 parent 1b4e33f commit 520d817
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 55 deletions.
4 changes: 2 additions & 2 deletions docs/content/en/docs/specification/closures.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ menu:
toc: true
---
```js
newGreeter = fn(greeting) {
return fn(name) { puts(greeting + " " + name); }
newGreeter = def (greeting) {
return def (name) { puts(greeting + " " + name); }
};

hello = newGreeter("Hello");
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/docs/specification/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ All following content up to the end of the line is part of the comment.

```js
// This is a comment
fn(a) {
def (a) {
// This is also a comment
a + a // And this
}
Expand Down
6 changes: 3 additions & 3 deletions docs/content/en/docs/specification/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ toc: true
Implicit and explicit return statements are supported.

```js
fibonacci = fn(x) {
fibonacci = def (x) {
if (x == 0) {
0
} else {
Expand All @@ -26,8 +26,8 @@ fibonacci = fn(x) {
Functions can now also be created as named functions:

```js
🚀 > fn test() { puts("test")}
=> fn() {
🚀 > def test() { puts("test")}
=> def () {
puts(test)
}

Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/docs/specification/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ For example take this module:
a = 1
A = 5

Sum = fn(a, b) {
Sum = def (a, b) {
return a + b
}

Expand Down
18 changes: 9 additions & 9 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,25 @@ func Eval(node ast.Node, env *object.Environment) object.Object {
return nil
}

func applyFunction(fn object.Object, args []object.Object) object.Object {
switch fn := fn.(type) {
func applyFunction(def object.Object, args []object.Object) object.Object {
switch def := def.(type) {
case *object.Function:
extendedEnv := extendFunctionEnv(fn, args)
evaluated := Eval(fn.Body, extendedEnv)
extendedEnv := extendFunctionEnv(def, args)
evaluated := Eval(def.Body, extendedEnv)
return unwrapReturnValue(evaluated)

case *object.Builtin:
return fn.Fn(args...)
return def.Fn(args...)

default:
return newError("not a function: %s", fn.Type())
return newError("not a function: %s", def.Type())
}
}

func extendFunctionEnv(fn *object.Function, args []object.Object) *object.Environment {
env := object.NewEnclosedEnvironment(fn.Env)
func extendFunctionEnv(def *object.Function, args []object.Object) *object.Environment {
env := object.NewEnclosedEnvironment(def.Env)

for paramIdx, param := range fn.Parameters {
for paramIdx, param := range def.Parameters {
env.Set(param.Value, args[paramIdx])
}

Expand Down
40 changes: 20 additions & 20 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestErrorHandling(t *testing.T) {
`, "unknown operator: BOOLEAN + BOOLEAN",
},
{`"Hello" - "World"`, "unknown operator: STRING - STRING"},
{`{"name": "Monkey"}[fn(x) { x }];`, "unusable as hash key: FUNCTION"},
{`{"name": "Monkey"}[def(x) { x }];`, "unusable as hash key: FUNCTION"},
}

for _, tt := range tests {
Expand Down Expand Up @@ -196,26 +196,26 @@ func TestAssignStatements(t *testing.T) {
}

func TestFunctionObject(t *testing.T) {
input := "fn(x) { x + 2; };"
input := "def(x) { x + 2; };"

evaluated := testEval(input)
fn, ok := evaluated.(*object.Function)
def, ok := evaluated.(*object.Function)
if !ok {
t.Fatalf("object is not Function. got=%T (%+v)", evaluated, evaluated)
}

if len(fn.Parameters) != 1 {
t.Fatalf("function has wrong parameters. Parameters=%+v", fn.Parameters)
if len(def.Parameters) != 1 {
t.Fatalf("function has wrong parameters. Parameters=%+v", def.Parameters)
}

if fn.Parameters[0].String() != "x" {
t.Fatalf("parameter is not 'x'. got=%q", fn.Parameters[0])
if def.Parameters[0].String() != "x" {
t.Fatalf("parameter is not 'x'. got=%q", def.Parameters[0])
}

expectedBody := "(x + 2)"

if fn.Body.String() != expectedBody {
t.Fatalf("body is not %q. got=%q", expectedBody, fn.Body.String())
if def.Body.String() != expectedBody {
t.Fatalf("body is not %q. got=%q", expectedBody, def.Body.String())
}
}

Expand All @@ -224,12 +224,12 @@ func TestFunctionApplication(t *testing.T) {
input string
expected int64
}{
{"identity = fn(x) { x; }; identity(5);", 5},
{"identity = fn(x) { return x; }; identity(5);", 5},
{"double = fn(x) { x * 2; }; double(5);", 10},
{"add = fn(x, y) { x + y; }; add(5, 5);", 10},
{"add = fn(x, y) { x + y; }; add(5 + 5, add(5, 5));", 20},
{"fn(x) { x; }(5)", 5},
{"identity = def(x) { x; }; identity(5);", 5},
{"identity = def(x) { return x; }; identity(5);", 5},
{"double = def(x) { x * 2; }; double(5);", 10},
{"add = def(x, y) { x + y; }; add(5, 5);", 10},
{"add = def(x, y) { x + y; }; add(5 + 5, add(5, 5));", 20},
{"def(x) { x; }(5)", 5},
}

for _, tt := range tests {
Expand All @@ -239,8 +239,8 @@ func TestFunctionApplication(t *testing.T) {

func TestClosures(t *testing.T) {
input := `
newAdder = fn(x) {
fn(y) { x + y };
newAdder = def(x) {
def(y) { x + y };
};
addTwo = newAdder(2);
Expand Down Expand Up @@ -482,9 +482,9 @@ func TestNamedFunctionStatements(t *testing.T) {
input string
expected int64
}{
{"fn five() { return 5 } five()", 5},
{"fn ten() { return 10 } ten()", 10},
{"fn fifteen() { return 15 } fifteen()", 15},
{"def five() { return 5 } five()", 5},
{"def ten() { return 10 } ten()", 10},
{"def fifteen() { return 15 } fifteen()", 15},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion fixtures/module.rl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
a = 1
A = 5

Sum = fn(a, b) {
Sum = def (a, b) {
return a + b
}
4 changes: 2 additions & 2 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestNextToken(t *testing.T) {
input := `five = 5;
ten = 10;
add = fn(x, y) {
add = def(x, y) {
x + y;
};
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestNextToken(t *testing.T) {
{token.SEMICOLON, ";"},
{token.IDENT, "add"},
{token.ASSIGN, "="},
{token.FUNCTION, "fn"},
{token.FUNCTION, "def"},
{token.LPAREN, "("},
{token.IDENT, "x"},
{token.COMMA, ","},
Expand Down
4 changes: 2 additions & 2 deletions object/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ func TestFileObjectMethods(t *testing.T) {
tests := []inputTestCase{
{`open("../fixtures/module.rl").close()`, true},
{`a = open("../fixtures/module.rl"); a.close(); a.position()`, -1},
{`open("../fixtures/module.rl").content().size()`, 49},
{`open("../fixtures/module.rl").content().size()`, 51},
{`open("../fixtures/module.rl").content(1)`, "To many arguments: want=0, got=1"},
{`open("../fixtures/module.rl").read()`, "To few arguments: want=1, got=0"},
{`open("../fixtures/module.rl").read(1)`, "a"},
{`open("../fixtures/module.rl").position()`, 0},
{`a = open("../fixtures/module.rl"); a.read(1); a.content(); a.position()`, 0},
{`a = open("../fixtures/module.rl"); a.read(1); a.position()`, 1},
{`a = open("../fixtures/module.rl"); a.read(1); a.content().size()`, 49},
{`a = open("../fixtures/module.rl"); a.read(1); a.content().size()`, 51},
{`open("../fixtures/module.rl").lines().size()`, 7},
{`a = open("../fixtures/module.rl"); a.read(25); a.lines().size()`, 7},
{`open("../fixtures/nope")`, "open ../fixtures/nope: no such file or directory"},
Expand Down
2 changes: 1 addition & 1 deletion object/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (f *Function) Inspect() string {
params = append(params, p.String())
}

out.WriteString("fn")
out.WriteString("def ")
out.WriteString("(")
out.WriteString(strings.Join(params, ", "))
out.WriteString(") {\n")
Expand Down
14 changes: 7 additions & 7 deletions object/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (

func TestFunctionObjectMethods(t *testing.T) {
tests := []inputTestCase{
{`fn(){}.nope()`, "Failed to invoke method: nope"},
{`def(){}.nope()`, "Failed to invoke method: nope"},
}

testInput(t, tests)
}
func TestFunctionType(t *testing.T) {
tests := []inputTestCase{
{"fn(){}", "fn() {\n\n}"},
{"fn(a){puts(a)}", "fn(a) {\nputs(a)\n}"},
{"def(){}", "def () {\n\n}"},
{"def(a){puts(a)}", "def (a) {\nputs(a)\n}"},
}

for _, tt := range tests {
fn := testEval(tt.input).(*object.Function)
fnInspect := fn.Inspect()
def := testEval(tt.input).(*object.Function)
defInspect := def.Inspect()

if fnInspect != tt.expected {
t.Errorf("wrong string. expected=%#v, got=%#v", tt.expected, fnInspect)
if defInspect != tt.expected {
t.Errorf("wrong string. expected=%#v, got=%#v", tt.expected, defInspect)
}
}
}
10 changes: 5 additions & 5 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ func TestIfExpression(t *testing.T) {
}

func TestFunctionLiteralParsing(t *testing.T) {
input := `fn (x, y) { x + y; }`
input := `def (x, y) { x + y; }`

program, p := createProgram(input)
checkParserErrors(t, p)
Expand Down Expand Up @@ -506,9 +506,9 @@ func TestFunctionParameterParsing(t *testing.T) {
input string
expectedParams []string
}{
{input: "fn() {};", expectedParams: []string{}},
{input: "fn(x) {};", expectedParams: []string{"x"}},
{input: "fn(x, y, z) {};", expectedParams: []string{"x", "y", "z"}},
{input: "def () {};", expectedParams: []string{}},
{input: "def (x) {};", expectedParams: []string{"x"}},
{input: "def (x, y, z) {};", expectedParams: []string{"x", "y", "z"}},
}

for _, tt := range tests {
Expand Down Expand Up @@ -717,7 +717,7 @@ func TestParsingHashLiteralWithExpressions(t *testing.T) {
}

func TestNamedFunctionLiteralParsing(t *testing.T) {
input := `fn test(x, y) { x + y; }`
input := `def test(x, y) { x + y; }`

program, p := createProgram(input)

Expand Down
2 changes: 1 addition & 1 deletion token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const (
)

var keywords = map[string]TokenType{
"fn": FUNCTION,
"def": FUNCTION,
"true": TRUE,
"false": FALSE,
"if": IF,
Expand Down

1 comment on commit 520d817

@vercel
Copy link

@vercel vercel bot commented on 520d817 Jan 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.