Reading commands
A terminal on a laptop that understands commands: say hello writes hello, sum 3 4 writes 3 + 4 = 7, and quit ends it. Along the way, say hides every creeper behind stars, and a command it doesn't know gets a friendly ?.
Functions: INSTR, MID, REPLACE, VAL, STR, and FIX of c0de/math
What you need
A trigger block with a button and a laptop. The program has no parameters and no results, so the trigger block needs no items.
The idea
INPUT reads a whole line into a STRING. To understand it, the program cuts it into words: the first word is the command, the rest are its arguments. INSTR finds the first space, and MID takes the text in front of it and the text behind it.
Numbers arrive as text, too: "3" is not 3. VAL reads a number from a text and says whether it is a whole number; STR goes the other way and turns a number into a text again. A text that is no number at all makes VAL raise an error, which the program catches to answer with a ? instead of crashing.
The program
PROGRAM commands
NOOP
INCLUDE c0de/strings
INCLUDE c0de/math
NOOP
FUNCTION split
PARAMETER line STRING
RESULT word STRING
RESULT rest STRING
BEGIN
VARIABLE space INTEGER
CALL c0de/strings/INSTR
PARAMETER text line ;
PARAMETER search " " ;
RESULT result space
IF space < 0 ;
BEGIN
ASSIGN word line ;
RETURN
END IF
CALL c0de/strings/MID
PARAMETER text line ;
PARAMETER start 0 ;
PARAMETER length space ;
RESULT result word
CALL c0de/strings/MID
PARAMETER text line ;
PARAMETER start space + 1 ;
RESULT result rest
END FUNCTION
NOOP
FUNCTION say
PARAMETER text STRING
BEGIN
VARIABLE found INTEGER
CALL c0de/strings/INSTR
PARAMETER text text ;
PARAMETER search "creeper" ;
RESULT result found
WHILE found ≥ 0 ;
BEGIN
CALL c0de/strings/REPLACE
PARAMETER text text ;
PARAMETER start found ;
PARAMETER insert "*******" ;
RESULT result text
CALL c0de/strings/INSTR
PARAMETER text text ;
PARAMETER search "creeper" ;
PARAMETER start found ;
RESULT result found
END WHILE
CALL PRINT
PARAMETER value text ;
END FUNCTION
NOOP
FUNCTION sum
PARAMETER numbers STRING
BEGIN
VARIABLE first STRING
VARIABLE second STRING
VARIABLE a FLOAT
VARIABLE b FLOAT
VARIABLE wholeA BOOLEAN
VARIABLE wholeB BOOLEAN
VARIABLE total INTEGER
VARIABLE answer STRING
VARIABLE problem ERROR
CALL split
PARAMETER line numbers ;
RESULT word first
RESULT rest second
CALL c0de/strings/VAL problem
PARAMETER text first ;
RESULT value a
RESULT isInteger wholeA
CALL c0de/strings/VAL problem
PARAMETER text second ;
RESULT value b
RESULT isInteger wholeB
IF problem.code ≠ 0 OR NOT ( wholeA AND wholeB ) ;
BEGIN
CALL PRINT
PARAMETER value "? sum needs two whole numbers" ;
RETURN
END IF
CALL c0de/math/FIX
PARAMETER value a + b ;
RESULT result total
CALL c0de/strings/STR
PARAMETER value total ;
RESULT result answer
CALL PRINT
PARAMETER value first ⊕ " + " ⊕ second ⊕ " = " ⊕ answer ;
END FUNCTION
NOOP
FUNCTION terminal
BEGIN
VARIABLE line STRING
VARIABLE word STRING
VARIABLE rest STRING
CALL CLS
CALL PRINT
PARAMETER value "Commands: say <text>, sum <a> <b>, quit" ;
REPEAT
BEGIN
CALL INPUT
PARAMETER variable line ;
CALL split
PARAMETER line line ;
RESULT word word
RESULT rest rest
IF word = "say" ;
BEGIN
CALL say
PARAMETER text rest ;
END IF
IF word = "sum" ;
BEGIN
CALL sum
PARAMETER numbers rest ;
END IF
IF word ≠ "say" AND word ≠ "sum" AND word ≠ "quit" ;
BEGIN
CALL PRINT
PARAMETER value "? unknown command: " ⊕ word ;
END IF
END REPEAT word ≠ "quit" ;
CALL PRINT
PARAMETER value "Bye!" ;
END FUNCTIONBuilding and running it
- Place the laptop, right-click it, click the buttons until they show
commandsandterminal, and click ✔. - Set the trigger block to the same program and function and press the button.
- Open the laptop. Below the screen, the input line shows
INPUT line (STRING):. Type the commands one by one and press Enter after each:
say hello
say a creeper and another creeper
sum 3 4
sum 3 four
dance
quitThe screen shows each line you typed and the answer below it:
Commands: say <text>, sum <a> <b>, quit
say hello
hello
say a creeper and another creeper
a ******* and another *******
sum 3 4
3 + 4 = 7
sum 3 four
? sum needs two whole numbers
dance
? unknown command: dance
quit
Bye!
sum 2.5 1 and sum 12 get the same ? as sum 3 four.
How it works
splitis the heart of the program. INSTR returns the position of the first space, or -1 if there is none – then the whole line is the word. Otherwise MID withstart0 andlength spacegives the word, and MID withstart space + 1and nolengthgives the rest of the line. Positions start at 0, so the position of the space is also the length of the word in front of it.- The main loop calls
splitonce and compares the word. There is no ELSE IF, so a third IF catches everything that is neithersaynorsumnorquit. saylooks forcreeperwith INSTR. REPLACE overwrites the 7 characters at that position with 7 stars; it never changes the length of a text, so the positions after it stay the same. The next INSTR starts the search atfound, and when it returns -1, the WHILE loop ends.sumusessplitagain to cut its arguments into two numbers. VAL gives a FLOATvalueand a BOOLEANisInteger."four"is no number: VAL raises INVALID_ARGUMENT, and the error variableproblemon the CALL line catches it.sum 12has no second number, and VAL of the empty text fails the same way.2.5is a number, butisIntegeris false.- VAL always gives a FLOAT, so FIX of c0de/math turns the sum into an INTEGER. STR then makes it a text – the same text that
⊕would make, but in a variable of its own, which you can measure with LEN, cut with LEFT or pass to any function that wants a STRING.
Try it yourself
- Accept commands in any case:
SAY HelloandSum 3 4should work too.
Solution
PROGRAM commandscase
NOOP
INCLUDE c0de/strings
INCLUDE c0de/math
NOOP
FUNCTION split
PARAMETER line STRING
RESULT word STRING
RESULT rest STRING
BEGIN
VARIABLE space INTEGER
CALL c0de/strings/INSTR
PARAMETER text line ;
PARAMETER search " " ;
RESULT result space
IF space < 0 ;
BEGIN
ASSIGN word line ;
RETURN
END IF
CALL c0de/strings/MID
PARAMETER text line ;
PARAMETER start 0 ;
PARAMETER length space ;
RESULT result word
CALL c0de/strings/MID
PARAMETER text line ;
PARAMETER start space + 1 ;
RESULT result rest
END FUNCTION
NOOP
FUNCTION say
PARAMETER text STRING
BEGIN
VARIABLE found INTEGER
CALL c0de/strings/INSTR
PARAMETER text text ;
PARAMETER search "creeper" ;
RESULT result found
WHILE found ≥ 0 ;
BEGIN
CALL c0de/strings/REPLACE
PARAMETER text text ;
PARAMETER start found ;
PARAMETER insert "*******" ;
RESULT result text
CALL c0de/strings/INSTR
PARAMETER text text ;
PARAMETER search "creeper" ;
PARAMETER start found ;
RESULT result found
END WHILE
CALL PRINT
PARAMETER value text ;
END FUNCTION
NOOP
FUNCTION sum
PARAMETER numbers STRING
BEGIN
VARIABLE first STRING
VARIABLE second STRING
VARIABLE a FLOAT
VARIABLE b FLOAT
VARIABLE wholeA BOOLEAN
VARIABLE wholeB BOOLEAN
VARIABLE total INTEGER
VARIABLE answer STRING
VARIABLE problem ERROR
CALL split
PARAMETER line numbers ;
RESULT word first
RESULT rest second
CALL c0de/strings/VAL problem
PARAMETER text first ;
RESULT value a
RESULT isInteger wholeA
CALL c0de/strings/VAL problem
PARAMETER text second ;
RESULT value b
RESULT isInteger wholeB
IF problem.code ≠ 0 OR NOT ( wholeA AND wholeB ) ;
BEGIN
CALL PRINT
PARAMETER value "? sum needs two whole numbers" ;
RETURN
END IF
CALL c0de/math/FIX
PARAMETER value a + b ;
RESULT result total
CALL c0de/strings/STR
PARAMETER value total ;
RESULT result answer
CALL PRINT
PARAMETER value first ⊕ " + " ⊕ second ⊕ " = " ⊕ answer ;
END FUNCTION
NOOP
FUNCTION terminal
BEGIN
VARIABLE line STRING
VARIABLE word STRING
VARIABLE rest STRING
CALL CLS
CALL PRINT
PARAMETER value "Commands: say <text>, sum <a> <b>, quit" ;
REPEAT
BEGIN
CALL INPUT
PARAMETER variable line ;
CALL split
PARAMETER line line ;
RESULT word word
RESULT rest rest
CALL c0de/strings/LCASE
PARAMETER text word ;
RESULT result word
IF word = "say" ;
BEGIN
CALL say
PARAMETER text rest ;
END IF
IF word = "sum" ;
BEGIN
CALL sum
PARAMETER numbers rest ;
END IF
IF word ≠ "say" AND word ≠ "sum" AND word ≠ "quit" ;
BEGIN
CALL PRINT
PARAMETER value "? unknown command: " ⊕ word ;
END IF
END REPEAT word ≠ "quit" ;
CALL PRINT
PARAMETER value "Bye!" ;
END FUNCTIONLCASE of the name board turns the word into small letters right after split. The rest of the line keeps its case, so SAY Hello writes Hello.
- Let
sumadd any number of numbers:sum 1 2 3 4writes= 10.
Solution
PROGRAM commandssum
NOOP
INCLUDE c0de/strings
INCLUDE c0de/math
NOOP
FUNCTION split
PARAMETER line STRING
RESULT word STRING
RESULT rest STRING
BEGIN
VARIABLE space INTEGER
CALL c0de/strings/INSTR
PARAMETER text line ;
PARAMETER search " " ;
RESULT result space
IF space < 0 ;
BEGIN
ASSIGN word line ;
RETURN
END IF
CALL c0de/strings/MID
PARAMETER text line ;
PARAMETER start 0 ;
PARAMETER length space ;
RESULT result word
CALL c0de/strings/MID
PARAMETER text line ;
PARAMETER start space + 1 ;
RESULT result rest
END FUNCTION
NOOP
FUNCTION say
PARAMETER text STRING
BEGIN
VARIABLE found INTEGER
CALL c0de/strings/INSTR
PARAMETER text text ;
PARAMETER search "creeper" ;
RESULT result found
WHILE found ≥ 0 ;
BEGIN
CALL c0de/strings/REPLACE
PARAMETER text text ;
PARAMETER start found ;
PARAMETER insert "*******" ;
RESULT result text
CALL c0de/strings/INSTR
PARAMETER text text ;
PARAMETER search "creeper" ;
PARAMETER start found ;
RESULT result found
END WHILE
CALL PRINT
PARAMETER value text ;
END FUNCTION
NOOP
FUNCTION sum
PARAMETER numbers STRING
BEGIN
VARIABLE number STRING
VARIABLE value FLOAT
VARIABLE whole BOOLEAN
VARIABLE total FLOAT
VARIABLE answer INTEGER
VARIABLE problem ERROR
WHILE numbers ≠ "" ;
BEGIN
CALL split
PARAMETER line numbers ;
RESULT word number
RESULT rest numbers
CALL c0de/strings/VAL problem
PARAMETER text number ;
RESULT value value
RESULT isInteger whole
IF problem.code ≠ 0 OR NOT whole ;
BEGIN
CALL PRINT
PARAMETER value "? not a whole number: " ⊕ number ;
RETURN
END IF
ASSIGN total total + value ;
END WHILE
CALL c0de/math/FIX
PARAMETER value total ;
RESULT result answer
CALL PRINT
PARAMETER value "= " ⊕ answer ;
END FUNCTION
NOOP
FUNCTION terminal
BEGIN
VARIABLE line STRING
VARIABLE word STRING
VARIABLE rest STRING
CALL CLS
CALL PRINT
PARAMETER value "Commands: say <text>, sum <numbers>, quit" ;
REPEAT
BEGIN
CALL INPUT
PARAMETER variable line ;
CALL split
PARAMETER line line ;
RESULT word word
RESULT rest rest
IF word = "say" ;
BEGIN
CALL say
PARAMETER text rest ;
END IF
IF word = "sum" ;
BEGIN
CALL sum
PARAMETER numbers rest ;
END IF
IF word ≠ "say" AND word ≠ "sum" AND word ≠ "quit" ;
BEGIN
CALL PRINT
PARAMETER value "? unknown command: " ⊕ word ;
END IF
END REPEAT word ≠ "quit" ;
CALL PRINT
PARAMETER value "Bye!" ;
END FUNCTIONThe loop splits off one number after the other until numbers is empty: split puts the first number into number and the rest back into numbers. sum 1 x 3 answers ? not a whole number: x.