-
Notifications
You must be signed in to change notification settings - Fork 0
/
cradle.c
179 lines (158 loc) · 3.19 KB
/
cradle.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
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
#include "cradle.h"
void GetToken()
{
char temp[4];
int length = 0;
char ch = fgetc(fp);
while( ch != EOF && ch != '\0' &&
ch != (char)0x20 && ch != '\n' &&
length < 4)
{
temp[length] = ch;
ch = fgetc(fp);
length++;
}
char *string = malloc(length * sizeof(char));
/*
* Checks if string is actually a number
*/
int isNumber = 1;
int i;
for(i = 0;i<length;i++)
{
if(temp[i] <= 47 ||
temp[i] >= 58)
isNumber = 0;
}
//Assumes number is in base 10
if(isNumber == 0)
{
for(i = 0;i< length;i++)
string[i] = UPCASE(temp[i]);
Look->value.string = string;
Look->strLength = length;
}
else
{
int tempNum = 0;
for(i = 0;i<length;i++)
tempNum = tempNum * 10 + temp[i] - 48;
Look->value.number = tempNum;
Look->strLength = 0;
}
}
void Error(char *s)
{
printf("\nError: %s",s);
}
void Expected(char *s)
{
printf("\n Expected %s",s);
}
/*
* Returns 1 if string cmp matches the string
* in current Look token. String must be delimited
* by newline or '\0'
*/
int TokenType(char* cmp)
{
if(Look->strLength == 0)
return 0;
int i = 0;
while(cmp[i] != '\0' && cmp[i] != '\n')
{
if(cmp[i] != Look->value.string[i])
return 0;
i++;
}
return 1;
}
/*
* Returns true if current token is an operator
*/
int IsOperator()
{
int i;
for(i = 0;i<10;i++)
{
if(TokenType(OpList[i]) == 1)
return 1;
}
return 0;
}
/*
* Returns true if current token is a register, 5
* is number of registers
*/
int IsRegister()
{
int i;
for(i = 0;i < 5;i++)
{
if(TokenType(RegList[i]) == 1)
return 1;
}
return 0;
}
/*
* Returns true if current token is a number
*/
int IsNumber()
{
if(Look->strLength == 0)
return 1;
return 0;
}
/*
* Sets file pointer to instruction corresponding
* to the instruction pointer
*/
void MoveFP(int insp)
{
int InstrCount = 0;
fseek(fp,0,SEEK_SET);
while(InstrCount != insp + 1)
{
GetToken();
if(IsOperator())
InstrCount++;
if(feof(fp))
{
Error("Jumped past EOF");
break;
}
}
}
void Init(char *filename)
{
Look = malloc(sizeof(token));
OpList = (char**) malloc(10 * sizeof(char*));
int i;
for(i = 0;i < 10;i++)
OpList[i] = (char*)malloc(3*sizeof(char));
OpList[0] = "ADD";
OpList[1] = "SUB";
OpList[2] = "SHL";
OpList[3] = "SHR";
OpList[4] = "SET";
OpList[5] = "AND";
OpList[6] = "JMP";
OpList[7] = "NOP";
OpList[8] = "PRT";
OpList[9] = "OR";
RegList = (char**) malloc(5 * sizeof(char*));
for(i = 0;i < 5;i++)
RegList[i] = (char*)malloc(4*sizeof(char));
RegList[0] = "REGA";
RegList[1] = "REGB";
RegList[2] = "REGC";
RegList[3] = "REGX";
RegList[4] = "INSP";
fp = fopen(filename, "r");
if(fp == NULL)
{
Error("No input file specified or file does not exist\n");
exit(1);
}
GetToken();
}