0atman/blaze

Discuss code splicing

Bert-Proesmans opened this issue · 0 comments

Hi, Youtube suggested me your older video about literate programming. (The blogpost link in the description doesn't work anymore, btw)

I was wondering if you considered implementing something alike code splicing?
Now, splicing can happen in two ways;

  1. Leave markers in code to be replaced by the next code block
  2. In the next code block, target a specific point of syntax in previous code block to insert new code after/before

Both approaches come with challenges; what to splice into, where to splice into
And, of course, how to do this in a minimalistic way. Preferable programming language agnostic?

Do you happen to have thought about another approach? Thoughts about more/different challenges?
Below I wrote naïve examples, those still need a lot of thought about syntax, comment-syntax compatibility, type-safety, splice scope, single-or multiple replacements, should the tool be language agnostic working as a text tool or syntax/semantic model aware...

I feel like with the Rust compiler for type inference, LSP for easing on pesky syntax requirements, and the current level of care for error messages, we are at a point in time where literate programming could be elevated to a next level.
EDIT; Also, we now have solid syntax parsers that support multiple programming languages at once.


Example, approach 1, see below. Note that the square brackets are for demonstration purposes and not part of the literate document;

[Descriptive text]
... We iterate over our list of numbers, and print them in the end

[Code]

let numbers = [-1,5,8];
for number in numbers.into_iter()<!-- Process numbers --!> {
  println!{"{:?}, number);
}

[Descriptive text]
... And for that reason we need the natural square root of these numbers

[Code]

<!-- include -->
use std::f64;
<!-- include -->

<!-- START Process numbers --!>
.filter(|n| *n > 0)
.map(|n| (*n as f64).sqrt())
<!-- END Process numbers --!>

[Code result]

use std::f64;

let numbers = [-1,5,8];
for number in numbers.into_iter()
.filter(|n| *n > 0)
.map(|n| (*n as f64).sqrt())
{
  println!{"{:?}, number);
}

Example, approach 2, see below. Note that the square brackets are for demonstration purposes and not part of the literate document;

[Descriptive text]
... We iterate over our list of numbers, and print them in the end

[Code]

let numbers = [-1,5,8];
for number in numbers.into_iter() {
  println!{"{:?}, number);
}

[Descriptive text]
... And for that reason we need the natural square root of these numbers

[Code]

<!-- @after include -->
use std::f64;
<!-- @after include -->

<!-- @after fn_call into_iter--!>
.filter(|n| *n > 0)
.map(|n| (*n as f64).sqrt())
<!--  @after fn_call into_iter --!>

[Code result]

use std::f64;

let numbers = [-1,5,8];
for number in numbers.into_iter()
.filter(|n| *n > 0)
.map(|n| (*n as f64).sqrt())
{
  println!{"{:?}, number);
}