2

From http://www.golfscript.com/golfscript/syntax.html ,

Ruby is slow to start with so GolfScript is even slower. There is nothing innately slow about GolfScript. Except for the string evaluate method, everything could be statically compiled into C, and analysis could be done to remove most if not all stack use. I do not plan on making a more efficient interpreter, as the purpose of language is not numerical analysis, however if any feels like creating one, I would be delighted to use it.

Could someone illustrate with simple examples what are stacks, what does it mean to eliminate all stack use and how that could be done?

MaiaVictor
  • 51,090
  • 44
  • 144
  • 286

1 Answers1

1

GolfScript is a stack-based language. Its behavior is similar to an RPN calculator. Each builtin consumes some number of the topmost stack values and pushes its results back onto the stack for future operations. If you want to test if a number is less than a constant, you'd use code like .5< where the . duplicates the value (because otherwise it would be consumed and lost) and then the constant is pushed. Finally < pops the copy and the constant and pushes back the result. A compiler could easily see a pattern like .X< and generate code which skips the intermediate steps (just "peek" at the top of the stack and compare). This would be in the category of "peephole" optmizations, which look for small output patterns and replace them with more efficient patterns.

Sometimes it would not be possible, if the values on the top of the stack came from complex (unpredictable) calculations.

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150