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:
Here are some examples you might like to try:
Source code is in my repository and will be in the main Factor repository when Slava pulls from it.
Categories: factor, javascript
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 + ] mapI'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.
Categories: factor, javascript

4 Comments:
Hi Chris, I wasn't sure if my e-mail went through. My name is Slim, and I'm trying to port IO for the nintendo DS. I was wondering if I could ask you a few questions about how you ported IO to the symbian OS. (i.e. what modifications you had to make, what features were difficult to port, how long it took, etc...) Lemme know. My e-mail is djslim (at) gmail.com thanks
Hi Slim, I thought I had replied, sorry! I'll send you an email in the next day or so outlining what I did. Or if you enter #concatenative on freenode and let me know your there I'll pop into #io and discuss it.
It would be really cool if the result would be more in the style of normal javascript, bug I have no idea how you could do that, except for using multiple arguments on push.
Hi Sjoerd, I agree. It's something I want to look at. Things like:
2 dup * alert
Should translate to alert(2 * 2) rather than doing the stack pushes and it would look more like normal javascript. I hope to look into this.
Post a Comment
<< Home