Originally published on: Sat, 4 Feb, 2012.
Something that had been missing from my first Android device was a REPL ( Read-Evaluate-Print-Loop ) for a utility programming-language. I know that the Android Scripting Environment is available, but I opted for something of my own creation.
I wrote a PhoneGap application that exposes an HTML TEXTAREA and a few buttons. One of the buttons is labeled "Execute". Clicking this button evaluates the text in the TEXTAREA block as JavaScript using the Android browser.
I named my creation "ProtoTerp".
ProtoTerp is available in the Android Market at this location:
https://market.android.com/details?id=com.lawlessguy
Technically, ProtoTerp is not a full REPL as the letter "P" in the word stands for an automatic printing of the evaluated expression. ProtoTerp requires one to use the alert(); function or another output mechanism to see the results of the evaluated JavaScript code.
I had originally intended to use ProtoTerp simply for calculations. Who needs a tip-calculator app when you can enter:
...into a REPL and see the result immediately?
ProtoTerp was essentially going to be my counterpart to the interpreted BASIC's that graced home computers decades ago:
As a side-effect of using the PhoneGap framework ( http://phonegap.com/), the entire PhoneGap API is exposed to the ProtoTerp JavaScript interpreter. One can enter:
To see an alert box with the internal device name used by your android device.
One can cause their Android device to issue a beep by entering:
The PhoneGap API exposes many other internal niceties of the Android platform including the accelerometer, SQLite database, the file system, and file upload via HTTP. A complete list of the API functions can be found here: http://docs.phonegap.com/en/1.4.1/index.html
Please note that not all devices support all functions.
Since the application is essentially a web application, one can view the source code by issuing the following:
That's the cool thing about JavaScript hosted inside the browser; the user can change anything they want about the hosted system, internal functions, ...etc. just by applying some code.
You'll note that in the comments, I added the blurb "use at your own risk". I'll probably reword that in later releases to explain that using things like the SQLite API or accessing your contacts might cause you to muck up your own data. If you do that, it's on you.
In addition to the Execute button, I added a button with the label Net Load. By pressing this button, one can load a JavaScript document from a web site. For instance, one could enter:
http://www.mailsend-online.com/test.js
In the prompt box and the resulting code would appear in the TEXTAREA:
This was a convenient mechanism to allow me to quickly tinker with code by editing and publishing sample code from my main computer while testing it directly from my Android device.
If you look at the function pt.doNetLoad()
You'll note that I use the XMLHttpRequest object to load the remote script. Since Android is serving up ProtoTerp's content via the file-system, requests aren't bound by same-origin policy; you can use the XMLHttpRequest object to issue requests to any site.
I had meant to include buttons to allow one to save and re-load blocks of JavaScript text in the initial release, but I am still on the fence about the particular approach that I want to use. I would like to try and add those features soon.
I hope that you find ProtoTerp to be either useful or a nice toy. I also hope that it would be the gateway to programming, for some people. There's a big push in 2012 for getting non-programmers to learn how to code. I would be very happy if people learn a thing or two about coding from tinkering with the ProtoTerp REPL.
Unless otherwise noted, all text is Copyright ©2012 by James K. Lawless.
Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks

Subscribe to this blog!

Click **here**
A Forthcoming Marvel Movie Villain
Uninstalling Problematic Windows Software
A JavaScript REPL for Android Devices
A Review of Kevin Mitnick's Book Ghost in the Wires
Preventing Windows Screen-Saver Activation
Understanding TRS-80 CMD Files
Changing the C64 Text Color in C
Stacking Images with PerlMagick
Screen Capture from Multiple Monitors in Java
Switching a Console Window to Full Screen or Windowed Mode
Why Some Web Sites will go Dark on Jan 18th
Book Review : Paull Allen - Idea Man
A 90's Experiment in Online Systems - The U.S. West CommunityLink Service