From 220950cf2596260c1649ab9373cb9626c1fbfbb9 Mon Sep 17 00:00:00 2001 From: Ian Hobson Date: Wed, 8 Jan 2025 12:54:53 +0100 Subject: [PATCH] Add Compiler::compile_ast Compiling an AST directly was removed in e8546bb3b5acd0207d58ebefe338331cedc15746 but it turns out that this is useful in some cases, particularly for tools like `koto-ls` that want to process an AST after checking that it compiles correctly. --- CHANGELOG.md | 7 +++++++ crates/bytecode/src/compiler.rs | 20 ++++++++++++++------ crates/bytecode/src/module_loader.rs | 1 + 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f9e2db8..cf970d8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,13 @@ The Koto project adheres to ## [0.16.0] Unreleased +### Added + +#### API + +- `Compiler::compile_ast` has been added, useful for tools that want to work with the AST + after checking that it compiles correctly. + ### Changed #### Language diff --git a/crates/bytecode/src/compiler.rs b/crates/bytecode/src/compiler.rs index b8fc44e2..16ef360f 100644 --- a/crates/bytecode/src/compiler.rs +++ b/crates/bytecode/src/compiler.rs @@ -4,7 +4,6 @@ use crate::{ }; use circular_buffer::CircularBuffer; use derive_name::VariantName; -use koto_memory::Ptr; use koto_parser::{ Ast, AstBinaryOp, AstFor, AstIf, AstIndex, AstNode, AstTry, AstUnaryOp, AstVec, ChainNode, ConstantIndex, Function, ImportItem, KString, MatchArm, MetaKeyId, Node, Parser, Span, @@ -261,13 +260,24 @@ pub struct Compiler { } impl Compiler { - /// Compiles a Koto script using the provided settings, returning a compiled [Chunk] + /// Compiles a script using the provided settings, returning a compiled [Chunk] pub fn compile( script: &str, script_path: Option, settings: CompilerSettings, - ) -> Result> { + ) -> Result { let ast = Parser::parse(script)?; + let mut result = Self::compile_ast(ast, script_path, settings)?; + result.debug_info.source = script.to_string(); + Ok(result) + } + + /// Compiles an [Ast] using the provided settings, returning a compiled [Chunk] + pub fn compile_ast( + ast: Ast, + script_path: Option, + settings: CompilerSettings, + ) -> Result { let mut compiler = Compiler { settings, ..Default::default() @@ -284,8 +294,6 @@ impl Compiler { return compiler.error(ErrorKind::ResultingBytecodeIsTooLarge(compiler.bytes.len())); } - compiler.debug_info.source = script.to_string(); - let result = Chunk { bytes: compiler.bytes, constants: ast.consume_constants(), @@ -293,7 +301,7 @@ impl Compiler { debug_info: compiler.debug_info, }; - Ok(result.into()) + Ok(result) } fn compile_node( diff --git a/crates/bytecode/src/module_loader.rs b/crates/bytecode/src/module_loader.rs index 2a80830d..7b0477ee 100644 --- a/crates/bytecode/src/module_loader.rs +++ b/crates/bytecode/src/module_loader.rs @@ -119,6 +119,7 @@ impl ModuleLoader { settings: CompilerSettings, ) -> Result, ModuleLoaderError> { Compiler::compile(script, script_path.clone(), settings) + .map(Ptr::from) .map_err(|e| ModuleLoaderError::from_compiler_error(e, script, script_path)) }