Skip to content

Commit bee263e

Browse files
committed
Switch to using func instead of fn
1 parent 7f0603f commit bee263e

File tree

10 files changed

+27
-27
lines changed

10 files changed

+27
-27
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CGO_ENABLED=0 go install -trimpath -ldflags="-w -s" -tags no_net,no_json github.
88
Sample:
99
```shell
1010
gorepl -parse
11-
$ fact = fn(n) {if (n<1) {return 1} n*fact(n-1)}
11+
$ fact = func(n) {if (n<=1) {return 1} n*fact(n-1)}
1212
$ n=fact(6)
1313
== Parse ==> (n = fact(6))
1414
== Eval ==> 720

apply.gr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
a = [ 1, 3, 5, 7]
44

5-
apply = fn(f, a) {
5+
apply = func(f, a) {
66
if (len(a)==0) {
77
return []
88
}
99
return [f(first(a))]+apply(f,rest(a))
1010
}
1111

12-
apply(fn(x) {2*x}, a)
12+
apply(func(x) {2*x}, a)
1313

1414
// ^^^ [2, 6, 10, 14]

ast/ast.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (b *Builtin) String() string {
283283
}
284284

285285
type FunctionLiteral struct {
286-
Base // The 'fn' token
286+
Base // The 'func' token
287287
Parameters []*Identifier
288288
Body *BlockStatement
289289
}

eval/eval_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestEvalIntegerExpression(t *testing.T) {
3636
{"19 % 5", 4},
3737
{"20 % -5", 0},
3838
{"-21 % 5", -1},
39-
{`fact = fn(n) {if (n<2) {return 1} n*fact(n-1)}; fact(5)`, 120},
39+
{`fact = func(n) {if (n<2) {return 1} n*fact(n-1)}; fact(5)`, 120},
4040
}
4141

4242
for i, tt := range tests {
@@ -208,7 +208,7 @@ func TestErrorHandling(t *testing.T) {
208208
expectedMessage string
209209
}{
210210
{
211-
"let f=fn(x,y) {x+y}; f(1)",
211+
"let f=func(x,y) {x+y}; f(1)",
212212
"<wrong number of arguments. got=1, want=2>",
213213
},
214214
{
@@ -256,7 +256,7 @@ if (10 > 1) {
256256
"<unknown operator: STRING - STRING>",
257257
},
258258
{
259-
`{"name": "Monkey"}[fn(x) { x }];`,
259+
`{"name": "Monkey"}[func(x) { x }];`,
260260
"FUNCTION not usable as map key",
261261
},
262262
}
@@ -298,7 +298,7 @@ func TestLetStatements(t *testing.T) {
298298
}
299299

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

303303
evaluated := testEval(t, input)
304304
fn, ok := evaluated.(object.Function)
@@ -327,12 +327,12 @@ func TestFunctionApplication(t *testing.T) {
327327
input string
328328
expected int64
329329
}{
330-
{"let identity = fn(x) { x; }; identity(5);", 5},
331-
{"let identity = fn(x) { return x; }; identity(5);", 5},
332-
{"let double = fn(x) { x * 2; }; double(5);", 10},
333-
{"let add = fn(x, y) { x + y; }; add(5, 5);", 10},
334-
{"let add = fn(x, y) { x + y; }; add(5 + 5, add(5, 5));", 20},
335-
{"fn(x) { x; }(5)", 5},
330+
{"let identity = func(x) { x; }; identity(5);", 5},
331+
{"let identity = func(x) { return x; }; identity(5);", 5},
332+
{"let double = func(x) { x * 2; }; double(5);", 10},
333+
{"let add = func(x, y) { x + y; }; add(5, 5);", 10},
334+
{"let add = func(x, y) { x + y; }; add(5 + 5, add(5, 5));", 20},
335+
{"func(x) { x; }(5)", 5},
336336
}
337337
for _, tt := range tests {
338338
testIntegerObject(t, testEval(t, tt.input), tt.expected)
@@ -341,8 +341,8 @@ func TestFunctionApplication(t *testing.T) {
341341

342342
func TestClosures(t *testing.T) {
343343
input := `
344-
let newAdder = fn(x) {
345-
fn(y) { x + y };
344+
let newAdder = func(x) {
345+
func(y) { x + y };
346346
};
347347
348348
let addTwo = newAdder(2);

lexer/lexer_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func TestNextToken(t *testing.T) { //nolint:funlen // this is a test function wi
1010
input := `let five = 5;
1111
let ten = 10;
1212
13-
let add = fn(x, y) {
13+
let add = func(x, y) {
1414
x + y;
1515
};
1616
@@ -53,7 +53,7 @@ return // nil return
5353
{token.LET, "let"},
5454
{token.IDENT, "add"},
5555
{token.ASSIGN, "="},
56-
{token.FUNCTION, "fn"},
56+
{token.FUNCTION, "func"},
5757
{token.LPAREN, "("},
5858
{token.IDENT, "x"},
5959
{token.COMMA, ","},

object/object.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (f Function) Type() Type { return FUNCTION }
152152
func (f Function) Inspect() string {
153153
out := strings.Builder{}
154154

155-
out.WriteString("fn")
155+
out.WriteString("func")
156156
out.WriteString("(")
157157
ast.WriteStrings(&out, f.Parameters, ", ")
158158
out.WriteString(") ")

parser/parser_book_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ func TestIfElseExpression(t *testing.T) {
521521
}
522522

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

526526
l := lexer.New(input)
527527
p := New(l)
@@ -573,9 +573,9 @@ func TestFunctionParameterParsing(t *testing.T) {
573573
input string
574574
expectedParams []string
575575
}{
576-
{input: "fn() {};", expectedParams: []string{}},
577-
{input: "fn(x) {};", expectedParams: []string{"x"}},
578-
{input: "fn(x, y, z) {};", expectedParams: []string{"x", "y", "z"}},
576+
{input: "func() {};", expectedParams: []string{}},
577+
{input: "func(x) {};", expectedParams: []string{"x"}},
578+
{input: "func(x, y, z) {};", expectedParams: []string{"x", "y", "z"}},
579579
}
580580

581581
for _, tt := range tests {

parser/parser_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ func Test_OperatorPrecedenceParsing(t *testing.T) {
358358
"let x = (41 * 6)",
359359
},
360360
{
361-
"let foo = fn(a,b) {return a+b}",
362-
"let foo = fn(a, b) {\nreturn (a + b)\n}",
361+
"let foo = func(a,b) {return a+b}",
362+
"let foo = func(a, b) {\nreturn (a + b)\n}",
363363
},
364364
}
365365

sample.gr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// <--- comments
33
// See also the other *.gr files
44

5-
fact=fn(n) { // function
5+
fact=func(n) { // function
66
if (n<=1) {
77
return 1
88
}

token/token.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const (
7070
var _ = EOF.String() // force compile error if go generate is missing.
7171

7272
var keywords = map[string]Type{
73-
"fn": FUNCTION,
73+
"func": FUNCTION,
7474
"let": LET,
7575
"true": TRUE,
7676
"false": FALSE,

0 commit comments

Comments
 (0)