My goal is to write a smaller compiler-like program, which allows me to draw geometric shapes into a 3D Diagram. There is no need for turing-completeness and the program should only be viewed as an exercise and not as a program used by anybody. However, I want the program to have a compiler-like nature.
At the moment the user provides a text file like this:
(1,45,6)
(7,8,5)
(10,77,88)
(99999,1,1)
(5,7,6)
(1,2,3)
(4,5,6)
These points will be translated into a python file, which will later display all points in a 3D-Diagram when executed. For the moment, I just want it to print out a list of points when executed.
--> [(1, 45, 6), (7, 8, 5), (10, 77, 88), (99999, 1, 1), (5, 7, 6), (1, 2, 3), (4, 5, 6)]
The python file looks like this:
list = []
list.append((1,45,6))
list.append((7,8,5))
list.append((10,77,88))
list.append((99999,1,1))
list.append((5,7,6))
list.append((1,2,3))
list.append((4,5,6))
print(list)
Therefore, I build the following code using C (just to improve C-skills, I am aware that writing it in python would be more applicable)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
size_t seek(char* buffer, size_t start, const size_t end, char to_be_seeked);
int translateString2Number(char* c, long length);
int power(int base, int exponent);
int main(int argc, const char * argv[]) {
if(argc <= 2)return -1;
char file_name[100];
strncpy(file_name, argv[1], 100);
FILE* fp = fopen(file_name, "read");
if(!fp)return -1;
fseek(fp, 0, SEEK_END);
const size_t elements_num = ftell(fp);
rewind(fp);
remove("translation.py");
FILE * python_file_pointer = fopen("translation.py", "ab+");
fprintf(python_file_pointer, "list = []\n");
//Do parsing
char* buffer = malloc(sizeof(char) * elements_num);
fread(buffer, elements_num, 1, fp);
size_t start = 0;
while(start < elements_num){
if(buffer[start] != '(') return -1;
size_t end = seek(buffer, start, elements_num, ')');
if(end == -1) return -1;
size_t comma_pos[2];
comma_pos[0] = seek(buffer, start, end, ',');
comma_pos[1] = seek(buffer, comma_pos[0]+1, end, ',');
if(comma_pos[0] == -1 || comma_pos[1] == -1 )return -1;
char first_number_size = comma_pos[0]-start-1;
char first_number[first_number_size];
for(size_t i = 0; i < first_number_size; i++){
first_number[i] = buffer[start+1+i];
}
char second_number_size = comma_pos[1]-comma_pos[0]-1;
char second_number[second_number_size];
for(size_t i = 0; i < second_number_size; i++){
second_number[i] = buffer[comma_pos[0]+1+i];
}
char third_number_size = end - comma_pos[1]-1;
char third_number[third_number_size];
for(size_t i = 0; i < third_number_size; i++){
third_number[i] = buffer[comma_pos[1]+1+i];
}
if( (first_number_size < 0) || second_number_size < 0|| third_number_size < 0){
return -1;
}
if( (first_number_size > 11) || second_number_size > 11|| third_number_size > 11){ //Avoid potential overflow
return -1;
}
int first = translateString2Number(first_number, first_number_size);
int second = translateString2Number(second_number, second_number_size);
int third = translateString2Number(third_number, third_number_size);
fprintf(python_file_pointer, "list.append((%d,%d,%d))\n", first,second,third);
const size_t value = seek(buffer, end, elements_num, '\n');
if(value == -1)break;
start = value+1;
}
fprintf(python_file_pointer, "print(list)\n");
fclose(python_file_pointer);
system("python3 translation.py");
fclose(fp);
}
int power(int base, int exponent){
int result = 1;
for(int i = 0; i < exponent; i++){
result *= base;
}
return result;
}
int translateString2Number(char* c, long length){
int res = 0;
for(int i = 0; i < length; i++){
res += (c[i]-'0')*power(10, (int)(length-i-1));
//printf("\n%d", res);
}
return res;
}
size_t seek(char* buffer, size_t start, const size_t end, char to_be_seeked){
do{
if(buffer[start] == to_be_seeked)return start;
} while(++start < end);
return -1;
}
system("python3 translation.py");
to generate the output. \$\endgroup\$size_t seek(const char *buffer, size_t start, size_t end, char to_be_seeked);
\$\endgroup\$