bind expression (:=) instead of let
Some checks failed
Build / build (push) Successful in 10m26s
Test / build (push) Failing after 16m44s

This commit is contained in:
Chuck Smith
2024-03-21 17:43:03 -04:00
parent 66d5453ecc
commit 6282075e66
36 changed files with 425 additions and 583 deletions

View File

@@ -8,15 +8,14 @@ import (
"testing"
)
func TestLetStatements(t *testing.T) {
func TestBindExpressions(t *testing.T) {
tests := []struct {
input string
expectedIdentifier string
expectedValue interface{}
input string
expected string
}{
{"let x = 5;", "x", 5},
{"let y = true;", "y", true},
{"let foobar = y;", "foobar", "y"},
{"x := 5;", "x:=5"},
{"y := true;", "y:=true"},
{"foobar := y;", "foobar:=y"},
}
for _, tt := range tests {
@@ -25,20 +24,7 @@ func TestLetStatements(t *testing.T) {
program := p.ParseProgram()
checkParserErrors(t, p)
if len(program.Statements) != 1 {
t.Fatalf("program.Statements does not contain 1 statements. got=%d",
len(program.Statements))
}
stmt := program.Statements[0]
if !testLetStatement(t, stmt, tt.expectedIdentifier) {
return
}
val := stmt.(*ast.LetStatement).Value
if !testLiteralExpression(t, val, tt.expectedValue) {
return
}
assert.Equal(t, tt.expected, program.String())
}
}
@@ -994,35 +980,17 @@ func TestParsingHashLiteralsWithExpressions(t *testing.T) {
}
}
func TestFunctionLiteralWithName(t *testing.T) {
input := `let myFunction = fn() { };`
func TestFunctionDefinitionParsing(t *testing.T) {
assert := assert.New(t)
input := `add := fn(x, y) { x + y; }`
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\n",
1, len(program.Statements))
}
stmt, ok := program.Statements[0].(*ast.LetStatement)
if !ok {
t.Fatalf("program.Statements[0] is not ast.LetStatement. got=%T",
program.Statements[0])
}
function, ok := stmt.Value.(*ast.FunctionLiteral)
if !ok {
t.Fatalf("stmt.Value is not ast.FunctionLiteral. got=%T",
stmt.Value)
}
if function.Name != "myFunction" {
t.Fatalf("function literal name wrong. want 'myFunction', got=%q\n",
function.Name)
}
assert.Equal("add:=fn<add>(x, y) (x + y)", program.String())
}
func TestWhileExpression(t *testing.T) {
@@ -1094,32 +1062,6 @@ func TestAssignmentExpressions(t *testing.T) {
}
}
func testLetStatement(t *testing.T, s ast.Statement, name string) bool {
if s.TokenLiteral() != "let" {
t.Errorf("s.TokenLiteral not 'let'. got=%q", s.TokenLiteral())
return false
}
letStmt, ok := s.(*ast.LetStatement)
if !ok {
t.Errorf("s not *ast.LetStatement. got=%T", s)
return false
}
if letStmt.Name.Value != name {
t.Errorf("letStmt.Name.Value not '%s'. got=%s", name, letStmt.Name.Value)
return false
}
if letStmt.Name.TokenLiteral() != name {
t.Errorf("letStmt.Name.TokenLiteral() not '%s'. got=%s",
name, letStmt.Name.TokenLiteral())
return false
}
return true
}
func testInfixExpression(t *testing.T, exp ast.Expression, left interface{},
operator string, right interface{}) bool {
@@ -1249,7 +1191,7 @@ func TestComments(t *testing.T) {
{"#!monkey", "!monkey"},
{"# foo", " foo"},
{" # foo", " foo"},
{" // let x = 1", " let x = 1"},
{" // x := 1", " x := 1"},
}
for _, tt := range tests {