I am currently developing a compiler for a very limited object oriented language. I want to treat all values as objects and operators on those values will be implemented as methods. The compiler transforms the program into assembler for a stack-based virtual machine.
During compilation I transform integer literals into objects of a special "Integer" class. Arithmetic operators are implemented as methods of that class (using inline assembler). So that 4 + 5
basically equals to 4.add(5)
.
The problem I am facing right now is the special case for boolean values. If there is an if
statement:
if(10 > 5 || 12 < 10)
this would currently be transformed into: 10.greaterThan(5).or(12.lessThan(10))
Now obviously those integer literals can also be calls to a function with side-effects. Implementing those binary operators as method calls yields a problem in this case as short-circuit evaluation gets impossible.
So my questions are:
How do other languages achieve short-circuit evaluation but still treating every value as an object?
According to Wikipedia "ALGOL 68 used "proceduring" to achieve user defined short-circuit operators & procedures." - How does this work?