about summary refs log tree commit diff
path: root/pkg/lang/parser/parser_test.go
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-07-27 22:53:35 +0000
committerMel <einebeere@gmail.com>2022-07-27 22:53:35 +0000
commitd79973cb9df8660fe89810507557f5ba86256f30 (patch)
tree201774dae1fa63e6b962e7be9aeea20c033bb5e3 /pkg/lang/parser/parser_test.go
parent4f23155ca7f8591cae0be6938610386513d24b7f (diff)
downloadjinx-d79973cb9df8660fe89810507557f5ba86256f30.tar.zst
jinx-d79973cb9df8660fe89810507557f5ba86256f30.zip
Parse type declaration statements
Diffstat (limited to 'pkg/lang/parser/parser_test.go')
-rw-r--r--pkg/lang/parser/parser_test.go109
1 files changed, 109 insertions, 0 deletions
diff --git a/pkg/lang/parser/parser_test.go b/pkg/lang/parser/parser_test.go
index 8cecfa8..de97928 100644
--- a/pkg/lang/parser/parser_test.go
+++ b/pkg/lang/parser/parser_test.go
@@ -662,6 +662,115 @@ func TestForInStmt(t *testing.T) {
 	}, program.Stmts[0])
 }
 
+func TestTypeDeclStmt(t *testing.T) {
+	src := sourceify(
+		`type Foo {`,
+		`	(bar) {`,
+		`		this.bar = bar`,
+		`	}`,
+		`	`,
+		`	fn baz(this) {`,
+		`		return this.bar`,
+		`	}`,
+		`}`,
+	)
+
+	p := cheatWithScanner(t, src)
+	program, err := p.Parse()
+	require.NoError(t, err)
+
+	require.Equal(t, 1, len(program.Stmts))
+	require.Equal(t, ast.Stmt{
+		At:   source.NewLoc(0, 0),
+		Kind: ast.StmtKindTypeDecl,
+		Value: ast.StmtTypeDecl{
+			Name: ast.IdentNode{At: source.NewLoc(0, 5), Value: "Foo"},
+			Methods: []ast.TypeMethodNode{
+				{
+					At:            source.NewLoc(1, 1),
+					HasThis:       false,
+					IsConstructor: true,
+					Name:          ast.IdentNode{},
+					Args: []ast.IdentNode{
+						{At: source.NewLoc(1, 2), Value: "bar"},
+					},
+					Body: ast.BlockNode{
+						At: source.NewLoc(1, 7),
+						Stmts: []ast.Stmt{
+							{At: source.NewLoc(1, 8), Kind: 0, Value: ast.StmtEmpty{}},
+							{
+								At:   source.NewLoc(2, 2),
+								Kind: ast.StmtKindExpr,
+								Value: ast.StmtExpr{
+									Value: ast.Expr{
+										At:   source.NewLoc(2, 2),
+										Kind: ast.ExprKindBinary,
+										Value: ast.ExprBinary{
+											Left: ast.Expr{
+												At:   source.NewLoc(2, 2),
+												Kind: ast.ExprKindBinary,
+												Value: ast.ExprBinary{
+													Left: ast.Expr{At: source.NewLoc(2, 2), Kind: ast.ExprKindThis, Value: ast.ExprThis{}},
+													Op:   ast.BinOpDot,
+													Right: ast.Expr{
+														At:    source.NewLoc(2, 7),
+														Kind:  ast.ExprKindIdent,
+														Value: ast.ExprIdent{Value: ast.IdentNode{At: source.NewLoc(2, 7), Value: "bar"}},
+													},
+												},
+											},
+											Op: ast.BinOpAssign,
+											Right: ast.Expr{
+												At:   source.NewLoc(2, 13),
+												Kind: ast.ExprKindIdent,
+												Value: ast.ExprIdent{
+													Value: ast.IdentNode{At: source.NewLoc(2, 13), Value: "bar"},
+												},
+											},
+										},
+									},
+								},
+							},
+						},
+					},
+				},
+				{
+					At:            source.NewLoc(5, 1),
+					HasThis:       true,
+					IsConstructor: false,
+					Name:          ast.IdentNode{At: source.NewLoc(5, 4), Value: "baz"},
+					Args:          []ast.IdentNode{},
+					Body: ast.BlockNode{
+						At: source.NewLoc(5, 14),
+						Stmts: []ast.Stmt{
+							{At: source.NewLoc(5, 15), Kind: ast.StmtKindEmpty, Value: ast.StmtEmpty{}},
+							{
+								At:   source.NewLoc(6, 2),
+								Kind: ast.StmtKindReturn,
+								Value: ast.StmtReturn{
+									Value: ast.Expr{
+										At:   source.NewLoc(6, 9),
+										Kind: ast.ExprKindBinary,
+										Value: ast.ExprBinary{
+											Left: ast.Expr{At: source.NewLoc(6, 9), Kind: ast.ExprKindThis, Value: ast.ExprThis{}},
+											Op:   ast.BinOpDot,
+											Right: ast.Expr{
+												At:    source.NewLoc(6, 14),
+												Kind:  ast.ExprKindIdent,
+												Value: ast.ExprIdent{Value: ast.IdentNode{At: source.NewLoc(6, 14), Value: "bar"}},
+											},
+										},
+									},
+								},
+							},
+						},
+					},
+				},
+			},
+		},
+	}, program.Stmts[0])
+}
+
 func sourceify(lines ...string) string {
 	return strings.Join(lines, "\n")
 }