Get the latest tech news

Emit


Sunday, October 13, 2024 One of the interesting aspects of a concatenative language like Factor is that blocks of logic can be easily extracted and easily reused since they apply logic to objects on the stack. For example, if this was a word that operated on stack values: : do-things ( a b -- c d ) [ sqrt * ] [ swap sqrt + ] 2bi ; One change we could easily make is to extract and name the two pieces of logic: : calc-c ( a b -- c ) sqrt * ; : calc-d ( a b -- d ) swap sqrt + ; : do-things ( a b -- c d ) [ calc-c ] [ calc-d ] 2bi ; We could also convert it to operate on local variables: :: do-things ( a b -- c d ) a b sqrt * a sqrt b + ; And extract those same two pieces of logic: :: calc-c ( a b -- c ) a b sqrt * ; :: calc-d ( a b -- d ) a sqrt b + ; :: do-things ( a b -- c d ) a b calc-c a b calc-d ; But, notice that we have to specify that the local variable a and b have to be put back on the stack before we can call our extracted words that make the computations.

But, notice that we have to specify that the local variable a and b have to be put back on the stack before we can call our extracted words that make the computations. Today, someone on the Factor Discord server asked about this very issue, wanting to have extractable pieces of logic that would effectively be operating on nested local variables, wherever they are used. Given a quotation that we have parsed in an emit description, we can build a word to replace all these lazy variables by looking them up in the current vocabulary manifest:

Get the Android app

Or read this on Hacker News