DevLog 20250724: C# Scripting, Pure 2, Updated + Some Recap

DevLog 20250724: C# Scripting, Pure 2, Updated + Some Recap

BackerLeader posted Originally published at dev.to 2 min read

Intro

Pure has been our in-house C# scripting solution for a few years now.
After experimenting with the well-known CS-Script and some other solutions I can no longer recall, I began experimenting with Roslyn in .NET around 2022/2023, and it's been working well ever since.

Usage

Below is a basic Pure script and its execution:

using static System.Console;

var sum = File.ReadLines("Data.csv")
    .Skip(1)
    .Select(l => l.Split(','))
    .Sum(parts => double.Parse(parts[1]));
WriteLine(sum);

Result

Recap

Version 0.0.3

v0.0.3

The first version of Pure was just a simple wrapper around Roslyn. Later, we expanded it to support syntax like Import(MathNet.Numerics). It wasn't until we added native libraries like CSV, ODBC, and CentralSnippets that things started to get interesting.

Motivation

Below is an old table I compiled when first drafting the design document for Pure (1), with some updates to mention newer solutions:

Table 1

What's Special

Pure 2 is a lightweight C# scripting platform with modern C# 12 syntax and easy NuGet import support. Pure is based on Roslyn. In addition to the base .NET runtime, Pure offers additional libraries for handling common tasks, along with a handy scripting interface (REPL and Notebook) and a place where people can share their snippets.

Pure 2 is conceptually similar to Pure but architecturally very different - it directly builds upon Parcel NExT's Ama runtime (the same runtime used by Divooka). Pure is designed for very short snippets and getting everyday tasks done faster.

Pure 2 uses C# 12 and is similar to top-level statements in C#, with the following features:

  1. You can define types and functions anywhere in the middle of the script.
  2. Pure has its own package import behavior (default namespace usage and package lookup rules).
  3. Default global scope math functions, and default using for System.IO and System.Linq.
  4. Top-level function and variable definitions, alongside classes and other common C# constructs.
  5. Simplifies common tasks even further.
  6. Macros to support text-based parsing, file inclusion, and module imports.

Features

  • Single-word standard package names. One-command NuGet package import and on-the-fly package invocation (through Import).
  • Full compatibility with Divooka packages and Divooka graphs.
  • Pure uses PUREPATH to search for scripts when using Include and when executing script files directly from the command line as the first argument.
  • Pure is very lightweight and will always be a thin layer on top of Roslyn/.NET runtime.
  • CLI REPL; GUI Notebook.

Basic Syntax

Certain macros/syntax are provided to implement language-level scripting constructs:

Table 2

Summary

Be aware that adopting a scripting system - any scripting system - comes with significant risks. It's wise to plan carefully and pick the best tools for the task at hand.

References

If you read this far, tweet to the author to show them you care. Tweet a Thanks
0 votes
0 votes

More Posts

DevLog 20250706: Analyzing (C#) Project Dependencies

Methodox - Jul 6

DevLog 20250726: C# is the way, Python gets things done, JavaScript is convenient

Methodox - Jul 26

DevLog 20250510 Dealing with Lambda

Methodox - May 16

DevLog 20250506 C# Video Processing Foundation Library

Methodox - May 16

Why Records in C# Are Great (and So Nice to Work With)

Spyros - May 10
chevron_left