Compiling rust problem

#1

I am actually writing a rust to Java transpiler and I’ve got many problems:
My compiler can read rust code like that:

fn main() {
   println!("{}",runtest(1.3));
}
fn runtest(a: f32) -> f32 {
  a * 8.0 - 6.0
}

but it don’t understand the match expression, the if let statement the usage of if like a statement …
So I want something to compile the rust to a more simple rust.

fn main() {
    println!("{}",if "test" == "test" {
1.0
} else {
0.0
});
}

This code don’t work.
Thank’s reading me,
ccgauche.

0 Likes

#2

if let can be simplified to a match. However, match doesn’t have a simpler Rust equivalent. It needs to be able to read enum's internal state. How enums are tagged is an implementation detail of the Rust compiler.

So to implement match you’d first need to implement enum in your compiler.

0 Likes

#3

Yes but for the return for a block like:

if expression {
 SomeValue
} else {
 AnotherValue
}

I’ve watched MIR HIT LIR.

0 Likes

#4

Sorry, I’m not sure what you’re asking here. About Rust’s semantics? About Java equivalent of that code?

0 Likes

#5

I want this to be a more simple rust code where if is not a statement:

fn a() {
let a = if expression {
 SomeValue
} else {
 AnotherValue
}
}

0 Likes

#6

I’m not sure if this is what you’re asking…

In this example, if is an expression. If the let a = part wasn’t there, it would be a statement. But assuming you meant “expression”:

fn a() {
    let a;
    if expression {
        a = { SomeValue };
    } else {
        a = { AnotherValue };
    }
}
0 Likes

#7

Yes it is that but are you sure that we can create let a; without any error

0 Likes

#8

You can let a; without value only if the compiler can statically prove that it will be assigned a value exactly once before it is used. With let mut a; it must be assigned at least once, i.e. it could be reassigned on some paths.

0 Likes