Created
November 28, 2024 22:59
-
-
Save joseoliv/c333aaaa477d05c341055a96f17a8b9c to your computer and use it in GitHub Desktop.
Dynamically typed language translation to C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// also in https://www.onlinegdb.com/edit/xhqFlnv8f | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <malloc.h> | |
struct TypeData { | |
void (**virtualTable)(void); | |
char **methodNameList; | |
int methodNameListSize; | |
char *typeName; | |
}; | |
// The struct, a type, that IS the class MyList | |
typedef struct MyListClass { | |
struct TypeData *type; | |
int *data; | |
} MyListClass ; | |
int get(MyListClass *self, int index) { | |
return self->data[index]; | |
} | |
int set(MyListClass *self, int index, int value) { | |
self->data[index] = value; | |
} | |
// functions insert, remove, and size were not codified | |
// This represents the data associated with class MyList. That is, | |
// the class name, the number of methods, and pointers to methods. | |
// The methods are get, set, insert, remove, and size. | |
struct TypeData myListClass = { | |
( void (*[])(void) ) { (void (*)(void)) get, ( void (*)(void)) set }, | |
(char *[]) { "get", "set", "insert", "remove", "size" }, | |
5, | |
"MyList" | |
} ; | |
void main() { | |
/* | |
This is the code generated for the expression "a.get(0)" in the | |
following code: | |
int k; | |
var a = new MyList(4, 5, 7); | |
k = a.get(0) | |
in a dynamically-typed language | |
*/ | |
// int *data = (int *) malloc(sizeof(int)*3); | |
// data[0] = 4; | |
// data[1] = 5; | |
// data[2] = 7; | |
int data[] = { 4, 5, 7 }; | |
int k = -1; | |
// whenever an object of MyListClass is created, fields | |
// 'type' and 'data' should be initialized. This usually is | |
// made by the constructor of the class. The user is not aware | |
// of this. | |
struct MyListClass *aList = (MyListClass *) malloc(sizeof(MyListClass)); | |
aList->type = &myListClass; | |
aList->data = (int *) data; | |
int r = 0; | |
int i = 0; | |
// The code between // #begin and // #end | |
// is the translation of the message passing "a.set(0, 17);" | |
// in an object-oriented language to language C. | |
// first step is look for the method in the virtual table | |
// #begin | |
int resultIndex = -1; | |
for(; i < aList->type->methodNameListSize; i++) { | |
if (aList->type->methodNameList[i] == "set") { | |
resultIndex = i; | |
break; | |
} | |
} | |
if ( resultIndex < 0 ) { | |
printf("Runtime type error: method 'set' not found in class \n"); | |
exit(1); | |
} | |
else { | |
((int (*)(MyListClass *, int, int)) aList->type->virtualTable[resultIndex])(aList, 0, 17); | |
} | |
// The code between // #begin and // #end | |
// is the translation of the assignment "r = a.get(0)" | |
// in an object-oriented language to language C. | |
// first step is look for the method in the virtual table | |
// #begin | |
resultIndex = -1; | |
for(i = 0; i < aList->type->methodNameListSize; i++) { | |
if (aList->type->methodNameList[i] == "get") { | |
resultIndex = i; | |
break; | |
} | |
} | |
if ( resultIndex < 0 ) { | |
printf("Runtime type error: method 'get' not found in class \n"); | |
exit(1); | |
} | |
else { | |
r = ((int (*)(MyListClass *, int)) aList->type->virtualTable[resultIndex])(aList, 0); | |
} | |
// #end | |
printf("aList[0] = %d\n", r); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment