// main-s.txt
//   public release som-v2   30 Dec 2004

to eval ref =
	syscall {5 ref}		// eval in C

to parse | f e a =
	lex
	while tok != tkEOF
		if tok == tkTO
			f = top		// throw away flag
			e = ypop
			def = append def e
			genex e
		else
			f = top
			e = ypop
			a = CP
			genex e
			outs icEnd
			patchCalli 3 CP-1
			eval a		// immediately eval it
			CP = a		// and throw away code

to showGlobal | i nm =
	prints "Global: "
	for i 0 tablesize-1
		nm = getName i
		if (nm != 0) & ((getAttr i) != 0)
			if (getType i) == tyGVAR
				prints nm space
	nl

to epilog | id =
	patchCalli 3 CP-1
	id = install "main"
	if (getAttr id) != 0	// if found call main
		putcode 1 icCall getRef id
		putcode 2 icEnd 0
		if noeval == 0
			eval 1

to initkeysym =
	enterGlobal install "to" tyKEY tkTO
	enterGlobal install "if" tyKEY tkIF
	enterGlobal install "else" tyKEY tkELSE
	enterGlobal install "while" tyKEY tkWHILE
	enterGlobal install "for" tyKEY tkFOR
	enterGlobal install "break" tyKEY tkBREAK
	enterGlobal install "array" tyKEY tkARRAY
	enterGlobal install "case" tyKEY tkCASE
	enterGlobal install "enum" tyKEY tkENUM
	enterGlobal install "syscall" tyKEY tkSYSCALL

// code segment 1..CP-1, data segment userDS..DP
to outobj | dp =
	print OBJ_SOMv2 nl
	outM CS 1 CP-1
	dp = array 0	// find where is DP
	outM M userDS dp-1

to init_all =
	initSom
	init_list
	initlex
	init_symtab
	initkeysym
	initcell
	def = list newatom OPER tkBB
	startlex
	userDS = array 0   // find where is DS

to doObject =
	init_all
	verbose = 0
	noeval = 1
	parse
	epilog
	outobj

to doListing =
	init_all
	verbose = 0
	noeval = 1
	parse
	epilog
	showCode 1 CP-1

to main =
	init_all
	parse
//	prlist def nl
	epilog
//	if verbose
//		showGlobal
//		dumpSym

// End
