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

Transpiling to C #5

Open
3 tasks
julian-hartl opened this issue Nov 11, 2023 · 0 comments
Open
3 tasks

Transpiling to C #5

julian-hartl opened this issue Nov 11, 2023 · 0 comments
Labels
enhancement New feature or request
Milestone

Comments

@julian-hartl
Copy link
Owner

julian-hartl commented Nov 11, 2023

Transpiling to C

Motivation

Transpiling to C allows leveraging the widespread support and efficiency of C compilers, enabling our compiler to target
virtually any platform. This approach not only expands compatibility but also paves the way for generating raw
executables, potentially enhancing performance over running programs in an interpreter. The advantages of using C as an
intermediary language include improved performance and compatibility with a wide range of systems and tools.

Necessary Steps

  • Transpile internal AST to C-AST: Convert the abstract syntax tree (AST) of our language into a C-compatible
    AST. This step is crucial for aligning our language's structure with C's syntax and semantics.
  • Generate valid C code from C-AST: Transform the C-AST into syntactically and semantically correct C code,
    ensuring functional equivalence with the original code.
  • Compile C code using any C compiler: Utilize existing C compilers to compile the generated C code, benefiting
    from the optimizations and support provided by these compilers.

Possible Challenges

Transpiling from a language with certain expression capabilities to C, which is predominantly statement-based, presents
unique challenges. For example, constructs that are expressions in our language might need to be converted into
statements in C, potentially requiring additional variables or restructuring of the code.

Challenge 1: if as an Expression

  • Original Code:
    func main() -> int {
        let a = 10
        let b = if a == 10 { 2 } else { 3 }
        return b
    }
  • Transpiled Code:
    int main(void) {
      int a = 10;
      int _0;
      if (a == 10) {
          _0 = 2;
      } else {
          _0 = 3;
      } 
      int b = _0;
      return b;
    }
  • Explanation: Here, the if expression in our language is converted into a statement in C. This necessitates the
    introduction of a temporary variable (_0) to hold the result of the conditional expression.

Challenge 2: Loop Condition with Assignment

  • Original Code:
    let a = 0;
    while (a = { a + 1 }) < 10 {
    }
  • Naive Transpilation:
    int a = 0;
    int _0 = a + 1;
    while ((a = _0) < 10) {
    }
  • Explanation:
    In this case, directly translating the loop condition results in incorrect behavior in C, as the loop condition isn't updated properly in each iteration. A more sophisticated translation approach is needed to maintain the semantics.
@julian-hartl julian-hartl added this to the Alpha milestone Nov 11, 2023
@julian-hartl julian-hartl added the enhancement New feature or request label Nov 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant