Giter Site home page Giter Site logo

felixeder / turing.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from olian04/turing.js

0.0 2.0 0.0 305 KB

Turing.js is an Event-Driven-Language-Design-API based on the definition of a turing machine.

Home Page: https://olian04.github.io/Turing.js

TypeScript 100.00%

turing.js's Introduction

CircleCI codecov Npm dependancies

Turing.js logo

Turing.js is an Event-Driven-Language-Design-API based on the definition of a turing machine.

A Turing machine is an abstract machine that manipulates symbols on a strip of tape according to a table of rules. The machine operates on an infinite memory tape divided into discrete cells. The machine positions its head over a cell and "reads" the symbol there. Then, as per the symbol and its present place in a finite table of user-specified instructions, the machine (i) writes a symbol (e.g. a digit or a letter from a finite alphabet) in the cell, then (ii) either moves the tape one cell left or right, then (iii) (as determined by the observed symbol and the machine's place in the table) either proceeds to a subsequent instruction or halts the computation.

Typescript & Typings

Turing.js is written in and is designed to be used in Typescript, this means that Turing.js will always include its own typings. Because Turing.js comes with its own typings it lends it self very well to editors such as vs code that utilizes typings in both typescript and javascript to improve intellisense.

Links & Install

Install: npm i --save turingjs

Playground: https://runkit.com/olian04/turing-js-demo

Docs: https://olian04.github.io/Turing.js/

Demo

/* Super simple counter */
import { Language } from 'turingjs';

new Language<{ sum: number }>()
    .token({
        '+': state => { state.data.sum++ },
        '-': state => { state.data.sum-- }
    })
    .data({
        sum: 0
    })
    .run('++-+++--')
    .then(state => console.log(state.data.sum)/* 2 */);
    

/* The full Brainfuck language */
import { Language, GetTagFunction, Skip } from 'turingjs';

// Step 1: Define a new language
let brainfuck = new Language<{ in: string[], out: string[], loops: number[] }>()
    .token({
        '+': state => { state.stack[state.index]++ },
        '-': state => { state.stack[state.index]-- },
        '>': state => { state.index++ },
        '<': state => { state.index-- },
        ',': state => { state.stack[state.index] = state.data.in.shift().charCodeAt(0) },
        '.': state => { state.data.out.push(String.fromCharCode(state.stack[state.index])) },
        ']': (state, token) => { state.tokenPointer = state.data.loops.pop() - 1 },
        '[': (state, token) => {
            if (state.stack[state.index] === 0) {
                return Skip.until.balanced('[', ']', 1);
            }
            state.data.loops.push(token.position)
        }
    })
    .on('eof', state => state.data.loops.length === 0)
    .data({ in: [], out: [], loops: [] });

// Step 2: Run some code in the new language    
brainfuck
    .data({ in: 'ABC'.split('') })
    .run('+++[->,.+++.<]')
    .then(finalState => console.log(finalState.data.out.join(''))/* ADBECF */)
    .catch(error => console.log(error));

// Same thing as step 2, but using a tag function instead
let bf = GetTagFunction(brainfuck); //  Create tag function
let finalState = bf`+++[->,.+++.<]${{ in: 'ABC'.split('') }}`; // Run some code
console.log(finalState.data.out.join('')); // Get the output: ADBECF 

Developing

Built with

Tested with

Prerequisites

Setting up dev

git clone [email protected]:Olian04/Turing.js.git
cd Turing.js
npm install

Building

npm run build

Testing

npm run test

Publishing

Refer to: https://www.tsmean.com/articles/how-to-write-a-typescript-library/

Api reference

At the core of every Turing.js language lies a Language object, this object keeps track of what rules your language abides by. It is also this Language object that takes care of executing code written in your new language.

The Language object has 4 functions, each with several overloads, token, data, on, and run.

Function Description
token Adds TokenHandlers, these are the bread and butter of your languages grammar.
data Provides a way of adding custom state properties to your language's interpreter.
on Adds event handlers for various events fired during an execution of code.
run Runs some code written in your new language.

For further reference please see the auto generated docs.

Credits

turing.js's People

Contributors

olian04 avatar

Watchers

James Cloos avatar Felix Eder avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.