Showing posts with label Yet Another Bright Idea. Show all posts
Showing posts with label Yet Another Bright Idea. Show all posts

Monday, July 21, 2008

Part 0: Building a Basic Expression Evaluator

Quite often in our work, we come across situations where we would have to customize some business-logic quickly and on-the-fly. One such situation is when we have a workflow coded up, but the logic for transitioning between states is not fully known or is likely to change when the customers finally decide what they want.

In some of such situations, we would really love it if we could store the state transition-logic in a form that is easily editable at customization-time and dynamically evaluable to allow the workflow's behaviour to evolve and refine over time.

Being a classic computer scientist at heart, I decided to see if we could embed a little LISP evaluation engine and perhaps write and store the customization logic as a LISP expression, which would be evaluated at run-time and allow for the flexibility we needed. Then, being a classic masochist, I decided to spend a few days and see if I could build the evaluation engine itself. (Actually, I did google up for an embeddable LISP engine, but found that the gymnastics involved in integrating the engine into our environment wouldn't be worth the trouble!)

The evaluator we built takes a string s-expression value, and reduces it to a single scalar value. We built it in stages, and I'll describe the building process similarly. Unlike a full blown LISP implementation, all our functions return a single scalar - so there isn't support for the cons primitive.

The lexical structure for the little language we're building is:

  • Program ::= Atom | SExpr
  • Atom ::= number | boolean | string 
  • SExpr ::= "(" Atom | SExpr ")"
  • number is a pattern conforming to the following IEEE regular expression for a floating point: ^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]*\\.?[0-9]+)?$.
  • boolean is one of the following strings : t, true, false, nil and f. Each corresponding to its commonly known, self-explanatory value
  • string is a pattern conforming to an unquoted sequence of non-white-space characters, or a quoted sequence of characters including white-space

Semantically, we will use the general LISP convention where the car or first element of the SExpr is the function we want to apply to the cdr or the remainder of the SExpr. We will also pre-define some of the well-known functions we want to process.

So for example, we might want to evaluate the following strings and expect the associated results:

  • (+ 1 2 3 4) => 10
  • (+ 1 2 (* 3 2)) => 8
  • (+ "hello" " " "world") => "hello world"
  • (and (> 4 2 1) (<= 3 4 4 5)) => true

The cool thing to notice is that we want our engine to deal with dynamically typing the values into either numbers, boolean values or strings. The well-known functions also have to be intelligent and decide, for example, when to sum numbers and when to concatenate strings.

Of course, as the engine evolves, we want to pay special attention to integrating it into an environment from which it could be called, and introduce support for custom functions to supplement the well-known ones. Further improvements would include support for recursive and mutually recursive functions.

The posts following will outline the approach to building the evaluation engine and providing the support we desire.

Tuesday, January 30, 2007

Gimme..Gimme..Gimme..


So...

I feel more and more like a Luddite these days...

I still have a laptop which weighs about half a ton, is made of pig-iron and runs on coal and steam...

I keep thinking that someday I need to get a new one, if only to retire the mule that hauls it around for me, and I keep looking for that new feature that will finally make me give it all up and buy a new one...

It's a Dell Inspiron 8200 more or less gifted to me by a good friend - and against all odds, it's survived around 3 years with me. I love its 15" screen with 1600 x 1200 resolution - makes it easy to see a reasonable block of code in Emacs or Visual Studio.

So I lied - it was a cutting edge machine in its day...

The real problem is that while it's bulky and really does give my arms a workout carrying it, it is perfectly sufficient for my use - and I do actually write code and stuff on it :)

What I actually noticed is that over the last few months, my travel kit has grown to prodigious proportions with power supplies and adapters of all shapes and sizes, so my next purchases will all try to minimize the number of wall-warts and bricks I need to carry around to stay powered up...

  • One laptop from the mesazoic era with its brick...
  • Pair of simple headphones and mic for Skype...
  • Mobile phone and its wart...
  • Bluetooth headset - ok..so I'm not a Luddite after all - and its extension. The extension plugs into the phone's wart so it's not so bad...
  • A small point-and-shoot digital camera (for undercover paparazzi work :)) and its charger
  • USB Multi-card reader...
  • assorted cables...
  • assorted adapters to deal with international power socket form factors...
  • A small USB web-cam..

Now this is after I got rid of

  • The PDA and its charger - my phone does a reasonable approximation thereof
  • The CD-player and its charger - my phone plays music in Offline mode, and I can always boot up the laptop...
And I didn't buy an iPod for roughly the same reasons :)

What would be lovely is if the laptop:

  • got a lot smaller (yes..that's within reach) and had battery power to last an 8 hour flight (that's OK too)
  • could play music without having to have it open and turned on and taking up my tray-table on a flight (the newer HPs do just this - score! :)) and
  • have a fast-enough startup time to actually use as a PDA to check flight schedules and things (this should be easy...my antediluvian Dell comes out of "Hibernate" in 12 seconds flat...)

Now...

...if only the laptop could also act as my mobile and use/charge my headset, I could get rid of having to carry my phone on my business trips - my laptop could just be in my backpack as always and I could take calls on the wired or the bluetooth headsets...or heck - just get rid of the wired headset and buy a bluetooth noise-cancelling headband with mic. (Yep - I really was lying about being a Luddite!)

This shouldn't be too difficult to do - the newer cars already have the ability to accept SIM cards and behave like telephones - muting the music and routing the call to the audio system of the car - so why not on a laptop?

So that's what its going to take for me to buy a new laptop...