命名为LuckyScript,有点古怪的名字,为什么这么叫呢?因为过去的两年时间里我一直都觉得自己不太顺,今年是本人的本命年,图个吉利,希望所有事情都会好起来..有多少跟我一样唯心主义的程序员?
打算让这门无类型的语言最低限度地支持面向对象,起码能构造类跟重载函数吧,其他没想好,写着玩,最近还是挺闲的.这几天把汇编器跟虚拟机搞了出来.
测试用的汇编脚本,比较长
1Func square
2{
3 Param a;
4
5 Var c;
6
7 Mov c,1;
8 Mul c,a;
9 Mul c,a;
10
11 Ret c;
12}
13Func addSquare
14{
15 Param a;
16 Param b;
17
18 Push a;
19 Call square;
20 Mov a,RetVal;
21 Push b;
22 Call square;
23 Mov b,RetVal;
24
25 Var c;
26 Mov c,0;
27 Add c,a;
28 Add c,b;
29
30 Ret c;
31}
32
33Func sum
34{
35 Param from;
36 Param to;
37
38 Var i;
39 Var s;
40
41 Mov s,0;
42 Mov i,from;
43
44Lab : next
45 Add s,i;
46 Inc i;
47 Jle i,to,next;
48
49 Ret s;
50}
51
52Func Main
53{
54 Var a;
55 Var b;
56 Var c;
57
58 Mov a,2;
59 Mov b,3;
60
61 Push "Runtime info:";
62 CallHost print;
63 CallHost newLine;
64 Push "call addSquare function";
65 CallHost print;
66 CallHost newLine;
67 Push "Param: ";
68 CallHost print;
69 Push a;
70 CallHost print;
71 Push b;
72 CallHost print;
73 CallHost newLine;
74
75 Push a;
76 Push b;
77 Call addSquare;
78
79 Mov c,RetVal;
80
81 Push "addSquare function return: ";
82 CallHost print;
83 Push c;
84 CallHost print;
85 CallHost newLine;
86
87 Push "call sum function";
88 CallHost print;
89 CallHost newLine;
90 Push "Param: ";
91 CallHost print;
92 Push a;
93 CallHost print;
94 Push c;
95 CallHost print;
96 CallHost newLine;
97
98 Push a;
99 Push c;
100 Call sum;
101
102 Mov c,RetVal;
103
104 Push "sum function return: ";
105 CallHost print;
106 Push c;
107 CallHost print;
108}
主程序:
1#include "stdafx.h"
2#include "luckyscript.h"
3
4using namespace LUCKY_SCRIPT;
5
6void print(RuntimeState* state)
7{
8 int opType = lucky_getValType(state,-1);
9
10 if(opType == OP_FLAG_TYPE_INT)
11 {
12 int val = lucky_popValueAsInt(state);
13 printf("%d ",val);
14 }
15 else if(opType == OP_FLAG_TYPE_FLOAT)
16 {
17 float val = lucky_popValueAsFloat(state);
18 printf("%f ",val);
19 }
20 else if(opType == OP_FLAG_TYPE_STRING)
21 {
22 const char* val = lucky_popValueAsString(state);
23 printf(val);
24 }
25}
26
27void newLine(RuntimeState* state)
28{
29 printf("\n");
30}
31
32int _tmain(int argc, _TCHAR* argv[])
33{
34 lucky_initScript();
35 lucky_compileAsmFile("test.lsm","test","");
36
37 RuntimeState* state = lucky_loadlxeFile("test.lxe");
38 lucky_registerHostFunc(state,print,"print");
39 lucky_registerHostFunc(state,newLine,"newLine");
40 lucky_execute(state);
41
42 getch();
43 return 0;
44}
运行结果:
posted on 2009-02-24 17:25
清風 阅读(2475)
评论(9) 编辑 收藏 引用 所属分类:
LuckyScript