Home
 

ctrl.C

ctrl.C

/* header */
/*
 *  ctrl.C  ControllerCls Implementation Module
 */

#include <iostream.h>
#include <stdio.h>
#include <string.h>

#include "scanparse.h"
#include "p_tree.h"
#include "symtab.h"

#include "ctrl.h"
/* end-header */

// global declaration
bool debugFlag;

// static data
int OptionCls::list = 0;
int OptionCls::emit = 1;

OptionCls :: OptionCls() {
    if (debugFlag) cout << "OptionCls() " << endl;
}

int OptionCls :: option_info() {
    return (list );
}
/* end-OptionCls */
int  ControllerCls :: open_file(char* source_file) {
    if (debugFlag) cout << "ControllerCls::open_file()" << source_file << endl;
    int length = strlen(source_file);
    //Check for  .p  extension
    if ((length > 1) && ((source_file[length -2] == '.') &&
                     (source_file[length -1] == 'p')  )) {
    if (!freopen(source_file, "r", stdin)) {
        cout << " Cannot open file -- Sorry " << endl;
        //should be done by an ErrorCls object 
        return 0;
    } else {
        return 1;
    }
    } else if (length == 0) {
        cout << " No file specified" << endl;
        return 0;
    } else {
        cout << " File must have a  .p  extension" << endl;
        //should be done by an ErrorCls object 
    return 0;
    }
}
/* end-ControllerCls-open */

ControllerCls :: ControllerCls(int argc, char** argv) {
    if (debugFlag) cout << "ControllerCls() " << endl;


    this -> std_table = new SymtabCls; 
    PScopeCls scp = new ScopeCls;
    scp -> vista = this -> std_table;

    char *source_file = new char[80];
    this -> parse_tree = 0;
    if (argc <= 1) {
    cout << " Usage:  epc [-elds] <filename>.p" << endl;
    return;
    } else {
    for (int i = 1; i < argc; i++) {
        if (*argv[i] == '-') {
        while (*++argv[i]) {
            switch(*argv[i]) {
            case 'l':
            OptionCls :: list = 1;
            continue;

            case 'e':
            OptionCls :: emit = 2;
            continue;

            case 'd':
                        debugFlag = true;
                        continue;

            default:
            cerr << "Unknown option " << 
                    *argv[i] << endl;
            }
        }
        } else {
        source_file = argv[i];
        }
    }
    }
    if (open_file(source_file)) {
    ios::sync_with_stdio();
    PScanParseCls sp = new ScanParseCls;
    this -> parse_tree = sp -> parse_tree;

    if (OptionCls::emit) {
        parse_tree -> emit();
    } else {
            cout << "Unable to execute: interpreter not implemented." << endl;
    }
    }
}
/* end-ControllerCls-Cont */    

int MMCls::mem_pos = 0;

int MMCls::allocate(int size) {
  int mem;

  if (mem_pos + size < 200) {
    mem = mem_pos;
    mem_pos +=size;
  }
  else {
    cout << "error: memory overflow!" << endl;
    exit(1);
  }

  return mem; 
}