-- JaMFnsImpl.mesa
-- Last changed by Doug Wyatt, 20-Jan-82 16:40:30

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

JaMFnsImpl: PROGRAM
IMPORTS JaMOps, Inline
EXPORTS JaMFns = {
OPEN JaMInternal, JaMBasic;

StreamHandle: TYPE = StreamDefs.StreamHandle;

FrameObject: PUBLIC TYPE = FrameRecord; -- exported TYPE for JaMFns

GetFrame: PROC[f: Frame] RETURNS[Frame] = INLINE {
  RETURN[IF f=NIL THEN JaMOps.defaultFrame ELSE f] };

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

Execute: PUBLIC PROC[string: LONG STRING, f: Frame] = {
  frame: Frame ← GetFrame[f];
  ob: string Object ← JaMOps.MakeString[string];
  ob.tag ← X; JaMOps.Execute[frame, ob];
  };

PushInteger: PUBLIC PROC[i: INTEGER, f: Frame] = {
  frame: Frame ← GetFrame[f];
  JaMOps.PushInteger[frame.opstk,i];
  };
PopInteger: PUBLIC PROC[f: Frame] RETURNS[INTEGER] = {
  frame: Frame ← GetFrame[f];
  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, f: Frame] = {
  frame: Frame ← GetFrame[f];
  JaMOps.PushInteger[frame.opstk,i];
  };
PopLongInteger,GetLongInteger: PUBLIC PROC[f: Frame] RETURNS[LONG INTEGER] = {
  frame: Frame ← GetFrame[f];
  RETURN[JaMOps.PopInteger[frame.opstk]];
  };

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

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

PushString: PUBLIC PROC[s: LONG STRING, f: Frame] = {
  frame: Frame ← GetFrame[f];
  ob: string Object ← JaMOps.MakeString[s];
  JaMOps.Push[frame.opstk,ob];
  };
PopString: PUBLIC PROC[s: LONG STRING, f: Frame] = {
  frame: Frame ← GetFrame[f];
  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, f: Frame] = {
  frame: Frame ← GetFrame[f];
  ob: stream Object ← JaMOps.MakeStream[s];
  JaMOps.Push[frame.opstk,ob];
  };
PopStream: PUBLIC PROC[f: Frame] RETURNS[StreamHandle] = {
  frame: Frame ← GetFrame[f];
  ob: stream Object ← JaMOps.PopStream[frame.opstk];
  RETURN[JaMOps.GetStream[ob]];
  };

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

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

}.