summaryrefslogtreecommitdiffstats
path: root/crates
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2024-05-16 14:01:26 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2024-05-16 14:01:26 +0200
commit86d8a996235bb8c9186b823aa7dbcfa8915a1077 (patch)
tree9320af4d6df1d66b59df29a097b5601c1b1ca008 /crates
parent34e3bbfa376c97fd7ab38c0946949be42dc352d4 (diff)
downloadrebel-86d8a996235bb8c9186b823aa7dbcfa8915a1077.tar
rebel-86d8a996235bb8c9186b823aa7dbcfa8915a1077.zip
rebel-lang: optimize handling of blocks with a single expressionmain
No new scope is required when the only statement in a block is an expression.
Diffstat (limited to 'crates')
-rw-r--r--crates/rebel-lang/src/typing.rs4
-rw-r--r--crates/rebel-lang/src/value.rs4
2 files changed, 8 insertions, 0 deletions
diff --git a/crates/rebel-lang/src/typing.rs b/crates/rebel-lang/src/typing.rs
index 3ee681a..34280cd 100644
--- a/crates/rebel-lang/src/typing.rs
+++ b/crates/rebel-lang/src/typing.rs
@@ -454,6 +454,10 @@ impl<'scope> Context<'scope> {
}
fn type_block(&mut self, block: &ast::Block) -> Result<Type> {
+ if let [ast::BlockStmt::Expr { expr }] = &block.0[..] {
+ return self.type_expr(expr);
+ }
+
let (ret, upvalues) = self.type_scope(&block.0)?;
self.0.initialize_all(upvalues);
Ok(ret)
diff --git a/crates/rebel-lang/src/value.rs b/crates/rebel-lang/src/value.rs
index 8ee202d..bca3fee 100644
--- a/crates/rebel-lang/src/value.rs
+++ b/crates/rebel-lang/src/value.rs
@@ -395,6 +395,10 @@ impl<'scope> Context<'scope> {
}
fn eval_block(&mut self, block: &ast::Block) -> Result<Value> {
+ if let [ast::BlockStmt::Expr { expr }] = &block.0[..] {
+ return self.eval_expr(expr);
+ }
+
let (ret, upvalues) = self.eval_scope(&block.0)?;
self.0.initialize_all(upvalues);
Ok(ret)