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.

Fix of Powerpoint 2007 Not Accepting Chinese/Japanese Input

I seldom use Powerpoint because I am kind of dislike it. I especially dislike the slides made with company’s official templates: if those logos and fancy things are removed, a lot of ink will be saved when printing it. If Powerpoint is not used, all those contents are condensed into a Word or Excel document, a lot of trees will be saved (maybe Powerpoint files are not supposed to be printed out at all, since they are slides, and should be played on the screen with a projector). And this can also save a lot of money for the companies, especially in such a bad economy.

Today, when I tried to use Powerpoint 2007 at home for the FIRST time (I even hesitated if I should have installed it or not when I installed Office 2007), I was astonished that I cannot type Chinese in it! Nor Japanese can be input. But if I type some Chinese characters into a text editor, and do a copy-and-paste, it shows in Powerpoint properly.

According to some investigations via internet, it seems to be caused by a Windows XP update. BTW, my Office 2007 was installed in Windows XP, I don’t have later versions of Windows at hand, so I am not sure if this applies to other Windows or not.

The fix is relatively straightforward, but the Registry should be modified. I made a .reg file for this. Just right click on it and choose “Merge” in popup menu. It takes effect at once without restarting Windows or Powerpoint. It works for both Chinese and Japanese input (I guess it may also work for other languages such as Korean but I am not sure). But this is totally at your own risk, ie. I won’t be responsible for any damages caused by using this file. So if you want to use this, please backup the Registry first (I didn’t though).

Right-click here and choose “Save Link As” to download fix-powerpoint-2007-chinese-japanese-input.reg

Published
Categorized as IT Tagged

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.

How to setup VNC

Task: setup VNC in Linux box, so that we can access the Linux box with GUI from windows PC.

On Server Side (Linux):

Install VNC server if it’s not yet installed.

Add allowed VNC client’s IP address in /etc/hosts.allow

Xvnc:192.168.0.10  # supposing your PC's address is this one

Set VNC password

> vncpasswd

Start VNC server once to generate startup script, and then stop it

> vncserver
> vncserver -kill :1

Edit startup script: ~/.vnc/xstartup

  • comment out “twm &”
  • add following lines to use Gnome and Chinese/Japanese input methods
export GTK_IM_MODULE="scim"
scim -d &
gnome-session &

Open port 5901 (when needed, 5902/5903/…) in firewall

Start VNC server

> vncserver

On Client Side (PC):

Windows:

Install VNC client software (such as RealVNC), and connect to Linux server.

Mac:

Choose “Move | Connect to server” in menu bar, and type in following address:

vcn://server_host_name:5901

Notes:

1. In order to use Chinese/Japanese input method (scim) in Linux, hotkey “<CTRL>-<SPACE>” in PC should be disabled, otherwise scim cannot be launched. This hotkey usually comes from Chinese input method in Windows.

2. In order to input Chinese/Japanese in Linux, on client (PC) side, IME should be turned off (i.e. English).

3. For security reasons, it’s a good practice to start VNC server in Linux only when needed, instead of making it a auto-started service. This can be done with a terminal software (such as PuTTY).

4. I did this because I wanted to access a Linux server from a PC, and I needed Chinese/Japanese support. But after doing so, I found out that PuTTY actually supports Chinese/Japanese: in its config dialog, click “Window | Translation”, then set “Received data assumed to be in which character set” to “UTF-8”. Then the Chinese/Japanese input methods in PC can be used directly in this terminal! (if it still doesn’t work, then do the following two things: 1, click “Windows | Appearance” and choose a proper font; 2, in vi, do “:set enc=utf-8”.)

5. ssh tunneling way to do it:

  • in Cygwin, execute this command: ssh -NTf -L 5901:localhost:5901 <user>@<linux-server-host-name>
  • in command prompt, change dir to “C:\Program Files\RealVNC\VNC Viewer” (or the right place), and execute this command: vncviewer localhost:5901
Published
Categorized as IT Tagged ,

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.

MacBook and Windows

Ideally, we should only use OS X in a MacBook, that’s why Mac should be used anyway, since Apple used a lot of proprietary things that optimizes the performance of the whole system. But due to Microsoft’s huge influence, no matter we’d like to admit it or not, there are many reasons for which we have to use Windows. A few examples include:

  • Microsoft Office: of course, there are Mac versions, but GUIs are different, and you need to pay for them again…
  • Visual Studio: good IDE for C++, and good RAD tool like VB and C#, etc.
  • Windows Live Mail: if you have some Hotmail accounts for historical reasons like me, you will find it very handy.
  • IE: oops, but there are some banks like those in China with out-of-date tech that requires this.

So I managed to install Windows right after I got my MacBook. It was a pain by the way, since my Windows XP installation CD is the MSDN version, with more than one versions of Windows in one CD, this proved to have problem installing: Mac’s Boot Camp failed to handle the prompt of choosing Windows to install. So I had to customize and burn a CD with only one version of Windows…

It seems that there are still some issues with Boot Camp. I feel the following two major ones annoying:

  • It consumes more resources: When I use OS X, the temperature of the machine is fine; while when I boot to Windows, it’s hot, even when it’s not busy (for example, just put it there without doing anything). I think that this could be caused by Boot Camp since Windows XP is not that bad.
  • The Blue Screen of Death occurs occasionally. This seldom happens (in fact, almost not) in my previous PCs and notebooks. So this may also be caused by Boot Camp.

Although there are these problems, I still enjoy using Windows in MacBook. One good thing is that this notebook has 4GB RAM, while Windows XP can only use 3GB. So I used the extra 1GB to create a RAM disk, and I configured the system to use this place to store garbages like temp files and cache data of web browsers. This is really cool.

Note: After I wrote this article, I found in Microsoft’s web site that Windows XP can also be configured to use 4GB of memory. But I still keep the current settings since I really feel the idea of having a 1GB RAM disk is very cool…

Published
Categorized as IT