forked from ground/cground
Add int, char, and bool to compiler
This commit is contained in:
@@ -102,7 +102,7 @@ Commands:
|
||||
|
||||
CGround now includes an experimental Ground -> x86_64 Linux ASM compiler. You can try it by adding the `-c` flag when running Ground. This will print the generated ASM to the console.
|
||||
|
||||
At present only the `int` data type is supported.
|
||||
At present only the `int`, `char`, and `bool` data types are supported.
|
||||
|
||||
Supported instructions so far:
|
||||
|
||||
|
||||
@@ -114,10 +114,10 @@ GroundValueType getInstructionReturnType(GroundInstruction* gi, VariableTable* t
|
||||
}
|
||||
case SET: {
|
||||
if (gi->args.length == 2) {
|
||||
if (gi->args.args[0].type == VALUE) {
|
||||
return gi->args.args[0].value.value.type;
|
||||
} else if (gi->args.args[0].type == VALREF) {
|
||||
VariableInfo* var = getVariable(table, gi->args.args[0].value.refName);
|
||||
if (gi->args.args[1].type == VALUE) {
|
||||
return gi->args.args[1].value.value.type;
|
||||
} else if (gi->args.args[1].type == VALREF) {
|
||||
VariableInfo* var = getVariable(table, gi->args.args[1].value.refName);
|
||||
if (var == NULL) {
|
||||
return NONE;
|
||||
}
|
||||
@@ -177,6 +177,9 @@ GroundValueType getInstructionReturnType(GroundInstruction* gi, VariableTable* t
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
if (rightType == INT && leftType == INT) {
|
||||
return INT;
|
||||
}
|
||||
return NONE;
|
||||
} else {
|
||||
return NONE;
|
||||
@@ -223,6 +226,9 @@ GroundValueType getInstructionReturnType(GroundInstruction* gi, VariableTable* t
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
if (rightType == INT && leftType == INT) {
|
||||
return INT;
|
||||
}
|
||||
return NONE;
|
||||
} else {
|
||||
return NONE;
|
||||
@@ -269,6 +275,9 @@ GroundValueType getInstructionReturnType(GroundInstruction* gi, VariableTable* t
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
if (rightType == INT && leftType == INT) {
|
||||
return INT;
|
||||
}
|
||||
return NONE;
|
||||
} else {
|
||||
return NONE;
|
||||
@@ -285,13 +294,29 @@ char* processValueString(GroundArg arg) {
|
||||
return buf;
|
||||
}
|
||||
if (arg.type == VALUE) {
|
||||
if (arg.value.value.type != INT) {
|
||||
printf("Only int is supported right now\n");
|
||||
exit(1);
|
||||
switch (arg.value.value.type) {
|
||||
case INT: {
|
||||
char* buf = malloc(sizeof(char) * 64);
|
||||
snprintf(buf, sizeof(char) * 260, "%" PRId64, arg.value.value.data.intVal);
|
||||
return buf;
|
||||
break;
|
||||
}
|
||||
case BOOL: {
|
||||
return arg.value.value.data.boolVal ? "1" : "0";
|
||||
break;
|
||||
}
|
||||
case CHAR: {
|
||||
char* buf = malloc(8);
|
||||
snprintf(buf, 8, "%d", (int) arg.value.value.data.charVal);
|
||||
return buf;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
printf("For now, only int, bool, and char are supported data types in the compiler.");
|
||||
exit(1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
char* buf = malloc(sizeof(char) * 64);
|
||||
snprintf(buf, sizeof(char) * 260, "%" PRId64, arg.value.value.data.intVal);
|
||||
return buf;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user