Skip to content

Commit 1405f98

Browse files
committed
commands: Add commands to compile text to specific types
Add commands that are similar to `Str→` but perform additional validation on their input. These commands are mostly useful to validate user `Input`. The commands include: * `Text→Algebraic` parses a single algebraic * `Text→Number` parses a single number * `Text→Integer` parses a single integer * `Text→Positive` parses a single positive (non-negative) number * `Text→Real` parses a single real number * `Text→Object` parses a single object * `Text→Expression` parses a single expression Signed-off-by: Christophe de Dinechin <christophe@dinechin.org>
1 parent e466b61 commit 1405f98

File tree

8 files changed

+424
-5
lines changed

8 files changed

+424
-5
lines changed

doc/commands/text.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,104 @@ Convert an object to its text representation.
1717

1818
Compile and evaluate the text, as if it was typed on the command line.
1919

20-
`"1 2 + 4" TEXT→` will push `3` and `4` on the stack.
20+
```rpl
21+
" 1 2 + 4 * " TEXT→
22+
@ Expecting 12
23+
```
24+
25+
## CompileToNumber
26+
27+
Compile and evaluate a text argument to get a number.
28+
If the argument is not a number, then an `Invalid input` error is generated.
29+
30+
```rpl
31+
"25.5" Text→Number
32+
@ Expecting 25.5
33+
```
34+
35+
## CompileToAlgebraic
36+
37+
Compile and evaluate a text argument to get an algebraic value.
38+
If the argument is not an algebraic value, then an `Invalid input` error is
39+
generated.
40+
41+
```rpl
42+
"[ 25.5 2 ]" Text→Algebraic
43+
@ Expecting [ 25.5 2 ]
44+
```
45+
46+
## CompileToInteger
47+
48+
Compile and evaluate a text argument to get an integer value.
49+
If the argument is not an integer, then an `Invalid input` error is generated.
50+
51+
```rpl
52+
"25" Text→Integer
53+
@ Expecting 25
54+
```
55+
56+
This command is typically used in validation code for the `Input` command. For
57+
example, the following code will only accept integers that are multiple of 3.
58+
59+
```rpl
60+
«
61+
"Enter a multiple of 3"
62+
{ 42 0 « Text→Integer → x « if x 3 mod 0 = then x end » » }
63+
INPUT
64+
»
65+
```
66+
67+
## CompileToPositive
68+
69+
Compile and evaluate a text argument to get a positive integer.
70+
If the argument is not a positive integer, then an `Invalid input` error is
71+
generated.
72+
73+
```rpl
74+
"25" Text→Positive
75+
@ Expecting 25
76+
```
77+
78+
## CompileToReal
79+
80+
Compile and evaluate a text argument to get an real number, which includes
81+
integers, fractions and decimal values.
82+
If the argument is not a real number, then an `Invalid input` error is
83+
generated.
84+
85+
```rpl
86+
"25/3" Text→Real
87+
@ Expecting 8 ¹/₃
88+
```
89+
90+
## CompileToObject
91+
92+
Compile and evaluate a text argument to get a single object.
93+
If the argument is not a single object, then an `Invalid input` error is
94+
generated.
95+
96+
```rpl
97+
"{ 1 2 3 }" Text→Object
98+
@ Expecting { 1 2 3 }
99+
```
100+
101+
This command is typically used for `Input` validation. The HP48-compatible
102+
approach suggested the use of `Compile`, which made it difficult to prevent
103+
users from inputing values that would have bad side effects, e.g. placing
104+
additional values on the stack or changing global variables.
105+
106+
## CompileToExpression
107+
108+
Compile and evaluate a text argument to get an expression.
109+
If the argument is not an expression, then an `Invalid input` error is
110+
generated.
111+
112+
```rpl
113+
"2+3*ABC" Text→Expression
114+
@ Expecting '3·ABC+2'
115+
```
116+
117+
This command is typically used for `Input` validation.
21118

22119
## Char→Code
23120

doc/commands/ui.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,31 @@ In the more general case, the validation program is a program that drops a value
120120
on the stack if successful. The input is only accepted after the program pushes
121121
a single value on the stack.
122122

123+
For example, the following program lets the user enter a number, with a default
124+
value of `42`, and then adds one to it:
123125

126+
```rpl
127+
«
128+
"Enter number" { 42 0 number } INPUT
129+
1 +
130+
"Next number" →Tag
131+
»
132+
```
133+
134+
The program as written above will reject non-number input values.
135+
136+
The program below shows how to use custom validation. It will only
137+
accept an input value that is a multiple of 42.
138+
139+
```rpl
140+
«
141+
"Enter multiple of 42"
142+
{ 42 0 « Text→Integer → x « if x 42 mod 0 = then x end » » }
143+
INPUT
144+
1 +
145+
"Next number" →Tag
146+
»
147+
```
124148

125149
## KEYEVAL
126150
Simulate a keypress from within a program

help/db48x.md

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12729,7 +12729,104 @@ Convert an object to its text representation.
1272912729

1273012730
Compile and evaluate the text, as if it was typed on the command line.
1273112731

12732-
`"1 2 + 4" TEXT→` will push `3` and `4` on the stack.
12732+
```rpl
12733+
" 1 2 + 4 * " TEXT→
12734+
@ Expecting 12
12735+
```
12736+
12737+
## CompileToNumber
12738+
12739+
Compile and evaluate a text argument to get a number.
12740+
If the argument is not a number, then an `Invalid input` error is generated.
12741+
12742+
```rpl
12743+
"25.5" Text→Number
12744+
@ Expecting 25.5
12745+
```
12746+
12747+
## CompileToAlgebraic
12748+
12749+
Compile and evaluate a text argument to get an algebraic value.
12750+
If the argument is not an algebraic value, then an `Invalid input` error is
12751+
generated.
12752+
12753+
```rpl
12754+
"[ 25.5 2 ]" Text→Algebraic
12755+
@ Expecting [ 25.5 2 ]
12756+
```
12757+
12758+
## CompileToInteger
12759+
12760+
Compile and evaluate a text argument to get an integer value.
12761+
If the argument is not an integer, then an `Invalid input` error is generated.
12762+
12763+
```rpl
12764+
"25" Text→Integer
12765+
@ Expecting 25
12766+
```
12767+
12768+
This command is typically used in validation code for the `Input` command. For
12769+
example, the following code will only accept integers that are multiple of 3.
12770+
12771+
```rpl
12772+
«
12773+
"Enter a multiple of 3"
12774+
{ 42 0 « Text→Integer → x « if x 3 mod 0 = then x end » » }
12775+
INPUT
12776+
»
12777+
```
12778+
12779+
## CompileToPositive
12780+
12781+
Compile and evaluate a text argument to get a positive integer.
12782+
If the argument is not a positive integer, then an `Invalid input` error is
12783+
generated.
12784+
12785+
```rpl
12786+
"25" Text→Positive
12787+
@ Expecting 25
12788+
```
12789+
12790+
## CompileToReal
12791+
12792+
Compile and evaluate a text argument to get an real number, which includes
12793+
integers, fractions and decimal values.
12794+
If the argument is not a real number, then an `Invalid input` error is
12795+
generated.
12796+
12797+
```rpl
12798+
"25/3" Text→Real
12799+
@ Expecting 8 ¹/₃
12800+
```
12801+
12802+
## CompileToObject
12803+
12804+
Compile and evaluate a text argument to get a single object.
12805+
If the argument is not a single object, then an `Invalid input` error is
12806+
generated.
12807+
12808+
```rpl
12809+
"{ 1 2 3 }" Text→Object
12810+
@ Expecting { 1 2 3 }
12811+
```
12812+
12813+
This command is typically used for `Input` validation. The HP48-compatible
12814+
approach suggested the use of `Compile`, which made it difficult to prevent
12815+
users from inputing values that would have bad side effects, e.g. placing
12816+
additional values on the stack or changing global variables.
12817+
12818+
## CompileToExpression
12819+
12820+
Compile and evaluate a text argument to get an expression.
12821+
If the argument is not an expression, then an `Invalid input` error is
12822+
generated.
12823+
12824+
```rpl
12825+
"2+3*ABC" Text→Expression
12826+
@ Expecting '3·ABC+2'
12827+
```
12828+
12829+
This command is typically used for `Input` validation.
1273312830

1273412831
## Char→Code
1273512832

@@ -13050,7 +13147,31 @@ In the more general case, the validation program is a program that drops a value
1305013147
on the stack if successful. The input is only accepted after the program pushes
1305113148
a single value on the stack.
1305213149

13150+
For example, the following program lets the user enter a number, with a default
13151+
value of `42`, and then adds one to it:
13152+
13153+
```rpl
13154+
«
13155+
"Enter number" { 42 0 number } INPUT
13156+
1 +
13157+
"Next number" →Tag
13158+
»
13159+
```
13160+
13161+
The program as written above will reject non-number input values.
13162+
13163+
The program below shows how to use custom validation. It will only
13164+
accept an input value that is a multiple of 42.
1305313165

13166+
```rpl
13167+
«
13168+
"Enter multiple of 42"
13169+
{ 42 0 « Text→Integer → x « if x 42 mod 0 = then x end » » }
13170+
INPUT
13171+
1 +
13172+
"Next number" →Tag
13173+
»
13174+
```
1305413175

1305513176
## KEYEVAL
1305613177
Simulate a keypress from within a program

0 commit comments

Comments
 (0)