Archive for November, 2007

BooTer Reduced

Friday, November 30th, 2007

Edit: Apparently the way to prove a new esoteric language is turing-complete is to implement a BF interpreter. This technique has been used to show that LOLCODE is turing complete, so I suppose I could target that as the first non-trivial program to write as soon as I get a working BooTer interpreter :D Or, I suppose I could implement a LOLCODE interpreter in BooTer in which I could run a BF interpreter…

Thinking about BooTer, I decide that I wasn’t making it esoteric enough..so I’ve slimmed down my specification for simpler implementation, and much more difficulty doing anything useful with it :)

The only thing allowed is simple expressions and boolean ternary expressions. No comma-separated lists of expressions, no assigning expressions to variables, no symbols, no quoting of expressions.

Maybe in the future I’ll re-expand it out into a more “real” language, but for now I just want to do something that I can do simply and easily without accidentally building an entire broken LISP.

So, with all that stuff gone..what’s left? What’s different now?

  • The boolean-ternary operator is in an order which won’t drive me insane to write programs in. The boolean expression comes first. If true, the expressions evaluates to the second expression, if not, it evaluates to the third. This now works basically just like the ternary operator in most other languages…but with a different syntax and the possibility of using the re-eval operator.

  • A BooTer program is now a single boolean-ternary (booter) expression, with optional nested booter expressions.

  • The center portion of a booter expression (the “boo” part), must evaluate to either true or false. False is represented by the integer 0, or by null. Anything else is true.

  • null is represented by the lack of a value. This is useful for instances where the left or right portion of an expression will never be used, for example:

( *…do something here…* : x = 5 : )
  • There are only numbers and strings as primitive types.

  • Variable names can start with upper or lowercase letters.

  • There’s only a single array type. Array items are assigned and accessed using the standard [] subscript operator. Creation of an array is implicit with the assignment of the first item to that array. If you use the subscript operator to make an assignment to a previously existing variable, the old variable is overwritten with the new array. The previous array literal syntax still applies. Only primitive values and variable names can be used in the array literal syntax.

  • Math and boolean order of operations are the same as in most programming languages these days.

  • The re-eval operator, ^, still works as previously stated.

  • Comments start with a semicolon and continue to the end of a line.

The previous example programs are both a bit more complex than they were previously (but hey, the parser and interpreter will be much easier to implement!):

Infinite NOOP loop:

( ^ : : )

100 bottles of beer:

(n = 101 :
  (
    (n = n-1 : 
      print [n, "bottles of beer on the wall!\n"] : 
    ) : 
    ^ : 
  ) : 
)

In the beer example, assume that print prints the items passed into its array to the stdout and then evaluates to the same string it printed out.

  • While n is greater than 0, the sub-expression will evaluate to the string returned by print, which by the rules above equates to true, thus causing the re-eval operator to be evaluated, looping back to the expression (n = n-1).

  • When n finally gets assigned 0, the expression will evaluate to the third, empty, sub-expression, which equates to false, which means the center of the outer expression now evaluates to false as well, and thus the third expression of the outer expression is evaluated. It evaluates to null, which is the return of the program.

Here’s a BooTer “for loop” with more of an explanation of what’s going on:

(i = -1 :              ; initialize loop variable
  (
    (100 > i = i + 1 : ; increment loop variable, check it against condition
      ...              ; do things, must evaluate to true
    :
      ...              ; this one must evaluate to false (leaving it empty works)
    ) 
  :
    ^                  ; re-evaluate this expression, causing the loop
  :
    ...                ; whatever can go here, this will be evaluated after the loop is done
  )
: )                    ; done, this last sub-expression will never be run

Even with this new “simplified” syntax, it’s enough to make your head hurt. Now I just need to actually write a reference implementation with some standard library calls, then try to write something non-trivial with it. Who wants to try their hand at writing a BooTer web server?

return false; it’s good for you.

Sunday, November 25th, 2007

Today I was looking around for blogs about JavaScript when I stumbled upon The Strange Zen of JavaScript by Scott Andrew LePera (Hacker/Singer/Songwriter), which looks to be quite informative, though sadly it seems as though it has been in hibernation since July.

I was relieved to see in reading The Mark Of The n00b that I’m not a n00b (at least according to that particular metric). Though I suppose I should point it out to others so they can avoid being n00bs as well:

use href=”#” and return false from your onclick event handlers to avoid jumping! (Ironically there were comments asking how to avoid the jumping scrollbars in the Mark of the n00b post, which was posted immediately after the return false post. Whoops.) Hopefully adding yet another post about this will help people googling for solutions to this frustrating problem to be more likely to find it.

More about returning false

I hate to just link to a post without adding something to the discussion, so I’ll throw in a bit of extra information about JavaScript event handler return values.

Return values are handy in most any event handler, not just onclick. Returning false prevents the event from “bubbling” up to the browser

Here are some suggestions for ways in which to use event handler return values:

  • onsubmit

    Add an onsubmit handler to a form using onsubmit=”return validate_form()”, if the form input is valid, return true, allowing the form to be submitted. If it is not valid, return false, preventing it from being submitted, then let the user know why.

  • onmousemove

    When using onmousedown and onmousemove to make an element drag-n-drop-able, then you will probably want to return false from the onmousemove handler to prevent any text on the page from being selected.

  • onmousedown, onmouseup

    Say you want to respond to a right click on a link, but you want to leave alone regular left clicks. Register an onmouseup event handler (which takes a parameter e) for that link, check the value of e.button. If it is 0, return true and the link is followed. If it is 2, do your thing (pop up a context menu, perhaps?), then return false so that the browser doesn’t pop up its own context menu. This is the technique used by those annoying scripts that “prevent” you from downloading images from their sites.

For an enormous amount of more information, check out the DOM Event Compatibility Tables over at Quirksmode

Fun With Bookmarklets

Saturday, November 24th, 2007

I’ve got a project planned for the near future which will require some sort of interaction with webpages other than my own. There are two ways I’m considering doing this: browser plugin, or bookmarklet(s). The problem with doing a browser plugin is that I’d have to write a separate plugin for each browser or, worse, only support a single browser. So I’m leaning toward bookmarklets to begin with. As with most things, I figured I’d get a bit of experience with bookmarklets before trying to do anything too complicated.

My first bookmarklet

According to this page on bookmarklets, the longest I can make a bookmarklet and have it still run in every browser (read: IE6) is 488 characters. That’s hardly enough to do any sort of cool Web2.0 stuff, so I need to load in an external script. Despite what the previously linked site says, it is indeed possible to insert script tags into the header of more than just IE, so that’s what I’ll do:

(function() {
    var head = document.getElementsByTagName(‘head’)[0];
    var newscript = document.createElement(’script’);
    newscript.type=‘text/javascript’;
    newscript.src=‘http://blog.paulbonser.com/files/bm/js/google.js’;
    head.appendChild(newscript);
})()

There, only 250 characters when I take out all the whitespace and add “javascript:” to the beginning. Notice that everything is wrapped in anonymous functions; this avoids putting unnecessary stuff into global namespace, which should prevent this code from conflicting with any code already on the page it’s being inserted into. The one thing that I do put into the global namespace is the blog_paulbonser_com_close_google_box() function, which I named to avoid conflicts.

google.js:

function blog_paulbonser_com_close_google_box() {
    var gb = document.getElementById(‘blog_paulbonser_com_google_box’);
    gb.parentNode.removeChild(gb);    
}

(function() {
    var body = document.getElementsByTagName(‘body’)[0];
    var head = document.getElementsByTagName(‘head’)[0];
   
    var newstyle = document.createElement(‘link’);
    newstyle.href = "http://blog.paulbonser.com/files/bm/css/google.css";
    newstyle.rel="stylesheet";
    head.appendChild(newstyle);
   
    body.innerHTML += ‘<div id="blog_paulbonser_com_google_box"><div><a href="#" onclick="blog_paulbonser_com_close_google_box(); return false;">Close</a></div><iframe src="http://google.com"></iframe></div>’;
})();

Go ahead, give it a try.

Example Bookmarklet: open an iframe

There you go, google popping up in the middle of my page. The real power of this, however, is that it should work on any page (except for pages with frames, since they don’t have a top-level body element). If you drag the link up to your bookmark toolbar, or rightclick and select “Bookmark this link”, etc, you will then be able to go to any page, click on that bookmark, and have a google search page pop up right in the middle.

My first “useful” bookmarklet

That’s a good start, but how about something that might actually be useful? Here’s an attempt at just that.

Lightboxify Bookmarklet: lightboxify page

This bookmarklet, plus a slightly modified version of the wonderful Lightbox plugin by Lokesh Dhakar, will take any links to images on the current page and “lightboxify” them. Give it a try on this page with the links below (some random images pulled from my computer). Before running the bookmarklet, it will navigate away from this page to the image. After running it: instant fancy image gallery. (Okay, there are still no thumbnails, but that would require a bit of extra work.)

If you feel so inclined, you can also drag this bookmarklet to your bookmarks toolbar, go to the Apache-generated directory listing, and give it a whirl there, as well.

1 2 3 4 5 6 7 8

An Introduction to BooTer

Friday, November 23rd, 2007

Intro

For a long time I’ve had lots of ideas for a programming language with all sorts of advanced features. Of course, I’ve never written a programming language before, so I’d like to get a bit of experience before attempting to create the next killer language. So what I needed was a simpler language to implement first. BooTer (Boolean-Ternary) is the result of some brainstorming of a “simple” first language.

I’m calling it an Esoteric Programming Language, since it probably won’t be that useful for any sort of real programming. It will be Turing Complete, if a bit difficult to write anything non-trivial in.

I don’t have the whole language figured out yet, so any of the information here will be subject to change at any time.

Syntax

A BooTer program is a series of nested expressions. Every expression evaluates to a value. An expression can be one of the following:

  • A simple value.

    These are your standard primitive values, such as integers, floating point numbers, strings, symbols, or variables:

      1
      1.1
      "blah blah blah"
      fooSymbol
      BarVar

    A symbol is a symbolic constant, with no specified value. Symbols start with a lowercase letter, while variables start with an uppercase letter.

  • An array or hash.

    These are similar to javascript array and object literals:

      [1, 2, "blah blah", 4.2]
      {blah: "blah blah blah", foo: "foo foo foo", aNumber: 124}
    

    Both arrays and hashes can hold any expressions, including more arrays and hashes, of course. Hash keys can be any of the simple types, numbers, strings, or symbols.

  • A comma-separated list of expressions.

    This will evaluate to the value of the last expression in the list:

     1, 2, "blah blah", 4.2
    

    Each expression will be evaluated in order. This is useful for doing things like processing input and output or changing the value of a variable before evaluating another expression using it.

  • A variable assignment.

    This will evaluate to the value of the variable after it is assigned:

     Avariable = "something"
    

    The right side of the assignment can be any expression, optionally wrapped in a set of parenthesis for disambiguation, in case the variable is being assigned the value of a comma-separated list of expressions.

    Avariable = 1, 2, 3, 4

    Will assign the Avariable to the interger 1 and then go on to evaluate the rest of the expression, whereas

    Avariable = (1, 2, 3, 4)

    will assign the value of Avariable to be the integer 4 after evaluating the rest of the expression.

  • A boolean expression.

    These are expressions with the standard ==, <, > <=. >=, !=, !, &&, ||. The result of any boolean expression is either “true” or “false”, and in the case of !, &&, and ||, those are the only valid values to pass in to them. This means that !Avar is not a valid boolean expression unless Avar’s value is currently one of the two boolean symbols.

  • A boolean-ternary expression.

    ("yes" : X < 42 : "no")
    

    This expression was the inspiration for the BooTer language (hence the name), and is the sole form of flow control that the language has. In the center is a boolean expression. If the boolean expression evaluates to true, then the expression evaluates to the evaluation of the subexpression on the left. Likewise, if the center expression evaluates to false, then the expression evaluates to the evaluation of the subexpression to the right.

    Why the emphasis on “the evaluation”? Because the subexpressions are not evaluated at all unless the center expression evaluates in their favor, at which point the value of the whole boolean-ternary expression becomes the value of the evaluated subexpression.

  • Special forms. For lack of a better name, everything else that is needed for a complete language will be called a special form.

    • built-in functions.

      These are functions built into the language which allow for things such as IO and Math. I haven’t fully made up my mind about the syntax of calling one of these built-in functions, but I’m thinking it will be the name of the function followed by a single expression, allowing for single values to be passed in for simple functions, or arrays or hashes to be passed into functions requiring more parameters. Passing in a single expression keeps the syntax simple.

    • ^ - The Re-eval operator.

      This operator causes the parent expression of the current expression to be re-evaluated, or the parent of a parent, etc.. This, combined with the boolter operator, provides the language with a means for looping. An endless loop could be acheived with the following complete BoolTer program:

      print "Hello World!", ^
      

    The simplest possible BoolTer program could consist of a single ^, thus looping forever doing nothing. A (slightly) more useful example would be a program to print the song 100 Bottles of Beer on the Wall:

      N = 100,
      (N = N - 1, ^ : print [N, " bottle(s) of beer on the wall!\n"], N > 0 : done)
      

    “done” has no meaning, it’s just used as something to evaluate to, since the expression has to evaluate to something. For going back up more than one parent-expression, simply put the appropriate number of re-eval operators next to eachother, without a comma. For example, changing “done” to “^^” in the previous example would cause the program to loop endlessly, counting down from 100 beers again and again.

    • ‘ - The quote operator.

      This operator, causes the following expression to evaluate to the expression itself, rather than to the evaluation of the expression. In other words, it defers the evaluation to a later time. This can be used to save expressions into variables or pass expressions into built-in functions. Most importanty, it allows expressions to be used as something like functions.

    • Temporary bind.

      I haven’t figured out how I want to do this yet, but I want to allow temporary binding of variables for a certain expression, something like let in lisp.

There are still a few details that I need to work out in regards to how everything is going to work, but I do have a working lexer and a mostly-complete parser, thanks to Flex and Lemon. All I need to do now is decide how I want to represent the parse tree, build it, and then get a working interpreter going. Due to the way the language is set up, it seems like it wouldn’t be too hard to generate assembler from it and make compilable programs…but first I want to see the language running at all.

Weekly Review

Monday, November 19th, 2007

Week 46, 2007

This last week (mostly in the last 48 hours, actually) I accomplished the following (non-work accomplishments):

  • I transitioned from bBlog to WordPress, which is simply a much more powerful (and more used, therefore more supported and more plugins) platform.
  • Re-implemented my custom bBlog skin for WordPress.
  • Set up Angie with her own blog
  • Set up a Mail server, Mail Transfer Agent, Mail Filter and webmail:
    • Dovecot Secure IMAP Server. The Debian standard setup was just what I needed.
    • Postfix Mail Transfer Agent. After fiddling with Sendmail for far too long, I decided to try Postfix instead. The config run by apt-get install postfix was all I needed to get the server working exactly the way I wanted. And all that config did was ask me a single question.
    • RoundCube Webmail. A kinda-nice, if somewhat feature-lacking AJAX webmail client. If they add threaded messages and the ability to edit procmail filters, I’ll be fully convinced.
    • Procmail. After a bit of looking around, there were some nice example .procmailrc files included with the installation, and once I figured out the right places to point the filters at (/var/mail/pib and ~/mail), it was pretty simple to set up filters for my various mailing lists. If the need ever arises I’ll add SpamAssassin into my setup as well.
  • Signed up for aNobii and entered in all the books in my immediate vicinity (the ones at my desk, not any of the ones on my bookshelf behind me).
  • Started on the lexer/parser of an esoteric programming language (more posts about this in the near future).
  • Finally wrote (am writing?) another blog post.

All this mail server setup is the first step in an attempt to reduce my dependence on Google (i.e. replace GMail), as well as set myself up with a long-overdue paulbonser.com email address. It’s not that I don’t trust Google (well, actually, I guess I don’t have that much of a reason to trust Google…), but I like to be able to have more control (and actual ownership) of things. I’ll write more about “Quitting Google” in the future.

Extending Javascript: Tail Recursion

Sunday, November 18th, 2007

Javascript is a very powerful, yet underrated, programming language. Despite its name, it is neither Java-like nor “just a scripting language.” It uses a generally unfamiliar concept known as Prototype-based Programming, which is object-oriented, but without classes. A couple of common questions about Javascript are “How can I run multiple threads?” and “How do I pause execution of my program for X seconds?”

The common answer is “You can’t, but you can do something similar with setTimeout.”

If you want to wait for X milliseconds before doing something, you simply setup a callback with

   setTimeout(’something’, X);

However, any callbacks will have to wait until the current function finished executing before executing, because Javascript is single threaded, and setTimeout simply puts its function call on a queue of things to be done later. After each function is finished running, the queue is checked for scheduled tasks, and any task whose timeout has expired will be run. Firefox even seems to defer updating of page content until after the currently running Javascript function returns. This is why on some pages with poorly crafted Javascript browsers will give warnings aboout scripts running for too long or consuming too much processor time.

So what do you do if you want to do large amounts of calculations, perhaps in loops that will run for many seconds or minutes? Once again, setTimeout is the way to go. As long as each function only runs for a very short period of time, you can run several different tasks “simultaneously” without killing your browser.

There is one thing, however, that bothers me about using setTimeout: It makes for some ugly code, and usually requires you to either wrap a string or anonymous function around your callbacks. Worse, it requires you to think about callbacks at all. So something as simple as running a loop that won’t hijack your browser becomes a pain. For example, here’s a simple function to count between two numbers within an element on the page:

(more…)

What I'm Listening to

Loading...