Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

The Online Interpreter for Standard ML, written in TypeScript.

License

NotificationsYou must be signed in to change notification settings

SOSML/SOSML

Repository files navigation

SOSML is the online interpreter for the functional programming language Standard ML (SML), written in TypeScript.SOSML is used in a freshman class at Saarland University; you can check it out athttps://sosml.org.

Features

Correctly lexing, parsing, elaborating and interpreting any SML core language program, i.e. a program that may contain the following constructs:

  • Supported declarations (partially supported declarations in italics)
    • value declarations (val x = 42;)
    • function declarations (fun f 0 = 42 | f x = f (x-1);)
    • type alias declarations (type t = 'a -> (int * int))
    • datatype declarations (datatype tree = T of tree list;)
      • withtype will not be supported.
    • abstract datatype declarations (abstype tree = T of tree list with val empty = T [];)Note thatabstype is implemented as a derived form as proposed by Successor ML.
      • withtype will not be supported.
    • exception declarations (exception E of int;)
    • local declarations (local val x = 42; in val y = x + 2; end;)
    • declaring operators as infix, nonfix, left, and right associative viainfix,infixr, andnonfix
    • open declarations
    • structure declarations (structure S = struct end;)
    • signature declarations (signature S = sig end;)
    • functor declarations (experimental,functor F = struct end;)
  • Supported expressions (This list is non-exhaustive)
    • tuple ((1, 2, 3)), records ({a = 1, b = 2}), and lists ([1, 2, 3])
    • application expressions (f x)
    • infix expressions (3 + 4 - 5 * 6)
    • andalso andorelse expressions
    • raise andhandle expressions
    • conditionals (if true then 2 else 3)
    • case analyses (case x of 0 => 0 | _ => 1)
    • abstractions (fn 0 => 0 | _ => 1)
    • while loops
    • print
    • ref,!, and:=
    • structures
    • signatures
    • functors (experimental)
  • Supported standard library constructs (Note that all available libraries are loaded per default, currently SOSML has not implemented any user-space loading of modules.)
    • Math library
    • Char libraryord,chr,Char.isLower,Char.isUpper,Char.isDigit,Char.isAlpha
    • Int libraryInt.minInt,Int.maxInt, andInt.compare
    • Real libraryReal.compare,Real.fromInt,Real.round,Real.floor, andReal.ceil
    • Option library
    • List and Listsort libraries
    • Vector libraryfromList,tabulate,length,sub,update,app,map,foldl,foldr
    • Array libraryfromList,tabulate,length,sub,update,vector,foldl,foldr
    • String library

Using SOSML as an Interpreter for Standard ML (SML)

If you just want to test SOSML, just head tohttps://sosml.org/editor and enter your code.Adding a; will then start the evaluation.

If you don't like web browsers, but still want to test SOSML, you can install the experimental CLI vianpm

npm install -g @sosml/interpreter@latest

This makes the commandsosml available, which behaves like any other run-of-the-mill interpreter for SML.Note that due to its experimental state, the CLI currently does not take any options or parameters.

Using SOSML as a Component in Your Project

Thenode Way

You may use the interpreter bundled in SOSML or parts of it to build your own fancy SML interpreter:First, install SOSML via

npm install --save @sosml/interpreter@latest

Now, to get your SML code interpreted by SOSML, importinterpret andgetFirstState from the package you just installedand you are good to go:

import{interpret,getFirstState,State}from'@sosml/interpreter';// Obtain an initial state for the interpretationletinitialState:State=getFirstState();// Let's interpret some codeletinterpretationResult=interpret('val x = "Hello World!";',initialState);console.log(interpretationResult.state.toString({stopId:initialState.id+1}));// Prints "val x = "Hello World!": string;"// Let's interpret some more code; note how we use the state obtained from the last stepinterpretationResult=interpret('fun f y = x | f 10 = "???";',interpretationResult.state);// Note that the last code produced a warning:console.log(interpretationResult.warnings);// Something like "Rules after "y" unused in pattern matching."// Similarly, interpretationResult.evaluationErrored may contain an Error if the interpretation of your code failed// Lastly, SML exceptions raised by your code that are not handled end up in interpretationResult.error.

Check out thesrc/cli.ts file for an example SML interpreter using SOSML.

The “But I Hatenode.js” Way

Starting with version1.5.0, you can directly run SOSML in a<script> tag in HTML:

<!DOCTYPE html><html><head><metacharset="utf-8"><scriptsrc="https://unpkg.com/@sosml/interpreter@^1.5.0/build/interpreter.min.js"></script></head><body><script>letinitialState=Interpreter.getFirstState();letinterpretationResult=Interpreter.interpret('val x = "Hello World!";',initialState);console.log(interpretationResult.state.toString({stopId:initialState.id+1}));interpretationResult=Interpreter.interpret('fun f y = x | f 10 = "???";',interpretationResult.state);console.log(interpretationResult.warnings);</script></body></html>

Contributing to SOSML

We welcome you to open an Issue for any error or squid you may find, and we will try to fix it ASAP.Further, if you want additional parts of the Standard Library or other features in general implemented,feel free to open a new Issue.

If you want to contribute via writing code, you may check the Issues page for any unresolved problemsor unimplemented features and then submit a pull request after solving that problem or implementing that feature.

Cloning and Building

To get started on writing code for SOSML, clone the repository and install the dependencies:

git clone https://github.com/SOSML/SOSMLnpm install

To build the interpreter and pack it into one file usingWebpack run:

npm run build

This will create a file in the directorybuild calledinterpreter.js.

To also minify the result run:

npm run dist

This will create a file in the directorybuild calledinterpreter.min.js.

To build the CLI, run

npm run clinpm link

Testing Your Changes

SOSML comes with an extensive set of tests which can be run via

npmtest

This runs all tests located in thetest directory.When contributing new code, please make sure that you add the appropriate tests and that no old tests begin to fail.


[8]ページ先頭

©2009-2025 Movatter.jp