/*	Copyright  (c)	Günter Woigk 2010 - 2016
					mailto:kio@little-bat.de

	This file is free software

 	This program is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	• Redistributions of source code must retain the above copyright notice,
	  this list of conditions and the following disclaimer.
	• Redistributions in binary form must reproduce the above copyright notice,
	  this list of conditions and the following disclaimer in the documentation
	  and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
	THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
	CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
	OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
	WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
	OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
	ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/



//	Standard vcc header
//
//	8 bpb systems


type FD = {};

const bool yes=1,  no=0;
const bool true=1, false=0;


// ____________________________________________________________________
// System:

extern
{

// ________________________________________________
// integer:

	uint8 	msbit	(uint16);
	uint8 	msbit	(uint32);

 	int		sign	(int16);
 	int		sign	(int32);

	uint16	abs		(int16);
 	uint32	abs		(int32);

	uint16	swap_bytes	(uint16);		// revert byte order == swap halfes
	uint32	revert_bytes(uint32);		// revert byte order
 	uint32	swap_words	(uint32);		// swap halfes

 //	int16	hilo	(int16)			= opcode hilo
 //	uint16	hilo	(uint16)		= opcode hilo
 //	int32	hilo	(int32)			= opcode hilo
 //	uint32	hilo	(uint32)		= opcode hilo

 //	int16	lohi	(int16)			= opcode lohi
 //	uint16	lohi	(uint16)		= opcode lohi
 //	int32	lohi	(int32)			= opcode lohi
 //	uint32	lohi	(uint32)		= opcode lohi

 //	int 	pow		(int,int)		= opcode pow
 //	uint	pow		(uint,int)		= opcode pow
 //	long	pow		(long,int)		= opcode pow
 //	ulong 	pow   	(ulong,int)		= opcode pow

 // int16	random	(uint)			= opcode random
 //	ulong 	random	(ulong)			= opcode random

//	uint16	max		(uint16,uint16)	= opcode MAXu;		handled by compiler
//	uint16	min		(uint16,uint16)	= opcode MINu;		handled by compiler

	uint16 	random	();				// range [0 .. 0xfffe]
	uint32	mul_wwl	(uint16,uint16);

// ________________________________________________
// Arrays:


#define DEFINE(T)										\
	void	flip	(T[:])			= opcode flip;		\
	void	rol		(T[:])			= opcode rol;		\
	void	ror		(T[:])			= opcode ror;		\
	void	sort	(T[:])			= opcode sort;		\
	void	rsort	(T[:])			= opcode rsort;		\
	void	shuffle	(T[:])			= opcode shuffle;	\
	void	revert	(T[:])			= opcode revert;	\
														\
	T[]		join	(T[][],T)		= opcode join;		\
	T[]		join	(T[][],T[])		= opcode join;		\
	T[][]	split	(T[],T)			= opcode split;		\
	T[][]	split	(T[],T[])		= opcode split;		\
														\
	T		min		(T,T);			\
	T		min		(T,T,T);		\
	T		min		(T[:]);			\
	T		max		(T,T);			\
	T		max		(T,T,T);		\
	T		max		(T[:]);			\
														\
	uint	find	(T[:],T)		= opcode find;		\
	uint	find	(T[:],T[])		= opcode find;		\
	uint	rfind	(T[:],T)		= opcode rfind;		\
	uint	rfind	(T[:],T[])		= opcode rfind;		\
														\
	void	shrinktofit(T[]&);					\
	T[to]	substr	(T[],uint a,uint e);		\
	T[to]	midstr	(T[],uint i,uint n);		\
	T[to]	leftstr	(T[],uint n);				\
	T[to]	rightstr(T[],uint n);				\
									\
	T		numval	(ucs1str);		\
	T		numval	(ucs2str);		\

DEFINE(int8)
DEFINE(uint8)
DEFINE(int16)
DEFINE(uint16)
DEFINE(int32)
DEFINE(uint32)
DEFINE(ucs1char)
DEFINE(ucs2char)
#undef DEFINE


	bool	is_letter	(char);
	bool	is_bin_digit(char);
	bool	is_dec_digit(char);
	bool	is_hex_digit(char);
	uint	digit_value	(char);		// up to base 36

};//extern


// ________________________________________________
// float:

#define DEFINEf(T)	 		\
	T	round	(T)	;		\
	T	floor	(T)	;		\
	T	ceil	(T)	;		\
	T	integ	(T)	;		\
	T	fract	(T)	;		\
	int	sign	(T)	;		\
	T	abs		(T)	;		\
	T	min		(T,T);		\
	T	max		(T,T);		\
	T	min		(T,T,T);	\
	T	max		(T,T,T);	\
	T	random	()	;		\
	T	random	(T)	;		\
	T	sin		(T)	;		\
	T	cos		(T)	;		\
	T	tan		(T)	;		\
	T	asin	(T)	;		\
	T	acos	(T)	;		\
	T	atan	(T)	;		\
	T	sinh	(T)	;		\
	T	cosh	(T)	;		\
	T	tanh	(T)	;		\
	T	exp10	(T)	;		\
	T	log10	(T)	;		\
	T	exp2	(T)	;		\
	T	log2	(T)	;		\
	T	expe	(T)	;		\
	T	loge	(T)	;		\
	T	exp		(T,T);		\
	T	log		(T,T);		\
	T	pow		(T,int);	\
	T	sqrt	(T)	;		\
	str	numstr	(T)	;		\
	T	numval	(str);		\
	T	min		(T[]);		\
	T	min		(T[:]);		\
	T	max		(T[]);		\
	T	max		(T[:]);

#if __bpf != 0
DEFINEf(float64)
#endif
#if __bpsf != __bpf
DEFINEf(float32)
#endif
#if __bplf != __bpf
DEFINEf(float128)
#endif
#undef DEFINEf



// ____________________________________________________________________
// Strings:

#define DEFINE_STRINGFUNCTIONS(T)				\
												\
	T[]		spacestr	(uint);					\
	T[]		spacestr	(uint,T);				\
	T[]		charstr		(uint);					\
	T		hexchar		(uint);					\
												\
	T[]		hexstr		(uint8);				\
	T[]		hexstr		(uint16);				\
	T[]		hexstr		(uint32);				\
	T[]		hexstr		(uint, uint digits);	\
	T[]		hexstr		(ulong,uint digits);	\
												\
	T[]		binstr		(uint8);				\
	T[]		binstr		(uint16);				\
	T[]		binstr		(uint32);				\
	T[]		binstr		(uint, uint digits);	\
	T[]		binstr		(ulong,uint digits);	\
	T[]		binstr		(uint, T[] o, T[] l);	\
	T[]		binstr		(ulong,T[] o, T[] l);	\
												\
	T[]		numstr		(int);					\
	T[]		numstr		(uint);					\
	T[]		numstr		(long);					\
	T[]		numstr		(ulong);				\
												\
	T[]		toupper		(T[]);					\
	T[]		tolower		(T[]);					\
	T[]		escape		(T[], T leftquote);		\
	T[]		unescape	(T[]);					\
	T[]		urlencode	(T[]);					\
	T[]		urldecode	(T[]);					\
	T[]		entab		(T[], uint tabwidth);	\
	T[]		detab		(T[], uint tabwidth);	\
												\
	T[]		toupper		(T[:]);					\
	T[]		tolower		(T[:]);					\
	T[]		escape		(T[:], T leftquote);	\
	T[]		unescape	(T[:]);					\
	T[]		urlencode	(T[:]);					\
	T[]		urldecode	(T[:]);					\
	T[]		entab		(T[:], uint tabwidth);	\
	T[]		detab		(T[:], uint tabwidth);	\
												\
	ucs1str	toucs1		(T[]); 					\
	ucs2str	toucs2		(T[]);					\
	utf8str	toutf8		(T[]);					\
												\
	ucs1str	toucs1		(T[:]); 				\
	ucs2str	toucs2		(T[:]);					\
	utf8str	toutf8		(T[:]);					\
												\
	T[]		min			(T[],T[]);				\
	T[]¢	min			(T[][]);				\
	T[]¢	min			(T[][:]);				\
	T[]		max			(T[],T[]);				\
	T[]¢	max			(T[][]);				\
	T[]¢	max			(T[][:]);				\
												\
	void	flip		(T[][:]);				\
	void	rol			(T[][:]);				\
	void	ror			(T[][:]);				\
	void	sort		(T[][:]);				\
	void	rsort		(T[][:]);				\
	void	shuffle		(T[][:]);				\
	void	revert		(T[][:]);				\


scope ucs4str { DEFINE_STRINGFUNCTIONS(ucs4char) }
scope ucs2str { DEFINE_STRINGFUNCTIONS(ucs2char) }
scope ucs1str { DEFINE_STRINGFUNCTIONS(ucs1char) }
//scope utf8str { DEFINE_STRINGFUNCTIONS(utf8char,char)     }

#if __bpc == 8
	DEFINE_STRINGFUNCTIONS(ucs1char)
#elif __bpc == 16
	DEFINE_STRINGFUNCTIONS(ucs2char)
#elif __bpc == 32
	DEFINE_STRINGFUNCTIONS(ucs4char)
//#elif _istr_is_utf8
//	DEFINE_STRINGFUNCTIONS(utf8char,char)
#endif
#undef DEFINE_STRINGFUNCTIONS


// ____________________________________________________________________
// Time:

enum	// for timestr & timeval
	tm_utc			= 0,	// convert to/from UTC
	tm_localtime	= 1,	// convert to/from local time
	tm_time			= 2,	// include time
	tm_date			= 4,	// include date
	tm_timezone		= 8,	// include timezone
	tm_sec			= 16,	// include seconds in time
	tm_msec			= 32;	// include micro seconds in time

extern
{
	float	now		();
	str		timestr	( float seconds, uint flags );	// 2011-04-13 09:05:01'033 CEST
	float	timeval	( str¢, uint flags );
}







#if 0

type Sema	= {}
type Mutex	= {}
type Irpt	= {}
type Thread	= {}
type Tid	= Thread¢

extern int	  error;	// errno
extern Stream[] streams;
extern str[]	  env;		//	{ "NAME=VALUE", … }
extern str[]	  args;		//	cmdline args + cli args:
							//	{ "VALUE", … "NAME=VALUE", … "NAME=FILENAME", "FILEDATA", … }

const	// Math:
{
	float pi = 3.141592653589793//238462		auskomm. wg. assembler TODO
	float ec = 2.718281828459045//235360
}

#ifdef _bits_per_float
type Time = float64
#else
#ifdef _bits_per_short_float
type Time = float32
#endif
#endif

#endif


#if 0
// ____________________________________________________________________
// i/o

enum int FD =
{
	stdin,
	stdout,
	stderr,
	fd_void = -1
}

enum int16 s_type =		// filetype: matches OS enum
{
	s_none			= -1,
	s_unknown		= 0,			//	DT_UNKNOWN
	s_any			= 0,			//	used for selecting files by type
	s_pipe			= 1,			//	DT_FIFO
	s_tty			= 2,			//	DT_CHR
	s_dir			= 4,			//	DT_DIR
	s_block			= 6,			//	DT_BLK
	s_file			= 8,			//	DT_REG
	s_link			= 10,			//	DT_LNK
	s_socket		= 12,			//	DT_SOCK
	s_erased		= 14			//	DT_WHT
}

enum			// flag bits for match():
{
	fnm_pathname	= 0x02,			//	directory separators must match directly
	fnm_nohidden	= 0x04,			//	match starting dots only with explicit dots in pattern
	fnm_casefold	= 0x10			//	compare case-insensitive
}

enum			// flag bits for fullpath():
{
	f_follow_symlink = 1,			// don't deref symlink in last path component
	f_create_path	 = 2			// auto create path as you go
}

type FileInfo =
{
	str				filename;		// "file_or_subdirectory_name"
	uint16			filetype;		// mode_t: file type
	uint16			permissions;	// mode_t: permissions
	uint32			user;			// uid_t:  user id
	uint32			group;			// gid_t:  group id
#if _bytes_per_pointer==8			// TODO: alignment macros
	uint32			_dummy;
#endif
	uint64			count;			// file length or num files in dir
	Time			ctime;			// t_time last modification of meta data
	Time			mtime;			// t_time last modification of contents
	Time			atime;			// t_time last access
};

enum			// Stream.streamtype:
{
	s_packetized 	= 1<<0,			// packetized stream
	s_connected	 	= 1<<1,			// socket: connected
	s_unreliable 	= 1<<2,			// socket: unreliable datagrams
	s_listening  	= 1<<3			// socket: listening
}

type Stream =
{
	int16			fd;				// unix file descriptor
	int8/*s_type*/	filetype;		// device type
	int8			streamtype;		// SocketDomainType
	uint32			stopctls;		// stop ctls in use for async input

	str				filepath;		// open() path

	int32			out_state;		// stream state, error code from signal handler
	int32			in_state;		// stream state, error code from signal handler

	Irpt			out_irpt;		// triggered when async i/o complete
	Time			out_lastio;		// timestamp of last io
	Time			out_timeout;	// setting: timeout	(provided for caller)
	int8[]			out_data;		// current output buffer
	int8*			out_ptr;		// ptr++ in output buffer

	Irpt			in_irpt;		// triggered when async i/o complete
	Time			in_lastio;		// timestamp of last io
	Time			in_timeout;		// setting: timeout	(provided for caller)
	int8[]			in_data;		// current input buffer
	int8*			in_ptr;			// ptr++ in input buffer

	int8[]			putbackbuffer;	// putback buffer for overread data
}

enum SocketDomainType =
{
	local			= 0,
	inet			= 0x10,
	stream			= 1,
	packet			= 2,
	datagram		= 3,
	tcp				= inet+stream,
	udp				= inet+datagram
}

extern
{
	bool		match		( str¢ string, str¢ pattern, uint flags )	= opcode match
	str			fullpath	( str¢ path, uint flags )					= opcode fullpath
	s_type		nodetype	( str¢ path, bool resolve_symlink )			= opcode nodetype
	FileInfo	fileinfo	( str¢ path, bool resolve_symlink )			= opcode fileinfo
	void		renamefile	( str¢ path, str¢ newpath, bool replace )	= opcode renamefile
	void		swapfiles	( str¢ path, str¢ path2 )					= opcode swapfiles
	str			tempfile	( str¢ renamable_to_path )					= opcode tempfile
	FD			tempfile	()											= opcode tempfile
//	Stream[]¢	streams[FD]

	utf8str		readfile	( str¢ path )								= opcode readfile
	str			readlink	( str¢ path )								= opcode readlink
	FileInfo[]	readdir		( str¢ path )								= opcode readdir
	void		createfile	( str¢ path, uint8[]¢ data, uint how, uint mode )	= opcode createfile
	void		createfile	( str¢ path, utf8str¢ data, uint how, uint mode )	= opcode createfile
	void		createlink	( str¢ path, str¢ data )					= opcode createlink
	void		createdir	( str¢ path, uint mode )					= opcode createdir
	void		createpipe	( str¢ path, uint mode )					= opcode createpipe
	void		rmvfile		( str¢ path )								= opcode rmvfile	// empty dir or any other non-link
	void		rmvlink		( str¢ path )								= opcode rmvlink
	void		rmvdir		( str¢ path, bool fulltree )				= opcode rmvdir

	FD			socket		( SocketDomainType, str¢ myaddress )		= opcode socket		//	socket() + bind()
	void		connect		( FD, str¢ peeraddress )					= opcode connect	//	connect()
	FD			accept		( FD )										= opcode accept		//	listen() + accept()


	FD		open		(str¢, uint how, uint mode)	= opcode open
	FD		open		(str¢, uint how)			= opcode open	// mode = 6<<6+6<<3 = rw-rw----
	void	close		(FD)			= opcode close
	long	fpos		(FD)			= opcode fpos
	long	flen		(FD)			= opcode flen
	void	seek		(FD,ulong)		= opcode fseek
	void	truncate	(FD)			= opcode truncate

	str		errorstr	(int error)		= opcode errorstr
	str		errorstr	()				= opcode errorstr
//	int32	error

	void	print		(str)			= opcode print		// stdout
	void	print		(str¢)			= opcode print
	void	print		(int)			= opcode print
	void	print		(uint)			= opcode print
	void	print		(long)			= opcode print
	void	print		(ulong)			= opcode print
	void	putchar		(utf8char)		= opcode putbyte
	void	putchar		(ucs1char)		= opcode putchar
	void	putchar		(ucs2char)		= opcode putchar
	void	putchar		(ucs4char)		= opcode putchar
	char	getchar		()				= opcode getchar	// stdin

	void	write		(FD,int8)		= opcode write
	void	write		(FD,int16)		= opcode write
	void	write		(FD,int32)		= opcode write
	void	write		(FD,int64)		= opcode write
	void	write		(FD,uint8)		= opcode write
	void	write		(FD,uint16)		= opcode write
	void	write		(FD,uint32)		= opcode write
	void	write		(FD,uint64)		= opcode write
	void	write		(FD,str¢)		= opcode write
	void	write		(FD,str)		= opcode write
//	void	write		(FD,str¢,char)	= opcode write
//	void	write		(FD,str,char)	= opcode write

	int8	read		(FD)			= opcode read
	int16	read		(FD)			= opcode read		// native byte order
	int32	read		(FD)			= opcode read
	int64	read		(FD)			= opcode read
	uint8	read		(FD)			= opcode read
	uint16	read		(FD)			= opcode read
	uint32	read		(FD)			= opcode read
	uint64	read		(FD)			= opcode read
	str		read		(FD)			= opcode read		// delimiters: char 0,4,10,12,13
	void	read		(FD,str¢)		= opcode read		// delimiters: char 0,4,10,12,13
	void	read		(FD,str¢,uint32)= opcode read

	bool	isfile		(str¢ path)
	bool	isdir		(str¢ path)
	bool	islink		(str¢ path)
//	str		fullpath	(str¢ path)
	FileInfo fileinfo	(str¢ path)
	void	createfile	(str¢ path,uint8[]¢ data,uint how)
	void	createfile	(str¢ path,uint8[]¢ data)
	void	createdir	(str¢ path)		// mode = 7<<6 + 7<<3 + 0<<0
	void	createpipe	(str¢ path)		// mode = 6<<6 + 6<<3 + 0<<0

	/*
	FD	fopen_r	(str¢ name)				= inline { return fopen(name,'r',0);    }
	FD	fopen_rw(str¢ name)				= inline { return fopen(name,'rw',0);   }
	FD	fopen_w	(str¢ name,int mode)	= inline { return fopen(name,'w',mode); }
	FD	fopen_a	(str¢ name,int mode)	= inline { return fopen(name,'a',mode); }
	FD	fopen_n	(str¢ name,int mode)	= inline { return fopen(name,'n',mode); }
	*/
}




// ____________________________________________________________________
// Multithreading:


enum int16 tid_state1 =				// Thread.state:				linked in:
{									// -----------------			-------------
	not_linked		= 0,
	running			= 1,			// threads running or willing	t_running
	timeshed		= 2,			// sheduled for timer			t_timeshed
	suspended		= 3				// thread is suspended			t_suspended
}
enum int16 tid_state2 =
{
	not_sheduled	= 0,
	semashed		= 1,			// waiting for semaphor			sema.waiters
	mutexshed		= 2,			// waiting for mutex			mutex.waiters
	irptshed		= 3				// waiting for interript		irpt.waiters
};

type Thread =
{
// dispatcher data:
	tid_state1	state1;				// list1: running, timeshed, suspended
	tid_state2	state2;				// list2: blocking list
	int16		prio;				// actual priority: higer is better		prio = -log2(reaktionszeit/sec)
	int16		prio_org;			// nominal priority (most times same as prio)

	Tid			next1,prev1;		// list1: running, timeshed, suspended
	Tid			next2,prev2;		// list2: blocking Sema, Mutex or Irpt

	void*		blocker;			// Sema, Mutex or Irpt for which Thread is waiting
	Time		time;				// alarm time for which Thread is sheduled
	Mutex*		mutexes;			// linked list of mutexes blocked by this Thread  =>  Mutex.next,Mutex.prev

// interpreter data:
	uint16*[]	rstack;				// return stack & context heap
	int[]		vstack;

// Thread state:
	int8*		context0;
	int8*		context1;
	uint16*		ip;					// instruction pointer
	uint16**	rp;					// return stack pointer
	int32*		vp;					// variables stack pointer
	int8*		gp;					// only t_root: global data base
	int32		_errno;
#if _bytes_per_pointer==8			// TODO: alignment macros
	int32	_dummy;
#endif
}

scope Thread
{
	Thread	kill		(Thread)				= opcode kill
//	void	run			fu(args)				= built-in
}

	Thread	new			()						= opcode new
	Thread	new			(int16 prio)			= opcode new
	Thread	new			(int16 prio, uint vsz, uint rsz) = opcode new

	Tid		self		()						= opcode self
	int		num_cores	()						= opcode num_cores
	uint	vfree		()						= opcode vfree
	uint	rfree		()						= opcode rfree
	void	freeze		(bool)					= opcode freeze

//	void	run			fu(args)				= built-in

	void	termi		(Tid)					= opcode termi		// deprecated, leaks memory
	void	suspend		(Tid)					= opcode suspend
	void	resume		(Tid)					= opcode resume

	void	termi		()						= opcode termi		// self
	void	suspend		()						= opcode suspend	// self
	void	wait		()						= opcode wait		// until next event
	void	wait		(float sec)				= opcode wait
	void	shedule		(float when)			= opcode shedule
	void	busywait	(uint usec)				= opcode busywait


type Sema =
{
	Tid		waiters			// linked list of Threads waiting for this Sema => Thread.next2,Thread.prev2
	uint32	count			// available resources
#if _bytes_per_pointer==8	// TODO: alignment macros
	int32	_dummy;
#endif
}

scope Sema
{
	void	request		(Sema¢)					= opcode request
	void	request		(Sema¢, uint n)			= opcode request
	void	release		(Sema¢)					= opcode release
	void	release		(Sema¢, uint n)			= opcode release
	bool	tryrequest	(Sema¢)					= opcode tryrequest
	bool	tryrequest	(Sema¢, float timeout)	= opcode tryrequest
	void	clear		(Sema¢)					= opcode clear		//inline { s.count=0; }
	Sema	kill		(Sema)					= opcode kill
}
	Sema	new			()						= opcode new


type Mutex =
{
	Tid		waiters			// linked list of Threads waiting for this Mutex => Thread.next2,Thread.prev2
	uint32	count			// recursive locks
	int16	oprio			// original (boosted) prio of owner Thread
#if _bytes_per_pointer==8	// TODO: alignment macros
	int16	_dummy;
#endif
	Tid		owner
	Mutex¢	next,prev
}

scope Mutex
{
	void	lock		(Mutex¢)				= opcode lock
	void	unlock		(Mutex¢)				= opcode unlock
	bool	trylock		(Mutex¢)				= opcode trylock
	bool	trylock		(Mutex¢, float timeout)	= opcode trylock
	Mutex	kill		(Mutex)					= opcode kill
}
	Mutex	new			()						= opcode new


type Irpt =
{
	Tid		waiters			// linked list of Threads waiting for this Irpt => Thread.next2,Thread.prev2
	uint32	count			// interrupt counter
#if _bytes_per_pointer==8	// TODO: alignment macros
	int32	_dummy;
#endif
}

scope Irpt
{
	void	wait		(Irpt¢)					= opcode wait
	void	wait		(Irpt¢, float timeout)	= opcode wait
	void	trigger		(Irpt¢)					= opcode trigger
	void	clear		(Irpt¢)					= opcode clear		//inline { i.count = 0; }
	Irpt	kill		(Irpt)					= opcode kill
}
	Irpt	new			()						= opcode new


// ____________________________________________________________________
// Object:

type Object =
{
	void*	senders
}

scope Object
{
	Object	kill		(Object)				= opcode kill
}


// ____________________________________________________________________
// VT100:

scope vt100
{
	extern
	{
		str request_position				// --> Terminal responds
		str save_position
		str restore_position
		str cursor_left_1
		str cursor_right_1
		str cursor_up_1						// no scroll
		str cursor_down_1					// no scroll

		str clear_right						// incl. crsr loc.
		str clear_left						// incl. crsr loc.
		str clear_down						// incl. crsr loc.
		str clear_up						// incl. crsr loc.
		str clear_line
		str clear_screen
		str cls								// clear scrn & home crsr

		str locate(int row,int col)			// top,left = 1,1
		str hlocate(int col)				// top,left = 1,1
		str vlocate(int row)				// top,left = 1,1
		str cursor_left(int n)
		str cursor_right(int n)
		str cursor_up(int n)				// no scroll
		str cursor_down(int n)				// no scroll
	}
}


/*	frequently used values for stopctls:
*/
#define stopctls_none	0x00000000
#define	stopctls_crlf	((1<<0)|(1<<4)|(1<<10)|(1<<12)|(1<<13))		// cr,lf,ff,eof etc.
#define stopctls_all	0xFFFFFFFF


#endif


















