My BASIC Interpreter

In a project in my previous job, I used a product that provides TCL as an extensible API for users. TCL is pretty good and easy to extend, but when you write a bigger script, things become terrible. Also, according to my personal experience, the biggest weakness of TCL is that it cannot evaluate expressions. You have to call ‘expr’ command to evaluate anything, which is pretty troublesome. It’s flow control is also kind of ugly. So Richard Stallman published an article “Why you should not use Tcl” long time ago, said that one should avoid TCL when choosing an extension language:

As interest builds in extensible application programs and tools, and some programmers are tempted to use Tcl, we should not forget the lessons learned … a language for extensions should not be a mere “extension language”. It should be a real programming language, designed for writing and maintaining substantial programs … Extensions are often large, complex programs in their own right, and the people who write them deserve the same facilities that other programmers rely on.

I couldn’t agree more. But I don’t like Python for it restricts you to add certain number of tabs in front of every line… It’s also a pain to integrate Python with C++ or Java code. I don’t like JavaScript (Rhino) either. Since I got some time lately, I decided to write an interpreter to use as an embedded language tool in the future… I chose BASIC as the prototype, for it was the first programming language I learned long time ago. The previously finished expression evaluator was also for this project.

After some work, I finished the main part of it. It can be downloaded here as an executable: mybasic.zip. (Please use Winzip or Winrar to unzip it. Or, here are the unzipped files: mybasic.exe, readme in English, Chinese and Japanese.

The current form of it is a command line tool to interprete BASIC programs or evaluate expressions. It writes the result to stdout and returns 0. When error occurs, it writes error message to stderr, and returns 1.

This is version 0.6, without array & datetime support yet. Since the initial purpose of this is to make an embedded language interpreter, I simplified the BASIC syntax to only keep the important parts (and also borrowed some new stuffs from Visual Basic).

This software was written in C++, with STL, without 3rd-party software (such as yacc and so on).

Usage:

    mybasic -e <expression>

or

    mybasic -f <BASIC_program_file_name>

The sample BASIC program used above (test.bas):

function mySum (byval x as integer) as integer
	if x <= 1 then
		return x
	else
		return x + mySum(x - 1)
	end if
end function

print mysum(100)

Here is the spec of it:

Expression Evaluatorr

The expression should be written in BASIC syntax. algebra, string, comparison and logic calculations are supported.

The spec of it is as follows:

Data types

  • boolean
  • integer
  • double
  • string

Operators

  • math: ^, +(unary), -(unary), *, /, \, MOD, +, –
  • string: +
  • relational: =, <>, <, <=, >, >=
  • logic: NOT, AND, OR
  • others: (, ) (to change precedences of operators)

Math Functions

abs, sign, int, sqrt, exp, log, log10, rad (change degrees to radians), deg (change radians to degrees), sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, sec, csc, pi (return value of Pi), random (return random number in [0, 1])

String Functions

str(num) (convert number to string), space(n), tab(n), ltrim(str), rtrim(str), trim(str), len(str), ucase(str), lcase(str), val(str) (parse string to number), isNumeric(str), left(str, n), right(str, n), mid(str, from, n), instr(str1, str2) (return index starting from 0; if not found, return -1)

Differences from traditional BASIC language

  • some function names are different, for example, “sqrt” instead of “sqr”
  • indexes in a string starts from 0 instead of 1
  • BASIC type characters (such as “2#” for 2 of double type) not supported

BASIC Interpreter

This interpreter supports a practically simplified BASIC syntax.

Spec of ‘practically simplified BASIC syntax’ (besides things listed above):

Supported

  • comment: REM, ‘ (single quotation mark)
  • variable declarison: DIM … AS …
  • condition (block IF): IF, THEN, ELSEIF, ELSE, END IF
  • FOR loop: FOR, TO, STEP, NEXT, CONTINUE FOR, EXIT FOR
  • DO loop: DO, WHILE, UNTIL, LOOP, CONTINUE DO, EXIT DO
  • function & sub-routine: FUNCTION, END FUNCTION, SUB, END SUB, RETURN, BYVAL, BYREF
  • output: PRINT
  • others: END

Not supported

  • LET (most useless command. ignored by the interpreter.)
  • line number (too old)
  • GOTO (evil)
  • GUSUB…RETURN (too old)
  • DEF FN (too old)
  • label (since GOTO/GOSUB/etc are not supported, there is no need to have labels)
  • Single-line IF
  • READ…DATA (too old)
  • SELECT CASE (don’t like the syntax. don’t want to introduce something too different, such as the ‘switch/case’ in C/C++/Java. can be replaced with IF)
  • ON [ERROR] … (too old)
  • WHILE … WEND (old but not good; replaced by the more powerful & flexible DO … LOOP)
  • EXIT SUB, EXIT FUNCTION (use RETURN instead)
  • CALL (but functions can be called directly as sub-routines)
  • variant data type (decreases code quality)
  • user defined type (class)
  • PRINT USING (if it is really necessary, will consider adding it)
  • file I/O (OPEN, etc… will provide new set of file I/O API in later versions)

Differences from normal BASIC language

  • variables must be declared (with DIM) before use (similar to always declaring ‘OPTIONAL EXPLICIT’ in Visual Basic. to help improve code quality.)
  • “DIM a, b, c AS DOUBLE” is OK, but “DIM a AS INTEGER, b AS DOUBLE” not supported (to help improve code quality)
  • Boolean type not allowed to convert from & to Integer (to help improve code quality)
  • functions: BYVAL or BYREF must be specified (no default) (to help improve code quality)
  • in PRINT statement:
    • “,” means inserting a tab, instead of starting to print at a certain place (too old-fashioned and not practical any more)
    • “;” means to start right after the previous place (same as original BASIC spec), but won’t insert a space before a number (programmer should be responsible for this when necessary!)

Not yet supported (will be supported in later versions)

  • data type:
    • array (multi-dimensional)
      • array index starts from 0, cannot be specified to another number
      • in ‘DIM A(N) AS INTEGER’, N is not upperbound, but size of array
    • date
    • time
    • datetime
  • built-in functions
    • some statistical functions
    • some algorithms
    • file I/O
    • other interoperabilities (?)
  • commands
    • INPUT (?)

My Expression Evaluator

Recently I have been taking some time to work on a personal project. A sub-project of it is an expression evaluator. I just finished it today. It was written in C++ with STL, without 3rd-party software. (download “my expression evaluator”)

This small tool work as a command line application, it take an argument as the expression. After evaluating the expression, it writes the result to stdout and returns 0. When error occurs, it writes error message to stderr, and returns 1.

The expression should be written in BASIC syntax. algebra, string, comparison and logic calculations are supported. The spec of it is as follows:

Data types

  • boolean
  • integer
  • double
  • string

Operators

  • math: ^, +(unary), -(unary), *, /, \, MOD, +, –
  • string: +
  • relational: =, <>, <, <=, >, >=
  • logic: NOT, AND, OR
  • others: (, ) (to change precedences of operators)

Math Functions

abs, sign, int, sqrt, exp, log, log10, rad (change degrees to radians), deg (change radians to degrees), sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, sec, csc, pi (return value of Pi), random (return random number in [0, 1])

String Functions:

str(num) (convert number to string), space(n), tab(n), ltrim(str), rtrim(str), trim(str), len(str), ucase(str), lcase(str), val(str) (parse string to number), isNumeric(str), left(str, n), right(str, n), mid(str, from, n), instr(str1, str2) (return index starting from 0; if not found, return -1)

Note that here are some differences with the traditional BASIC language:

  • some function names are different, for example, “sqrt” instead of “sqr”
  • indexes in a string starts from 0 instead of 1
  • BASIC type characters (such as “2#” for 2 of double type) not supported

This software is licensed under “HSL“, you can use it freely. When there are bugs, please let me know, I will try to fix them.

Block Access from a Range of IP Addresses

There are some guys from certain locales who keep posting spamming comments to my web site on a daily basis. Although most of them were caught automatically and won’t be shown, yet some of the spammers spam so frequently with very big posts, it’s really annoying to delete these periodically in order to avoid the waste of resourses.

Analyzing the IP addresses of these spammers, we can find that some of the most active spammers use a different IP address every time (they may intentionally use dynamic IP address provided by their ISP’s), so that you can hardly block them, for enumerating all the IP addresses in config files of Apache or firewall is very tedious. But fortunately, all these IP addresses belong to the same IP range (I think that the spammer didn’t use a proxy), and normal internet surfers from this IP range are hardly willing to browse my web site due to language / locale / interest reasons. So we can simply block the access from all IP addresses in this range. For this reason, I created this “IP Range Calculator” online tool to make this process easier: it does the tedious IP address mask & CIDR calculation and generates firewall commands as well as proper lines for Apache’s config file.

Here is a real example. Note that I don’t have any discrimination to people or IP addresses in this range, just as I said, a real surfer from this range won’t actually browse my web site, and the only ones who do access my web site are usually spammers. So it’s not unfair to restrict access from this range.

First, get one of the most frequent spammer’s IP addresses:

Then use “whois” service to get the IP range:

Now launch the “IP Range Calculator“, enter the IP range got from whois, and click “Calculate” button. Then the calculation result is used to generate related firewall commands and lines for Apache config file to block the IP addresses. You can use either of them according to your needs.

Buddhists’ Calendar

I made a small online tool: Buddhists’ Chinese calendar:

http://www.idogicat.com/cal_fojiao.html

Here is the Japanese version:

http://www.idogicat.com/cal_fojiao_jp.html

Here only the Buddhism festivals celebrated in China are adopted, since it’s mainly targeted to the Chinese Buddhism practitioners (this also includes most – if not all – of those in Japan and Korea).

Buddhists' Calendar

Online Character Conversion Tool Set

Many kinds of characters are used in computer systems, even for the same language. This sometimes causes a lot of inconvenience.

In Chinese world, there are two kind of writing systems: simplified Chinese and traditional Chinese. The former is mainly used in mainland China and Singapore, while the latter is used in Taiwan and Hong Kong. It’s a lot of trouble to convert an article written in one system to another. In order to make this easy, I wrote this simplified & traditional Chinese characters online conversion tool. With this tool, you can convert an article in a snap. This tool is actively used by Christian organizations in Vancouver where materials in both simplified and traditional Chinese are needed. Some Chinese language students also find it useful to learn characters or convert materials in the two writing systems. Even some professional translators are users of this tool.

In Chinese & Japanese character sets, there are full-width characters for English letters, numbers and symbols. I don’t really know when they should be used. In my opinion, in most cases we should use the ordinary half-width characters. But when you turn on the input method, it’s very easy to type a lot of full-width characters in an article. It’s very troublesome to change all these stuffs from full-width to half-width, or vice versa. This full-width & half-width character online conversion tool provides a quick solution.

A problem with Japanese character sets is that the not-so-good but old half-width katakana characters are still widely used nowadays. These characters are mainly used in following two situations:

  • bank fund transfer
  • mobile phone oriented web sites

Maybe it’s very tricky to renew all bank systems and ATM’s to switch from half-width katakana to full-width ones, when you wire some money via internet, you must enter the beneficiary’s information in half-width katakana (and English letters, numbers and symbols when applicable). The problem is that usually people show you their information in full-width katakana in their web sites or emails: if they write them in half-width ones, these characters may show scrambled. As a result, you have to type these information by yourself with half-width characters. This is error-prone, also sometimes you don’t have Japanese input method installed at all. So it’s always painful to wire money using web sites of Japanese banks. In order to make this process easier, I developed this full-width & half-width katakana online conversion tool.

BTW, I really don’t think it looks good to show a bunch of half-width katakana characters in an article full of full-width kanji and hiragana characters. But due to the size of mobile phones, it seems that there is no other choice to make Japanese mobile web sites show reasonable amount of information in a small screen, unless everyone throw away their mobile phones at once and switch to something like iPhone…

These tools, together with the Suzhou numerals & Arabic numerals online conversion tool, forms the online character conversion tool set provided by me. It’s advised to check the result thoroughly after conversion, and of course I won’t be responsible for any bad result that these tools may cause. Hope these tools can help you out and make your life easier in Asia.

Suzhou Numerals Conversion Tool

In 2010 British TV series “Sherlock” episode 2 “The Blind Banker”, the smugglers used a set of mysterious symbols to pass on messages. Those symbols are in fact numbers written in “Suzhou Numerals”, which is an ancient way of writing numbers in China.

Suzhou numerals derived from a kind of rod numerals in ancient China, but different from the counting rods that were for math and engineering purposes, Suzhou numerals were mainly used for accounting and business purposes. It is much easier than the formal Chinese written numbers system, made it much easier for almost everyone to understand, no matter what their education levels were.

Today, in some of the small traditional stores in Taiwan and Hong Kong, Suzhou numerals can still be seen occasionally. But it’s gradually disappearing with the wide spread of western style supermarkets, where of course Arabic numerals are used.

In order to save the traditional culture, I developed a small online tool to convert numbers written in Suzhou numerals and Arabic numerals. In the following screen shot, it converted Pi into Suzhou numerals.

PC Awaker

Click to download it.
Click to download it.

Usually there are a lot of rules in global companies. One of them is the rule of forcing PC go into screen saving after 5 minutes, and if you want to use the PC again, you have to enter your password to unlock the PC.

I admit that this is necessary for security reasons. But sometimes it’s not so reasonable for some of the PCs. For example, if one needs to support a product in a fast paced environment, they cannot afford entering password every time they need the PC – and in fact, the PC’s screen should never be turned off during the work time at all.

The tricky part is that usually we cannot change the screen saver settings because this feature is locked to enforce this rule. In this case, this ‘PC Awaker’ comes to the rescue.

This application is a tiny Windows utility, it is designed as a ‘tray application’, that is, it usually stays in the ‘tray’ of Windows task bar, unless you bring it out. The way how it keeps PC ‘awake’ is to send simulated key strokes periodically – to press F14 key every 4 minutes, to be precise. These are hardcoded since I don’t think it’s necessary to make these configurable.

The license of this software is ‘HSL‘. Help yourself if you need it.

Download it here. It was built in Windows XP using Visual C++ 6.0. I hope that it can work in Windows 9x/ME/NT/2000/XP. Don’t know if it works in Windows Vista…

My Tetris with AI Functionalities

(click on image to play tetris)

Tetris game was invented by a Russian scientist. The concept is very simple but effective – even today there are a lot of people enjoying this game.

I am not only interested in playing this game, but also interested in writing it (since I am a programmer 🙂 ). It has been my habit to implement Tetris games to practise GUI programming with a newly learned language. The earliest one was done on MS-DOS using C language (in an Borland IDE called Turbo C, by the way), I worked on some tricks like writing directly to Video-RAM to increase the performance, etc; then I did it using Visual Basic 6.0; then Visual C++ 6.0. Unfortunately all these versions are lost due to hardware upgrades.

Then in 2000, I joined a New York based company called Financial Sciences Company. There Java was chosen as the main language to work on their next generation trading applications. I knew nothing about Java at that time (I was accepted because of my C++ skills). So I learned it by reading ‘Core Java 2’ and ‘Professional Java Server Programming (J2EE edition)’, and practised and sharpened the skill by writing some programs in my spare time. This implementation of Tetris was the result of my study and practice of Java GUI programming using Swing. I think that it is the best one among my Tetris series.

Lately I don’t have the time to play with these Swing stuffs any more, so I just got some time to improved the algorithm in AI part to make it demo better. You can choose ‘X’ level to see the demo.

Features:

* Employing M-V-C pattern, game logic, data and the game screen are seperated, this makes the code very clean;
* The game panel is encapsulated as a component, so it can be dropped to any container conveniently using an IDE;
* Except the control buttons etc., it doesn’t use any control in Java API, instead, it draws directly to screen;
* It supports multimedia;
* It supports internationalization (i18n), with built in English, Chinese (simplified & traditional) and Japanese languages support;
* It has a very easy-to-use and intuitive user interface;
* It has an effective AI logic to demonstrate how to play this game.

About AI:

The AI part was implemented in following way:

The basic idea was to try to turn and put the coming block at each possible positions, evaluate the results, then choose the best one. But how to evaluate the result is the main problem.

First, we need to find which factors affect the final result, and get them out. For example, the height is definitely not a good thing (0 is the best); and holes under other blocks are evil, and so on. Then put these factors into a formula, with a set of coefficients, one for each of the factors. Now the remaining problem is to find the optimal vector of coefficients.

In order to do that, a training program is needed. I prefer the ‘quick-death’ way to screen out the bad ones as quickly as possible. So I just put it in a server and let it run during the day while I left home to work, and when I came back home at night, I got the result…

Click here to have fun with this Tetris game!

Japanese Calendar

(click on image to use calendar)
(click on image to use calendar)

This is an online Japanese calendar, based on the wonderful online Chinese calendar written by Sean Lin.

This is very handy to check when you don’t have a calendar at hand. I made mistakes like rushing to office and eventually found that I was the only one there because it was a holiday. This calendar can help to prevent this kind of mistakes.

What I did:

  • replaced Chinese holidays with Japanese holidays
  • added the algorithm of calculating ‘furikae kyuujitu’ (振替休日)
  • added calculation of ‘rokuyou’ (六曜)
  • i18n & l10n
  • designed the GUI or the layout as shown in the screen shot.

Check Japanese calendar here.