// compile-h-s.txt
// header file for som-in-som project
//  public release som-v24 10 January 2007
//    Celebrating H.M.King 60 years accend to the throne.
//  public release som v3.0   5 March 2007 (Maka-bucha day)
//  public release som v3.1   19 Aug 2007  (Draft vote day)
//  public release som v4.0   2 July 2008
//  public release som v4.1   9 Aug 2008 (Birthday)
//  som v5	starts            7 Oct 2009
//  som v5.1  starts          12 Dec 2010
//  public release som v5.1   24 Dec 2010 (Christmas)

// sequence of loading files
//
// lib2.som
// string-s.txt
// compile-h-s.txt
// list-s.txt
// symtab-s.txt
// token-s.txt
// parse-h-s.txt
// stmt-s.txt
// parse2.som
// icode-s.txt
// gencode-s.txt
// macro-s.txt
// main-s.txt

enum	// marker
	0 NIL MARK BMARK
enum	// atom
	0 SP OPER NUM GNAME LNAME CNAME STRING ADS

enum	// token type
	14 tkIDEN tkNUMBER tkSTRING tkEOF tkERROR
enum	// token
	50 tkSTAR tkSLASH tkMINUS tkPLUS tkEQ tkEQEQ
	tkAND tkBAR tkCARET tkMOD tkNOT tkNE tkLT tkLE
	tkLTLT tkGT tkGE tkGTGT tkCOLON tkLPAREN tkRPAREN
	tkLBRACKET tkRBRACKET tkBB tkBE tkTO tkIF tkELSE
	tkWHILE tkFOR tkBREAK tkARRAY tkCASE tkENUM tkSYSCALL
enum	// symbol type
	6 tyFUNC tyLOCAL tyGVAR tyENUM tyKEY tyNEW tyMAC tySP
enum	// denote gvar arg for DS relocation
	0 VSCALAR VARRAY VSTRING

enum	// t-code
	1 tAdd tSub tMul tDiv tMod tAnd tOr tXor tEq tNe
	tLt tLe tGt tGe tShl tShr tNot tMov tLdx tStx
	tNop tPush tCall tCallt tFun tRet tEfor tCase
	tJmp tJt tJf tJeq tJne tJlt tJle tJgt tJge
	tSys // 38
enum
	60 mV m2 mC mJ mW mY mF		// op format

enum {38 EOC}
enum {5678951 OBJ_SOM51}
enum {64 RETVAL}				// global retval reg
enum {0 nil}					// empty

//  memory model
//  M [ MAXMEM  ]		 declared in C
//  M[1..MAXSYS]  		 system area
//	CS[MAXCS]			 code segment
//       ^
//       CP
//  ystack[MAXYSTK]		 parser stack
//           ^
//          ysp
//  cell[MAXCELL]		 store list (parse-tree)
//  data segment is absolute M[]

enum {20000 MAXCS}	// size of code segment
enum {1000 MACSIZE}	// size of macro
enum {2000 MAXYSTK}	// size of parser stack
enum {200  MAXCALL}	// size of call forward list
enum {100  MAXLV}	// size of lv table

//  system area  1 .. MAXSYS-1
//  101  mode
//  102  tokvalue
//  103  tokcol
//  104  line
//  105  lexmode
//  250..299 tokstring

enum {101 mode_ads}
enum {105 lexmode_ads}

to initSom | a =
	M = 0				// base of M, alias with C M[]
	CP = 6  			// code pointer
	CS = array MAXCS
	ystack = array MAXYSTK  // parser stack
	ysp = 0
	currentf = 0		// current fn idx, im mode
	nenum = 0			// enum number
	verbose = 1			// control compiler message
//	cmode = 0			// compile mode, 0 def, 1 im, 2 rep
	callis = array MAXCALL	// list of call forward
	CPX = MAXCS-MACSIZE	// immediate & macro code area
	exsym = array 500 	// export symbol
	nxsym = 0
//	line = 0
	M[104] = 0			// line
	FI = 0				// input file
	FO = 1				// output file
	vrec = array MAXLV
	vsp = 0				// vrec sp
	jplis = array 1000	// ads of icJmp
	relis = array 200 	// reloc list
	lastCP = 0
	nlocal = 0
	userDS = 0

: line = M[104]
: setline n = M[104] = n

: fspace = fprintc FO 32
: fnl = fprintc FO 10

// when no line number is available (gencode, eval, etc.)
to error x =
	prints "error: " prints x nl
	exit

// error during parsing
to seterror s =
	prints "line " print line space
	prints s nl
	exit

to warning nm mess =
	if verbose
		prints "Warning: "
		if nm != 0
			prints nm space
		prints mess nl

// End
