Skip to content

Commit

Permalink
support option to omit fun return type
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Nov 16, 2023
1 parent 7bafa11 commit e4f06ac
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/core/chuck.y
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ function_definition
{ $$ = new_func_def( $1, $2, $3, $4, $6, $8, TRUE, @1.first_line, @1.first_column ); }
| function_decl static_decl type_decl2 ID LPAREN RPAREN code_segment
{ $$ = new_func_def( $1, $2, $3, $4, NULL, $7, TRUE, @1.first_line, @1.first_column ); }
| function_decl static_decl ID LPAREN arg_list RPAREN code_segment
{ $$ = new_func_def( $1, $2, NULL, $3, $5, $7, TRUE, @1.first_line, @1.first_column ); }
| function_decl static_decl ID LPAREN RPAREN code_segment
{ $$ = new_func_def( $1, $2, NULL, $3, NULL, $6, TRUE, @1.first_line, @1.first_column ); }
| function_decl AT_CTOR LPAREN arg_list RPAREN code_segment // 1.5.1.9 (ge) added for constructors
{ $$ = new_func_def( $1, ae_key_instance, NULL, "@construct", $4, $6, TRUE, @1.first_line, @1.first_column ); }
| function_decl AT_CTOR LPAREN RPAREN code_segment // 1.5.1.9 (ge) added for constructors
Expand Down
15 changes: 12 additions & 3 deletions src/test/01-Basic/236-ctors-basic.ck
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class Foo
1 => int num;

// constructor 1 "default"
fun void Foo()
fun @construct()
{
2 => num;
<<< "constructor 1:", num >>>;
}

// constructor 2
fun void Foo( int x )
fun @construct( int x )
{
x => num;
<<< "constructor 2:", x >>>;
Expand All @@ -28,6 +28,13 @@ class Foo
x*y => num;
<<< "constructor 3:", x, y >>>;
}

// constructor 4 (okay to omit void return type)
fun Foo( int x, int y, int z )
{
x*y*z => num;
<<< "constructor 4:", x, y, z >>>;
}
}

// declare a Foo, invoke constructor 1
Expand All @@ -38,6 +45,8 @@ Foo f1();
Foo f2(15);
// instantiate a Foo, invoke constructor 3
new Foo(8,9) @=> Foo @ f3;
// instantiate a Foo, invoke constructor 4
new Foo(10,11,12) @=> Foo @ f4;

// print
<<< f0.num, f1.num, f2.num, f3.num >>>;
<<< f0.num, f1.num, f2.num, f3.num, f4.num >>>;
3 changes: 2 additions & 1 deletion src/test/01-Basic/236-ctors-basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ constructor 1: 2
constructor 1: 2
constructor 2: 15
constructor 3: 8 9
2 2 15 72
constructor 4: 10 11 12
2 2 15 72 1320

0 comments on commit e4f06ac

Please sign in to comment.