Comment
==================================================================

This section contains the logic for processing IF statements. I'm
writing an interpreter language in MarxMenu. You can add your own
conditionals for IniTyme here.

==================================================================
EndComment


Procedure PushStack (V)
   AppendArray(Stack,V)
EndProc


Procedure PopStack
var X
   if NumberOfElements(Stack) = 0
      Error ('Stack Empty!',LoopIndex)
   endif
   X = Stack[NumberOfElements(Stack)]
   delete(Stack,NumberOfElements(Stack),1)
   Return X
EndProc


Procedure Nugget
var Word P FirstChar
   Trim(IfLine)
   FirstChar = Left(IfLine,1)
   Word = NextWord(IfLine)

   if FirstChar Within('0','9')
      PushStack(Value(Word))

   elseif (FirstChar = '"') or (FirstChar = "'")
      PushStack(Word)

   else
      P = PosInSortedList(Word,LibList)
      if P > 0
         Run LibProcList[P]
      else
         Error ('Word "' + Word + '" not Recognized!',LoopIndex)
      endif
   endif

EndProc


Procedure Interpret
   while IfLine > ''
      Nugget
   endwhile
EndProc


Procedure ExecValueOf
var V P St
   V = UpperCase(NextWord(IfLine))
   if Right(V,1) <> '=' then V = V + '='
   P = FindLine(V)
   if P = 0
      PushStack('')
   else
      St = Orig[P]
      delete(St,1,pos('=',St))
      Trim(St)
      PushStack(St)
   endif
EndProc


Procedure ExecPlus
var A B
   Nugget
   B = PopStack
   A = PopStack
   PushStack(A + B)
EndProc


Procedure ExecMinus
var A B
   Nugget
   B = PopStack
   A = PopStack
   PushStack(A - B)
EndProc


Procedure ExecEqual
var A B
   Nugget
   B = PopStack
   A = PopStack
   PushStack(A = B)
EndProc


Procedure ExecNotEqual
var A B
   Nugget
   B = PopStack
   A = PopStack
   PushStack(A <> B)
EndProc


Procedure ExecGreater
var A B
   Nugget
   B = PopStack
   A = PopStack
   PushStack(A > B)
EndProc


Procedure ExecLess
var A B
   Nugget
   B = PopStack
   A = PopStack
   PushStack(A < B)
EndProc


Procedure ExecGreaterEqual
var A B
   Nugget
   B = PopStack
   A = PopStack
   PushStack(A >= B)
EndProc


Procedure ExecLessEqual
var A B
   Nugget
   B = PopStack
   A = PopStack
   PushStack(A <= B)
EndProc


Procedure ExecAnd
var A B
   Nugget
   B = PopStack
   A = PopStack
   PushStack(A and B)
EndProc


Procedure ExecOr
var A B
   Nugget
   B = PopStack
   A = PopStack
   PushStack(A or B)
EndProc


Procedure ExecNot
   Nugget
   PushStack(not PopStack)
EndProc


Procedure ExecUpperCase
   Nugget
   PushStack(UpperCase(PopStack))
EndProc


Procedure ExecLeftParen
var OldParenLevel
   ParenLevel = ParenLevel + 1
   repeat
      Nugget
   until ParenLevel = OldParenLevel
EndProc


Procedure ExecRightParen
   ParenLevel = ParenLevel - 1
EndProc


Procedure ExecIf
   Interpret
   if not PopStack
      LoopIndex = GoToList[LoopIndex]
   endif
EndProc


Procedure ExecGoTo
   LoopIndex = GoToList[LoopIndex]
EndProc


Procedure ExecInGroup
var Group
   Group = UpperCase(NextWord(IfLine))
   PushStack(PosInSortedList(Group,GroupList) > 0)
EndProc


Procedure ExecExistFile
   PushStack(ExistFile(UpperCase(NextWord(IfLine))))
EndProc


Procedure ExecExistDir
   PushStack(ExistDir(UpperCase(NextWord(IfLine))))
EndProc


Procedure ExecEnvVar
   PushStack(ReadEnv(UpperCase(NextWord(IfLine))))
EndProc


Procedure ExecCPU
   PushStack(CPUClass)
EndProc


Procedure ExecMathChip
   PushStack(McpClass > 0)
EndProc


Procedure ExecUserName
   PushStack(NovLoginName)
EndProc


Procedure ExecStation
   PushStack(NovStationAddress)
EndProc


Procedure ExecDisplay
var Disp
   Disp = DisplayType

   if Disp = 0
      Return 'HERC'

   elseif Disp = 1
      Return 'CGA'

   elseif Disp = 3
      Return 'EGA'

   elseif Disp = 4
      Return 'VGA'

   else
      Return ''

   endif
EndProc


Procedure AddCommand (St,Cmd)
   AppendArray(LibList,St)
   AppendArray(LibProcList,Cmd)
EndProc


Procedure SetupLibWords

   AddCommand('(',Loc ExecLeftParen)
   AddCommand(')',Loc ExecRightParen)
   AddCommand('+',Loc ExecPlus)
   AddCommand('-',Loc ExecMinus)
   AddCommand('<>',Loc ExecNotEqual)
   AddCommand('<',Loc ExecLess)
   AddCommand('>',Loc ExecGreater)
   AddCommand('<=',Loc ExecLessEqual)
   AddCommand('>=',Loc ExecGreaterEqual)
   AddCommand('=',Loc ExecEqual)
   AddCommand('AND',Loc ExecAnd)
   AddCommand('OR',Loc ExecOr)
   AddCommand('NOT',Loc ExecNot)
   AddCommand('GOTO',Loc ExecGoto)
   AddCommand('IF',Loc ExecIf)
   AddCommand('INGROUP',Loc ExecInGroup)
   AddCommand('STATION',Loc ExecStation)
   AddCommand('USERNAME',Loc ExecUserName)
   AddCommand('VALUEOF',Loc ExecValueOf)
   AddCommand('UPPERCASE',Loc ExecUpperCase)
   AddCommand('EXISTFILE',Loc ExecExistFile)
   AddCommand('EXISTDIR',Loc ExecExistDir)
   AddCommand('ENVVAR',Loc ExecEnvVar)
   AddCommand('CPU',Loc ExecCPU)
   AddCommand('MATHCHIP',Loc ExecMathChip)
   AddCommand('DISPLAY',Loc ExecDisplay)

   SortArrayLinked(LibProcList,LibList)

EndProc

