Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/topic/etyp/compound-stmt'
Browse files Browse the repository at this point in the history
  • Loading branch information
evantypanski committed Mar 6, 2025
2 parents 7d30d55 + 9152a06 commit 8966228
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
1.13.0-dev.118 | 2025-03-06 09:05:38 -0500

* Fix `for` loop snippet to run as-is. (Evan Typanski, Corelight)

* GH-1538: Implement compound statements (`{...}`). (Evan Typanski, Corelight)

Closes #1538

The functionality was there, just not available in the parser.

1.13.0-dev.115 | 2025-03-06 12:00:38 +0100

* GH-1981: Reject function overloads which only differ in default'd parameters. (Benjamin Bannier, Corelight)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.13.0-dev.115
1.13.0-dev.118
2 changes: 1 addition & 1 deletion doc/programming/language/examples/_statement-for.spicy
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ for ( i in b"abc" ) {
print i;
}

local v = vector("a", "b", "c");
global v = vector("a", "b", "c");

for ( i in v )
print i;
2 changes: 1 addition & 1 deletion doc/programming/language/statements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Examples:
print i;
}

local v = vector("a", "b", "c");
global v = vector("a", "b", "c");

for ( i in v )
print i;
Expand Down
34 changes: 33 additions & 1 deletion doc/programming/language/variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,41 @@ prefixed with ``local`` this time:
function f() {
local x: bytes;
local y = "Y";

}

Usual scoping rules apply to locals. Just like globals, locals are
always initialized to a well-defined value: either their default if
given, or the type's null value.

Local variables can also have their visibility limited to a block
enclosed by curly braces:

.. spicy-code::

function f() {
{
local x = "One";
print x;
}
{
local x = "Two";
print x;
}

# Shadowing variables also works
local shadowed = "Outer";
{
local shadowed = "Inner";
print shadowed;
}
print shadowed;
}

The above function would print:

.. code::
One
Two
Inner
Outer
10 changes: 7 additions & 3 deletions hilti/toolchain/src/compiler/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace hilti { namespace detail { class Parser; } }

%glr-parser
%expect 113
%expect-rr 209
%expect-rr 211

%{

Expand Down Expand Up @@ -467,8 +467,11 @@ opt_func_default_expr : '=' expr { $$ = std::move($2); }

/* Statements */

block : braced_block { $$ = std::move($1); }
| stmt { $$ = builder->statementBlock({$1}, __loc__); }
block : stmt { if ( ! $1->isA<hilti::statement::Block>() )
$$ = builder->statementBlock({std::move($1)}, __loc__);
else
$$ = std::move($1);
}
;

braced_block : '{' opt_stmts '}' { $$ = builder->statementBlock(std::move($2), __loc__); }
Expand All @@ -481,6 +484,7 @@ stmts : stmts stmt { $$ = std::move($1); $$.push_b

stmt : stmt_expr ';' { $$ = std::move($1); }
| stmt_decl { $$ = std::move($1); }
| braced_block { $$ = std::move($1); }
| RETURN ';' { $$ = builder->statementReturn(__loc__); }
| RETURN expr ';' { $$ = builder->statementReturn(std::move($2), __loc__); }
| THROW expr ';' { $$ = builder->statementThrow(std::move($2), __loc__); }
Expand Down
10 changes: 7 additions & 3 deletions spicy/toolchain/src/compiler/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace spicy { namespace detail { class Parser; } }

%glr-parser
%expect 119
%expect-rr 170
%expect-rr 172

%{

Expand Down Expand Up @@ -521,8 +521,11 @@ opt_init_expression : '=' expr { $$ = std::move($2); }

/* Statements */

block : braced_block { $$ = std::move($1); }
| stmt { $$ = builder->statementBlock({$1}, __loc__); }
block : stmt { if ( ! $1->isA<hilti::statement::Block>() )
$$ = builder->statementBlock({std::move($1)}, __loc__);
else
$$ = std::move($1);
}
;

braced_block : '{' opt_stmts '}' { $$ = builder->statementBlock(std::move($2), __loc__); }
Expand All @@ -535,6 +538,7 @@ stmts : stmts stmt { $$ = std::move($1); $$.push_b

stmt : stmt_expr ';' { $$ = std::move($1); }
| stmt_decl { $$ = std::move($1); }
| braced_block { $$ = std::move($1); }
| ASSERT expr ';' { $$ = builder->statementAssert(std::move($2), {}, __loc__); }
| ASSERT_EXCEPTION expr_no_or_error ':' expr ';'
{ $$ = builder->statementAssert(hilti::statement::assert::Exception(), std::move($2), {}, std::move($4), __loc__); }
Expand Down
9 changes: 9 additions & 0 deletions tests/Baseline/hilti.statements.block/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
Testing block:
1
hello :)

Testing shadowing:
outer
inner
outer
9 changes: 9 additions & 0 deletions tests/Baseline/spicy.statements.block/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
Testing block:
1
hello :)

Testing shadowing:
outer
inner
outer
31 changes: 31 additions & 0 deletions tests/hilti/statements/block.hlt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# @TEST-EXEC: ${HILTIC} -j %INPUT >output
# @TEST-EXEC: btest-diff output

module Block {

import hilti;

function void block() {
hilti::print("Testing block:");
{
local x = 1;
hilti::print(x);
}
local x = "hello :)";
hilti::print(x);
}

function void shadowed() {
hilti::print("\nTesting shadowing:");
local shadowed = "outer";
hilti::print(shadowed);
{
local shadowed = "inner";
hilti::print(shadowed);
}
hilti::print(shadowed);
}

block();
shadowed();
}
28 changes: 28 additions & 0 deletions tests/spicy/statements/block.spicy
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# @TEST-EXEC: ${SPICYC} -j %INPUT >output
# @TEST-EXEC: btest-diff output

module Block;

function block() {
print "Testing block:";
{
local x = 1;
print x;
}
local x = "hello :)";
print x;
}

function shadow() {
print "\nTesting shadowing:";
local shadowed = "outer";
print shadowed;
{
local shadowed = "inner";
print shadowed;
}
print shadowed;
}

block();
shadow();

0 comments on commit 8966228

Please sign in to comment.