Diese Seite auf Deutsch

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 ​

c0de
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 FUNCTION

Building and running it ​

  1. Place the laptop, right-click it, click the buttons until they show commands and terminal, and click ✔.
  2. Set the trigger block to the same program and function and press the button.
  3. 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
quit

The 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!
the terminal of the laptop after the run: the typed commands say, sum and dance with their answers, and Bye! at the end

sum 2.5 1 and sum 12 get the same ? as sum 3 four.

How it works ​

  • split is 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 with start 0 and length space gives the word, and MID with start space + 1 and no length gives 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 split once and compares the word. There is no ELSE IF, so a third IF catches everything that is neither say nor sum nor quit.
  • say looks for creeper with 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 at found, and when it returns -1, the WHILE loop ends.
  • sum uses split again to cut its arguments into two numbers. VAL gives a FLOAT value and a BOOLEAN isInteger. "four" is no number: VAL raises INVALID_ARGUMENT, and the error variable problem on the CALL line catches it. sum 12 has no second number, and VAL of the empty text fails the same way. 2.5 is a number, but isInteger is 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 ​

  1. Accept commands in any case: SAY Hello and Sum 3 4 should work too.
Solution
c0de
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 FUNCTION

LCASE 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.

  1. Let sum add any number of numbers: sum 1 2 3 4 writes = 10.
Solution
c0de
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 FUNCTION

The 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.

Read more ​