-
Notifications
You must be signed in to change notification settings - Fork 0
/
phase1.cpp
105 lines (82 loc) · 2.07 KB
/
phase1.cpp
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
/**
* @file phase1.cpp
* @author Zhouliang Yu ([email protected])
* @brief
* @version 0.1
* @date 2022-02-20
*
*/
//------------------------------------
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#include "LableTable.h"
#include <sstream>
#include "phase1.h"
using namespace std;
//------------------------------------
/*! Declaration of global variables
*
*/
int Label_idx = 0;
LableTable LableMap;
//------------------------------------
/*!
* To Detect the comment sysmbols and empty lines
*/
bool Comment_blank_detector(string line)
{
std::string copy (line);
std::string detector;
istringstream iss(copy);
iss >> detector;
return detector == "#" || detector == "";
}
void Lable_Detector(ifstream &in_file, int &Label_idx){
string line;
string p1;
string p2;
bool is_text = true;
while (!in_file.eof()){
getline(in_file, line);
istringstream iss(line);
iss >> p1 >> p2;
if (p1 == ".text") is_text = true;
if (p1.find('.') != string::npos && p1 != ".text") is_text = false;
//skip the blank, comments and .data fields
if (!Comment_blank_detector(line) && is_text && line != ".text")
{
//find the ":"
if (p1.find(':') != string::npos) {
//store the label in the LabelTable
int pos = p1.find(':');
string Lable = p1.substr(0, pos);
LableMap[Lable] = Label_idx;
continue;
}
if (p2.empty())
{
continue;
}
p2= "";
Label_idx += 1;
}
}
}
/*!
* @param filename
* @return a map contains Labels and corresponding addresses
*/
LableTable phase1(char* filename)
{
FILE *fp1;
fp1 = fopen(filename, "r");
if (fp1 == NULL) {
printf("Error: Files are not open correctly \n");
}
ifstream in_file;
in_file.open(filename);
Lable_Detector(in_file, Label_idx);
fclose(fp1);
return LableMap;
}