File: interpret.cpp

package info (click to toggle)
aces3 3.0.6-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 82,460 kB
  • sloc: fortran: 225,647; ansic: 20,413; cpp: 4,349; makefile: 953; sh: 137
file content (222 lines) | stat: -rw-r--r-- 5,655 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
*  Copyright (c) 2003-2010 University of Florida
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  The GNU General Public License is included in this distribution
*  in the file COPYRIGHT.
*/ 
/****************************************************
  Lei Wang
  wang@qtp.ufl.edu
  May 2004

  Program which does lexical analysis and syntax analysis of a 
  super instruction assembly language(SIAL).
****************************************************/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include "lexical.h"
#include "keywdcnt.h"
#include "variable.h"
#include "syntax.h"
#include "qcarray.h"
#include "variable.h"
#include "instruct.h"
#include "interpret.h"
#include "parser_interface.h"
using namespace std;

/*#define debuglex*/
extern int outArray[];
extern int outarray_size;
extern int errors;
QCArrayClass QCArray;
VariableClass Variable;
InstructCls Instruct;
VariableClass InsVar;
extern int intconst[];
extern double floatconst[];
int idno=0;
string idname [max_id_size];


void allocidname()
{
    return ;
    //idname=new string [max_id_size];
    // if (!idname)
    // {
    //     printf("error in alloc idname\n");
    //     exit(1);
   //  }
}


/*====================================
readsourcefile reads the infile as characters, and stores them
into sourceArray, and returns the total number of characters
as integer. return -1 if there is an error.
====================================*/
int readsourcefile(char* infilename, char sourceArray[])
{
    int i=0;
    char temp;
    ifstream infile;

    infile.open(infilename);
    if(!infile)
    {
        cerr <<"Error. can not open source file:" <<infilename<<endl<<endl;
        return -1;
    }

    while(infile.get(temp)&&i<max_input_size)
    {
        /*convert to lower case*/
        if (temp<='Z'&&temp>='A')
            temp=temp+'a'-'A';

        sourceArray[i++]=temp;
    }
    infile.close();
    infile.clear();     /*have to use this in NT, but don't have to in Unix*/
    return i;
}



/*====================================
main function: will call some other functions to realize the requirement of 
this project. Because for this project the output is to the screen, some 
of the lines are set for comments for further usage of output to file.
====================================*/
int interpret (char* infilename, int lext, int indext, int arrayt, int inst)
{
/*
    char infilename[256];
    string outfilename ="out.txt";
    ofstream outfile (outfilename.c_str());
*/
    char *sourceArray;

    sourceArray=new char[max_input_size];
    if (!sourceArray)
    {
        printf("error in allocate sourceA. Please contact code writer with your input.");
        exit(1);
    }
    int sizeofsource = 0;

    int rnumber=1;

    // Set up global objects  

    QCArray.initializer();
    Variable.initializer();
    Instruct.initializer();
    InsVar.initializer();

    sizeofsource = readsourcefile(infilename, sourceArray);

    if (sizeofsource <0)
        return sizeofsource;

    lexical(sourceArray, sizeofsource, idno, idname, rnumber);


    int pos=0;
    Variable.insertconstant();
    functionsyntax(pos, rnumber);
/*
    if (errors==0)
    {
        printf("=           No error is printed by now,                   =\n");
        printf("=           it is safe to go to the next step.            =\n");
    }
*/
    if (errors)
    {
    printf("===========================================================\n");
        printf("=           errors found.                                 =\n");
        printf("=           please correct it before go to the next step. =\n");
    printf("===========================================================\n");
    }
    else
    {
    printf("   No compilation errors\n");
    }

    if (lext)
    {
        printf("\nThe following is printed for testing.\n\n");
        printf("\nlexical analysis\n");
        printf("================\n");
        printf("idno=%d\n", idno);

        int iconstprint=0;
        int fconstprint=0;
        for (int i=0, ii=0; i<outarray_size; i++)
        {
            if (outArray[i]==id)
            {
                printf("id{%s} ", idname[ii].c_str());
                ii++;
            }
            else if (outArray[i]==iconst)
            {
                printf("cosnt (%d)", intconst[iconstprint++]);
            }
            else if (outArray[i]==fconst)
            {
                printf("cosnt (%f)", floatconst[fconstprint++]);
            }

            else
            {
                printf("%s ", keywords[outArray[i]]);
            }
        }
    }

    if (indext)
    {
        printf("\nindex table\n");
        printf("================\n");
        Variable.print();
    }
    if (arrayt)
    {
        printf("\narray table\n");
        printf("================\n");
        QCArray.print();
    }
    if (inst)
    {
        printf("\ninstruction table\n");
        printf("================\n");
        Instruct.output();
    }
    
    if (errors==0)
    {
        Variable.outputll();
        QCArray.outputll();
        Instruct.outputll();
        return 0;
    }

    delete []sourceArray;
    return 1;
}