-- JaMFnsDefsImpl.mesa
-- Original version by John Warnock
-- Last changed by Bill Paxton, March 24, 1981  3:29 PM
-- Last changed by Doug Wyatt,  1-Oct-81 15:12:07
-- Last Edited by: Maxwell, January 18, 1983 11:04 am

DIRECTORY
  JaMBasic USING [Object],
  JaMFnsDefs USING [],
  JaMInternal USING [Frame],
  JaMOps USING [defaultFrame, Error, GetAbort, GetStream, MakeStream, MakeString,
    Pop, PopBoolean, PopInteger, PopReal, PopStream, PopString, Push, PushBoolean,
    PushInteger, PushReal, rangechk, RegisterImplicit, SetAbort, StartJaM, StringText],
  Inline USING [LowHalf],
  StreamDefs USING [StreamHandle];

JaMFnsDefsImpl: PROGRAM
IMPORTS JaMOps, Inline
EXPORTS JaMFnsDefs = {
OPEN JaMInternal, JaMBasic;

StreamHandle: TYPE = StreamDefs.StreamHandle;

Register: PUBLIC PROC[string: LONG STRING, proc: PROC] = {
  frame: Frame ← JaMOps.defaultFrame;
  JaMOps.RegisterImplicit[frame,string,proc];
  };

JaMExec: PUBLIC PROC[string: LONG STRING] = {
  frame: Frame ← JaMOps.defaultFrame;
  ob: string Object ← JaMOps.MakeString[string];
  ob.tag ← X; JaMOps.Push[frame.execstk, ob];
  };

PushInteger: PUBLIC PROC[i: INTEGER] = {
  frame: Frame ← JaMOps.defaultFrame;
  JaMOps.PushInteger[frame.opstk,i];
  };
PopInteger: PUBLIC PROC RETURNS[INTEGER] = {
  frame: Frame ← JaMOps.defaultFrame;
  i: LONG INTEGER ← JaMOps.PopInteger[frame.opstk];
  IF i IN[FIRST[INTEGER]..LAST[INTEGER]] THEN RETURN[Inline.LowHalf[i]]
  ELSE ERROR JaMOps.Error[JaMOps.rangechk];
  };

PushLongInteger: PUBLIC PROC[i: LONG INTEGER] = {
  frame: Frame ← JaMOps.defaultFrame;
  JaMOps.PushInteger[frame.opstk,i];
  };
PopLongInteger,GetLongInteger: PUBLIC PROC RETURNS[LONG INTEGER] = {
  frame: Frame ← JaMOps.defaultFrame;
  RETURN[JaMOps.PopInteger[frame.opstk]];
  };

PushReal: PUBLIC PROC[r: REAL] = {
  frame: Frame ← JaMOps.defaultFrame;
  JaMOps.PushReal[frame.opstk,r];
  };
PopReal,GetReal: PUBLIC PROC RETURNS[REAL] = {
  frame: Frame ← JaMOps.defaultFrame;
  RETURN[JaMOps.PopReal[frame.opstk]];
  };

PushBoolean: PUBLIC PROC[b: BOOLEAN] = {
  frame: Frame ← JaMOps.defaultFrame;
  JaMOps.PushBoolean[frame.opstk,b];
  };
PopBoolean: PUBLIC PROC RETURNS[BOOLEAN] = {
  frame: Frame ← JaMOps.defaultFrame;
  RETURN[JaMOps.PopBoolean[frame.opstk]];
  };

PushString: PUBLIC PROC[s: LONG STRING] = {
  frame: Frame ← JaMOps.defaultFrame;
  ob: string Object ← JaMOps.MakeString[s];
  JaMOps.Push[frame.opstk,ob];
  };
PopString: PUBLIC PROC[s: LONG STRING] = {
  frame: Frame ← JaMOps.defaultFrame;
  ob: string Object ← JaMOps.PopString[frame.opstk];
  WHILE s.maxlength<ob.length DO s ← SIGNAL StringOverflow[s] ENDLOOP;
  JaMOps.StringText[ob,s];
  };
StringOverflow: PUBLIC SIGNAL[s: LONG STRING] RETURNS[ns: LONG STRING] = CODE;

PushStream: PUBLIC PROC[s: StreamHandle] = {
  frame: Frame ← JaMOps.defaultFrame;
  ob: stream Object ← JaMOps.MakeStream[s];
  JaMOps.Push[frame.opstk,ob];
  };
PopStream: PUBLIC PROC RETURNS[StreamHandle] = {
  frame: Frame ← JaMOps.defaultFrame;
  ob: stream Object ← JaMOps.PopStream[frame.opstk];
  RETURN[JaMOps.GetStream[ob]];
  };

Pop: PUBLIC PROC = {
  frame: Frame ← JaMOps.defaultFrame;
  [] ← JaMOps.Pop[frame.opstk];
  };

GetJaMBreak: PUBLIC PROC RETURNS[BOOLEAN] = {
  frame: Frame ← JaMOps.defaultFrame;
  RETURN[JaMOps.GetAbort[frame]];
  };
SetJaMBreak: PUBLIC PROC[flag: BOOLEAN ← TRUE] = {
  frame: Frame ← JaMOps.defaultFrame;
  JaMOps.SetAbort[frame,flag];
  };

JaMOps.StartJaM[];

}.