-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodegenerator.c
143 lines (129 loc) · 3.99 KB
/
codegenerator.c
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
/*
CS3500 - Software Engineering Project
Created by Colin Kelleher
Contributed to by:
Jonathan Hanley
Karol Przestrzelski
Liam de la Cour
*/
/*
Include & Define statement block
<stdio.h> is useful for dealing with inputs and outputs
<string.h> is useful for dealing with strings & arrays of characters
<ctype.h> is useful for testing and mapping characters / character handling
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "codegenerator.h"
/*
defining the input & output files - ease of maintenance and
only have to change in one place should these files need to be
renamed
*/
#define _inputFile_ "postfixed.txt"
#define _outputFile_ "codegenerated.txt"
/*
INSTRUCTION SET
ease of maintenance and only have to change in one place
should these insructions need to be given new values/ new instruction
set.
*/
#define floatinstr "LOADFLOAT"
#define intinstr "LOADINT"
#define multiplyinstr "MUL"
#define divideinstr "DIV"
#define addinstr "ADD"
#define subtractinstr "SUB"
/*
The string array that holds the corresponding keys associated with each integer value
Assigned in startCodeGenerator
*/
char * instructionKeys[] = {floatinstr, intinstr, multiplyinstr, divideinstr, addinstr, subtractinstr};
void readLine(char lineread[]){
// 3
FILE * inputFile;
inputFile = fopen(_inputFile_, "r");
// fgets(pointer to array of chars, max num of chars to be read, file pointer)
fgets(lineread,100,inputFile);
fclose(inputFile);
}
void classifyChar(CharacterType *charType, char character){
// || - if isdigit(character) OR character == '.' is true
// If one of them is true run the code following
if (isdigit(character) || character == '.'){
charType->content[charType->pointer] = character;
charType->pointer ++;
// if there is a '.' it is assigned a type of 0, corresponding to a float
if (character == '.'){
charType->type = 0;
}
}
// If the character ='*' meaning multiplication, type 2 is assigned
else if (character == '*') {
charType->type = 2;
} else if (character == '/') {
charType->type = 3;
} else if (character == '+') {
charType->type = 4;
} else if (character == '-') {
charType->type = 5;
}
}
/*
This is the 'main' function where the code is ran from.
1) I open & close the output file to reset it back to an original blank file (this
is to ensure that the file is clear of the previous calculation)
2) I am creating the struct object to be used for Int, Float & Operators
and store them in this struct
3) I am openeing the input file in "r" = read mode. I am reading the line from the
input file. I only read a line as this is all that is required, as everything should
always be on a single line following infix2postfix and according to the documentation
The inputted string is stored in lineread
4) I am checking the type of the inputter characters(numbers/operators)Here.
If they contrain a '.' they are assigned a type of 0 which means it is a float,
1 if it is an Integer.....and so on... resultToFile is then called to write
the Instruction to the file
The output file is then closed
*/
int startCodeGenerator () {
// 1)
FILE *outputFile;
outputFile = fopen(_outputFile_, "w");
// 2
CharacterType *charType = newCharacterType();
// 3
char lineread[100];
readLine(lineread);
char character;
// 4
for (int index = 0; index < strlen(lineread); index ++) {
character = lineread[index];
// Infix to postfix has a space seperating each part so write on that
// Set the key (instruction) first
if (character == ' ')
resultToFile(outputFile, charType, instructionKeys);
else
classifyChar(charType, character);
}
fclose(outputFile);
return 0;
}
#ifdef NOMAIN
int main(){
return startCodeGenerator();
}
#endif
/*
Sample Input:
1.33 24.6 366 * + / -
Sample Output:
LOADFLOAT 1.33
LOADFLOAT 24.6
LOADINT 366
MUL
ADD
DIV
SUB
*/