Befehle lesen
Ein Terminal auf einem Laptop, das Befehle versteht: say hello schreibt hello, sum 3 4 schreibt 3 + 4 = 7, und quit beendet es. Nebenbei versteckt say jeden creeper hinter Sternchen, und ein Befehl, den es nicht kennt, bekommt ein freundliches ?.
Funktionen: INSTR, MID, REPLACE, VAL, STR und FIX aus c0de/math
Was du brauchst
Einen Auslöserblock mit Knopf und einen Laptop. Das Programm hat keine Parameter und keine Ergebnisse, der Auslöserblock braucht also keine Karten.
Die Idee
INPUT liest eine ganze Zeile in einen STRING. Um sie zu verstehen, zerlegt das Programm sie in Wörter: Das erste Wort ist der Befehl, der Rest sind seine Argumente. INSTR findet das erste Leerzeichen, und MID nimmt den Text davor und den Text dahinter.
Auch Zahlen kommen als Text an: "3" ist nicht 3. VAL liest eine Zahl aus einem Text und sagt, ob sie ganz ist; STR geht den umgekehrten Weg und macht aus einer Zahl wieder einen Text. Ein Text, der gar keine Zahl ist, lässt VAL einen Fehler auslösen, den das Programm abfängt, um mit einem ? zu antworten, statt abzustürzen.
Das Programm
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 FUNCTIONSo baust und startest du es
- Platziere den Laptop, klicke ihn mit rechts an, klicke die Knöpfe, bis sie
commandsundterminalzeigen, und klicke auf ✔. - Stelle den Auslöserblock auf dasselbe Programm und dieselbe Funktion ein und drücke den Knopf.
- Öffne den Laptop. Unter dem Bildschirm zeigt die Eingabezeile
INPUT line (STRING):. Tippe die Befehle nacheinander ein und drücke nach jedem Enter:
say hello
say a creeper and another creeper
sum 3 4
sum 3 four
dance
quitDer Bildschirm zeigt jede Zeile, die du getippt hast, und darunter die Antwort:
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 und sum 12 bekommen dasselbe ? wie sum 3 four.
So funktioniert es
splitist das Herz des Programms. INSTR gibt die Position des ersten Leerzeichens zurück, oder -1, wenn es keins gibt – dann ist die ganze Zeile das Wort. Sonst liefert MID mitstart0 undlength spacedas Wort, und MID mitstart space + 1und ohnelengthden Rest der Zeile. Positionen beginnen bei 0, also ist die Position des Leerzeichens zugleich die Länge des Wortes davor.- Die Hauptschleife ruft
spliteinmal auf und vergleicht das Wort. Es gibt kein ELSE IF, darum fängt ein drittes IF alles ab, was wedersaynochsumnochquitist. saysucht mit INSTR nachcreeper. REPLACE überschreibt die 7 Zeichen an dieser Position mit 7 Sternchen; es ändert nie die Länge eines Textes, die Positionen dahinter bleiben also gleich. Das nächste INSTR beginnt die Suche beifound, und wenn es -1 zurückgibt, endet die WHILE-Schleife.sumzerlegt seine Argumente wieder mitsplitin zwei Zahlen. VAL liefert einen FLOATvalueund ein BOOLEANisInteger."four"ist keine Zahl: VAL löst INVALID_ARGUMENT aus, und die Fehlervariableproblemin der CALL-Zeile fängt den Fehler ab.sum 12hat keine zweite Zahl, und VAL des leeren Textes scheitert genauso.2.5ist eine Zahl, aberisIntegerist falsch.- VAL liefert immer einen FLOAT, darum macht FIX aus c0de/math die Summe zu einem INTEGER. STR macht daraus dann einen Text – denselben Text, den
⊕machen würde, aber in einer eigenen Variablen, die du mit LEN messen, mit LEFT kürzen oder an jede Funktion übergeben kannst, die einen STRING will.
Probier es selbst
- Nimm Befehle in jeder Schreibweise an: Auch
SAY HelloundSum 3 4sollen funktionieren.
Lösung
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 aus dem Namensschild macht das Wort direkt nach split klein. Der Rest der Zeile behält seine Schreibweise, SAY Hello schreibt also Hello.
- Lass
sumbeliebig viele Zahlen addieren:sum 1 2 3 4schreibt= 10.
Lösung
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 FUNCTIONDie Schleife trennt eine Zahl nach der anderen ab, bis numbers leer ist: split legt die erste Zahl in number und den Rest wieder in numbers. sum 1 x 3 antwortet ? not a whole number: x.