posts/compiler-development-rust-or-ocaml #4
Replies: 0 comments 5 replies
-
| 
         First  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         I loved reading this, really clear and straightforward!  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Here are stats on the hack typechecker https://github.com/facebook/hhvm/tree/master/hphp/hack: 
 My experience with the C# compiler https://github.com/dotnet/roslyn was that a huge amount of the codebase was (1) error reporting, heuristics, tracking information to make good compiler error messages. And (2) "compiler as a service" so that plugins and IDE could access things like the concrete syntax tree, the typed-AST, the symbol graph, the IL, and so on. For this the most important language criterion was making a friendly API, for which functional style immutable data was important, and object orientation was handy. Arguably that might even mean implementing the API in the same language that users write plugins (because users will want to write plugins in the language they're most familiar with), which might mean implementing the compiler in that language too. The internals of the C# compiler also doesn't have deep PL semantics problems to solve -- it just needs to represent its type system okay. The PL work is a few switch statements, a load of visitors, a few graph algorithms, and that's about it. Many languages would have done fine. I think things like SSA form and register coloring come at the IL/JIT layer, not the compiler.  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         OCaml GADT looks pretty good. struct Bool(bool);
struct Int(i32);
mod sealed {
    pub trait Sealed {}
    impl Sealed for super::Bool {}
    impl Sealed for super::Int {}
}
pub trait TermState: sealed::Sealed {}
struct Term<TermState>(TermState);
type BoolTerm = Term<Bool>;
type IntTerm = Term<Int>;
impl BoolTerm {
    fn new(value: bool) -> Self {
        Self(Bool(value))
    }
    fn not(self) -> Self {
        Self(Bool(!self.0 .0))
    }
    fn and(self, right: Self) -> Self {
        Self(Bool(self.0 .0 && right.0 .0))
    }
    fn val(self) -> bool {
        self.0 .0
    }
}
impl IntTerm {
    fn new(value: i32) -> Self {
        Self(Int(value))
    }
    fn neg(self) -> Self {
        Self(Int(-self.0 .0))
    }
    fn add(self, right: Self) -> Self {
        Self(Int(self.0 .0 + right.0 .0))
    }
    fn val(self) -> i32 {
        self.0 .0
    }
}
fn main() {
    let b = BoolTerm::new(true).and(BoolTerm::new(false)).not();
    let i = IntTerm::new(2).add(IntTerm::new(2)).neg();
    println!("{:?}", b.val());
    println!("{:?}", i.val());
} | 
  
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
posts/compiler-development-rust-or-ocaml
https://hirrolot.github.io/posts/compiler-development-rust-or-ocaml.html
Beta Was this translation helpful? Give feedback.
All reactions