about summary refs log tree commit diff
path: root/tvix/eval/src/compiler.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-08-23T19·54+0300
committertazjin <tazjin@tvl.su>2022-09-01T21·56+0000
commit6f31c895ffe2dc5dd8281d812252d5db0644ec77 (patch)
tree9e45e371bc7d066b8666a3df532c6d241ee1165b /tvix/eval/src/compiler.rs
parent4715f9a3a0135e1b6bc1f24fbafc9b1ce1a9bc20 (diff)
refactor(tvix/eval): return a lambda from the compiler r/4576
Changes the internal compiler plumbing to not just return a chunk of
code, but the same chunk wrapped inside of a lambda value.

This is one more step towards compiling runtime lambdas.

Change-Id: If0035f8e65a2970c5ae123fc068a2396e1d8fd72
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6240
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
Diffstat (limited to 'tvix/eval/src/compiler.rs')
-rw-r--r--tvix/eval/src/compiler.rs13
1 files changed, 7 insertions, 6 deletions
diff --git a/tvix/eval/src/compiler.rs b/tvix/eval/src/compiler.rs
index 7ccb004327..167ac731d6 100644
--- a/tvix/eval/src/compiler.rs
+++ b/tvix/eval/src/compiler.rs
@@ -21,14 +21,14 @@ use std::path::{Path, PathBuf};
 use crate::chunk::Chunk;
 use crate::errors::{Error, ErrorKind, EvalResult};
 use crate::opcode::{CodeIdx, OpCode};
-use crate::value::Value;
+use crate::value::{Lambda, Value};
 use crate::warnings::{EvalWarning, WarningKind};
 
 /// Represents the result of compiling a piece of Nix code. If
 /// compilation was successful, the resulting bytecode can be passed
 /// to the VM.
 pub struct CompilationResult {
-    pub chunk: Chunk,
+    pub lambda: Lambda,
     pub warnings: Vec<EvalWarning>,
     pub errors: Vec<Error>,
 }
@@ -90,7 +90,7 @@ struct Scope {
 }
 
 struct Compiler {
-    chunk: Chunk,
+    lambda: Lambda,
     scope: Scope,
 
     warnings: Vec<EvalWarning>,
@@ -102,7 +102,8 @@ struct Compiler {
 // structures of the compiler.
 impl Compiler {
     fn chunk(&mut self) -> &mut Chunk {
-        &mut self.chunk
+        std::rc::Rc::<Chunk>::get_mut(self.lambda.chunk())
+            .expect("compiler flaw: long-lived chunk reference")
     }
 
     fn emit_constant(&mut self, value: Value) {
@@ -910,7 +911,7 @@ pub fn compile(expr: ast::Expr, location: Option<PathBuf>) -> EvalResult<Compila
 
     let mut c = Compiler {
         root_dir,
-        chunk: Chunk::default(),
+        lambda: Lambda::new_anonymous(),
         warnings: vec![],
         errors: vec![],
         scope: Default::default(),
@@ -919,7 +920,7 @@ pub fn compile(expr: ast::Expr, location: Option<PathBuf>) -> EvalResult<Compila
     c.compile(expr);
 
     Ok(CompilationResult {
-        chunk: c.chunk,
+        lambda: c.lambda,
         warnings: c.warnings,
         errors: c.errors,
     })