home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pascal.zip / stackman / stackman.c < prev    next >
Text File  |  1995-10-29  |  6KB  |  325 lines

  1. /*
  2.  *        C . A . P .   I N T E R P R E T E R
  3.  *
  4.  *        C O R E   P R O C E D U R E S
  5.  *
  6.  *        Stéphane Charette @ C.A.P. Services
  7.  *
  8.  *        Last modified:  Stéphane Charette, 1995 October 29
  9.  *
  10.  *****************************************************************************
  11.  *
  12.  *        Project:    BILL
  13.  *        Group:    interpreter
  14.  *        File:        stackman\stackman.c
  15.  *        Version:    0.1.4
  16.  *
  17.  *        This file contains all of the source code that makes up the parser
  18.  *        used with the interpreter BILL.
  19.  */
  20.  
  21.  
  22.  
  23. /*
  24.  *    Versions:
  25.  *
  26.  *    0.0.1    - design of structure and implementation, Stéphane Charette, 94Apr12
  27.  *    0.1.0    - first working version, SC, 94Apr12
  28.  *    0.1.1    - worked out bugs in stack and code addressing, SC, 94Apr12-13
  29.  *    0.1.2    - added trap for "divide by zero", SC, 94Apr13
  30.  *    0.1.3    - added opcode for odd function, SC, 94Apr14
  31.  *    0.1.4    - ported to OS/2, SC, 94Apr23-27
  32.  *    0.1.4 - changed text formatting, SC, 95Oct29
  33.  */
  34.  #define _STACKMAN_VERSION "Stack machine v0.1.4, Stéphane Charette, 95October29\n"
  35.  
  36.  
  37.  
  38. /*
  39.  *    Includes
  40.  */
  41.     #include "..\pcode\pcode_id.h"
  42.     #include <stdio.h>
  43.     #include <stdlib.h>
  44.  
  45.  
  46.  
  47. /*
  48.  *    Global vars
  49.  */
  50.     FILE *inputfileptr;
  51.     signed int CS[ 10000 ];        // code segment
  52.     signed int SS[ 1000 ];        // stack segment
  53.     signed int IP;                    // instruction pointer
  54.     signed int SP;                    // stack pointer
  55.  
  56.  
  57.  
  58. /*
  59.  *    Prototyping
  60.  */
  61.     int RunStackMachine( void );
  62.     int LoadProgramme( void );
  63.  
  64.  
  65.  
  66. /*
  67.  *    FUNCTION:  main
  68.  */
  69. int RunStackMachine( )
  70. {
  71.     printf( "B.I.L.L. interpreter v0.1.2\nDesigned and implemented by Stéphane Charette\n"
  72.             "Compiled for OS/2 v3.0 using IBM's C++ compiler v2.1.\nLoading code..." );
  73.     LoadProgramme( );
  74.     printf( "...finished!\nStarting execution.\n\n" );
  75.  
  76.     IP = 0;
  77.     SP = 0;
  78.  
  79.     while( CS[IP] != HLT )
  80.     {
  81.         switch( CS[IP] )
  82.         {
  83.             case NEG:
  84.             {
  85.                 SS[SP-1] = - SS[SP-1];
  86.                 break;
  87.             }
  88.             case ADD:
  89.             {
  90.                 SS[SP-2] = SS[SP-2] + SS[SP-1];
  91.                 SP--;
  92.                 break;
  93.             }
  94.             case SUB:
  95.             {
  96.                 SS[SP-2] = SS[SP-2] - SS[SP-1];
  97.                 SP--;
  98.                 break;
  99.             }
  100.             case MUL:
  101.             {
  102.                 SS[SP-2] = SS[SP-2] * SS[SP-1];
  103.                 SP--;
  104.                 break;
  105.             }
  106.             case DVD:
  107.             {
  108.                 if( SS[SP-1] == 0 )
  109.                 {
  110.                     printf( "Hey buddy!  Wake up!  I may be good, but not that good.\n"
  111.                             "Since when do you think I can divide by zero?  Jeez...\n\a" );
  112.                             exit( 1 );
  113.                 }
  114.                 else
  115.                 {
  116.                     div_t tmpnum = div( SS[SP-2], SS[SP-1] );
  117.                     SS[SP-2] = tmpnum.quot;
  118.                     SP--;
  119.                 }
  120.                 break;
  121.             }
  122.             case EQL:
  123.             {
  124.                 SS[SP-2] = SS[SP-2] == SS[SP-1];
  125.                 SP--;
  126.                 break;
  127.             }
  128.             case NEQ:
  129.             {
  130.                 SS[SP-2] = SS[SP-2] != SS[SP-1];
  131.                 SP--;
  132.                 break;
  133.             }
  134.             case GTR:
  135.             {
  136.                 SS[SP-2] = SS[SP-2] > SS[SP-1];
  137.                 SP--;
  138.                 break;
  139.             }
  140.             case LSS:
  141.             {
  142.                 SS[SP-2] = SS[SP-2] < SS[SP-1];
  143.                 SP--;
  144.                 break;
  145.             }
  146.             case LEQ:
  147.             {
  148.                 SS[SP-2] = SS[SP-2] <= SS[SP-1];
  149.                 SP--;
  150.                 break;
  151.             }
  152.             case GEQ:
  153.             {
  154.                 SS[SP-2] = SS[SP-2] >= SS[SP-1];
  155.                 SP--;
  156.                 break;
  157.             }
  158.             case STK:
  159.             {
  160.                 // print the stack here...  NOT IMPLEMENTED
  161.                 printf( "\nPRINT THE STACK!\nI don't know what you're trying to"
  162.                         "do to me, but I don't like it - so I'm ignoring you!\a" );
  163.                 break;
  164.             }
  165.             case PRN:
  166.             {
  167.                 printf( "%i", SS[SP-1] );
  168.                 SP--;
  169.                 break;
  170.             }
  171.             case PRS:
  172.             {
  173.                 signed int i = SP - SS[SP-1] - 2;
  174.                 while( ++i < ( SP - 1 ) )
  175.                 {
  176.                     printf( "%c", (unsigned char)( SS[i] ) );
  177.                 }
  178.                 SP = SP - SS[SP-1] - 1;
  179.                 break;
  180.             }
  181.             case NLN:
  182.             {
  183.                 printf( "\n" );
  184.                 break;
  185.             }
  186.             case INN:
  187.             {
  188.                 scanf( "%i", &(SS[SS[SP-1]]) );
  189.                 SP--;
  190.                 break;
  191.             }
  192.             case INT:
  193.             {
  194.                 SP = SP + CS[IP+1];
  195.                 IP++;
  196.                 break;
  197.             }
  198.             case LDI:
  199.             case LDA:
  200.             {
  201.                 SS[SP] = CS[IP+1];
  202.                 SP++;
  203.                 IP++;
  204.                 break;
  205.             }
  206.             case LDV:
  207.             {
  208.                 SS[SP-1] = SS[SS[SP-1]];
  209.                 break;
  210.             }
  211.             case STO:
  212.             {
  213.                 SS[SS[SP-2]] = SS[SP-1];
  214.                 SP -= 2;
  215.                 break;
  216.             }
  217.             case BRN:
  218.             {
  219.                 IP = CS[IP+1];
  220.                 break;
  221.             }
  222.             case BZE:
  223.             {
  224.                 if( SS[SP-1] == 0 ) IP = ( CS[IP+1] - 2 );
  225.                 SP--;
  226.                 IP++;
  227.                 break;
  228.             }
  229.             case NOP:
  230.             {
  231.                 break;
  232.             }
  233.             case OD:
  234.             {
  235.                 if( ( SS[SP-1] % 2 ) == 0 )
  236.                 {
  237.                     SS[SP-1] = 0;
  238.                 }
  239.                 else
  240.                 {
  241.                     SS[SP-1] = 1;
  242.                 }
  243.                 break;
  244.             }
  245.             default:
  246.             {
  247.                 printf( "\nUnrecognized opcode (%i). I think you're doing it\n"
  248.                         "on purpose to make me mad.  Find me some real code!\n\a", CS[IP] );
  249.                 exit( 2 );
  250.                 break;
  251.             }
  252.         }    // end of switch statement
  253.         IP++;
  254.     }    // end of while loop
  255.  
  256.     printf( "\nProgramme execution finished.\n" );
  257.  
  258.     return;
  259. }
  260.  
  261.  
  262.  
  263. /*
  264.  *    FUNCTION:  LoadProgramme
  265.  */
  266. int LoadProgramme( )
  267. {
  268.     inputfileptr = fopen( "pcode.lst", "r" );
  269.     if( ! inputfileptr )
  270.     {
  271.         printf( "\n\nSorry buddy...  Can't find the opcodes!  Where did\n"
  272.                 "you put the pcode output file?\n\a" );
  273.         exit( 1 );
  274.     }
  275.     else
  276.     {
  277.         signed int i = 0;
  278.         signed int count = 0;
  279.  
  280.         while( ! feof( inputfileptr ) )
  281.         {
  282.             signed int code;
  283.             signed int result;
  284.  
  285.             result = fscanf( inputfileptr, "%i\n", &code );
  286.             if( result != 1 )
  287.             {
  288.                 printf( "\n\nSorry buddy...  Had problems reading the list\n"
  289.                         "of opcodes.  Are you sure this file is ok?\n\a" );
  290.                 exit( 1 );
  291.             }
  292.  
  293.             count ++;
  294.  
  295.             // store the values into the programme buffer
  296.             CS[i++] = code;
  297.  
  298.             switch( code )
  299.             {
  300.                 case INT:
  301.                 case LDI:
  302.                 case LDA:
  303.                 case BRN:
  304.                 case BZE:
  305.                 {    // second code required for these opcodes
  306.                     result = fscanf( inputfileptr, "%i\n", &code );
  307.                     if( result != 1 )
  308.                     {
  309.                         printf( "\n\nHum...something is really wrong.  I attempted to read\n"
  310.                                 "an opcode parameter, but it somehow messed up on me...\n\a" );
  311.                         exit( 1 );
  312.                     }
  313.                     CS[i++] = code;
  314.                     count++;
  315.                 }
  316.             }
  317.         }
  318.         printf( "(read %i opcodes)",count );
  319.     }
  320.  
  321.     fclose( inputfileptr );
  322.     return 0;
  323. }
  324.  
  325.