Template:Prelude/general.a68: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎ALGOL_68/prelude/general.a68: useful base routines.)
 
m (lang -> syntaxhighlight)
 
(5 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 #
# Define some general routines and MODES #
##########################################
##########################################
PRIO MIN=8, MAX=8;
MODE UTFCHAR = STRING;
OP MIN = (INT a,b)INT: (a<b|a|b),
MODE UTF=FLEX[0]UTFCHAR;
MODE SIMPLEOUT = [0]UNION(INT, CHAR, STRING, []STRING);
MAX = (INT a,b)INT: (a>b|a|b);
OP MIN = (REAL a,b)REAL: (a<b|a|b),
MODE MOID = VOID; # Use in cases where the OP should really return a MODE #
MAX = (REAL a,b)REAL: (a>b|a|b);


MODE IUR = UNION(INT, REAL);
PROC raise exception = (STRING type, SIMPLEOUT argv)VOID:(
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;
BOOL exception = FALSE;
putf(stand error, ($g$, "Exception"," ",type, ": ", argv, $l$));
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)
ASSERT (exception)
);
);


# Python style exceptions #
PROC raise undefined = (SIMPLEOUT argv)VOID:
raise exception("Undefined", argv);
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 #
PROC raise value error = (SIMPLEOUT argv)VOID:
INT match=0, no match=1, out of memory error=2, other error=3; # for RegEx searches #</syntaxhighlight>
raise exception("Value Error",argv);
<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.