Skip to main content
68 votes

Why do so many programming languages not have a "built-in" way to do simple math functions?

factorial isn't useful Imperative programming languages don't have factorial built-in because, on its own, it isn't useful. ...
Keiji's user avatar
  • 607
47 votes

Why do compilers typically convert code into abstract syntax/parse trees before the final product?

I never found the need to produce an intermediate representation before the final output. Well then, you go! what is the purpose of producing an intermediate representation such as a tree before ...
Eric Lippert's user avatar
  • 3,439
46 votes

What's the relation between a language being managed and its compiler being reversible?

The main reason why Java (and in theory C# as well) is more easily decompilable than languages which get compiled to native machine code is that Java and C# both provide a feature which makes this ...
Doc Brown's user avatar
  • 561
38 votes
Accepted

Why do so many programming languages not have a "built-in" way to do simple math functions?

The bottom line is you can't have everything. The "elusive eight" statistical functions referenced in another answer might be absolutely essential to statisticians, but as a web application ...
IMSoP's user avatar
  • 2,428
34 votes
Accepted

How to ensure that an optimising compiler will terminate?

The roots of the problem: cost models, and monotonicity You're really getting at the heart of the problem when you say: each rewrite rule should make the program "more efficient" After all,...
Moonchild's user avatar
  • 879
33 votes

Why is it so hard to evaluate functions with constant arguments at compile-time?

There are many things in C++ that C has chosen not to adopt. Every feature adds complexity to the language, which burdens both its users (since they may have to read or use code that uses it, even if ...
Alexis King's user avatar
  • 8,721
33 votes

Why do so many programming languages not have a "built-in" way to do simple math functions?

Ultimately it comes down to convention and opinion: languages can't have a builtin for everything, and generally developers prefer to have smaller standard libraries with "conventional" ...
tarzh's user avatar
  • 3,924
30 votes
Accepted

Why do compilers typically convert code into abstract syntax/parse trees before the final product?

Many modern languages require multiple passes over the input, i.e. those that allow forward references without forward declarations, like Java & C#.  The input isn't fully understood without those ...
Erik Eidt's user avatar
  • 741
29 votes

What are the pros and cons of single-pass compilers?

I’m going to assume this question is about ahead-of-time compilers; JIT compilers are a different story altogether. With that in mind… The performance benefits of single-pass compilers are overstated ...
Alexis King's user avatar
  • 8,721
27 votes
Accepted

How can switch statements with sparse cases be compiled?

In general, it depends how many cases there are and exactly how sparse the cases are. One way to think about the sparseness problem that may help is to rephrase the question as "How do I turn a ...
Pseudonym's user avatar
  • 4,726
25 votes

Why do so many programming languages not have a "built-in" way to do simple math functions?

There are three places where a language designer can choose to place some functionality: language core (e.g. operator) standard library leave it for third-party libraries Putting something into the ...
Ralf Kleberhoff's user avatar
24 votes

Can "no cyclic data structures" be enforced at compile time?

Yes, it can be. There are three simple ways: Ban recursive type definitions, so e.g. a Foo can't have a field of type Foo, or a ...
kaya3's user avatar
  • 19.7k
22 votes
Accepted

What are the advantages of requiring forward declaration of methods/fields like C/C++ does?

There is only really one advantage ─ it means that name resolution can be done in a single pass, because names cannot be used before they are declared. This was more important in the olden days, when ...
kaya3's user avatar
  • 19.7k
22 votes

What are some techniques for faster, fine-grained incremental compilation and static analysis?

What are some of these techniques used by language servers? I can speak to the "Roslyn" C# compiler as I was on the design and implementation team for it. Briefly: Lexical and grammatical ...
Eric Lippert's user avatar
  • 3,439
21 votes

What are the pitfalls of using an existing IR/compiler infrastructure like LLVM?

Very large and very complicated The LLVM project has over 2 million lines, or 1.3 GB, of code. It's one of the largest projects on Github. It's so large that (at least at one point) you couldn't even ...
tarzh's user avatar
  • 3,924
21 votes

How to ensure that an optimising compiler will terminate?

The existing answer is good and representative of the techniques currently used in most optimizing compilers. I’d like to add to that by mentioning equality saturation, a newer technique that seems ...
Alexis King's user avatar
  • 8,721
19 votes

Executable files with a bytecode compiler/interpreter

Here is one way to do it, which is used by the LÖVE game engine: The bytecode gets stored in a .zip file which gets concatenated to the interpreter. When the interpreter starts up, it tries to open ...
Jasmijn's user avatar
  • 990
18 votes

Why is it so hard to evaluate functions with constant arguments at compile-time?

I have no specific insight into the decisions made by the C language designers, but I can offer a few reasons why this might not have been included: Replacing an expression like ...
kaya3's user avatar
  • 19.7k
18 votes
Accepted

What languages give you access to the AST to modify during compilation?

Accessing the original AST is a type of metaprogramming, and it is customary to call this ability the macro. But the word macro is overused, often used to refer to ...
Aster's user avatar
  • 3,228
17 votes

How to transpile goto to a language that lacks it?

Programs with goto can have arbitrary control-flow graphs (CFGs), so the goal here is to translate from any CFG into structured control-flow, according to some ...
kaya3's user avatar
  • 19.7k
17 votes

Why do compilers typically convert code into abstract syntax/parse trees before the final product?

How could it not take on an intermediate form? That's actually rather tricky. Consider this fragment: x + y... What bytecode do you emit? Perhaps it's loading the value of x, then loading the ...
Cort Ammon's user avatar
16 votes
Accepted

Can regex be compiled into efficient machine code?

Most languages that have regex have a regex parsing library that interprets the regex at runtime and matches them to strings. This is mostly right, but "interpret" isn't really accurate. ...
kaya3's user avatar
  • 19.7k
15 votes
Accepted

Are very explanatory compiler error messages worth the effort needed to implement them?

For context, in case any readers are unfamiliar with Rust's error messages, they are very detailed. Here's a typical example; yes, this is the message for one error. ...
kaya3's user avatar
  • 19.7k
15 votes
Accepted

What are the pros and cons of a compiler with a single-pass parser?

What constitutes a single-pass parser? Where you draw the line is somewhat arbitrary. Most programming languages tokenize their input and then parse the token stream; arguably this is already a multi-...
Alexis King's user avatar
  • 8,721
14 votes

Are very explanatory compiler error messages worth the effort needed to implement them?

It depends on the audience for your language If your language is intended to be used by people already familiar with programming, and uses generally the same programming syntax as other languages, ...
pppery's user avatar
  • 920
14 votes

Are very explanatory compiler error messages worth the effort needed to implement them?

Let me put it this way... Error messages are the user interface of a compiler Except for language extensions, the language that a typical compiler accepts is more or less independent of the compiler. ...
Pseudonym's user avatar
  • 4,726
14 votes

What's the relation between a language being managed and its compiler being reversible?

Your specific questions are not really answerable, because your question contains a number of assumptions that are not true (or at least, not entirely accurate). the compilation process was ...
Philip Kendall's user avatar
13 votes

How to ensure correct transformations with `break`, `continue` and other context-dependent statements?

The solution to this is to use a different choice of IR that doesn’t suffer from these problems (or at least makes the invariants easier to maintain). For imperative languages, an almost universal ...
Alexis King's user avatar
  • 8,721
13 votes

Why do compilers typically convert code into abstract syntax/parse trees before the final product?

what is the purpose of producing an intermediate representation such as a tree before producing the final product? Compilers have to parse the source code and produce machine code. Those ...
Adrian McCarthy's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible