-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.c
145 lines (128 loc) · 4.27 KB
/
context.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
//
// Created by colin on 5/15/2021.
//
#include "context.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void goNext(context_t* context) {
context->currentLen = getCurrentLen(context->executionPoint);
context->executionPoint += (context->currentLen) + 1;
if(*(context->executionPoint) == 0){
context->currentLen = 0;
//printf("End of program\n");
return;
}
context->currentLen = getCurrentLen((context->executionPoint));
}
char* lookAhead(context_t* context, char* begin, int numWords) {
for(int i = 0; i < numWords; i++) {
while ((*begin) != ' ' && (*begin) != 0) {
begin++;
}
if ((*begin) == 0) {
//printf("lookAhead reached end of program.\n");
return begin;
}
begin++; //Begin now points to the beginning of the next word.
}
return begin;
}
int getLenNext(context_t* context) {
char* copy = context->executionPoint + context->currentLen + 1;
int i = 0;
//Get the length of the next word, without null terminator
while((*copy) != ' ') {
copy += 1;
i++;
}
return i + 1;
}
int getCurrentLen(char* string) {
int i = 0;
while(*(string + i) != ' ' && *(string) != 0) {
i++;
}
return i; //Number of characters till the whitespace, inclusive.
}
void safeCopy(char* source, char* dest, int len) {
for(int i = 0; i < len; i++) {
*(source + i) = *(dest + i);
}
}
void init(char* program, context_t* context){
//context->executionPoint = program;
for(int i = 0; i < strlen(program); i++){
if (*(program + i) == '\n'){
*(program + i) = ' ';
}
}
context->currentLen = getCurrentLen(program);
context->addressStack = malloc(sizeof(stack_t));
context->conditionalStack = malloc(sizeof(stack_t));
context->stack = malloc(sizeof(stack_t));
context->stack->size = 0;
context->stack->top = NULL;
context->addressStack->size = 0;
context->addressStack->top = NULL;
context->conditionalStack->size = 0;
context->conditionalStack->top = NULL;
context->globals = malloc(sizeof(varList_t));
context->globals->first = NULL;
context->globals->size = 0;
context->customWords = malloc(sizeof(funcList_t));
context->customWords->first = NULL;
context->customWords->size = 0;
setProgram(program, context);
}
void contextDestructor(context_t* context){
clearContext(context);
}
void setProgram(char* program, context_t* context){
char* lookAt = program;
int programLength = strlen(program); //DEBUG
char* pos2 = program + 1; char* pos3 = program + 2; char* pos4 = program + 3;
while((*lookAt) != 0){
lookAt = lookAhead(context, lookAt, 1);
}
if(*(lookAt - 1) != ' '){
//printf("Adding whitespace.\n");
unsigned long size = strlen(program) + 2; // + whitespace + nullterm
context->programBegin = malloc(size);
strncpy(context->programBegin, program, size);
(context->programBegin)[size-2] = ' '; (context->programBegin)[size-1] = 0;
}
else {
context->programBegin = malloc(programLength + 1);
strncpy(context->programBegin, program, programLength);
(context->programBegin)[programLength] = 0;
}
context->executionPoint = context->programBegin;
context->currentLen = getCurrentLen(context->executionPoint);
}
void clearProgram(context_t* context){
free(context->programBegin);
}
void clearContext(context_t* context){
free(context->programBegin); //Need to reset to beginning of program.
while(context->conditionalStack->size > 0){
popLong(context->conditionalStack);
}
while(context->addressStack->size > 0){
popString(context->addressStack);
}
while(context->stack->size > 0){
popLong(context->stack);
}
del(context->globals);
delFuncs(context->customWords);
}
void dumpContext(context_t* context) {
printf("\n__CONTEXT__\n");
printf("__DATA__\n");
dumpStack(context->stack);
printf("__VARS__\n");
dumpVars(context->globals);
printf("__USERDEFINED__\n");
dumpFunc(context->customWords);
}