Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: C# backend #155

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

WIP: C# backend #155

wants to merge 2 commits into from

Conversation

rockwalrus
Copy link

I use Unity in my day job, and looked into using Inform 7 with it. While it's technically possible to use the two together using the C backend, there's still quite a lot of impedance mismatch. I got permission to work about one week a quarter on a C# backend, which I started working on in March 2023. I'm pleased to report it's now in good enough shape to be able to run the Eg1.i7 Hello World example (modulo a small, fixable issue to be described later) and I've gotten permission for it to eventually be upstreamed if you so desire.

The one issue preventing use out-of-the-box is that the code generator generates a few gotos that jump into the middle of a block. C# requires gotos targets to be in the lexical scope of the goto statement. That's a pretty reasonable sounding requirement, just one that we don't adhere to. I've experimented by hand and it seems to be possible to fix the problematic code generation by lowering any blocks the target is inside into sections of code guarded by if (!condition) goto if1; and similar constructs. (Brings back memories of writing unstructured BASIC as a kid!) Presumably the best way to do this is to traverse the Inter tree and massage it a bit before calling the code generation on the function, but I'm open to advice on the best way to do that.

The impedance mismatch can further be reduced by using the native C# class system instead of a port of the bespoke C implementation. This is not yet implemented. That should also allow greatly reducing the amount of name mangling required.

int and bool are different types in C#, but the code generation currently handles this complication very naively through abundant and often unnecessary calls to System.Convert.ToBoolean and System.Convert.ToInt32. Most of these could be avoided if the caller knew whether the output was going to be used in a boolean context.

Other miscellaneous improvements needed are a comprehensive update of the text of the files to match the code and eventually to remove the comments currently generated by the backend to aid in locating the code that generated certain constructs.

This is enough to make Hello World work, modulo a few issues that can't
be solved with a straight port.
@ganelson
Copy link
Owner

This is a very interesting development. I'd certainly like Inform to be able to play nicely with Unity, and clearly we have at least a working draft of C# output. So in principle I'm absolutely open to adding this.

It's quite a large donation of code and was written on your employer's time, so I guess we should be careful about the legalities here. Perhaps you could write me an email?

The best way to verify the rightness of this is probably to use intest to run all of the current C tests against C# instead, then compiling the result in C#, and checking that the textual output via C# matches that via C. If it does, then I think the C# implementation can be declared to be working, and maybe that's the best time to worry about refactoring into more suitable classes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants