Bluish Coder

Programming Languages, Martials Arts and Computers. The Weblog of Chris Double.


2006-12-12

Compiling Factor to Javascript

A while back I wrote a toy Factor to Javascript compiler to use as an example in my Compilers and Interpreters post. I ended up using a different example in that post so didn't do any further work on it.

Yuuki from #concatenative is writing a Factor tutorial and I thought it might be useful to put something online to try some of the examples so I decided to do more work on it.

The current work in progress is here. Entering a simple Factor expression in the textarea and clicking 'submit' will send the factor code to the server where it is compiled to Javascript. The Javascript is returned to the browser and evaluated. The compiled javascript is displayed as well as the current stack contents.

It's pretty basic at the moment - it's more of a compiler from a stack based language with similar syntax to Factor rather than an actual Factor compiler. I hope to expand on it though. It can handle arrays, quotations, conditionals, some library words, some combinators and defining new words. The built in functions are:

  • dup
  • drop
  • nip
  • over
  • +
  • -
  • *
  • /
  • .
  • call
  • map
  • reduce
  • clear
  • =
  • if
  • t
  • f
  • empty?

Here are some examples you might like to try:

{ 1 2 3 } [ 2 * ] map
{ 1 2 3 } 0 [ + ] reduce
"hello world!" .
: fac dup 1 = [ ] [ dup 1 - fac * ] if ;
5 fac .
: product 1 [ * ] reduce ;
{ 2 3 4 } product
5 { 1 2 3 } [ over + ] map

I'll add more words and functionality over soon, as well as trying some DOM and Ajax functionality. Leave a comment if there's a feature you'd like to see!

Source code is in my repository and will be in the main Factor repository when Slava pulls from it.

Tags


This site is accessable over tor as hidden service 6vp5u25g4izec5c37wv52skvecikld6kysvsivnl6sdg6q7wy25lixad.onion, or Freenet using key:
USK@1ORdIvjL2H1bZblJcP8hu2LjjKtVB-rVzp8mLty~5N4,8hL85otZBbq0geDsSKkBK4sKESL2SrNVecFZz9NxGVQ,AQACAAE/bluishcoder/-61/


Tags

Archives
Links