|
Introduction to the REPL and Interrupting a Running Program |
You can open the Sheerpower console in several ways:
by starting SPDEV, using Visual Studio Code,
or typing sp4gl at the Windows command prompt.
This launches the Sheerpower console, which functions like a
REPL (Read—Eval—Print Loop) found in many other programming
languages.
A REPL is an interactive programming environment where you enter code,
have it evaluated immediately, and see the results printed right away.
In Sheerpower, you can also enter multiple statements on a single line by
separating them with a backslash (\).
Each section below opens with the idea; click a Show me line for the keystrokes and examples, or Expand all.
There are three ways to see a value at the prompt. print expr is
the full statement; as a shorthand, a line that begins with = prints
the expression, and so does simply typing the expression on its own line:
To inspect a variable — its type and its value
— begin the line with ?. Sheerpower prints the variable's
typeof$ description, then its value:
? works on any variable: a scalar, an array (you see its bounds and
size, then the elements), or a cluster (you see *Cluster* and its
row count).
Press TAB and Sheerpower completes what you are typing, aware of the grammar at that point in the statement. When one completion fits it is filled in for you; when several fit, a Choices menu opens; a misspelling is corrected to the closest match.
It completes:
print you are offered at, using, the
screen colors, then the functions);-> — type
cities-> then TAB to list city$,
country$, …
Type keys at the prompt and the console prints a one-screen
reference of every special keystroke it understands, grouped by purpose —
Editing the line, Completing,
Remembering, Running, and the
Console words themselves. Everything covered on this page is
listed there — TAB completion, your abbreviations, Ctrl/R
history search, Ctrl/S starring, ALT/B to halt
a running program — so it is the quickest reminder when you forget a key,
without leaving what you are doing.
Type help at the prompt to open the getting-started page —
this tutorial — in your browser, so the language reference and the worked
examples are always one word away. Both keys and help
are named in the console's opening banner (Type KEYS or HELP for
assistance), so you never have to remember where to find them.
Type an abbreviation with its dot and press Enter: its expansion appears on the next line, already typed, with the cursor at the end. Press Enter again to run it — the same keystroke twice — or edit it first, or press Ctrl/U to erase it and walk away.
TAB expands an abbreviation too — in place, so it works in the middle of a line as well, and pressing TAB after each one lets you expand several abbreviations into a single line as you type it.
The built-in shorthand:
ot. open table | in. include | r. routine |
of. open file | ex. exclude | er. end routine |
et. extract table | sb. sort by | pr. private routine |
ee. end extract | sd. sort descending by | we. when exception in |
st. set table | p. print | u. use |
cc. collect cluster | pc. print # | ew. end when |
ec. end collect | pa. print at | sc. select case |
ac. add cluster | pe. print _extracted | ce. case else |
ea. end add | li. line input | es. end select |
fe. for each | lm. line input menu | ei. end if ed. end do |
ne. next | ls. line input screen | xd. xf. xr. exit do / for / routine |
e. edit | l. list |
You do not have to remember them: a lone . and Enter (or TAB)
pops up an Abbreviations menu of every one (your own first,
then these);
pick one and it lands on the line. o. and TAB narrows the menu to
the ones starting with o, and when only one fits it simply
expands. keys prints every keystroke and console word on this page,
in colour.
Define an abbreviation right at the prompt — a name ending in a dot, an equals sign, and what it stands for. Nothing is echoed; it is in effect at once and kept for good.
A starred abbreviation is listed first in the .-TAB
menu, marked with a *: define it as *t. = run 'sptests:sptest',
or star an existing one with *t. (the same again un-stars it), or
press Ctrl/S with just t. on the line. Your own
abbreviations take precedence over the built-in ones, so ot. can be
made to mean whatever you like.
An abbreviation can leave holes to fill in. Write a hole as
{name}. When you expand
the abbreviation, the first hole is selected — shown in reverse video —
and whatever you type replaces it. Tab moves to the next hole,
Shift/Tab back, and Enter moves to the next hole
until none are left, then runs the line. Ctrl/U erases the whole
thing if you change your mind.
Two touches make this fast. A hole's name matters: every hole
with the same name fills together, live, as you type — type idx
once and all three {var} become idx. And a hole may carry
a default, written like a named option, {name: value}:
tab past it to accept the default, or type to replace it.
The built-in block abbreviations already use holes, so et.,
cc., fe., sc., we.,
do. and fo. drop in a ready-to-fill skeleton. Because the
console runs one line at a time, a multi-line skeleton lands as a single line joined
by \ (Sheerpower's statement separator) — it runs exactly as
the block would.
To change an abbreviation, just define it again with the same name — the new expansion replaces the old one. There is no separate edit step, and no need to remove it first.
Defining one at a time at the prompt is convenient for a quick abbreviation. Once
you have a handful you care about — and especially the complex, hole-filled
ones you do not want to retype — edit them in bulk in the
file where they live: the [abbreviations] section of
sheerpower:sp4gl_cmds.txt (see
The Console's Memory, below). Each abbreviation is one line,
name. what it expands to, exactly as you would type the definition;
a hole is just {name} in that line, so a whole block skeleton is one editable line.
A // line is
a comment — use them to group and label your abbreviations.
The console rewrites that file for you whenever you star something or run a console command, so your hand edits and your prompt-defined ones live together, survive between sessions, and travel with the file if you copy it to another machine.
The Up and Down arrow keys walk back and forth through the commands you have already typed, so you can rerun or edit one without retyping it. The console keeps the last 500, and carries them all over to your next session, so tomorrow starts with today's commands already there.
When the command you want is further back, type the first few characters and press Ctrl/R. A pop-up lists every earlier command that starts with what you typed (case does not matter), newest first — the starred ones first of all, above a rule. With nothing typed it lists them all. Pick one and it lands on the line, ready to run or edit; ESC leaves the line as it was.
A command you use all the time can be starred: with it on the
line, press Ctrl/S and the console answers Starred.
From then on it heads the Ctrl/R list, in this session and every later one. To
un-star it, recall it onto the line and press Ctrl/S again: Un-starred
— it drops back into the plain history where its age puts it. Up to twenty
commands can be starred.
Everything above lives in one plain-text file next to sp4gl.exe:
sheerpower:sp4gl_cmds.txt. The console names it, with its counts, on
the line after the banner:
The file has two sections, and you may edit it by hand:
// sp4gl_cmds.txt -- the console's memory. [history] is the last 500 commands, oldest // first; a line starting "* " is a starred command (Ctrl/S) and is kept for good. // [abbreviations] is yours to edit. [abbreviations] // name. what it expands to (the dot is optional; TAB adds a space after the expansion; // a * in front = starred: listed first) * t. run 'sptests:sptest' pt. print typeof$(x) [history] * print cities->city$ print 5+10 print 'hi'
[abbreviations] (an older file's [shortcuts] header is still read, and the next save renames it): one per line as name. what it expands to
— the dot is assumed if you leave it out, a leading * stars it,
a line starting // is a comment. The expansion is taken as written;
TAB puts one space after it when it ends in a letter or digit.[history]: the commands, oldest first, newest at the bottom; a
* prefix marks a starred one.
The console writes the file whenever you star something, whenever you type a
console command such as run, save, edit,
build, new, old, exit or
stop, and when it exits — so it is never more than one real action
behind, even if a session ends badly. Only the section being written is touched;
your comments and ordering in the other one are kept as they are.
sp4gl /headless < transcript.txt
every menu — the Ctrl/R list, the Abbreviations list, a program's
line input menu — comes out as numbered text and reads its pick
from the next line of the transcript, and a raw Ctrl/R or Ctrl/S byte in a
transcript line means that keystroke at that point. So everything on this page
can be exercised, and pinned in a regression test, without a window.
You do not have to leave the console to write a whole program. Indent
a line (begin it with spaces) and Sheerpower collects it into a program
instead of running it immediately — and that includes whole
multi-line, multi-statement constructs: indent a
for loop, an if block, or a routine and every indented
line is added to the program, structure and all. Keep indenting to build it up;
then a bare run runs it:
And it appends as you go. Run it, try a few things at the
prompt, then indent another block — the new block is added to the
end of the program, not put in place of what was there. So a program
grows a piece at a time, in whatever order the pieces occur to you, and
edit is there when you want to reorder or remove one.
What you have built this way is a real, editable program, and the console gives you its whole life cycle:
edit — hands the program you have built to the
Sheerpower IDE for full, free-form editing, validated as you go.
When you leave the IDE, the edited program is reloaded into the console
— usually in under half a second, even for large programs —
and you are back at the >>> prompt, right where you
left off, ready to run it or keep working;save 'name' — writes it out to a file;build 'name' — compiles and deploys it as a
.sprun image;run — runs what you have written;
run 'name' runs a saved program (see the next section).
In many interactive prompts, what builds up as you type is a playground of live
values and definitions — handy for trying an idea out, but you cannot
really see it as a program, and it cannot become a finished application; you
experiment at the prompt and write the real code somewhere else. Here, what you
build up is real source: you can look at it and rearrange it
(edit), keep it (save 'name'), and turn it into a
deployable application (build 'name'). So the session is not a
throwaway scratchpad — it is the start of a real program, and
edit takes it into the full IDE when it outgrows the prompt.
A program you have saved in a file is run from the console with run
followed by the file's path in quotes. The .spsrc file type is the
default, so it can be left off; a logical name that points at a folder works
too (run 'sptests:hello'). The program is compiled first and then
run; its output appears in the console, and when it ends you are back at the
>>> prompt with every variable still available for inspection.
Say c:\work\hello.spsrc holds this small, correct program:
Because it compiles cleanly, it runs, prints its output, and leaves you at the prompt — where its variables are still set, so you can inspect them:
The run command runs your saved source (a
.spsrc file). When a program is ready to hand to other people,
you deploy it: sp4gl myprog /build (or Deploy in
SPDEV, F6 in Visual Studio Code) compiles it and writes a
.sprun file — a portable, encrypted image that runs exactly
the same but keeps your source code hidden. You run a .sprun the
same way; just include the .sprun on the name, since
.spsrc is what run assumes:
.sprun
beside a renamed copy of sp4gl.exe (for example
MyProg.exe) — running that .exe runs the
deployed program. See Building and Deploying.
When a saved program has mistakes, the same run reports them and
does not run — that is the next section.
If the program has mistakes, it does not run. Each mistake is shown as a
block: the message, an arrow to the line and column, the source
line with its line number in the gutter, a caret under the offending token, and
a labelled explanation — Help>> for what went wrong,
Format>> for the statement's correct shape and
Fix>> for what to do. The blocks are listed in line order.
Suppose c:\work\broken.spsrc holds:
Because the program did not run, the console offers to fix it: after the
errors it prints Press ENTER to edit the program. and waits at the
prompt. You read the errors, press ENTER, and Sheerpower opens
the program in the editor, ready for you to correct the lines it named. The
pause matters — it lets you read the error messages first, so you are
never thrown straight into the editor before you can see what was wrong. This
offer appears only at the interactive console — a batch or
/headless run simply reports the errors and stops.
Two things in that listing are worth knowing. The one-line
also: entry is a cascade: other(4) was never
declared because its dim line failed, so the later complaint about
it is reported as a consequence of the first error rather than as a separate
block — fix the dim line and both go away. And the count line
at the end tells you where to start: with more than a few errors it says
fix the first error first; later ones often follow from it. After ten
blocks the rest are only counted; run sp4gl broken /allerrors from
the Windows command prompt to see every one.
A program that compiles cleanly can still fail while running — a file that
is not there, a division by zero, an argument out of range. If nothing in the
program catches the exception (see Exception Handling tutorial for
when exception in ... use ... end when), the console shows a block
of the same shape: the message, the routine and line where it happened, the
failing statement, a Detail>> line with the specifics, and a
Called>> line for each level of the call stack, innermost
first. Suppose c:\work\oops.spsrc holds:
The line number is counted within the routine (SHOW_IT, line 1 is
the first statement of show_it), and the Called>>
line shows the statement in MAIN that called it. Because the
program says option abort, it ends and writes an
_error_ file that opens with this same block and goes on to list
every variable, the open files and the call stack — everything you need to
see what the program was doing. Without option abort the block is
shown and you are left at the >>> prompt inside the stopped
program, exactly as after ALT+B below, where print code
would show the 300 that caused it.
A line typed at the prompt is compiled and run on the spot, so a mistake in it is reported the same way, as a block — there is just no file and no line number, only the line you typed, the caret and the explanation:
Nothing is lost: the variables you set earlier are untouched, and the corrected line can be typed straight away.
When a Sheerpower program is running in its console window, you might encounter a situation where the program is taking too long to complete, or you suspect it's stuck in an infinite loop. For these moments, Sheerpower provides a powerful built-in debugging feature.
By clicking anywhere in the console window to give it focus and then pressing ALT+B, you can immediately halt the program's execution.
This action does more than just stop the program—it drops you directly into the Sheerpower REPL (Read—Eval—Print Loop) at the exact point where the program was interrupted. The console will display the source file and line number where the halt occurred, giving you immediate context.
print variable_name,
or the shorthand = variable_name, to see its current value.= balance * rate.variable_name = value, then go — the program
runs on with the value you set, so you can try a fix without editing and
restarting.show all to dump every
variable, array, table, and cluster, the open files, and the calling stack,
all at once.step to
run the next statement and stop again, or step 5 to run five. The
Esc key is a one-tap step; F1
steps over a routine call, and F3 steps out of the current
routine. Stepping opens a Program Trace window that logs each
statement as it runs; trace off stops it as does closing the trace window.go to let the program
proceed from where it left off.stop to end the program
completely, or simply close the window.
The names you reach this way are the program's module variables. To
read a routine's parameters or its private variables, prefix
the name with the routine's own name and a $. Given
routine do_taxes with rate, the parameter rate is
do_taxes$rate at the halted console — you can print it and
change it just like any other variable:
Instead of running straight on with go, you can walk the program
one statement at a time. Type step to run the next statement and
stop again, or step 5 to run five in a row; tapping
Esc is the same as step then Enter,
so you can rest a finger on it and watch the program advance. When the next
statement is a routine call, two function keys refine the step:
F1 steps over the call — it runs the whole
routine at full speed and stops on the line after — and F3
steps out of the routine you are in, running to its return and stopping
back in the caller. Stepping opens a Program Trace window that lists
each statement as it runs — the time, its MAIN.line
label, and the source text — so you see exactly the path the program
takes. Type trace off when you are done to close it.
\).print expr, a leading =, or just the expression prints a value; ?name shows a variable's type and value.->; the Up/Down arrows recall earlier commands.keys prints the keystroke reference; help opens this page.. and Enter lists them; pt. = ... defines your own, *pt. = ... a starred one; an abbreviation may leave {name} holes to fill in (same-named holes fill together as you type, {name: default} for a default).sheerpower:sp4gl_cmds.txt — [abbreviations] and [history] (the last 500, stars marked * and kept) — written at every Ctrl/S, console command, and exit.run / edit / save 'name' / build 'name'run 'path' compiles and runs a saved program; .spsrc is the default file type and a logical name (sptests:hello) works.option abort the same block opens the _error_ file.routine_name$variable, e.g. do_taxes$rate), run show all to dump every variable, table, cluster and the calling stack, single-step with step / step 5 (Esc is one step, F1 steps over a call, F3 steps out; a Program Trace window opens, trace off closes it), resume with go, or stop execution entirely.|
Hide Description
|
|
|
Enter or modify the code below, and then click on RUN |
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |