Template:Prelude/general.a68: Difference between revisions

From Rosetta Code
Content added Content deleted
m (moved ALGOL 68/prelude/general.a68 to Template:Prelude/general.a68: To be included into code as a template.)
m (lang -> syntaxhighlight)
 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
<syntaxhighlight lang="algol68"># -*- coding: utf-8 -*- #
COMMENT

This is an ALGOL 68 prelude file called prelude/general.
COMMENT
It contains small routine that are not part of any standard,
This is an ALGOL 68 prelude file called prelude/general.
but are none the less useful and widely used.
It contains small routine that are not part of any standard,
USAGE
but are none the less useful and widely used.
PR READ "prelude/general.a68" PR
USAGE
END COMMENT
PR READ "prelude/general.a68" PR
##########################################
END COMMENT
# Define some general routines and MODES #

##########################################
##########################################
MODE UTFCHAR = STRING;
# Define some general routines and MODES #
MODE UTF=FLEX[0]UTFCHAR;
##########################################
MODE SIMPLEOUT = [0]UNION(INT, CHAR, STRING, []STRING);
PRIO MIN=8, MAX=8;
MODE MOID = VOID; # Use in cases where the OP should really return a MODE #
OP MIN = (INT a,b)INT: (a<b|a|b),
MAX = (INT a,b)INT: (a>b|a|b);
PROC raise exception = (STRING type, SIMPLEOUT argv)VOID:(
OP MIN = (REAL a,b)REAL: (a<b|a|b),
BOOL exception = FALSE;
MAX = (REAL a,b)REAL: (a>b|a|b);
putf(stand error, ($g$, "Exception"," ",type, ": ", argv, $l$));

ASSERT (exception)
MODE IUR = UNION(INT, REAL);
);
OP R = (IUR x)REAL: (x|(REAL r):r, (INT i):REAL(i));

PROC raise undefined = (SIMPLEOUT argv)VOID:
MODE IURUC = UNION(INT, REAL, COMPL);
raise exception("Undefined", argv);
OP C = (IURUC x)COMPL: (x|(COMPL c): c, (REAL r):COMPL(r), (INT i):COMPL(i));

PROC raise value error = (SIMPLEOUT argv)VOID:
MODE CUCCUS = UNION(CHAR, #[]CHAR,# STRING);
raise exception("Value Error",argv);
OP S = (CUCCUS x)STRING: (x|(STRING s):s, ([]CHAR cc): STRING(cc), (CHAR c):STRING(c));

MODE
UCHAR = STRING, LONGCHAR = UCHAR, USTRING=FLEX[0]LONGCHAR,
LBITS = UNION(SHORT SHORT BITS, SHORT BITS, BITS, LONG BITS, LONG LONG BITS),
LBYTES = UNION(SHORT SHORT BYTES, SHORT BYTES, BYTES, LONG BYTES, LONG LONG BYTES),
LINT = UNION(SHORT SHORT INT, SHORT INT, INT, LONG INT, LONG LONG INT),
LREAL = UNION(SHORT SHORT REAL, SHORT REAL, REAL, LONG REAL, LONG LONG REAL),
LCOMPL = UNION(SHORT SHORT COMPL, SHORT COMPL, COMPL, LONG COMPL, LONG LONG COMPL);
MODE SIMPLEOUT = [0]UNION(BOOL, CHAR, STRING, USTRING, LBITS, # LBYTES,# LINT, LREAL, LCOMPL);
MODE SIMPLEOUTF = [0]UNION(FORMAT, SIMPLEOUT);
MODE USIMPLEOUT = SIMPLEOUT # UNION(SIMPLEOUT, SIMPLEOUTF) #;

PRIO REPR = 8;

# Use MOID with "*:=" OPerators who's where the returned MODE is often VOIDed for convenience #
MODE MOID = VOID;

PROC raise exception = (STRING type, USIMPLEOUT argv)VOID:(
BOOL exception = FALSE;
putf(stand error, ($g$, "Exception"," ",type, ": "));
#CASE argv IN#
# (SIMPLEOUSTRING argv):putf(stand error, argv),#
# (SIMPLEOUT argv):#
putf(stand error, ($g" "$, argv))
#ESAC#;
putf(stand error, $l$);
ASSERT (exception)
);

# Python style exceptions #
PROC raise undefined = (USIMPLEOUT argv)VOID: raise exception("Undefined", argv);
PROC raise unimplemented=(USIMPLEOUT argv)VOID: raise exception("Unimplemented", argv);
PROC raise value error = (USIMPLEOUT argv)VOID: raise exception("Value Error",argv);
PROC raise index error = (USIMPLEOUT argv)VOID: raise exception("Index Error",argv);
PROC raise type error = (USIMPLEOUT argv)VOID: raise exception("Type Error",argv);

BOOL debug := FALSE, trace := FALSE;

PRIO INKEYS = 5, INVALUES = 5, NOTINKEYS = 5, NOTINVALUES = 5;

# Algol68G specific declarations #
INT match=0, no match=1, out of memory error=2, other error=3; # for RegEx searches #</syntaxhighlight>
<noinclude>{{template}}</noinclude>
<noinclude>{{template}}</noinclude>

Latest revision as of 12:40, 13 April 2024

# -*- coding: utf-8 -*- #

COMMENT
  This is an ALGOL 68 prelude file called prelude/general.
  It contains small routine that are not part of any standard, 
  but are none the less useful and widely used.
USAGE
  PR READ "prelude/general.a68" PR
END COMMENT

##########################################
# Define some general routines and MODES #
##########################################
PRIO MIN=8, MAX=8;
OP MIN = (INT a,b)INT: (a<b|a|b),
   MAX = (INT a,b)INT: (a>b|a|b);
OP MIN = (REAL a,b)REAL: (a<b|a|b),
   MAX = (REAL a,b)REAL: (a>b|a|b);

MODE IUR = UNION(INT, REAL);
OP R = (IUR x)REAL: (x|(REAL r):r, (INT i):REAL(i));

MODE IURUC = UNION(INT, REAL, COMPL);
OP C = (IURUC x)COMPL: (x|(COMPL c): c, (REAL r):COMPL(r), (INT i):COMPL(i));

MODE CUCCUS = UNION(CHAR, #[]CHAR,# STRING);
OP S = (CUCCUS x)STRING: (x|(STRING s):s, ([]CHAR cc): STRING(cc), (CHAR c):STRING(c));

MODE 
  UCHAR =  STRING, LONGCHAR = UCHAR, USTRING=FLEX[0]LONGCHAR,
  LBITS =  UNION(SHORT SHORT BITS,  SHORT BITS,  BITS,  LONG BITS,  LONG LONG BITS),
  LBYTES = UNION(SHORT SHORT BYTES, SHORT BYTES, BYTES, LONG BYTES, LONG LONG BYTES),
  LINT =   UNION(SHORT SHORT INT,   SHORT INT,   INT,   LONG INT,   LONG LONG INT),
  LREAL =  UNION(SHORT SHORT REAL,  SHORT REAL,  REAL,  LONG REAL,  LONG LONG REAL),
  LCOMPL = UNION(SHORT SHORT COMPL, SHORT COMPL, COMPL, LONG COMPL, LONG LONG COMPL);
MODE SIMPLEOUT = [0]UNION(BOOL, CHAR, STRING, USTRING, LBITS, # LBYTES,# LINT, LREAL, LCOMPL);
MODE SIMPLEOUTF = [0]UNION(FORMAT, SIMPLEOUT);
MODE USIMPLEOUT = SIMPLEOUT # UNION(SIMPLEOUT, SIMPLEOUTF) #;

PRIO REPR = 8;

# Use MOID with "*:=" OPerators who's where the returned MODE is often VOIDed for convenience #
MODE MOID = VOID;

PROC raise exception = (STRING type, USIMPLEOUT argv)VOID:(
  BOOL exception = FALSE;
  putf(stand error, ($g$, "Exception"," ",type, ": "));
  #CASE argv IN#
  #  (SIMPLEOUSTRING argv):putf(stand error, argv),#
  #  (SIMPLEOUT argv):#
        putf(stand error, ($g" "$, argv))
  #ESAC#;
  putf(stand error, $l$);
  ASSERT (exception)
);

# Python style exceptions #
PROC raise undefined =   (USIMPLEOUT argv)VOID: raise exception("Undefined", argv);
PROC raise unimplemented=(USIMPLEOUT argv)VOID: raise exception("Unimplemented", argv);
PROC raise value error = (USIMPLEOUT argv)VOID: raise exception("Value Error",argv);
PROC raise index error = (USIMPLEOUT argv)VOID: raise exception("Index Error",argv);
PROC raise type error =  (USIMPLEOUT argv)VOID: raise exception("Type Error",argv);

BOOL debug := FALSE, trace := FALSE;

PRIO INKEYS = 5, INVALUES = 5, NOTINKEYS = 5, NOTINVALUES = 5;

# Algol68G specific declarations #
INT match=0, no match=1, out of memory error=2, other error=3; # for RegEx searches #

This is a template. There are many others. See Category:RCTemplates for a complete list of templates.