MobileBASIC 编程札记 (一)

MobileASM虽然完成了,但总觉得不甚满意。一个是Flag的设计错误一直懒得去修正了。二者那时才刚看Java一、两天,所有指令都是用if去判断的。现在想来应该把它用hash加速才好~~

过去就暂时先摆一边吧,不是不想继续做好,只是还有很多东西要学呢~

MobileASM里没有表达式的计算,也没有什么symbol,于是就开始了MobileBASIC。

 

时间仓促,于是一个暑假都还没把MobileBASIC写好,说来惭愧咯。

不过雏形是完全有了,是表达式计算的准备模块:

https://sourceforge.net/projects/mobilecinjava/

 

包括:

LineCodeStream

SymbolTable

ExpCalc

 

LineCodeStream负责加载一句代码,根据BASIC的要求设计了一个简单的分词器。

其实一开始准备用正则表达式的,不过呢还是自己编写分词器好玩些~~

 

重点当然是那个getWord函数。

 

首先skip空格和tab,

然后根据需要按照第一个字符确定所取word的初始化类型……

接着就是进行取word的操作了。

实际上比较容易,就是一个简单的状态机~~

 

SymbolTable应该不用说也懂,就是一个Vector,没有使用泛型是因为这是为Mobile准备的,

J2ME里没有泛型啊啊…………

只是有一点,为了加快查询,每个符号的name都计算了hash……

 

CalcTree是计算表达式最重要的家伙了。

它的pushNode最重要,进了函数里又被分为pushOperatorNode和pushValueNode。

pushNode是讲究顺序的,一般都需要先push一个操作符,然后是一个值或一个符号,除了遇到括号和函数的情况:

1。括号。

1+(-1+3) 中左括号右边的负号会被程序解析为pushNode(-) ≡ pushValueNode(0) , pushOperatorNode(-)。

但是1+(*2+3) 就会返回错误,因为BASIC里没有指针,也就没有像C里的*pointer的用法……

2。函数。

遇到f(x),我们操作的步骤就是:a.) pushNode(f)  b.) pushNode(x) ≡ pushOperatorNode(,) , pushValueNode(x)

遇到没有参数的函数f()就应该是: a.) pushNode(f)

 

总体就是这样了。

至于用法就看看代码包里的SyCoExp3in1Test吧。

当然,这里没有变量类型检测什么的,而且所有的值全是用double表示的。

这是变量申明:

 

 

这是函数申明,data里存入函数的id:

 

接着用LineCodeStream加载表达式好了~~~

 

把每个word都push到计算二叉树TreeCalc里。

 

最后是计算表达式值,这里用的是递归。我一直在思考如何很方便地把它转化为迭代的形式,当然应该是有解决方案的——结点逐级收缩,这里就不说那个啦~~

测试里的是:遇到值结点返回值,遇到操作符结点就调用函数自己,知道左右孩子都返回了值,然后A○B的计算。

测试里三个函数都是带一个参数的,如果带多个参数的话就要用到CallStack了,这个就等一下回再晒出来吧……

哎呀,还是自给自足好玩点~用Flex和Bison虽然是方便,但总觉得不自在~~

呵呵,渐渐可以自己设计自己的编成语言啦~~开心中~~^_^

 

J.Y. Liu

2010.09.05

 

.

.

practice my English -_-:

Finishing the project of MobileASM, I am really not satisfied.

There is a design error to FLAG unit that I have no time to correct.

And the project started after several days when Java met me.

The comparison between strings invite IF but I think a method with HASH could accelerate the code executing.

There is no expression calculator or symbol table, so a plan for MobileBASIC occur to me.

This is the link to the source code of the prototype of th expression calculator.

https://sourceforge.net/projects/mobilecinjava/

. including:

LineCodeStream

SymbolTable

ExpCalc

 

1.) LineCodeStream

It reads a line of BASIC plain and splits them into words such as if, 2, 8.0, +, >=, "hello", &h0ff, etc. .

And the function getWord() is center method in the class:

step 1. skip space and tab.

step 2. initialize the status by the head character of a word.

step 3. cut a word through a state machine.

step 4. record current position and return the word.

 

2.) SymbolTable

All symbols settle down in its vector. However, the source code is for J2ME and I couldn't use generic.

 

3.) ExpCalc

Calculating binary tree in ExpCalc relies on the method of pushNode().

pushNode is divided into two functions pushOperatorNode, pushValueNode.

To use the tree, we should do loops to push a value and then a operator except for:

a. when left parenthesis stands ahead sign.

e.g. 2+(-3-8)

pushNode(2)

pushNode(+)

meet '(' and deep++

pushNode(-) is equal to pushValueNode(0) automatically and pushOperatorNode(-)

pushNode(3)

pushNode(-)

pushNode(8)

meet ')' and deep--

.

e.g. 2+(*3-8)

pushNode(2)

pushNode(+)

meet '(' and deep++

pushNode(*) will raise an exception and err contains error code.

.

b. when the first parameter of a function comes in.

e.g f(x)

pushNode(f)

meet '(' and deep++

pushNode(x) is equal to pushOperatorNode(,) automatically and pushValueNode(x)

meet ')' and deep--

e.g f()

pushNode(f)

meet '(' and deep++

meet ')' and deep--

OK. Let's try the  SyCoExp3in1Test.
output: hello+cube(world) - (world + 2)*sqr(2) - sqr(cube(double(2)))/hello = 767.2
Ha~~I could try to design my own programming language in future~~
And enjoy DIY without Bison and Flex~~
Yeah! Hope you enjoy the source code. thnx...
Oh, God blesses my poor English........
J.Y.Liu
2010.09.05
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值