how eval() work?

short example  
(def main () () (+ 1 2))
=>  (fun.1.1 (+ lit.1 lit.2))

require : fun, add, lit

exp eval2(exp e){
. . .
	if( e == NIL) return NIL;
	if( ! isAtom(e) ) {
		e1 = tail(e);	// (arg1 arg2 ...)
		e2 = tail(e1);	// (arg2...) e2 may be NIL
		e = head(e);	
	}			// it is a dot-pair
	decode(e,&op,&arg);
	switch(op){
	case xLIT: return arg;
	case xGET: return SS[fp-arg];
. . .
	case xADD: return eval2(arg1(e)) + eval2(arg2(e));
. . .
	case xFUN:
		arity = arg >> 8;
		fs = arg & 0xff;
		k = fs-arity+1;
		SS[sp+k] = fp;		// new frame
		fp = sp+k;
		sp = fp;
		v = eval2(arg1(e));
		sp = fp-fs-1; 		// delete frame
		fp = SS[fp];
		return v;
. . .
}

recode fun.a.v

to simplify execution of the control unit for function call the header fun.a.v (a is arity, v is formal+local) is recoded into fun.k.fs where k = v - a + 1, k is the offset of fp from sp for creating a new frame, fs = v - 1, fs is the offset of sp from fp for deleting the current frame.  This is done by  the object code loader.  It is independent from the n-code because this depends on how the activation record is implemented, hence it is specific to the simulator.

This is the actual sequence of "fun":

	case xFUN:	
		fs = arg & 0xff;
		k = arg >> 8;		// already recode
		M[sp+k] = fp;		// new frame
		fp = sp+k;
		sp = fp;
		e1 = head(e1);
		v = eval4(e1);
		sp = fp - fs;		// already recode
		return v;


29 Nov 2004
P. Chongstitvatana
