Range expansion: Difference between revisions

Rewrote XPL0 example.
m (syntax highlighting fixup automation)
(Rewrote XPL0 example.)
Line 4,533:
 
=={{header|XPL0}}==
See Range Extraction for explanations.
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
<syntaxhighlight lang "XPL0">proc Expand(Range);
string 0; \use zero-terminated strings, instead of MSb
char StrRange;
int CharLo, InxHi, N;
[Text(8, Range);
 
loop [N0Lo:= GetNumIntIn(8);
 
proc GetCh; BackUp; \Getget characterwhat fromterminated Strnumber
case CharChIn(8) of
[Char:= Str(Inx);
^-: [GetChHi:= IntIn(8);
Inx:= Inx+1;
for IN:= N0+1Lo to N1Hi do \expand range
]; \GetCh
[IntOut(0,N0 N);
 
if N < Hi [ChOutthen Text(0,^ ",); IntOut(0,I")];
 
func GetNum; \Get number from Str and return its value];
N1:= GetNumBackUp;
int Neg, Num;
if CharChIn(8) =^, then$1A [GetCh;\EOF\ ChOut(0,^,)] elsethen quit];
[Neg:= false;
if Char = ^- then [Neg:= true; GetCh ];
^,: [GetCh; ChOutIntOut(0,^, Lo)];
Num:= 0;
other \EOF\ [IntOut(0, Lo); quit];
while Char>=^0 & Char<=^9 do
[Num:=Text(0, Num*10", + Char-^0");
GetCh;
];
return if Neg then -Num else Num;
]; \GetNum
 
 
int I, N0, N1;
[Str:= "-6,-3--1,3-5,7-11,14,15,17-20";
Inx:= 0;
GetCh; \one character look ahead
loop [N0:= GetNum;
IntOut(0,N0);
case Char of
^,: [GetCh; ChOut(0,^,)];
^-: [GetCh;
N1:= GetNum;
for I:= N0+1 to N1 do \expand range
[ChOut(0,^,); IntOut(0,I)];
if Char=^, then [GetCh; ChOut(0,^,)] else quit]
other quit; \must be 0 string terminator
];
];
CrLf(0);
]</syntaxhighlight>
 
[Str:= Expand("-6,-3--1,3-5,7-11,14,15,17-20";)
]</syntaxhighlight>
{{out}}
<pre>
-6, -3, -2, -1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20</pre>
</pre>
 
=={{header|Yabasic}}==
295

edits