reignstudios / CS2X

Transpiles a C# subset to non .NET languages. (Powered by Roslyn)
MIT License
134 stars 14 forks source link

WIP DateTime support for C transpiler #2

Closed pietervp closed 8 months ago

pietervp commented 5 years ago

Consider this an experiment, I'd like to get your feedback on. During my tests I ran into a StackoverflowException when trying to convert

public int Day => internalDate.tm_day;

The problem was that the Caller method kept returning the same object instead of 'this'.

Other then that added timer.h to the template and used that in DateTime class. No support for milliseconds though.

zezba9000 commented 5 years ago

Sounds like maybe a bug. When I get a chance I'll take a better look and get you some better feedback on what might be wrong.

Also just FYI, I plan on diving more into finishing the CoreLib API set after I flush out some more stuff listed here and add some missing syntax features I plan on supporting: https://github.com/reignstudios/CS2X/projects/1

zezba9000 commented 5 years ago

Your change looks good and your solution to the bug seems good (thanks for catching this). However it would be good to change it to "&& ((MemberAccessExpressionSyntax)expression.Parent).Expression != expression" as that will give runtime casting errors more specific than a null ref if something breaks. (I pushed this fixed up that way FYI to I can test as well).

Also an oddity with 'time_t' is it can be either 64 or 32 bit (or any bit for that matter). I need to add in an attribute that allows you to alias a C# primitive type to map to a C type in some fashion. So 'Int64' builds out to 'time_t' directly in your case.

Let me add in that feature for you and then I can merge in your pull request when its updated.

zezba9000 commented 5 years ago

Ok pushed up some code to better help handle the special 'time_t' type cases and more. Check out the mock up here: https://github.com/reignstudios/CS2X/blob/master/CS2X.CoreLib/DateTime.cs

Marking a struct as a 'NativeType' will ignore all its operator methods for C code gen. So in the case of time_t we know it will be a numerical primitive and so I we can resolve C# syntax by converting it to an int via a 'implicit operator' with no overhead if you look at the code gen.

Also you can mark 'struct tm' as a NativeType as well and have access to all its fields without having to duplicate the C type in code gen.

For example this would be the preferred approach for this struct as well.

[NativeType(NativeTarget.C)]
struct tm
    {
        public int tm_sec;         /* seconds,  range 0 to 59          */
        public int tm_min;         /* minutes, range 0 to 59           */
        public int tm_hour;        /* hours, range 0 to 23             */
        public int tm_mday;        /* day of the month, range 1 to 31  */
        public int tm_mon;         /* month, range 0 to 11             */
        public int tm_year;        /* The number of years since 1900   */
        public int tm_wday;        /* day of the week, range 0 to 6    */
        public int tm_yday;        /* day in the year, range 0 to 365  */
        public int tm_isdst;       /* daylight saving time             */
    }
zezba9000 commented 8 months ago

This is where runtime work will be handled IL2X: https://github.com/reignstudios/IL2X