2022-09-16

Vim9Script VLANG REPL in a Jupyter Notebook

The code in this Jupyter Notebook has been partially extracted from this repository. It shows a simple implementation of a REPL inside a Jupyter Notebook, in the first part a vim9script code snippet is passed to a VIM9 instance. The output is then serialized into a JSON file that can be displayed inside the notebook.

The second part shows the same method for a code snippet written in V. In this case, I included an HTML tailwind card updated with the output from the following program:

    // filename: kernel.v
    import os
    import json
    import time

    struct Res {
        mut:
            stdout string
            stderr string

      } 
    fn main() {
        mut pid := os.getpid()
        //pid = 1 //test pid
        //input := os.getwd() + "\\" + pid.str() + ".v"
        input := os.getwd() + "\\"  +"1.v"
        for {
        time.sleep(20000)

                  if !os.exists(input) {
                       continue
                   }

        mut res := &Res{}
        
        rstd := os.execute("C:\\Users\\Seve\\workplace\\v\\v.exe run $input")
        if rstd.exit_code == 0 {
            res.stdout = rstd.output
            }
        else {
            res.stderr = rstd.exit_code.str()
            }
        os.rm(input)?    
        //output := os.getwd() + "\\" + pid.str() + ".output"

        output := os.getwd() + "\\"  + "1.output"
        mut f := os.create(output) or {
                    println(err)
                    return
            }

        f.write_string(json.encode(res)) or { println(err) }
        break
          }

        }