-
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.
Add function literal declaration statement (#65)
- Loading branch information
1 parent
a326faa
commit 480b297
Showing
8 changed files
with
173 additions
and
0 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,38 @@ | ||
package parser | ||
|
||
import ( | ||
"github.com/0xM-D/interpreter/ast" | ||
"github.com/0xM-D/interpreter/token" | ||
) | ||
|
||
func (p *Parser) parseFunctionDeclarationStatement() *ast.FunctionDeclarationStatement { | ||
stmt := &ast.FunctionDeclarationStatement{Token: p.curToken} | ||
|
||
p.nextToken() // "fn" | ||
|
||
stmt.Name = p.parseIdentifier().(*ast.Identifier) | ||
|
||
if !p.expectPeek(token.LPAREN) { | ||
return nil | ||
} | ||
|
||
stmt.Parameters, stmt.Type.ParameterTypes = p.parseFunctionParameters() | ||
|
||
if !p.expectPeek(token.DASH_ARROW) { | ||
return nil | ||
} | ||
p.nextToken() | ||
|
||
stmt.Type.ReturnType = p.parseType() | ||
if stmt.Type.ReturnType == nil { | ||
return nil | ||
} | ||
|
||
if !p.expectPeek(token.LBRACE) { | ||
return nil | ||
} | ||
|
||
stmt.Body = p.parseBlockStatement() | ||
|
||
return stmt | ||
} |
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,51 @@ | ||
package parser | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/0xM-D/interpreter/ast" | ||
"github.com/0xM-D/interpreter/lexer" | ||
) | ||
|
||
func TestFunctionDeclarationStatement(t *testing.T) { | ||
input := `fn functionName(param1: int, param2: string) -> int { return param1 + param2 }` | ||
|
||
l := lexer.New(input) | ||
p := New(l) | ||
program := p.ParseProgram() | ||
checkParserErrors(t, p) | ||
|
||
if len(program.Statements) != 1 { | ||
t.Fatalf("program.Body does not contain %d statements. got=%d", 1, len(program.Statements)) | ||
} | ||
|
||
functionDeclaration, ok := program.Statements[0].(*ast.FunctionDeclarationStatement) | ||
if !ok { | ||
t.Fatalf("program.Statements[0] is not ast.FunctionDeclarationStatement. got=%T", program.Statements[0]) | ||
} | ||
|
||
if functionDeclaration.Name.Value != "functionName" { | ||
t.Fatalf("function name wrong. want=%s, got=%s", "functionName", functionDeclaration.Name.Value) | ||
} | ||
|
||
if len(functionDeclaration.Parameters) != 2 { | ||
t.Fatalf("function literal parameters wrong. want 2, got=%d\n", len(functionDeclaration.Parameters)) | ||
} | ||
|
||
testLiteralExpression(t, functionDeclaration.Parameters[0], TestIdentifier{"param1"}) | ||
testLiteralExpression(t, functionDeclaration.Parameters[1], TestIdentifier{"param2"}) | ||
|
||
if len(functionDeclaration.Body.Statements) != 1 { | ||
t.Fatalf("function.Body.Statements has not 1 statements. got=%d\n", | ||
len(functionDeclaration.Body.Statements)) | ||
} | ||
|
||
bodyStmt, ok := functionDeclaration.Body.Statements[0].(*ast.ReturnStatement) | ||
if !ok { | ||
t.Fatalf("function body stmt is not ast.ReturnStatement. got=%T", | ||
functionDeclaration.Body.Statements[0]) | ||
} | ||
|
||
testInfixExpression(t, bodyStmt.ReturnValue, TestIdentifier{"param1"}, "+", TestIdentifier{"param2"}) | ||
|
||
} |
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,22 @@ | ||
package runtime | ||
|
||
import ( | ||
"github.com/0xM-D/interpreter/ast" | ||
"github.com/0xM-D/interpreter/object" | ||
) | ||
|
||
func (r *Runtime) evalFunctionDeclarationStatement(node *ast.FunctionDeclarationStatement, env *object.Environment) (object.Object, error) { | ||
functionType, err := r.evalType(node.Type, env) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
function := &object.Function{ | ||
Parameters: node.Parameters, | ||
Env: env, | ||
Body: node.Body, | ||
FunctionObjectType: *functionType.(*object.FunctionObjectType), | ||
} | ||
|
||
return env.Declare(node.Name.Value, true, function) | ||
} |
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,22 @@ | ||
package runtime | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
) | ||
|
||
func TestFunctionDeclarationStatement(t *testing.T) { | ||
input := ` | ||
fn functionName(x: int64)->int64 { | ||
return x * 2; | ||
} | ||
functionName(50) | ||
` | ||
|
||
evaluated, err := testEval(input) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
testIntegerObject(t, evaluated, big.NewInt(100)) | ||
} |