cimplec / sim-c

A dynamically typed high-level front end for C

Home Page:https://cimplec.github.io/docs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG REPORT] Incorrect compilation of ptr_only_assign opcode

frankhart2018 opened this issue · comments

Describe the bug
The opcode ptr_only_assign gets compiled as * = =, when it should get compiled as * =

simC Code

MAIN
	var a = 1
	var *ptr = &a
	*ptr = 2
END_MAIN

C Code

#include <stdio.h>

int main() {
	int a = 1;
	int *ptr =  & a;
	**ptr = =;

	return 0;
}

How do I reproduce this bug?

The simc code:
MAIN

print( "Hello World\n" )
var a = 13
var *ptr = &a

print( "Var 'a' is: \n" )
print( a )
print( "\n" )

print( "Pointer to 'a' is: \n" )
print( ptr )
print( "\n" )

print( "Value of pointer to 'a' is: \n" )
print( *ptr )
print( "\n" )

END_MAIN

We get this C code:
#include <stdio.h>

int main() {

printf("Hello World\n");
int a = 13;
float *ptr =  & a;

printf("Var 'a' is: \n");
printf("%d", a);
printf("%c", '\n');

printf("Pointer to 'a' is: \n");
printf("%d", ptr);
printf("%c", '\n');

printf("Value of pointer to 'a' is: \n");
printf("%d",  * ptr);
printf("%c", '\n');
return 0;

}

This code seems okay to me.

Actually, on a side note, Sim-C is dynamically typed, so this should apply to pointers as well, right?

For this simc code:
MAIN

print( "Hello World\n" )
var a = 13
var *ptr = &a

print( "Value of pointer to 'a' is: \n" )
print( *ptr )
print( "\n" )

var b = 3.14

ptr = &b

print( "Pointer to 'b' is: \n" )
print( ptr )
print( "\n" )

print( "Value of pointer to 'b' is: \n" )
print( *ptr )
print( "\n" )

END_MAIN

We get this C code:
#include <stdio.h>

int main() {

printf("Hello World\n");
int a = 13;
float *ptr =  & a;

printf("Value of pointer to 'a' is: \n");
printf("%d",  * ptr);
printf("%c", '\n');

float b = 3.14;
ptr =  & b;
printf("Pointer to 'b' is: \n");
printf("%f", ptr);
printf("%c", '\n');

printf("Value of pointer to 'b' is: \n");
printf("%f",  * ptr);
printf("%c", '\n');

return 0;

}

This C code gives us the following error:
image

I think this simc code should produce a valid C code, but I'm not too sure.
Hope this made sense.