All Projects → microsoft → Schemy

microsoft / Schemy

Licence: mit
A lightweight embeddable Scheme-like interpreter for configuration

Programming Languages

csharp
926 projects
scheme
763 projects

Projects that are alternatives of or similar to Schemy

H-Calc
So, you want to write a DSL interpreter...
Stars: ✭ 21 (-92.19%)
Mutual labels:  interpreter
xcode-config
My Xcode config - Alfred Workflow, File templates, Xcode code snippets, themes, IDETextKeyBindingSet
Stars: ✭ 16 (-94.05%)
Mutual labels:  configuration
Tmux Powerline
A hackable statusbar for tmux consisting of dynamic & beautiful looking segments, inspired by vim-powerlline, written purely in bash.
Stars: ✭ 2,802 (+941.64%)
Mutual labels:  configuration
config
Simple configuration management for PHP
Stars: ✭ 15 (-94.42%)
Mutual labels:  configuration
rails-settings-ui
User interface for manage settings in rails application (using rails-settings gem) / Интерфейс для управления настройками в Rails приложении
Stars: ✭ 93 (-65.43%)
Mutual labels:  configuration
balloon-lang
The Balloon programming language and interpreter.
Stars: ✭ 17 (-93.68%)
Mutual labels:  interpreter
RISVM
A low overhead, embeddable bytecode virtual machine in C++
Stars: ✭ 21 (-92.19%)
Mutual labels:  interpreter
Zformat
The Hoa\Zformat library.
Stars: ✭ 265 (-1.49%)
Mutual labels:  configuration
myawesomerc
My Awesome WM config files
Stars: ✭ 86 (-68.03%)
Mutual labels:  configuration
pascal-interpreter
A simple interpreter for a large subset of Pascal language written for educational purposes
Stars: ✭ 21 (-92.19%)
Mutual labels:  interpreter
goconfig
.gitconfig syntax parser
Stars: ✭ 15 (-94.42%)
Mutual labels:  configuration
LKI
LKI's dotfiles.
Stars: ✭ 31 (-88.48%)
Mutual labels:  configuration
zshrc
📝 Zsh Configuration for nerds with zplug
Stars: ✭ 28 (-89.59%)
Mutual labels:  configuration
Charles-Proxy-Mobile-Guide
The mobile hackers' guide to Charles Proxy 👍
Stars: ✭ 105 (-60.97%)
Mutual labels:  configuration
Pyhcl
HCL is a configuration language. pyhcl is a python parser for it.
Stars: ✭ 260 (-3.35%)
Mutual labels:  configuration
Simple-YAML
A Java API that provides an easy-to-use way to store data using the YAML format.
Stars: ✭ 68 (-74.72%)
Mutual labels:  configuration
danube
The Danube Programming Language
Stars: ✭ 33 (-87.73%)
Mutual labels:  interpreter
Tweek
Tweek - an open source feature manager
Stars: ✭ 268 (-0.37%)
Mutual labels:  configuration
Dascript
daScript - high-performance statically strong typed scripting language
Stars: ✭ 259 (-3.72%)
Mutual labels:  interpreter
project-migration-tools
Project Migration tools to help you migrating to IAR Embedded Workbench more efficiently.
Stars: ✭ 36 (-86.62%)
Mutual labels:  configuration

Schemy

Schemy is a lightweight Scheme-like scripting language interpreter for embedded use in .NET applications. It's built from scratch without any external dependency. Its primary goal is to serve as a highly flexible configuration language. Example scenarios are to describe computational graph, workflow, or to represent some complex configuration.

Its design goals are:

  • easy to embed and extend in .NET
  • extensible in Scheme via macro expansion
  • safe without the need of complicated AppDomain sandboxing. It's safe because IO functions are not exposed by default
  • runs reasonably fast and low memory footprint

Non-goals:

  • be highly optimized - it's designed to load configurations and not part of any heavy computation, so being optimized is not the goal - e.g., there's no JIT compiling, etc.

Schemy's implementation is inspired by Peter Norvig's article on Lisp interpreter, but is heavily adapted to .NET and engineered to be easily extensible and embeddable in .NET applications.

Language Features

It has most features that a language would support:

  • number, boolean, string, list types
  • variable, function definition
  • tail call optimization
  • macro definition
  • lexical scoping

Many Scheme features are not (yet) supported. Among those are:

  • continuation (call/cc)
  • use square brackets [...] in place of parenthesis (...)

Why Schemy

Schemy was originally designed at Microsoft 365 to define complex machine learning model workflows that handle web API requests. Since Schemy scripts can be easier to develop, modify, and deploy than full fledged .NET application or modules, the development and maintenance become more agile, and concerns are better separated - request routing is handled by web server, request handling logics are defined by Schemy scripts. The command_server example application captures this design in a very simplified way.

More generically, for applications that require reading configuration, the usual resort is to configuration languages like JSON, XML, YAML, etc. While they are simple and readable, they may not be suitable for more complex configuration tasks, when dynamic conditioning, modularization, reusability are desired. For example, when defining a computational graph whose components depend on some runtime conditions, and when the graph is desirable to be composed of reusable sub-graphs.

The other end of the spectrum is to use full fledged scripting languages like Python (IronPhython), Lua (NLua), etc. While they are more flexible and powerful, footprint for embedding them can be heavy, and they pull in dependencies to the host application.

Schemy can be seen as sitting in the middle of the spectrum:

  • It provides more languages features for conditioning, modularization/reusability (functions, scripts), customization (macro), then simple configuration languages.

  • It's simpler in implementation (~1500 lines of code) and doesn't pull in extra dependencies and have a smaller footprint to the host application.

  • It can be safer in the sense that it doesn't provide access to file system by default. Although we run it in fully trusted environment, this could be useful as it doesn't need to be sandboxed. (IO is supported by virtual file system.)

Usage

To reference schemy.dll, either install it via Nuget (schemy), or build it from source.

Alternatively, you can just copy src/schemy/*.cs source code to include in your application. Since Schemy code base is small. This approach is very feasible (don't forget to also include the resource file init.ss).

Build

Schemy does not take any external dependency. So building it should be straightfoward: simply run msbuild in src/, or use your favorite IDE.

The project structure looks like so:

├───doc
└───src
    ├───schemy              // the core schemy interpreter (schemy.dll)
    ├───examples
    │   ├───command_server  // loading command handlers from schemy scripts
    │   └───repl            // an interactive interpreter (REPL)
    └───test

Embedding and Extending Schemy

The below sections describes how to embed and extend Schemy in .NET applications and in Scheme scripts. For a comprehensive example, please refer to src/examples/command_server.

Extending Schemy in .NET

Schemy can be extended by feeding the interpreter symbols with predefined .NET objects. Variables could be any .NET type. Procedures must implement ICallable.

An example procedure implementation:

new NativeProcedure(args => args, "list");

This implements the Scheme procedure list, which converts its arguments into a list:

schemy> (list 1 2 3 4)
(1 2 3 4)

NativeProcedure has convenient factory methods that handles input type checking and conversion, for example, NativeProcedure.Create<double, double, bool>() takes a Func<double, double bool> as the implementation, and handles the argument count checking (must be 2) and type conversion ((double, double) -> bool):

builtins[Symbol.FromString("=")] = NativeProcedure.Create<double, double, bool>((x, y) => x == y, "=");

To "register" extensions, one can pass them to the Interpreter's constructor:

Interpreter.CreateSymbolTableDelegate extension = itpr => new Dictionary<Symbol, object>
{
    { Symbol.FromString("list"), new NativeProcedure(args => args, "list") },
};

var interpreter = new Interpreter(new[] { extension });

Extending Schemy in Scheme

When launched, the interpreter tries to locate and load Scheme file .init.ss in the same directory as the executing assembly. You can extend Schemy by putting function, variable, macro definition inside this file.

Extending with functions

For example, this function implements the standard Scheme list reversion function reverse (with proper tail call optimization):

(define (reverse ls)
  (define loop
    (lambda (ls acc)
      (if (null? ls) acc
        (loop (cdr ls) (cons (car ls) acc)))))
  (loop ls '()))

Use it like so:

Schemy> (reverse '(1 2 "foo" "bar"))
("bar" "foo" 2 1)

Syntax augmentation in Scheme

For example, we want to augment Schemy with a new syntax for local variable definition, let. Here's what we want to achieve:

Schemy> (let ((x 1)     ; let x = 1
              (y 2))    ; let y = 2
          (+ x y))      ; evaluate x + y
3

The following macro implements the let form by using lambda invocation:

(define-macro let
  (lambda args
    (define specs (car args))  ; ((var1 val1), ...)
    (define bodies (cdr args)) ; (expr1 ...)
    (if (null? specs)
      `((lambda () ,@bodies))
      (begin
        (define spec1 (car specs)) ; (var1 val1)
        (define spec_rest (cdr specs)) ; ((var2 val2) ...)
        (define inner `((lambda ,(list (car spec1)) ,@bodies) ,(car (cdr spec1))))
        `(let ,spec_rest ,inner)))))

Use Interactively (REPL)

The interpreter can be run interactively, when given a TextReader for input and a TextWriter for output. The flexibility of this interface means you can not only expose the REPL via stdin/stdout, but also any streamable channels, e.g., a socket, or web socket (please consider security!).

/// <summary>Starts the Read-Eval-Print loop</summary>
/// <param name="input">the input source</param>
/// <param name="output">the output target</param>
/// <param name="prompt">a string prompt to be printed before each evaluation</param>
/// <param name="headers">a head text to be printed at the beginning of the REPL</param>
public void REPL(TextReader input, TextWriter output, string prompt = null, string[] headers = null)

This can be useful for expose a remote "shell" for the application, or as debugging purposes (see how src/examples/command_server/ uses the --repl command line argument).

There is an example REPL application in src/examples/repl/ that can be started as a REPL interpreter:

$ schemy.repl.exe
-----------------------------------------------
| Schemy - Scheme as a Configuration Language |
| Press Ctrl-C to exit                        |
-----------------------------------------------

Schemy> (define (sum-to n acc)
           (if (= n 0) 
              acc 
              (sum-to (- n 1) (+ acc n))))

Schemy> (sum-to 100 0)
5050

Schemy> (sum-to 10000 0)  ; proper tail call optimization prevents stack overflow
50005000

Run a script:

$ schemy.repl.exe <some_file>

Virtual File System

The interpreter's constructor takes a IFileSystemAccessor:

public interface IFileSystemAccessor
{
    /// <summary>
    /// Opens the path for read
    /// </summary>
    /// <param name="path">The path</param>
    /// <returns>the stream to read</returns>
    Stream OpenRead(string path);

    /// <summary>
    /// Opens the path for write
    /// </summary>
    /// <param name="path">The path</param>
    /// <returns>the stream to write</returns>
    Stream OpenWrite(string path);
}

There're two builtin implementations: a DisabledFileSystemAccessor, which blocks read/write, a ReadOnlyFileSystemAccessor, which provides readonly to local file system. The default behavior for an interpreter is DisabledFileSystemAccessor.

In addition to them, you can implement your own file system accessors. For example, you could implement it to provide access into a Zip archive, treating each zip archive entry as a file in a file system.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].