Posted on Jun 16, 2012

Achieved : Calculator

For quite some times now, I’ve been wondering if it would be that hard to ditch PHP in favor for Mono+C#. Being a web developer, I work a exclusively with dynamic language : PHP, Javascript and sometimes AS3. Since 2006 I built numerous homemade frameworks in PHP for my clients & employers. One component of that framework is a Template engine (similar to Django, H20). My understanding of such component has evolved through the years and has become quite stupid-simple : regular-expression mixed with some evals.

evals() is a dynamic-language-only feature that I use a lot even if I should not (for security reason it is not a good practice to use it in production). I began my search to understand lexical analysis and today I built my first calculator.

Calc.Eval("2 * 4");
// return 8
Calc.Eval("2 * (3 / 10)");
// return 0.6

Sources in my Google repo

A Lexer is a program that store a list of token and their pattern, called definition (line 16 to 23). It scans a text for these definitions and return in-order the token it finds so you can execute some code (line 37 to the end of file).

So that it, I manage to build a simple equation evaluator.

Acheivement unlocked!

Latest Tweets