You can enter and run your own script, or click some buttons to run sample scripts.
In Brief
This page runs scripts in SimpleTalk, a language inspired by HyperTalk. The compiler and interpreter are implemented in JavaScript, and run in the browser.
Some of the math examples measure their own performance. My modest laptop reaches 10 megaflops. (Restart your browser to get full performance: JavaScript interpreters slow down as they “age”.) Mobile devices are slower of course. The code has also been ported to Dart, with about a 2x speed improvement running in Chromium.
In addition, SimpleTalk has some primitive syntax for manipulating the Web page directly. (The buttons.)
Try It Yourself
To run or evaluate a single line of SimpleTalk, type it in the Interactive field and hit return.
Or...
RunScriptPropertiesTokensTree
To run a script, click a button below.
To edit a button’s script, click the Script icon above, then click the button.
Short cut: shift-click the button.
To edit a button’s properties, click the Properties icon above, then click the button.
These features are mostly for internal debugging, but may be of interest.
To see a script’s tokens, click the Tokens icon above, then click the button.
To see a script’s syntax tree, click the Tree icon above, then click the button.
To create a new button, click here.
If the math checks, SimpleTalk is bug-free, right? However, these trusting scripts can be broken by malicious input. ☹
Partially sum the harmonic series, and measure flops.
Factor a number using two tiny (self-modifying) loops.
Find Greatest Common Divisor with Euclid’s algorithm.
Following tradition, SimpleTalk assigns values with “put y into x”. This may seem clumsy, but x = y is just wrong. Better to say x gets y or x←y. The utility of put is to express assignment to ranges, like
put char 1 of word 2 of x before char 4 of word 5 of line 6 of y
It takes a lot of work to do this in other languages. SimpleTalk does support this syntax: text ranges can be both r-values and l-values.
Lists are first-class values. Associated features include
A binary operator ‘~’ for list linking
split and join commands
List expressions, like the words of myString
repeat with myItem in myList...
Using an undefined value is a runtime error. Variables can be tested for being defined, and can be un-defined.
SimpleTalk predefines variables, like space and comma (what you would expect) and 2π (yes, it starts with a digit), and built-in functions like the platformFlops, which measures (roughly) the speed of the underlying JavaScript interpreter.
In a deviation from HyperTalk, the word “empty” is not a variable name. With the introduction of lists, it is ambigiuous: does it mean '' or []? It is now an adjective and you can still write if x is empty...
The words is and isnt introduce prepositions, e.g. is finite or isnt a number.
Either double or single quotes can delimit a string.
Comments are introduced by a semicolon and last until the next newline. Currently we have no multi-line comments.
The backslash character escapes a newline, so commands can continue on the next line. All characters after the backslash until the newline are ignored, so you can comment a continued line.
SimpleTalk supports Unicode. E.g. you can substitute ≤ for <=. Strings and variable names are multilingual.
Future features
Using run/script/properties/tree modes is awkward, but works better with mobile devices. A better interface will allow you to set a wide range of properties, as well as direct manipulation, e.g. dragging.
More list operations, for example
pop L into x
if x is in L then...
apply f to L
More complex ranges of text as lvalues.
put 'z' into char 4 of y// works
put 'zz' into char 4 to 5 of y// not yet
More self-ref Web-page interaction. For example, syntax (and semantics) for drawing in canvases, or calling Web services.
See Also
Some other Web pages demonstrating the capabilities of HTML 5, JavaScript and Dart.
LL(1) Parsing — A Web tool for generating a parse table and using it to construct a leftmost derivation.
Enumeration — A Web tool for finding permutations and combinations.
on run
get the platformFlops
if it > 1e7 then
put 1e6 into numTerms
else
put 1e4 into numTerms
end if
ask "How many terms?" with prompt numTerms
put response into numTerms
put the seconds into startSecs
put 0 into ∑ ; Unicode is legal for variable names.
repeat with i = 1 to numTerms
add 1/i to ∑
end repeat
put the seconds - startSecs into secs
put numTerms ÷ secs into flops
put "The sum of " & numTerms & " terms is " & ∑
put "Time: " & secs & " seconds"
put "Performance: " & flops & " flops"
put it/flops into slowdown
put "JavaScript/SimpleTalk speed: " & slowdown
end run
on run
ask "Enter a number." with prompt 10007 * 99971 ; Almost prime.
put response into N
put "Prime factors of " & N & ":"
repeat with i = 2 to N ; Should use only primes.
repeat while N mod i = 0
put i
div N by i ; Modify loop bound. div is integer division.
end repeat
end repeat
end run
on run
ask "Enter a number: "
put response into x
ask "Enter another number: "
put response into y
put x into xOrig ; So we can print them out at the end.
put y into yOrig
put 0 into repeatCount
repeat while y ≠ 0 ; Euclid’s algorithm
if x > y then
put x - y into x
else
put y - x into y
end if
add 1 to repeatCount
end repeat
put "The GCD of" && xOrig && "and" && \ Continued line.
yOrig && "after" && repeatCount && "steps is" && x
end run
on run
put the seconds into start
put 0 into sum
repeat for 10000 times
add 0 to sum ; Nothing optimized out. :)
end repeat
put the seconds - start into Δ ; Just like the Nile one.
put 'Performance:' && 10000÷Δ && 'loops/second'
end run
on run
put "put 'word1 word2 word3' into S"
put "word1 word2 word3" into S
put "char 3 of word 2 of S ➜ " & char 3 of word 2 of S && '; Extracting substrings.' & newline
put "put 'abc' into word 2 of S ; Inserting substrings."
put 'abc' into word 2 of S
put "S ➜ " & S & newline
put "put 'x' before char 4 of word 3 of S ; Preposition, compound lvalue."
put 'x' before char 4 of word 3 of S
put "S ➜ " & S & newline
put "the chars of 'abc' ➜ " & the chars of 'abc' & ' ; Shorthand for splitting.'
put "the words of 'abc def' ➜ " & the words of 'abc def'
end run
on run
put 'put [1,2,[3,4]] ~ [5,"six"] into L ; Nesting and linking.'
put [1,2,[3,4]] ~ [5,"six"] into L
put "L ➜ " & L & newline
put 'put 7 into item 1 of L ; One-based indexing.'
put 7 into item 1 of L ; Familiar syntax.
put "L ➜ " & L & newline
put 'put 8 into item 1 of item 3 of L ; Chaining indices.'
put 8 into item 1 of item 3 of L
put "L ➜ " & L & newline
put 'put 9 into item item 2 of L of L ; Nested indexing.'
put 9 into item item 2 of L of L
put "L ➜ " & L & newline
put 'split "a,b,c,d" into L ; Comma is default separator.'
split "a,b,c,d" into L
put "L ➜ " & L & newline
put "join L with ';' into S"
join L with ';' into S
put 'S ➜ "' & S & '"' & newline
put "the words of 'w1 w2 w3' ➜ " & the words of 'w1 w2 w3' & ' ; List expressions'
put "the chars of 'a bc' ➜ " & the chars of 'a bc'
end run
on run
put "put {2:3, 'a':{true:6}, 5:2} into D ; Defining a Dict."
put {2:3, 'a':{true:6}, 5:2} into D
put "D ➜ " & D & '; Int keys converted to Strings' & newline
put 'put 5 into entry 2 of D ; Familiar syntax.'
put 5 into entry 2 of D
put "D ➜ " & D & newline
put "put 7 into entry true of entry 'a' of D ; Chaining indices."
put 7 into entry true of entry 'a' of D
put "D ➜ " & D & newline
put 'put 9 into entry entry 5 of D of D ; Nested indexing.'
put 9 into entry entry 5 of D of D
put "D ➜ " & D & newline
put "the keys of D ➜ " & the keys of D
put "the values of D ➜ " & the values of D & ' ; Synonym for ‘entries’.'
end run
on run
put "put 2 into x"
put 2 into x
put "x is defined ➜ " & x is defined & newline
put "undefine x"
undefine x
put "x is defined ➜ " & x is defined & newline
put "put [1,2] into x"
put [1,2] into x
put "item 3 of x is undefined ➜ " & item 3 of x is undefined & newline
put "To fail, edit this script and uncomment the next command."
put "; put y" & newline
; put y
put "This will fail too, in a different way."
put "; put undefined into z"
; put undefined into z
end run
on run
set color of "Clear" to "magenta" ; "F0F" works too.
end run
on run
set size of "Clear" to "large" ; Any other value resets size.
end run