Skip to content

johnhenry/reprrepr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

###READ ###EVALUATE ###PRINT ###REPEAT ###READ ###EVALUATE ###PRINT ###REPEAT

#Read Evaluate Print Repeat

A Javascript REPL with a customizable scope

##Features

###Multiple Languages reprrepr currently supports the folowing languages through use of the --language flag:

###Isolated Scope reprrepr executes in an isolated scope with most top level objects stripped out. You may define your own custom scope using the --set-scope flag (see below for more).

###Custom Rendering reprrepr needn't simply render to the console. Define your own custom render function with the --set-renderer flag (see below for more).

###Proxied Evaluation reprrepr isn't limited to simply evaluating the built in languages, or even on your local machine. Proxy your input with the --set-proxy flag (see below for more).

###Web Socket REPL reprrepr can host a repl using the --host flag (see below for more).

##Installation

npm install -g reprrepr

##Usage

###View Help

repr --help

###Open a Javascript REPL

repr

Note: Quit using Ctrl + c.

###Open a Lispyscript REPL

repr --language lispyscript

###View available languages

repr --languages

###Evaluate Javascript inline

repr --eval "1 + 1;"

###Evaluate code before starting a repl

repr --pre "var a = 1;"

###Evaluate piped code using an empty --eval flag

echo "1 + 1" | repr --eval

###Evaluate A Javascript file

repr --file javascript.js

###Evaluate A Lispyscript file

repr --file --language lispyscript lispyscript.lsjs

###Open a REPL with specified scope module

repr --set-scope scope.js

The scope module should export an object similar to the following:

module.exports = {
  Math : Math
}

###Open a REPL with specified renderer module

repr --set-renderer renderer.js

The renderer module should export a function similar to the following:

module.exports = function(input, output){
  return output;
};

###Proxy input through an external module

repr --set-proxy proxy.js

The proxy module should export a function similar to the following:

module.exports = function(input){
  return Promise.resolve([input, eval(input)]);
}

It should take an input and return a promise resolved an array of length 2; the first value must be the original input, the second value should be the evaluated input.

Note: Setting the proxy will override the language settings but not the renderer settings.

###Host a server at 127.0.0.1:8080

repr --host 8080

Multiple parties can connect to this server and send input over a socket. Output will be distributed to all connected parties.

###Flags can be mixed and matched (where it makes sense)

repr --language es6 \
--set-scope scope.js\
--verbose \
--errors \
--file input.es6.js > output.js

Note: Renderer, scope, and proxy modules must be written in Javascript, even if the REPL's language is set to something different.

BONUS!: Many languages can now be easily converted into javascript!

##.reprrc You can define a .reprrc file with pre-defined settings.

{
  "verbose"     : true,
  "renderer"    : "renderer.js",
  "language"    : "lispyscript",
  "scope" : "scope.js",
  "proxy"       : "proxy.js",
  "host"        : 8080
}

Note: The .reprrc is a json file and .reprrc.json can be used as well.

The following properties are available in the .reprrc file:

  • verbose -- show verbose console output
  • errors -- show errors in console output
  • renderer -- similar to --set-renderet flag
  • language -- similar to --language flag
  • scope -- similar to --set-enviornmet flag
  • proxy -- similar to --set-proxy flag
  • host -- similar to --host flag
  • eval -- similar to --eval flag
  • pre -- similar to --pre flag

##Advanced Usage Examples

###Access history from within your repl

  • 1 - Set up a module that exports a history array.

####history.js

var history = [];
module.exports = history;
  • 2 - Create an renderer that adds inputs to the history array.

####render.js

var history = require('./history');
var render = function(input, output){
  history.push(input);
  history.push(output);
  return output;
};
module.exports = render;
  • 3 - Create an scope with functions to access history

####scope.js

var history = require('./history');
var scope = {
  input:function(index){
    return history[2 * index];
  },
  output:function(index){
    return history[2 * index + 1];
  },
  history:history
}
  • 4 - Run with custom Flags
repl --set-scope scope.js --set-render render.js
> 1 + 1
2
> 3 + 2
5
> output(0);
2
> input(1)
3 + 2
> history[4]
output(0);

Note : Be careful when your repl leaks into its outer scope like this. It may lead to unintended side effects.

  • 4 (alternative) - Alternatively, you can instead set these in a .reprrc file like so:

####.npmrc

{
  "scope" : "scope.js",
  "render"      : "render.js"
}

###READ ###EVALUATE ###PRINT ###REPEAT ###...

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published