-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgetvariable.c
More file actions
47 lines (44 loc) · 1.82 KB
/
getvariable.c
File metadata and controls
47 lines (44 loc) · 1.82 KB
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
/*
*******************************************************************
*** This software is copyright 2021 by Michael H Riley ***
*** You have permission to use, modify, copy, and distribute ***
*** this software so long as this copyright notice is retained. ***
*** This software may not be used in commercial applications ***
*** without express written permission from the author. ***
*******************************************************************
*/
#include "header.h"
word getVariable(char* name) {
int i;
int vsize;
char vtype;
vtype = (use32Bits) ? 'L' : 'I';
if (name[strlen(name)-1] == '!') vtype = 'R';
if (name[strlen(name)-1] == '$') vtype = 'S';
if (name[strlen(name)-1] == '#') vtype = 'D';
if (vtype == 'S') vsize = 2;
else if (vtype == 'R') vsize = 4;
else if (vtype == 'D') vsize = 8;
else if (vtype == 'I') vsize = 2;
else if (vtype == 'L') vsize = 4;
for (i=0; i<numberOfVariables; i++) {
if (strcasecmp(name, variableNames[i]) == 0) return variableAddresses[i];
}
numberOfVariables++;
if (numberOfVariables == 1) {
variableNames = (char**)malloc(sizeof(char*));
variableAddresses = (word*)malloc(sizeof(word));
variableTypes = (char*)malloc(sizeof(char));
}
else {
variableNames = (char**)realloc(variableNames, sizeof(char*) * numberOfVariables);
variableAddresses = (word*)realloc(variableAddresses, sizeof(word) * numberOfVariables);
variableTypes = (char*)realloc(variableTypes, sizeof(char) * numberOfVariables);
}
variableNames[numberOfVariables-1] = (char*)malloc(strlen(name)+1);
strcpy(variableNames[numberOfVariables-1], name);
variableAddresses[numberOfVariables-1] = variableNextAddress;
variableTypes[numberOfVariables-1] = vtype;
variableNextAddress += vsize;
return variableAddresses[numberOfVariables-1];
}