Interactive Mode (REPL)

Interactive Mode Warp 10

Since the 2.0 version of Warp 10™, it is possible to submit WarpScript™ interactively to an instance: you can send WarpScript functions, get back the state of the stack, send other functions, get the new state, and so on. This functionality opens new possibilities and we will explore one of them in this article: a simple WarpScript interactive console a.k.a. a Read–Eval–Print Loop (REPL).

Setting it up

To test the interactive mode you will need your own Warp 10 instance. If you don’t have one yet and don’t want to bother with that now, you can set up Warp 10 in Docker with the following command: docker run -d -p 8080:8080 -p 8081:8081 warp10io/warp10:latest

Next, the client interactive console we developed is in Python which should be already installed on your computer. You will also need the ws4py module to connect through WebSockets to the Warp 10 endpoint. Install it with pip using the command: sudo -H pip install ws4py

Now download the Python program here and run it with the command python wscli.py ws://127.0.0.1:8080/api/v0/interactive.

WarpScript functions for interactive sessions

When in an interactive session, you have access to several new functions to specify how the Warp 10 instance responds to the input:

  • ECHOON to display part of the stack for each input
  • ECHOOFF to display nothing for each input
  • JSONSTACK to set the display of the stack as a JSON
  • WSSTACK to set the display of the stack as SNAPSHOT would do, this is the default
  • PSTACK to display once the stack, even if ECHOOFF

Examples

You can now interact with Warp 10 in WarpScript, typing commands like you would do in a terminal or a Python console. For instance, we can set the interactive session to display 100 levels of stack in JSON, create a GTS with 10 random locations and values, make a typo and compute the NSUMSUMSQ:

WS> 100 ECHOON JSONSTACK
WS> NEWGTS
 
/* 1 */ {"c":"","l":{},"a":{},"la":0,"v":[]}
 
WS<1> 1 10 <% RAND RAND RAND RAND ADDVALUE %> FOR
 
/* 1 */ {"c":"","l":{},"a":{},"la":0,"v":[[1,0.14244851656258106,0.7723094802349806,0,0.4028800004992321],[2,0.9560195496305823,0.4875935800373554,0,0.8172590674433254],[3,0.06541048642247915,0.8966675028204918,0,0.5340565768691775],[4,0.2994705643504858,0.47237539663910866,0,0.8628508467226303],[5,0.4486379725858569,0.5916609428822994,0,0.7499894134373479],[6,0.44851836282759905,0.532403402030468,0,0.7098629866242626],[7,0.6119268760085106,0.423255767673254,0,0.5095916527344659],[8,0.4194395709782839,0.23777506314218044,0,0.3418890713050531],[9,0.5356722185388207,0.28016519732773304,0,0.9452521164944698],[10,0.4281028127297759,0.33526724204421043,0,0.02181773745796667]]}
 
WS<1> IMADEATYPO
// ERROR Unknown function 'IMADEATYPO'
WS<1> NSUMSUMSQ
 
/* 3 */ 10
/* 2 */ 5.895449469587931
/* 1 */ 4.19689163617453

However, be aware that interactive does not mean step-by-step. When executing macros or using any function using macros, the state of the stack will be given only after the macro is executed. Here is an example:

WS> 100 ECHOON
WS> JSONSTACK
WS> []
 
/* 1 */ []
 
WS<1> 1 10
 
/* 3 */ []
/* 2 */ 1
/* 1 */ 10
 
WS<3> <% +! %>
 
/* 4 */ []
/* 3 */ 1
/* 2 */ 10
/* 1 */ "<% +!  %>"
 
WS<4> FOR
 
/* 1 */ [1,2,3,4,5,6,7,8,9,10]

Conclusion

If you’re new to WarpScript, this is a good way to understand how it works. You can really visualize how each function modifies the stack. If you’re already a WarpScript veteran the interactive mode can help you explore and debug your scripts. You can also develop your own tools to leverage the capabilities offered by the interactive endpoint.

Share