// main-s.txt
//   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)
//   public release som v4.2    9 Sept 2009  (triple 9, 9/9/2009)
//   public release som v5.0    5 Dec 2009   (Long live the King)

//  system area  1 .. MAXSYS-1
//   101     mode ads
//   110..149 "inputfile"  som-string
//   150..199 "input.lst"  som-string
//   200..249 "input.obj"  som-string

enum
    110 src_file
enum
    150 lst_file
enum
    200 obj_file

to parse | f e tk =
    setcdr im NIL		// clear im
    startlex
    lex
    while tok != tkEOF
        tk = tok
        f = top			// parse, throw away flag
        e = ypop
        if (tk == tkTO) | (tk == tkCOLON)
//			def = append def e
            genex e
        else
            im = append im e

to init_all =
    initSom
    init_list
    def = list newatom OPER tkBB
    im = list newatom OPER tkBB
    initkeysym
    hSym = array hSymsize
    initlex
    clearlis relis

to loadfile fn =
    if verbose
        prints "load " prints fn nl
    FI = fopen fn 0
    M[lexmode_ads] = 2	// batch
    parse
    fclose FI
    runimm im

// read-eval-print loop, stdin
to rep | tk f e =
    while 1
        FI = 0				// stdin
        M[lexmode_ads] = 1	// line
        printc 62			// >
        parse
        runimm im

to reloc disp | i k a v d =
    for i 0 nxsym-1
        k = exsym[i]
        if (getType k) == tyGVAR
            a = getArity k
            v = getRef k		// refresh symtab
            setRef k v+disp
            if a == VARRAY		// reloc value of static
                d = M[v] + disp
                M[v] = d
                setLv k d		// update symtab

    for i 1 (sizeoflis relis)	// reloc ads const
        a = relis[i]
        M[a] = M[a] + disp

    for i 9 CP-1
        relocCode i disp
        if (opAt i) == tCase
            i = i + (sizeJtab i)
        i = i + 2

to outobj fn | ds =
    ds = array 0				// end of user ds
    reloc 1000-userDS
    FO = fopen fn 1
    fprint FO OBJ_SOM5 fnl
    outhead 3 CP-1
    outCS 3 CP-1
    outhead 1000 1000+ds-1-userDS
    outM M userDS ds-1		// data
    fprint FO 0 fnl			// no link vector
    dumpSym					// symbol table
    fclose FO

to listing fn =
    FO = fopen fn 1
    showCode 3 CP-1
    fclose FO

to main =
    init_all
    userDS = array 0		// start of user ds
    loadfile "lib2.som"
    if M[mode_ads] == 1		// mode 1 interact, 2 compile
        rep
    else
        loadfile src_file
        genfinal
        hashSym
        listing lst_file
        outobj obj_file

// End
