Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
131
Task/Metaprogramming/ALGOL-68/metaprogramming.alg
Normal file
131
Task/Metaprogramming/ALGOL-68/metaprogramming.alg
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
# This example uses ALGOL 68 user defined operators to add a COBOL-style #
|
||||
# "INSPECT statement" to ALGOL 68 #
|
||||
# #
|
||||
# The (partial) syntax of the COBOL INSPECT is: #
|
||||
# INSPECT string-variable REPLACING ALL string BY string #
|
||||
# or INSPECT string-variable REPLACING LEADING string BY string #
|
||||
# or INSPECT string-variable REPLACING FIRST string BY string #
|
||||
# #
|
||||
# Because "BY" is a reserved bold word in ALGOL 68, we use "WITH" instead #
|
||||
# #
|
||||
# We define unary operators INSPECT, ALL, LEADING and FIRST #
|
||||
# and binary operators REPLACING and WITH #
|
||||
# We choose the priorities of REPLACING and WITH so that parenthesis is not #
|
||||
# needed to ensure the correct interpretation of the "statement" #
|
||||
# #
|
||||
# We also provide a unary DISPLAY operator for a partial COBOL DISPLAY #
|
||||
# statement #
|
||||
|
||||
# INSPECTEE is returned by the INSPECT unary operator #
|
||||
MODE INSPECTEE = STRUCT( REF STRING item, INT option );
|
||||
|
||||
# INSPECTTOREPLACE is returned by the binary REPLACING operator #
|
||||
MODE INSPECTTOREPLACE
|
||||
= STRUCT( REF STRING item, INT option, STRING to replace );
|
||||
# REPLACEMENT is returned by the unary ALL, LEADING and FIRST operators #
|
||||
MODE REPLACEMENT = STRUCT( INT option, STRING replace );
|
||||
|
||||
# REPLACING option codes, these are the option values for a REPLACEMENT #
|
||||
INT replace all = 1;
|
||||
INT replace leading = 2;
|
||||
INT replace first = 3;
|
||||
|
||||
OP INSPECT = ( REF STRING s )INSPECTEE: ( s, 0 );
|
||||
OP ALL = ( STRING replace )REPLACEMENT: ( replace all, replace );
|
||||
OP LEADING = ( STRING replace )REPLACEMENT: ( replace leading, replace );
|
||||
OP FIRST = ( STRING replace )REPLACEMENT: ( replace first, replace );
|
||||
OP ALL = ( CHAR replace )REPLACEMENT: ( replace all, replace );
|
||||
OP LEADING = ( CHAR replace )REPLACEMENT: ( replace leading, replace );
|
||||
OP FIRST = ( CHAR replace )REPLACEMENT: ( replace first, replace );
|
||||
|
||||
OP REPLACING = ( INSPECTEE inspected, REPLACEMENT replace )INSPECTTOREPLACE:
|
||||
( item OF inspected
|
||||
, option OF replace
|
||||
, replace OF replace
|
||||
);
|
||||
|
||||
OP WITH = ( INSPECTTOREPLACE inspected, CHAR replace with )REF STRING:
|
||||
BEGIN
|
||||
STRING with := replace with;
|
||||
inspected WITH with
|
||||
END; # WITH #
|
||||
|
||||
OP WITH = ( INSPECTTOREPLACE inspected, STRING replace with )REF STRING:
|
||||
BEGIN
|
||||
|
||||
STRING to replace = to replace OF inspected;
|
||||
INT pos := 0;
|
||||
STRING rest := item OF inspected;
|
||||
STRING result := "";
|
||||
|
||||
IF option OF inspected = replace all
|
||||
THEN
|
||||
# replace all occurances of "to replace" with "replace with" #
|
||||
WHILE string in string( to replace, pos, rest )
|
||||
DO
|
||||
result +:= rest[ 1 : pos - 1 ] + replace with;
|
||||
rest := rest[ pos + UPB to replace : ]
|
||||
OD
|
||||
|
||||
ELIF option OF inspected = replace leading
|
||||
THEN
|
||||
# replace leading occurances of "to replace" with "replace with" #
|
||||
WHILE IF string in string( to replace, pos, rest )
|
||||
THEN
|
||||
pos = 1
|
||||
ELSE
|
||||
FALSE
|
||||
FI
|
||||
DO
|
||||
result +:= replace with;
|
||||
rest := rest[ UPB to replace : ]
|
||||
OD
|
||||
|
||||
ELIF option OF inspected = replace first
|
||||
THEN
|
||||
# replace first occurance of "to replace" with "replace with" #
|
||||
IF string in string( to replace, pos, rest )
|
||||
THEN
|
||||
result +:= rest[ 1 : pos - 1 ] + replace with;
|
||||
rest := rest[ pos + UPB to replace : ]
|
||||
FI
|
||||
|
||||
ELSE
|
||||
# unsupported replace option #
|
||||
write( ( newline, "*** unsupported INSPECT REPLACING...", newline ) );
|
||||
stop
|
||||
FI;
|
||||
|
||||
result +:= rest;
|
||||
item OF inspected := result
|
||||
END; # WITH #
|
||||
|
||||
OP DISPLAY = ( STRING s )VOID: write( ( s, newline ) );
|
||||
|
||||
|
||||
PRIO REPLACING = 2, WITH = 1;
|
||||
|
||||
|
||||
|
||||
|
||||
main: (
|
||||
|
||||
# test the INSPECT and DISPLAY "verbs" #
|
||||
|
||||
STRING text := "some text";
|
||||
DISPLAY text;
|
||||
|
||||
INSPECT text REPLACING FIRST "e" WITH "bbc";
|
||||
DISPLAY text;
|
||||
|
||||
INSPECT text REPLACING ALL "b" WITH "X";
|
||||
DISPLAY text;
|
||||
|
||||
INSPECT text REPLACING ALL "text" WITH "some";
|
||||
DISPLAY text;
|
||||
|
||||
INSPECT text REPLACING LEADING "som" WITH "k";
|
||||
DISPLAY text
|
||||
|
||||
|
||||
)
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
use MONKEY_TYPING; # Needed to do runtime augmentation of a base class.
|
||||
use MONKEY-TYPING; # Needed to do runtime augmentation of a base class.
|
||||
|
||||
augment class Any {
|
||||
method nsort { self.list.sort: {$^a.lc.subst(/(\d+)/,->$/{0~$0.chars.chr~$0},:g)~"\x0"~$^a} }
|
||||
augment class List {
|
||||
method nsort { self.list.sort: {$^a.lc.subst(/(\d+)/, -> $/ {0 ~ $0.chars.chr ~ $0 }, :g) ~ "\x0" ~ $^a} }
|
||||
};
|
||||
|
||||
say ~<a201st a32nd a3rd a144th a17th a2 a95>.nsort;
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
macro addem($a,$b) { "($a + $b)" }
|
||||
say addem(3,4); # 7
|
||||
say "Foo = $foo\n"; # normal double quotes
|
||||
say Q:qq 【Foo = $foo\n】; # a more explicit derivation, new quotes
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue