home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / lr.zip / COMMANDS.C next >
Text File  |  1993-05-15  |  5KB  |  129 lines

  1.  
  2.  
  3.  /*
  4.   ╔═══════════════════════════════════════════════════════════════════════════╗
  5.   ║ Copyright (C) Transcendental Automation, 1993.                            ║
  6.   ╟───────────────────────────────────────────────────────────────────────────╢
  7.   ║ COMMANDS.C                                                                ║
  8.   ╟───────────────────────────────────────────────────────────────────────────╢
  9.   ║ This module contains the function that initializes semantic actions' table║
  10.   ║ and functions which define this semantic actions.All semantics action     ║
  11.   ║ work in the time of postfix traversal.                                    ║
  12.   ╚═══════════════════════════════════════════════════════════════════════════╝
  13.  */
  14.  #include "sample.h"
  15.  #include <stdio.h>
  16.  
  17.  void ASSIGN_func     (void*,SAMPLE_NPS*,LR_PT*);
  18.  void DIV_func        (void*,SAMPLE_NPS*,LR_PT*);
  19.  void MUL_func        (void*,SAMPLE_NPS*,LR_PT*);
  20.  void ADD_func        (void*,SAMPLE_NPS*,LR_PT*);
  21.  void SUB_func        (void*,SAMPLE_NPS*,LR_PT*);
  22.  void NEG_func        (void*,SAMPLE_NPS*,LR_PT*);
  23.  void FLOAT_func      (void*,SAMPLE_NPS*,LR_PT*);
  24.  void IDENTIFIER_func (void*,SAMPLE_NPS*,LR_PT*);
  25.  /*
  26.  _____________________________________________________________________________
  27.   Initialize table of semantic actions
  28.  _____________________________________________________________________________
  29.  */
  30.  void InitCmdTable()
  31.   {int i;
  32.    for(i=0;i<SAMPLE_n;i++) cmd [i] = 0;
  33.    cmd[SAMPLE_ASSIGN_n]        = ASSIGN_func;
  34.    cmd[SAMPLE_DIV_n]           = DIV_func;
  35.    cmd[SAMPLE_MUL_n]           = MUL_func;
  36.    cmd[SAMPLE_ADD_n]           = ADD_func;
  37.    cmd[SAMPLE_SUB_n]           = SUB_func;
  38.    cmd[SAMPLE_NEG_n]           = NEG_func;
  39.    cmd[SAMPLE_FLOAT_n]         = FLOAT_func;
  40.    cmd[SAMPLE_IDENTIFIER_n]    = IDENTIFIER_func;
  41.   }
  42.  
  43.  /*
  44.  ______________________________________________________________________________
  45.   The function realize semantics of assign
  46.  _____________________________________________________________________________
  47.  */
  48.  static void ASSIGN_func     (void* sysp, SAMPLE_NPS* NPS, LR_PT* pt)
  49.   {LR_PT* zt = pt->PL.f->d;      // the RHS of rule ASSIGN contain 3 nonterminals
  50.                                  // so the first son always exists.
  51.                                  // 'pt->PL.f->d' means pointer to first son.
  52.  
  53.    if(zt->t && lrx_n(zt) == SAMPLE_IDENTIFIER_n) //test if first son is identifier
  54.     {SetItem(zt->t,popFloat()); // the top of stack is value of expression
  55.      popFloat();                // the top of stack is value of identifier before
  56.                                 // calculation.
  57.     }
  58.   }
  59.  
  60.  /*
  61.  ______________________________________________________________________________
  62.   The function realize semantics of division.
  63.  _____________________________________________________________________________
  64.  */
  65.  static void DIV_func        (void* sysp, SAMPLE_NPS* NPS, LR_PT* pt)
  66.   {float a = popFloat();
  67.    if(a==0)
  68.     {puts("Divide by zero.");
  69.      exit(4);
  70.     }
  71.    pushFloat(popFloat()/a);
  72.   }
  73.  
  74.  /*
  75.  ______________________________________________________________________________
  76.   The function realize semantics of multiplication
  77.  _____________________________________________________________________________
  78.  */
  79.  static void MUL_func        (void* sysp, SAMPLE_NPS* NPS, LR_PT* pt)
  80.   {pushFloat( popFloat() * popFloat() );
  81.   }
  82.  
  83.  /*
  84.  ______________________________________________________________________________
  85.   The function realize semantics of addition
  86.  _____________________________________________________________________________
  87.  */
  88.  static void ADD_func        (void* sysp, SAMPLE_NPS* NPS, LR_PT* pt)
  89.   {pushFloat( popFloat() + popFloat() );
  90.   }
  91.  
  92.  /*
  93.  ______________________________________________________________________________
  94.   The function realize semantics of subtraction
  95.  _____________________________________________________________________________
  96.  */
  97.  static void SUB_func        (void* sysp, SAMPLE_NPS* NPS, LR_PT* pt)
  98.   {pushFloat( - popFloat() + popFloat() );
  99.   }
  100.  
  101.  /*
  102.  ______________________________________________________________________________
  103.   The function realize semantics of negation
  104.  _____________________________________________________________________________
  105.  */
  106.  static void NEG_func        (void* sysp, SAMPLE_NPS* NPS, LR_PT* pt)
  107.   {pushFloat( - popFloat());
  108.   }
  109.  
  110.  /*
  111.  ______________________________________________________________________________
  112.   The function realize semantics of constant
  113.  _____________________________________________________________________________
  114.  */
  115.  static void FLOAT_func      (void* sysp, SAMPLE_NPS* NPS, LR_PT* pt)
  116.   {pushFloat(atof(pt->t));
  117.   }
  118.  
  119.  /*
  120.  ______________________________________________________________________________
  121.   The function realize semantics of identifier
  122.  _____________________________________________________________________________
  123.  */
  124.  static void IDENTIFIER_func (void* sysp, SAMPLE_NPS* NPS, LR_PT* pt)
  125.   {pushFloat(LookUp(pt->t));
  126.   }
  127.    
  128.  
  129.