├── .gitignore ├── Makefile ├── README.md ├── demo ├── 2 ├── 1.scn ├── Torpedo.aml ├── cglib.h ├── demo1.aml ├── demo1.cpp ├── demo1.gif ├── demo2.aml ├── demo2.cpp ├── demo2.gif ├── demo3.gif ├── geolib.h ├── raytracing.cpp ├── runner_chaser_cp.cpp ├── small-origin.cpp ├── small.aml └── small.cpp ├── lib ├── cglib.h ├── geolib.h ├── header-agent.ml ├── header.ml └── tail.ml ├── src ├── ast.mli ├── lexer.mll ├── main.ml ├── parser.mly └── pre.ml └── tests ├── aml ├── arith.aml ├── array.aml ├── block.aml ├── comment.aml ├── demo1.aml ├── demo2.aml ├── function.aml ├── global_variable.aml ├── if.aml ├── io.aml ├── loop.aml ├── ternary.aml ├── variable.aml └── vector.aml ├── cpp ├── arith.cpp ├── array.cpp ├── block.cpp ├── comment.cpp ├── demo1.cpp ├── demo2.cpp ├── function.cpp ├── global_variable.cpp ├── if.cpp ├── io.cpp ├── loop.cpp ├── ternary.cpp ├── variable.cpp └── vector.cpp ├── std ├── arith.cpp ├── array.cpp ├── block.cpp ├── comment.cpp ├── demo1.cpp ├── demo2.cpp ├── function.cpp ├── global_variable.cpp ├── if.cpp ├── io.cpp ├── loop.cpp ├── ternary.cpp ├── variable.cpp └── vector.cpp └── test.py /.gitignore: -------------------------------------------------------------------------------- 1 | lib/test 2 | *.cmi 3 | *.mli~ 4 | *.ml~ 5 | *.in~ 6 | *.mll~ 7 | *.mly~ 8 | src/parser.ml 9 | Makefile~ 10 | src/lexer.ml 11 | *.cmo 12 | src/parser.mli 13 | _build/* 14 | *.ml# 15 | *.mli# 16 | *.aml~ 17 | *.output 18 | *.mly# 19 | *.swp 20 | 21 | main 22 | 23 | *.byte 24 | 25 | *.native 26 | 27 | example/loop.cpp 28 | 29 | example/agent.cpp 30 | 31 | example/calc.cpp 32 | 33 | example/variable.cpp 34 | 35 | *.out 36 | 37 | *.mll# 38 | 39 | *.aml# 40 | 41 | *.h~ 42 | 43 | *.cpp~ 44 | 45 | cglib.h 46 | 47 | geolib.h 48 | 49 | *.cpp# 50 | 51 | *.gch 52 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Pure OCaml, package from Opam, two directories 3 | # 4 | 5 | # - The -I flag introduces sub-directories 6 | # - -use-ocamlfind is required to find packages (from Opam) 7 | # - _tags file introduces packages, bin_annot flag for tool chain 8 | # - using *.mll and *.mly are handled automatically 9 | 10 | OCB_FLAGS = -use-ocamlfind -use-menhir -I src -I lib # uses menhir 11 | .PHONY: all clean byte native profile debug sanity test 12 | 13 | OCB = ocamlbuild $(OCB_FLAGS) 14 | 15 | all: native byte # profile debug 16 | 17 | native: sanity 18 | $(OCB)main.native 19 | 20 | clean: 21 | $(OCB)-clean 22 | 23 | test: native 24 | python tests/test.py 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Introduction 2 | 3 | Agent Manipulation Language (AML) is a programming languages that compiles to C. It support describing the behaviors of agents and the computation geometry. 4 | 5 | AML is implemented in OCaml.There are some slides made by myself. 6 | 7 | [Introduction to Ocaml](https://drive.google.com/file/d/1EE53Btye7TAuW0bBTSjNVrvH-40I1ifR/view?usp=sharing) 8 | 9 | [Introduction to AML](https://drive.google.com/file/d/1BtA4K1q3Tp2fpJ_MfmQF-BdTkDTEem14/view?usp=sharing) 10 | 11 | ## How to get it 12 | 13 | ###Build from source 14 | 15 | 1. install glut // for mac OS, Xcode bring the glut 16 | 2. opam install menhir 17 | 3. clone with git 18 | 4. make 19 | 5. ./main.native [file] 20 | 21 | ### Test 22 | 23 | ``` 24 | $ make test 25 | ``` 26 | 27 | ## Test things with openGL: 28 | 29 | ``` 30 | $ cd demo 31 | 32 | $ g++ -o main demo1.cpp -lglut -lGL # for ubuntu 33 | 34 | $ g++ -framework OpenGL -framework GLUT -framework Foundation -o main demo1.cpp # for mac OS 35 | 36 | $ ./main 37 | ``` 38 | 39 | ## Syntax 40 | 41 | ### Assignment 42 | ```c 43 | int i = 1; 44 | double f = 1.0; 45 | string s = "1"; 46 | bool b = true; 47 | ang d = (1, 0); 48 | vec2f v = (1, 1); 49 | ``` 50 | 51 | ### Arithmetic 52 | ```c 53 | println(false, true, 42); 54 | println(1 + (4 + 6) * 3); 55 | println(8 - 3 % 2); 56 | println(-9 - 9); 57 | println((2 + 8) / 3); 58 | ``` 59 | 60 | ### Comment 61 | ```c 62 | /* This is a multi-line comment */ 63 | // This is a single-line comment 64 | ``` 65 | 66 | ### Loop 67 | ```c 68 | int sum; 69 | for(int i = 1; i <= 100; i = i + 1) { 70 | sum = sum + i; 71 | } 72 | ``` 73 | 74 | ### Condition 75 | ```c 76 | int a = 3; 77 | if (a > 2) 78 | println("Yes"); 79 | else { 80 | println("No"); 81 | } 82 | ``` 83 | 84 | ### Recursion 85 | 86 | ```c 87 | int fibonacci(int num) { 88 | if (num == 0) 89 | return(0); 90 | else if(num == 1) 91 | return(1); 92 | else return (fibonacci(num - 2) + fibonacci(num - 1)); 93 | } 94 | ``` 95 | ## Contributors 96 | 97 | * [hzwer](https://github.com/hzwer) 98 | 99 | * [wmdcstdio](https://github.com/wmdcstdio) 100 | 101 | ## Reference 102 | 103 | [Batsh](https://github.com/BYVoid/Batsh) 104 | 105 | [ocamllex, ocamlyacc](http://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html) 106 | 107 | [An Introduction to Objective Caml](http://www1.cs.columbia.edu/~sedwards/classes/2014/w4115-fall/ocaml.pdf) 108 | 109 | [One-Day Compilers](http://venge.net/graydon/talks/mkc/html/index.html) 110 | -------------------------------------------------------------------------------- /demo/1.scn: -------------------------------------------------------------------------------- 1 | 12 1 2 | ambient 0.1 0.1 0.1 3 | specular 0.5 0.5 0.5 4 | shininess 1 5 | 6 | diffuse 0.2 0 0 7 | face 1 1 -1 -1 1 -1 -1 -1 -1 8 | face -1 -1 -1 1 -1 -1 1 1 -1 9 | 10 | face -1 -1 1 -1 1 1 1 1 1 11 | face 1 1 1 1 -1 1 -1 -1 1 12 | 13 | diffuse 0 0.2 0 14 | face 1 1 1 -1 1 1 -1 1 -1 15 | face -1 1 -1 1 1 -1 1 1 1 16 | 17 | face -1 -1 -1 -1 -1 1 1 -1 1 18 | face 1 -1 1 1 -1 -1 -1 -1 -1 19 | 20 | diffuse 0 0 0.2 21 | face -1 -1 -1 -1 1 -1 -1 1 1 22 | face -1 1 1 -1 -1 1 -1 -1 -1 23 | 24 | face 1 1 1 1 1 -1 1 -1 -1 25 | face 1 -1 -1 1 -1 1 1 1 1 26 | 27 | lightcol 1 1 1 28 | light 0 0 -0.8 29 | -------------------------------------------------------------------------------- /demo/2: -------------------------------------------------------------------------------- 1 | #include "geolib.h" 2 | #include "cglib.h" 3 | 4 | double _FPS=1; 5 | //unit is "meter" for them 6 | double _MAP_X=20,_MAP_Y=20; 7 | vec2f _MAP_CENTER(0,0);//5m 8 | double _SPEEDUP_RATE=1; 9 | 10 | //must allowed user-defination interfaces: 11 | void _Set_Map_XY(double _X,double _Y){ 12 | _MAP_X = _X; 13 | _MAP_Y = _Y; 14 | } 15 | void _Set_Map_Center(double _x,double _y){ 16 | _MAP_CENTER = vec2f(_x,_y); 17 | } 18 | void _Set_Speedup_Rate(double _rate){ 19 | _SPEEDUP_RATE=_rate; 20 | } 21 | void _SET_FPS(double _fps){ 22 | _FPS = _fps; 23 | } 24 | //interfaces end 25 | 26 | void _AML_Vertex2f(vec2f pos){ 27 | pos = pos - _MAP_CENTER; 28 | pos = (pos/_MAP_X*2.0, pos/_MAP_Y*2.0); 29 | glVertex2f(pos.x,pos.y); 30 | } 31 | 32 | class _Agent{ 33 | public: 34 | string name; 35 | virtual void _plot(double,double)=0; 36 | virtual void _step(double,double,_Agent*)=0; 37 | virtual void _copy_from(_Agent*)=0; 38 | }; 39 | template 40 | vector _Global_Filter(bool (*f)(_Agent*)); 41 | 42 | int PH=100,PW=100; 43 | double L=1; 44 | const double eps=1e-10; 45 | int facenum; 46 | vec3f *xyzs;//length=3*facenum 47 | double *rgbs;//length=3*facenum 48 | void print(vec3f p){ 49 | cout<<"("<0){ 93 | if(t > _agents; 139 | double _dt=1/_FPS*_SPEEDUP_RATE; 140 | double _cur_tim=0.0; 141 | template 142 | vector _Global_Filter(bool (*f)(_Agent*)){ 143 | vector ret; 144 | for(int i=0;i<_agents.size();i++){ 145 | if(f(_agents[i].second)){ 146 | ret.push_back((T*)_agents[i].second); 147 | } 148 | } 149 | return ret; 150 | } 151 | void _Plot(void){ 152 | glClear(GL_COLOR_BUFFER_BIT); 153 | for(int i=0;i<_agents.size();i++){ 154 | _agents[i].second->_plot(_cur_tim, _dt); 155 | } 156 | glutSwapBuffers(); 157 | } 158 | void _Step_Time(int _time_value){ 159 | //printf("step time: %d\n",_time_value); 160 | for(int i=0;i<_agents.size();i++){ 161 | _agents[i].second->_step(_cur_tim,_dt,_agents[i].first); 162 | _agents[i].first->_copy_from(_agents[i].second); 163 | } 164 | _cur_tim+=_dt; 165 | getchar(); 166 | glutPostRedisplay(); 167 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 168 | } 169 | 170 | int main(int argc, char *argv[]){ 171 | /*function calls such as _SET_MAP_XY must be filled here 172 | */ 173 | glutInit(&argc, argv); 174 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);//double buffer 175 | //todo: user shall be able to designate these things in AML 176 | glutInitWindowPosition(100, 100); 177 | glutInitWindowSize(100, 100); 178 | int glut_window = glutCreateWindow("AML"); 179 | //end todo 180 | _Set_Map_XY(2*L,2*L); 181 | read("1.scn"); 182 | for(int i=0;i l=[near];//相当于python中的filter,但是用C++的vector实现,它会列出所有满足near()=true的Ship* 25 | if(l.size()>0){ 26 | printf("torpedo exploded at time %lf\n",time); 27 | for(Agent *s:l){ 28 | erase(s); 29 | } 30 | erase();//删除自己 31 | } 32 | }; 33 | }; 34 | 35 | Agent Submarine{//它能探测并攻击5km之内的船,如果没有就 36 | vec pos; 37 | vec default_vel;//默认航向 38 | Ship *target; 39 | double rate;//航行速率 40 | double angular_speed;//转弯角速度 41 | string camp; 42 | Submarine(vec p){ 43 | pos=p; 44 | target=NULL; 45 | camp="red"; 46 | } 47 | bool sonar(Ship *s){//探测距离为5km 48 | return dis((*s).pos-pos) < 5000.0; 49 | } 50 | Action(double time,double dlt){ 51 | if(!active(target)) target=NULL;//active是系统内置函数,能看一个Agent是否已经消失 52 | if(target==NULL){ 53 | list l=[sonar];//列出能被探测到的所有船,它能自动忽略类型是Submarine/Torpedo等的Agent 54 | if(l.size()>0){ 55 | taregt = &l[0]; 56 | } 57 | } 58 | if(target!=NULL){//有目标,向目标航行 59 | deg lead_angle=intercept_angle(pos,rate,(*target).pos, '(*target).pos);//'求速度,deg是特有的“角度”类型,它能优雅地处理诸如角度范围这样的问题 60 | vec now_vel = 'pos; //'求自己的速度 61 | deg now_angle = atan2(now_vel.y, now_vel.x); 62 | now_angle:[now_angle-angular_speed*dlt, now_angle+angular_speed*dlt];//角度能朝向的方向范围 63 | now_angle->lead_angle;//尽量转向lead_angle航向,在deg类型中不用担心范围究竟是[-PI,PI]还是[0,2*PI]这样的问题 64 | pos += vec(cos(now_angle)*rate, sin(now_angle)*rate, 0);*dlt; 65 | double d=dis(pos,(*target).pos); 66 | if(d:<500){//:<是一个运算符,指“这个值刚刚变得小于1”,它会在进入500m范围后发射一枚鱼雷 67 | //除此之外还有诸如":<", ":<=", ":>=", ":==", ":!="这些运算符,它们同样支持deg类型 68 | Torpedo t(pos,vel); 69 | release(t);//让t“活起来” 70 | } 71 | } 72 | else{//沿航向自动行驶 73 | pos=pos + default_vel; 74 | } 75 | } 76 | }; 77 | 78 | Agent Patroler{//巡逻机,绕圈搜索10km范围内的潜艇,如果有就通知所有我方船只 79 | vec pos; 80 | double T,R;//周期和圆半径 81 | string camp; 82 | bool detector(Submarine *s){//探测10km范围内的潜艇 83 | return dis((*s).pos-pos) <= 4000.0; 84 | } 85 | bool same_camp(Ship *s){ 86 | return (*s).camp==camp; 87 | } 88 | Action(double time,double dlt){ 89 | double omega=PI*2.0/T;//角频率 90 | pos = Vec(cos(time*omega)*R, sin(time*omega)*R, 0); 91 | Vec vel='pos; //'代表一阶导数,vel就是它的速度 92 | deg angle=atan2(vec.y,vec.x);//航向的极角 93 | if(angle:<>0){//每次angle经过0的时候,即每次通过正东方向时 94 | printf("we turned a round\n"); 95 | } 96 | list sbms=[detector]; 97 | if(sbms.size():>0){//之前没有潜艇,现在有了 98 | printf("found submarines at time %lf\n",time); 99 | list sps=[same_camp]; 100 | if(sps.size()>0){ 101 | for(Ship *s:sps){ 102 | (*s).alert=true; 103 | } 104 | } 105 | } 106 | } 107 | }; 108 | 109 | Agent Ship{ 110 | vec pos; 111 | vec vel; 112 | vec goal; 113 | bool alert; 114 | Ship(vec p,vec g){ 115 | pos=p; 116 | goal=g; 117 | } 118 | Action(double time, double dlt){ 119 | if(!alert){ 120 | pos += vel*dlt; 121 | } 122 | else{ 123 | deg alpha=atan2(vel.y,vel.x); 124 | alpha+=PI/2;//转弯90° 125 | alert=false; 126 | } 127 | } 128 | }; 129 | 130 | main(void){ 131 | Ship a(....);//略具体参数 132 | Submarine b(....); 133 | Patroler c(....); 134 | release(a); 135 | release(b); 136 | release(c); 137 | //然后自动开始循环 138 | } 139 | -------------------------------------------------------------------------------- /demo/cglib.h: -------------------------------------------------------------------------------- 1 | #ifndef _CGLIB_H 2 | #define _CGLIB_H 3 | #include "geolib.h" 4 | #include 5 | //#include 6 | //#define gl_h_ 7 | //#include 8 | #include 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /demo/demo1.aml: -------------------------------------------------------------------------------- 1 | Agent Ball{ 2 | vec2f pos; 3 | vec2f vel; 4 | Ball(int _x, int _y){ 5 | pos = vec2f(_x, _y); 6 | } 7 | void plot(){ 8 | glColor3f(1.0, 1.0, 1.0); 9 | glBegin(GL_LINES); 10 | plotvec2f(pos); 11 | plotvec2f(pos + vel * 0.1 / _dtim); 12 | glEnd(); 13 | glColor3f(1.0, 0.0, 0.0); 14 | glBegin(GL_POINTS); 15 | plotvec2f(pos); 16 | glEnd(); 17 | } 18 | void step(){ 19 | pos = vec2f(pos.x, 10 - _tim * _tim * 4.9); // free fall 20 | vel = pos'; // first order difference 21 | } 22 | }; 23 | 24 | project(){ 25 | for(int i = 1; i <= 5; i = i + 1) 26 | register(Ball(-12 + 4 * i, 10)); 27 | } -------------------------------------------------------------------------------- /demo/demo1.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | // Add the header for agent 4 | #include "cglib.h" 5 | 6 | double _FPS=60.0; 7 | // unit is "meter" for them 8 | double _MAP_X=20,_MAP_Y=20; 9 | vec2f _MAP_CENTER(0,0); // 5m 10 | double _SPEEDUP_RATE=1; 11 | 12 | //must allowed user-defination interfaces: 13 | void _Set_Map_XY(double _X,double _Y){ 14 | _MAP_X = _X; 15 | _MAP_Y = _Y; 16 | } 17 | void _Set_Map_Center(double _x,double _y){ 18 | _MAP_CENTER = vec2f(_x,_y); 19 | } 20 | void _Set_Speedup_Rate(double _rate){ 21 | _SPEEDUP_RATE=_rate; 22 | } 23 | void _SET_FPS(double _fps){ 24 | _FPS = _fps; 25 | } 26 | void _AML_Vertex2f(vec2f pos){ 27 | pos = pos - _MAP_CENTER; 28 | pos = (pos/_MAP_X*2.0, pos/_MAP_Y*2.0); 29 | glVertex2f(pos.x,pos.y); 30 | } 31 | class _Agent{ 32 | public: 33 | virtual void _plot(double,double)=0; 34 | virtual void _step(double,double,_Agent*)=0; 35 | virtual void _copy_from(_Agent*)=0; 36 | }; 37 | class Ball: public _Agent{ 38 | public: 39 | vec2f pos; 40 | vec2f vel; 41 | Ball(int _x, int _y) { 42 | pos = vec2f(_x, _y); 43 | } 44 | void _plot(double _tim, double _dtim) { 45 | glColor3f(1., 1., 1.); 46 | glBegin(GL_LINES); 47 | _AML_Vertex2f(pos); 48 | _AML_Vertex2f((pos + ((vel * 0.1) / _dtim))); 49 | glEnd(); 50 | glColor3f(1., 0., 0.); 51 | glBegin(GL_POINTS); 52 | _AML_Vertex2f(pos); 53 | glEnd(); 54 | glFlush(); 55 | 56 | } 57 | void _step(double _tim, double _dtim, _Agent* _last_Agent) { 58 | Ball* _last = (Ball*)_last_Agent; 59 | pos = vec2f(pos.x, (10 - ((_tim * _tim) * 4.9))); 60 | /* free fall*/ 61 | vel = (pos - (_last->pos)); 62 | /* first order difference*/ 63 | } 64 | void _copy_from(_Agent *_from_Agent){ 65 | Ball* _from = (Ball*)_from_Agent; 66 | *this = *_from; 67 | } 68 | }; 69 | 70 | vector > _agents; 71 | double _dt=1/_FPS*_SPEEDUP_RATE; 72 | double _cur_tim=0.0; 73 | void _Plot(void){ 74 | glClear(GL_COLOR_BUFFER_BIT); 75 | for(int i=0;i<_agents.size();i++){ 76 | _agents[i].second->_plot(_cur_tim, _dt); 77 | } 78 | glutSwapBuffers(); 79 | } 80 | void _Step_Time(int _time_value){ 81 | //printf("step time: %d\n",_time_value); 82 | for(int i=0;i<_agents.size();i++){ 83 | _agents[i].second->_step(_cur_tim,_dt,_agents[i].first); 84 | _agents[i].first->_copy_from(_agents[i].second); 85 | } 86 | _cur_tim+=_dt; 87 | glutPostRedisplay(); 88 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 89 | } 90 | 91 | int main(int argc, char *argv[]) { 92 | glutInit(&argc, argv); 93 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 94 | glutInitWindowPosition(100, 100); 95 | glutInitWindowSize(500, 500); 96 | int glut_window = glutCreateWindow("AML"); 97 | void *_Agent1, *_Agent2; 98 | for(int i = 1; (i <= 5); i = (i + 1)) { 99 | _Agent1 = new Ball((-12 + (4 * i)), 10); 100 | _Agent2 = new Ball((-12 + (4 * i)), 10); 101 | _agents.push_back(make_pair((_Agent*)_Agent1, (_Agent*)_Agent2)); 102 | } 103 | glutDisplayFunc(&_Plot); 104 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 105 | glutMainLoop(); 106 | } 107 | -------------------------------------------------------------------------------- /demo/demo1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hzwer/AML/6eb61e38653f0e8ce87f8aadf8a856470862026f/demo/demo1.gif -------------------------------------------------------------------------------- /demo/demo2.aml: -------------------------------------------------------------------------------- 1 | Agent Blinker{ 2 | angle theta; 3 | vec2f pos; 4 | int color; 5 | double PI; 6 | Blinker(){ 7 | theta=0; 8 | color=0; 9 | PI=acos(-1.0); 10 | } 11 | void plot(){ 12 | vec2f a=pos+vec2f(0,1); 13 | vec2f b=pos+vec2f(-0.866,-0.5); 14 | vec2f c=pos+vec2f(0.866,-0.5); 15 | if(color==0) 16 | glColor3f(1.0,0.0,0.0); 17 | else 18 | glColor3f(0.0,0.0,1.0); 19 | glBegin(GL_TRIANGLES); 20 | plotvec2f(a); 21 | plotvec2f(b); 22 | plotvec2f(c); 23 | glEnd(); 24 | } 25 | void step(){ 26 | double dlt=_dtim/10*2*PI; 27 | theta = theta + dlt; 28 | pos=vec2f(cos(theta),sin(theta))*5; 29 | if(theta :> angle(0)){ // operator :> means "when > holds true for the first time" 30 | color = color ^ 1; 31 | } 32 | } 33 | }; 34 | project(){ 35 | register(Blinker()); 36 | } -------------------------------------------------------------------------------- /demo/demo2.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | // Add the header for agent 4 | #include "cglib.h" 5 | 6 | double _FPS=60.0; 7 | // unit is "meter" for them 8 | double _MAP_X=20,_MAP_Y=20; 9 | vec2f _MAP_CENTER(0,0); // 5m 10 | double _SPEEDUP_RATE=1; 11 | 12 | //must allowed user-defination interfaces: 13 | void _Set_Map_XY(double _X,double _Y){ 14 | _MAP_X = _X; 15 | _MAP_Y = _Y; 16 | } 17 | void _Set_Map_Center(double _x,double _y){ 18 | _MAP_CENTER = vec2f(_x,_y); 19 | } 20 | void _Set_Speedup_Rate(double _rate){ 21 | _SPEEDUP_RATE=_rate; 22 | } 23 | void _SET_FPS(double _fps){ 24 | _FPS = _fps; 25 | } 26 | void _AML_Vertex2f(vec2f pos){ 27 | pos = pos - _MAP_CENTER; 28 | pos = (pos/_MAP_X*2.0, pos/_MAP_Y*2.0); 29 | glVertex2f(pos.x,pos.y); 30 | } 31 | class _Agent{ 32 | public: 33 | virtual void _plot(double,double)=0; 34 | virtual void _step(double,double,_Agent*)=0; 35 | virtual void _copy_from(_Agent*)=0; 36 | }; 37 | class Blinker: public _Agent{ 38 | public: 39 | angle theta; 40 | vec2f pos; 41 | int color; 42 | double PI; 43 | Blinker() { 44 | theta = 0; 45 | color = 0; 46 | PI = acos(-1.); 47 | } 48 | void _plot(double _tim, double _dtim) { 49 | vec2f a = (pos + vec2f(0, 1)); 50 | vec2f b = (pos + vec2f(-0.866, -0.5)); 51 | vec2f c = (pos + vec2f(0.866, -0.5)); 52 | if((color == 0)) { 53 | glColor3f(1., 0., 0.); 54 | } 55 | else { 56 | glColor3f(0., 0., 1.); 57 | } 58 | glBegin(GL_TRIANGLES); 59 | _AML_Vertex2f(a); 60 | _AML_Vertex2f(b); 61 | _AML_Vertex2f(c); 62 | glEnd(); 63 | glFlush(); 64 | 65 | } 66 | void _step(double _tim, double _dtim, _Agent* _last_Agent) { 67 | Blinker* _last = (Blinker*)_last_Agent; 68 | double dlt = (((_dtim / 10) * 2) * PI); 69 | theta = (theta + dlt); 70 | pos = (vec2f(cos(theta), sin(theta)) * 5); 71 | if((!(_last->theta > angle(0))&&(theta > angle(0)))) { 72 | /* operator :> means "when > holds true for the first time"*/ 73 | color = (color ^ 1); 74 | } 75 | } 76 | void _copy_from(_Agent *_from_Agent){ 77 | Blinker* _from = (Blinker*)_from_Agent; 78 | *this = *_from; 79 | } 80 | }; 81 | 82 | vector > _agents; 83 | double _dt=1/_FPS*_SPEEDUP_RATE; 84 | double _cur_tim=0.0; 85 | void _Plot(void){ 86 | glClear(GL_COLOR_BUFFER_BIT); 87 | for(int i=0;i<_agents.size();i++){ 88 | _agents[i].second->_plot(_cur_tim, _dt); 89 | } 90 | glutSwapBuffers(); 91 | } 92 | void _Step_Time(int _time_value){ 93 | //printf("step time: %d\n",_time_value); 94 | for(int i=0;i<_agents.size();i++){ 95 | _agents[i].second->_step(_cur_tim,_dt,_agents[i].first); 96 | _agents[i].first->_copy_from(_agents[i].second); 97 | } 98 | _cur_tim+=_dt; 99 | glutPostRedisplay(); 100 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 101 | } 102 | 103 | int main(int argc, char *argv[]) { 104 | glutInit(&argc, argv); 105 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 106 | glutInitWindowPosition(100, 100); 107 | glutInitWindowSize(500, 500); 108 | int glut_window = glutCreateWindow("AML"); 109 | _Agent *_Agent1 = new Blinker(); 110 | _Agent *__Agent1 = new Blinker(); 111 | *__Agent1 = *_Agent1; 112 | _agents.push_back(make_pair(__Agent1, _Agent1)); 113 | glutDisplayFunc(&_Plot); 114 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 115 | glutMainLoop(); 116 | } 117 | -------------------------------------------------------------------------------- /demo/demo2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hzwer/AML/6eb61e38653f0e8ce87f8aadf8a856470862026f/demo/demo2.gif -------------------------------------------------------------------------------- /demo/demo3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hzwer/AML/6eb61e38653f0e8ce87f8aadf8a856470862026f/demo/demo3.gif -------------------------------------------------------------------------------- /demo/geolib.h: -------------------------------------------------------------------------------- 1 | #ifndef _GEOLIB_H 2 | #define _GEOLIB_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | typedef double Float; 11 | class vec2f{ 12 | public: 13 | Float x,y; 14 | vec2f(Float _x=0,Float _y=0):x(_x),y(_y){} 15 | vec2f operator + (const vec2f &b){return vec2f(x+b.x,y+b.y);} 16 | vec2f operator - (const vec2f &b){return vec2f(x-b.x,y-b.y);} 17 | Float operator * (const vec2f &b){return x*b.x+y*b.y;} 18 | vec2f operator * (Float b){return vec2f(x*b,y*b);} 19 | vec2f operator / (Float b){return vec2f(x/b,y/b);} 20 | Float _sqrlen(void){return x*x+y*y;} 21 | Float _len(void){return sqrt(_sqrlen());} 22 | Float _crossprd(const vec2f &b){return x*b.y-b.x*y;} 23 | Float _dot(const vec2f &b){return x*b.x+y*b.y;} 24 | vec2f _norm(void){return (*this)/_len();} 25 | }; 26 | class vec3f{ 27 | public: 28 | Float x,y,z; 29 | vec3f(Float _x=0,Float _y=0,Float _z=0):x(_x),y(_y),z(_z){} 30 | vec3f operator + (const vec3f &b){return vec3f(x+b.x,y+b.y,z+b.z);} 31 | vec3f operator - (const vec3f &b){return vec3f(x-b.x,y-b.y,z-b.z);} 32 | Float operator * (const vec3f &b){return x*b.x+y*b.y+z*b.z;} 33 | vec3f operator * (Float b){return vec3f(x*b,y*b,z*b);} 34 | vec3f operator / (Float b){return vec3f(x/b,y/b,z/b);} 35 | Float _sqrlen(void){return x*x+y*y+z*z;} 36 | Float _len(void){return sqrt(_sqrlen());} 37 | vec3f _crossprd(const vec3f &b){return vec3f(y*b.z-z*b.y,z*b.x-x*b.z,x*b.y-y*b.x);} 38 | Float _dot(const vec3f &b){return x*b.x+y*b.y+z*b.z;} 39 | vec3f _norm(void){return (*this)/_len();} 40 | }; 41 | 42 | const Float _PI=acos(-1.0); 43 | class angle{//[-PI,PI] 44 | private: 45 | angle(vec2f r){ 46 | p=r/sqrt(r._sqrlen()); 47 | } 48 | angle(Float _x,Float _y){ 49 | vec2f r(_x,_y); 50 | p=r/sqrt(r._sqrlen()); 51 | } 52 | public: 53 | vec2f p; 54 | angle(){} 55 | angle(Float r){ 56 | p=vec2f(cos(r),sin(r)); 57 | } 58 | operator Float(){ 59 | return atan2(p.y,p.x); 60 | } 61 | angle operator + (const angle &q){ 62 | //cos(a+b)=cos(a)cos(b)-sin(a)sin(b) 63 | //sin(a+b)=cos(a)sin(b)+sin(a)cos(b) 64 | return angle(p.x*q.p.x-p.y*q.p.y, p.x*q.p.y+p.y*q.p.x); 65 | } 66 | angle operator + (Float d){ 67 | return *this+angle(d); 68 | //return angle(p.x*cos(d)-p.y*sin(d), p.x*sin(d)+p.y*cos(d)); 69 | } 70 | angle operator - (const angle &q){ 71 | //cos(a-b)=cos(a)cos(b)+sin(a)sin(b) 72 | //sin(a-b)=sin(a)cos(b)-cos(a)sin(b) 73 | return angle(p.x*q.p.x+p.y*q.p.y, -p.x*q.p.y+p.y*q.p.x); 74 | } 75 | angle operator - (Float d){ 76 | return *this-angle(d); 77 | return angle(p.x*cos(d)+p.y*sin(d), -p.x*sin(d)+p.y*cos(d)); 78 | } 79 | bool operator < (const angle &b){ 80 | return p._crossprd(b.p)>0; 81 | } 82 | bool operator <= (const angle &b){ 83 | return p._crossprd(b.p)>=0; 84 | } 85 | bool operator > (const angle &b){ 86 | return p._crossprd(b.p)<0; 87 | } 88 | bool operator >= (const angle &b){ 89 | return p._crossprd(b.p)<=0; 90 | } 91 | bool operator == (const angle &b){ 92 | return p._crossprd(b.p)==0; 93 | } 94 | angle operator * (Float d){ 95 | Float r=atan2(p.y,p.x); 96 | return angle(r*d); 97 | } 98 | angle _approach(const angle &a_tgt,const angle &a_lw,const angle &a_hi){ 99 | vec2f tgt=a_tgt.p; 100 | vec2f lw=a_hi.p,hi=a_hi.p; 101 | assert(p._crossprd(lw) * p._crossprd(hi) <= 0.0); 102 | //instead of approaching this angle to tgt, "approach" tgt to this angle 103 | vec2f ret1,ret2;//ret1: countercloskwise, ret2: clockwise 104 | if(p._crossprd(tgt)>=0){ 105 | if(p._crossprd(lw)>=0&&lw._crossprd(tgt)>=0){ 106 | ret1=lw; 107 | ret2=hi; 108 | } 109 | else if(p._crossprd(hi)>=0&&hi._crossprd(tgt)>=0){ 110 | ret1=hi; 111 | ret2=lw; 112 | } 113 | else{ 114 | return a_tgt; 115 | } 116 | } 117 | else{ 118 | if(p._crossprd(lw)<=0&&lw._crossprd(tgt)<=0){ 119 | ret1=lw; 120 | ret2=hi; 121 | } 122 | else if(p._crossprd(hi)<=0&&hi._crossprd(tgt)<=0){ 123 | ret1=hi; 124 | ret2=lw; 125 | } 126 | else{ 127 | return a_tgt; 128 | } 129 | } 130 | if(fabs(ret1._crossprd(tgt))<=fabs(ret2._crossprd(tgt))){ 131 | return angle(ret1); 132 | } 133 | else{ 134 | return angle(ret2); 135 | } 136 | } 137 | }; 138 | #endif 139 | -------------------------------------------------------------------------------- /demo/raytracing.cpp: -------------------------------------------------------------------------------- 1 | #include "geolib.h" 2 | #include "cglib.h" 3 | 4 | double _FPS=1; 5 | //unit is "meter" for them 6 | double _MAP_X=20,_MAP_Y=20; 7 | vec2f _MAP_CENTER(0,0);//5m 8 | double _SPEEDUP_RATE=1; 9 | 10 | //must allowed user-defination interfaces: 11 | void _Set_Map_XY(double _X,double _Y){ 12 | _MAP_X = _X; 13 | _MAP_Y = _Y; 14 | } 15 | void _Set_Map_Center(double _x,double _y){ 16 | _MAP_CENTER = vec2f(_x,_y); 17 | } 18 | void _Set_Speedup_Rate(double _rate){ 19 | _SPEEDUP_RATE=_rate; 20 | } 21 | void _SET_FPS(double _fps){ 22 | _FPS = _fps; 23 | } 24 | //interfaces end 25 | 26 | void _AML_Vertex2f(vec2f pos){ 27 | pos = pos - _MAP_CENTER; 28 | pos = (pos/_MAP_X*2.0, pos/_MAP_Y*2.0); 29 | glVertex2f(pos.x,pos.y); 30 | } 31 | 32 | class _Agent{ 33 | public: 34 | string name; 35 | virtual void _plot(double,double)=0; 36 | virtual void _step(double,double,_Agent*)=0; 37 | virtual void _copy_from(_Agent*)=0; 38 | }; 39 | template 40 | vector _Global_Filter(bool (*f)(_Agent*)); 41 | 42 | int PH=50,PW=50; 43 | double L=1; 44 | const double eps=1e-10; 45 | int facenum; 46 | vec3f *xyzs;//length=3*facenum 47 | vec3f *ambients;//length=facenum 48 | vec3f *diffuses;//length=facenum 49 | vec3f *speculars;//length=facenum 50 | double *shininesses;//length=facenum 51 | int lightnum; 52 | vec3f *lightxyzs;//length=lightnum 53 | vec3f *lightrgbs;//length=lightnum 54 | void print(vec3f p){ 55 | cout<<"("<0){ 167 | if(t > _agents; 242 | double _dt=1/_FPS*_SPEEDUP_RATE; 243 | double _cur_tim=0.0; 244 | template 245 | vector _Global_Filter(bool (*f)(_Agent*)){ 246 | vector ret; 247 | for(int i=0;i<_agents.size();i++){ 248 | if(f(_agents[i].second)){ 249 | ret.push_back((T*)_agents[i].second); 250 | } 251 | } 252 | return ret; 253 | } 254 | void _Plot(void){ 255 | glClear(GL_COLOR_BUFFER_BIT); 256 | for(int i=0;i<_agents.size();i++){ 257 | _agents[i].second->_plot(_cur_tim, _dt); 258 | } 259 | glutSwapBuffers(); 260 | printf("enter to continue:"); 261 | getchar(); 262 | printf("go\n"); 263 | } 264 | void _Step_Time(int _time_value){ 265 | //printf("step time: %d\n",_time_value); 266 | for(int i=0;i<_agents.size();i++){ 267 | _agents[i].second->_step(_cur_tim,_dt,_agents[i].first); 268 | _agents[i].first->_copy_from(_agents[i].second); 269 | } 270 | _cur_tim+=_dt; 271 | glutPostRedisplay(); 272 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 273 | } 274 | 275 | int main(int argc, char *argv[]){ 276 | /*function calls such as _SET_MAP_XY must be filled here 277 | */ 278 | glutInit(&argc, argv); 279 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);//double buffer 280 | //todo: user shall be able to designate these things in AML 281 | glutInitWindowPosition(100, 100); 282 | glutInitWindowSize(50,50); 283 | int glut_window = glutCreateWindow("AML"); 284 | //end todo 285 | _Set_Map_XY(2*L,2*L); 286 | read("1.scn"); 287 | for(int i=0;i 57 | vector _Global_Filter(bool (*f)(_Agent*)); 58 | class _Runner: public _Agent{ 59 | public: 60 | vec2f pos; 61 | double rate; 62 | vec2f vel; 63 | int color; 64 | _Runner(double x,double y,double r,double deg){ 65 | name="Runner";//compiler add this 66 | pos=vec2f(x,y); 67 | rate=r; 68 | double alpha=deg*180/PI; 69 | vel=vec2f(cos(alpha),sin(alpha)); 70 | color=0; 71 | } 72 | void _plot(double _tim,double _dt){ 73 | //printf("%lf %lf\n",pos.x,pos.y); 74 | if(color==0){ 75 | glColor3f(1.0,0.0,0.0); 76 | } 77 | else{ 78 | glColor3f(0.0,0.0,1.0); 79 | } 80 | plot_square(pos); 81 | glFlush(); 82 | } 83 | void _step(double _tim,double _dt,_Agent* _last_Agent){ 84 | _Runner* _last=(_Runner*)_last_Agent; 85 | double t_rate=rate*_dt; 86 | pos=pos+vel*t_rate; 87 | if(!((_last->pos).x>=L)&&(pos.x)>=L) vel.x=-vel.x; 88 | if(!((_last->pos).x<=-L)&&(pos.x)<=-L) vel.x=-vel.x; 89 | if(!((_last->pos).y>=L)&&(pos.y)>=L) vel.y=-vel.y; 90 | if(!((_last->pos).y<=-L)&&(pos.y)<=-L) vel.y=-vel.y; 91 | } 92 | void _copy_from(_Agent* _from_Agent){ 93 | _Runner* _from=(_Runner*)_from_Agent; 94 | *this=*_from; 95 | } 96 | }; 97 | bool is_runner(_Agent *a){ 98 | return a->name=="Runner"; 99 | } 100 | class _Chaser: public _Agent{ 101 | public: 102 | vec2f pos; 103 | vec2f vel; 104 | double rate; 105 | _Chaser(double x,double y,double r){ 106 | name="Chaser";//compiler add this 107 | pos=vec2f(x,y); 108 | vel=vec2f(0,0); 109 | rate=r; 110 | } 111 | void _plot(double _tim,double _dt){ 112 | //printf("%lf %lf\n",pos.x,pos.y); 113 | glColor3f(1.0,1.0,1.0); 114 | plot_square(pos); 115 | glFlush(); 116 | } 117 | void _step(double _tim,double _dt,_Agent* _last_Agent){ 118 | pos=pos+vel; 119 | vector<_Runner*> agents=_Global_Filter<_Runner>(is_runner); 120 | _Runner* near=NULL; 121 | double sl=-1; 122 | for(int i=0;ipos; 124 | if(near==NULL||(q-pos)._sqrlen()pos-pos; 130 | vel=vel/sqrt(vel._sqrlen()); 131 | double t_rate=rate*_dt; 132 | pos=pos+vel*t_rate; 133 | vel=vel*t_rate; 134 | } 135 | void _copy_from(_Agent* _from_Agent){ 136 | _Chaser* _from=(_Chaser*)_from_Agent; 137 | *this=*_from; 138 | } 139 | }; 140 | vector > _agents; 141 | double _dt=1/_FPS*_SPEEDUP_RATE; 142 | double _cur_tim=0.0; 143 | template 144 | vector _Global_Filter(bool (*f)(_Agent*)){ 145 | vector ret; 146 | for(int i=0;i<_agents.size();i++){ 147 | if(f(_agents[i].second)){ 148 | ret.push_back((T*)_agents[i].second); 149 | } 150 | } 151 | return ret; 152 | } 153 | void _Plot(void){ 154 | glClear(GL_COLOR_BUFFER_BIT); 155 | for(int i=0;i<_agents.size();i++){ 156 | _agents[i].second->_plot(_cur_tim, _dt); 157 | } 158 | glutSwapBuffers(); 159 | } 160 | void _Step_Time(int _time_value){ 161 | //printf("step time: %d\n",_time_value); 162 | for(int i=0;i<_agents.size();i++){ 163 | _agents[i].second->_step(_cur_tim,_dt,_agents[i].first); 164 | _agents[i].first->_copy_from(_agents[i].second); 165 | } 166 | _cur_tim+=_dt; 167 | glutPostRedisplay(); 168 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 169 | } 170 | 171 | int main(int argc, char *argv[]){ 172 | /*function calls such as _SET_MAP_XY must be filled here 173 | */ 174 | glutInit(&argc, argv); 175 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);//double buffer 176 | //todo: user shall be able to designate these things in AML 177 | glutInitWindowPosition(100, 100); 178 | glutInitWindowSize(1000, 1000); 179 | int glut_window = glutCreateWindow("AML"); 180 | //end todo 181 | _Set_Map_XY(2*L,2*L); 182 | double R=50; 183 | for(int i=0;i<10;i++){ 184 | double deg=i*36.0; 185 | double x=cos(deg)*R; 186 | double y=sin(deg)*R; 187 | _Agent *r=new _Runner(x,y,15+i,deg); 188 | _Agent *_copy_r=new _Runner(x,y,15+i,deg);//our compiler just copy definition of r 189 | *_copy_r=*r; 190 | _agents.push_back(make_pair(_copy_r,r)); 191 | } 192 | _Agent *c=new _Chaser(1,0,5); 193 | _Agent *_copy_c=new _Chaser(1,0,5); 194 | *_copy_c=*c; 195 | _agents.push_back(make_pair(_copy_c,c)); 196 | glutDisplayFunc(&_Plot); 197 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 198 | glutMainLoop(); 199 | return 0; 200 | } 201 | 202 | -------------------------------------------------------------------------------- /demo/small-origin.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include // smallpt, a Path Tracer by Kevin Beason, 2008 3 | #include // Make : g++ -O3 -fopenmp smallpt.cpp -o smallpt 4 | #include // Remove "-fopenmp" for g++ version < 4.2 5 | struct Vec { // Usage: time ./smallpt 5000 && xv image.ppm 6 | double x, y, z; // position, also color (r,g,b) 7 | Vec(double x_=0, double y_=0, double z_=0){ x=x_; y=y_; z=z_; } 8 | Vec operator+(const Vec &b) const { return Vec(x+b.x,y+b.y,z+b.z); } 9 | Vec operator-(const Vec &b) const { return Vec(x-b.x,y-b.y,z-b.z); } 10 | Vec operator*(double b) const { return Vec(x*b,y*b,z*b); } 11 | Vec mult(const Vec &b) const { return Vec(x*b.x,y*b.y,z*b.z); } 12 | Vec& norm(){ return *this = *this * (1/sqrt(x*x+y*y+z*z)); } 13 | double dot(const Vec &b) const { return x*b.x+y*b.y+z*b.z; } // cross: 14 | Vec operator%(Vec&b){return Vec(y*b.z-z*b.y,z*b.x-x*b.z,x*b.y-y*b.x);} 15 | }; 16 | struct Ray { Vec o, d; Ray(Vec o_, Vec d_) : o(o_), d(d_) {} }; 17 | enum Refl_t { DIFF, SPEC, REFR }; // material types, used in radiance() 18 | struct Sphere { 19 | double rad; // radius 20 | Vec p, e, c; // position, emission, color 21 | Refl_t refl; // reflection type (DIFFuse, SPECular, REFRactive) 22 | Sphere(double rad_, Vec p_, Vec e_, Vec c_, Refl_t refl_): 23 | rad(rad_), p(p_), e(e_), c(c_), refl(refl_) {} 24 | double intersect(const Ray &r) const { // returns distance, 0 if nohit 25 | Vec op = p-r.o; // Solve t^2*d.d + 2*t*(o-p).d + (o-p).(o-p)-R^2 = 0 26 | double t, eps=1e-4, b=op.dot(r.d), det=b*b-op.dot(op)+rad*rad; 27 | if (det<0) return 0; else det=sqrt(det); 28 | return (t=b-det)>eps ? t : ((t=b+det)>eps ? t : 0); 29 | } 30 | }; 31 | Sphere spheres[] = {//Scene: radius, position, emission, color, material 32 | Sphere(1e5, Vec( 1e5+1,40.8,81.6), Vec(),Vec(.75,.25,.25),DIFF),//Left 33 | Sphere(1e5, Vec(-1e5+99,40.8,81.6),Vec(),Vec(.25,.25,.75),DIFF),//Rght 34 | Sphere(1e5, Vec(50,40.8, 1e5), Vec(),Vec(.75,.75,.75),DIFF),//Back 35 | Sphere(1e5, Vec(50,40.8,-1e5+170), Vec(),Vec(), DIFF),//Frnt 36 | Sphere(1e5, Vec(50, 1e5, 81.6), Vec(),Vec(.75,.75,.75),DIFF),//Botm 37 | Sphere(1e5, Vec(50,-1e5+81.6,81.6),Vec(),Vec(.75,.75,.75),DIFF),//Top 38 | Sphere(16.5,Vec(27,16.5,47), Vec(),Vec(1,1,1)*.999, SPEC),//Mirr 39 | Sphere(16.5,Vec(73,16.5,78), Vec(),Vec(1,1,1)*.999, REFR),//Glas 40 | Sphere(600, Vec(50,681.6-.27,81.6),Vec(12,12,12), Vec(), DIFF) //Lite 41 | }; 42 | inline double clamp(double x){ return x<0 ? 0 : x>1 ? 1 : x; } 43 | inline int toInt(double x){ return int(pow(clamp(x),1/2.2)*255+.5); } 44 | inline bool intersect(const Ray &r, double &t, int &id){ 45 | double n=sizeof(spheres)/sizeof(Sphere), d, inf=t=1e20; 46 | for(int i=int(n);i--;) if((d=spheres[i].intersect(r))&&df.y && f.x>f.z ? f.x : f.y>f.z ? f.y : f.z; // max refl 56 | if (++depth>5) if (erand48(Xi).1?Vec(0,1):Vec(1))%w).norm(), v=w%u; 60 | Vec d = (u*cos(r1)*r2s + v*sin(r1)*r2s + w*sqrt(1-r2)).norm(); 61 | return obj.e + f.mult(radiance(Ray(x,d),depth,Xi)); 62 | } else if (obj.refl == SPEC) // Ideal SPECULAR reflection 63 | return obj.e + f.mult(radiance(Ray(x,r.d-n*2*n.dot(r.d)),depth,Xi)); 64 | Ray reflRay(x, r.d-n*2*n.dot(r.d)); // Ideal dielectric REFRACTION 65 | bool into = n.dot(nl)>0; // Ray from outside going in? 66 | double nc=1, nt=1.5, nnt=into?nc/nt:nt/nc, ddn=r.d.dot(nl), cos2t; 67 | if ((cos2t=1-nnt*nnt*(1-ddn*ddn))<0) // Total internal reflection 68 | return obj.e + f.mult(radiance(reflRay,depth,Xi)); 69 | Vec tdir = (r.d*nnt - n*((into?1:-1)*(ddn*nnt+sqrt(cos2t)))).norm(); 70 | double a=nt-nc, b=nt+nc, R0=a*a/(b*b), c = 1-(into?-ddn:tdir.dot(n)); 71 | double Re=R0+(1-R0)*c*c*c*c*c,Tr=1-Re,P=.25+.5*Re,RP=Re/P,TP=Tr/(1-P); 72 | return obj.e + f.mult(depth>2 ? (erand48(Xi)

eps ? t : ((t=b+det)>eps ? t : 0); 14 | } 15 | }; 16 | const int SPHERE_NUM=9; 17 | Sphere spheres[SPHERE_NUM] = {//Scene: radius, position, emission, color, material 18 | Sphere(1e5, vec3f( 1e5+1,40.8,81.6), vec3f(),vec3f(.75,.25,.25),0),//Left 19 | Sphere(1e5, vec3f(-1e5+99,40.8,81.6),vec3f(),vec3f(.25,.25,.75),0),//Rght 20 | Sphere(1e5, vec3f(50,40.8, 1e5), vec3f(),vec3f(.75,.75,.75),0),//Back 21 | Sphere(1e5, vec3f(50,40.8,-1e5+170), vec3f(),vec3f(), 0),//Frnt 22 | Sphere(1e5, vec3f(50, 1e5, 81.6), vec3f(),vec3f(.75,.75,.75),0),//Botm 23 | Sphere(1e5, vec3f(50,-1e5+81.6,81.6),vec3f(),vec3f(.75,.75,.75),0),//Top 24 | Sphere(16.5,vec3f(27,16.5,47), vec3f(),vec3f(1,1,1)*.999, 1),//Mirr 25 | Sphere(16.5,vec3f(73,16.5,78), vec3f(),vec3f(1,1,1)*.999, 2),//Glas 26 | Sphere(600, vec3f(50,681.6-.27,81.6),vec3f(12,12,12), vec3f(), 0) //Lite 27 | }; 28 | double clamp(double x){ return x<0 ? 0 : x>1 ? 1 : x; } 29 | int toInt(double x){ return int(pow(clamp(x),1/2.2)*255+.5); } 30 | bool intersect(const Ray &r, double &t, int &id){ 31 | double d,inf=t=1e20; 32 | for(int i=SPHERE_NUM;i--;) if((d=spheres[i].intersect(r))&&df.y && f.x>f.z ? f.x : f.y>f.z ? f.y : f.z; // max refl 67 | depth++; 68 | if (obj.refl == 0){ // Ideal diffuse reflection 69 | double r1=2*M_PI*erand48(seed), r2=erand48(seed), r2s=sqrt(r2); 70 | vec3f w=nl, u=((fabs(w.x)>.1?vec3f(0,1):vec3f(1))%w)._norm(), v=w%u; 71 | vec3f d = (u*cos(r1)*r2s + v*sin(r1)*r2s + w*sqrt(1-r2))._norm(); 72 | colsum=colsum+mult(raycol,obj.e); 73 | raycol=mult(raycol,f); 74 | r=Ray(x,d); 75 | } 76 | else if (obj.refl == 1){ // Ideal 1ULAR reflection 77 | colsum=colsum+mult(raycol,obj.e); 78 | raycol=mult(raycol,f); 79 | r=Ray(x,r.d-n*2*n._dot(r.d)); 80 | } 81 | else{ 82 | Ray reflRay=Ray(x, r.d-n*2*n._dot(r.d)); // Ideal dielectric refraction 83 | bool into = n._dot(nl)>0; // Ray from outside going in? 84 | double nc=1, nt=1.5, nnt=into?nc/nt:nt/nc, ddn=r.d._dot(nl), cos2t; 85 | if ((cos2t=1-nnt*nnt*(1-ddn*ddn))<0){ // Total internal reflection 86 | colsum=colsum+mult(raycol,obj.e); 87 | raycol=mult(raycol,f); 88 | r=reflRay; 89 | } 90 | else{ 91 | vec3f tdir = (r.d*nnt - n*((into?1:-1)*(ddn*nnt+sqrt(cos2t))))._norm(); 92 | double a=nt-nc, b=nt+nc, R0=a*a/(b*b), c = 1-(into?-ddn:tdir._dot(n)); 93 | double Re=R0+(1-R0)*c*c*c*c*c,Tr=1-Re,P=.25+.5*Re,RP=Re/P,TP=Tr/(1-P); 94 | colsum=colsum+mult(raycol,obj.e); 95 | raycol=mult(raycol,f); 96 | if(depth>2){ 97 | if(erand48(seed) 5 | #define M_PI 3.14159265358979323846264338327950288 6 | std::default_random_engine generator; 7 | std::uniform_real_distribution distr(0.0, 1.0); 8 | double erand48(unsigned short *x) { 9 | return distr(generator); 10 | } 11 | 12 | double _FPS=1; 13 | //unit is "meter" for them 14 | double _MAP_X=20,_MAP_Y=20; 15 | vec2f _MAP_CENTER(0,0);//5m 16 | double _SPEEDUP_RATE=1; 17 | 18 | //must allowed user-defination interfaces: 19 | void _Set_Map_XY(double _X,double _Y){ 20 | _MAP_X = _X; 21 | _MAP_Y = _Y; 22 | } 23 | void _Set_Map_Center(double _x,double _y){ 24 | _MAP_CENTER = vec2f(_x,_y); 25 | } 26 | void _Set_Speedup_Rate(double _rate){ 27 | _SPEEDUP_RATE=_rate; 28 | } 29 | void _SET_FPS(double _fps){ 30 | _FPS = _fps; 31 | } 32 | //interfaces end 33 | 34 | void _AML_Vertex2f(vec2f pos){ 35 | pos = pos - _MAP_CENTER; 36 | pos = (pos/_MAP_X*2.0, pos/_MAP_Y*2.0); 37 | glVertex2f(pos.x,pos.y); 38 | } 39 | 40 | class _Agent{ 41 | public: 42 | string name; 43 | virtual void _plot(double,double)=0; 44 | virtual void _step(double,double,_Agent*)=0; 45 | virtual void _copy_from(_Agent*)=0; 46 | }; 47 | template 48 | vector _Global_Filter(bool (*f)(_Agent*)); 49 | 50 | struct Ray { vec3f o, d; Ray(vec3f o_, vec3f d_) : o(o_), d(d_) {} Ray(){}}; 51 | //0:DIFF, 1:SPEC, 2:REFR 52 | struct Sphere { 53 | double rad; // radius 54 | vec3f p, e, c; // position, emission, color 55 | int refl; // reflection type (0=diffuse, 1=specular, 2=refractive) 56 | Sphere(double rad_, vec3f p_, vec3f e_, vec3f c_, int refl_): 57 | rad(rad_), p(p_), e(e_), c(c_), refl(refl_) {} 58 | double intersect(Ray r){ // returns distance, 0 if nohit 59 | vec3f op = p-r.o; // Solve t^2*d.d + 2*t*(o-p).d + (o-p).(o-p)-R^2 = 0 60 | double t, eps=1e-4, b=op._dot(r.d), det=b*b-op._dot(op)+rad*rad; 61 | if (det<0) return 0; else det=sqrt(det); 62 | return (t=b-det)>eps ? t : ((t=b+det)>eps ? t : 0); 63 | } 64 | }; 65 | const int SPHERE_NUM=9; 66 | Sphere spheres[SPHERE_NUM] = {//Scene: radius, position, emission, color, material 67 | Sphere(1e5, vec3f( 1e5+1,40.8,81.6), vec3f(),vec3f(.75,.25,.25),0),//Left 68 | Sphere(1e5, vec3f(-1e5+99,40.8,81.6),vec3f(),vec3f(.25,.25,.75),0),//Rght 69 | Sphere(1e5, vec3f(50,40.8, 1e5), vec3f(),vec3f(.75,.75,.75),0),//Back 70 | Sphere(1e5, vec3f(50,40.8,-1e5+170), vec3f(),vec3f(), 0),//Frnt 71 | Sphere(1e5, vec3f(50, 1e5, 81.6), vec3f(),vec3f(.75,.75,.75),0),//Botm 72 | Sphere(1e5, vec3f(50,-1e5+81.6,81.6),vec3f(),vec3f(.75,.75,.75),0),//Top 73 | Sphere(16.5,vec3f(27,16.5,47), vec3f(),vec3f(1,1,1)*.999, 1),//Mirr 74 | Sphere(16.5,vec3f(73,16.5,78), vec3f(),vec3f(1,1,1)*.999, 2),//Glas 75 | Sphere(600, vec3f(50,681.6-.27,81.6),vec3f(12,12,12), vec3f(), 0) //Lite 76 | }; 77 | double clamp(double x){ return x<0 ? 0 : x>1 ? 1 : x; } 78 | int toInt(double x){ return int(pow(clamp(x),1/2.2)*255+.5); } 79 | bool intersect(const Ray &r, double &t, int &id){ 80 | double d,inf=t=1e20; 81 | for(int i=SPHERE_NUM;i--;) if((d=spheres[i].intersect(r))&&df.y && f.x>f.z ? f.x : f.y>f.z ? f.y : f.z; // max refl 117 | depth++; 118 | if (obj.refl == 0){ // Ideal diffuse reflection 119 | double r1=2*M_PI*erand48(seed), r2=erand48(seed), r2s=sqrt(r2); 120 | vec3f w=nl, u=((fabs(w.x)>.1?vec3f(0,1,0):vec3f(1,0,0))._crossprd(w))._norm(), v=w._crossprd(u); 121 | vec3f d = (u*cos(r1)*r2s + v*sin(r1)*r2s + w*sqrt(1-r2))._norm(); 122 | colsum=colsum+mult(raycol,obj.e); 123 | raycol=mult(raycol,f); 124 | r=Ray(x,d); 125 | } 126 | else if (obj.refl == 1){ // Ideal 1ULAR reflection 127 | colsum=colsum+mult(raycol,obj.e); 128 | raycol=mult(raycol,f); 129 | r=Ray(x,r.d-n*2*n._dot(r.d)); 130 | } 131 | else{ 132 | Ray reflRay=Ray(x, r.d-n*2*n._dot(r.d)); // Ideal dielectric refraction 133 | bool into = n._dot(nl)>0; // Ray from outside going in? 134 | double nc=1, nt=1.5, nnt=into?nc/nt:nt/nc, ddn=r.d._dot(nl), cos2t; 135 | if ((cos2t=1-nnt*nnt*(1-ddn*ddn))<0){ // Total internal reflection 136 | colsum=colsum+mult(raycol,obj.e); 137 | raycol=mult(raycol,f); 138 | r=reflRay; 139 | } 140 | else{ 141 | vec3f tdir = (r.d*nnt - n*((into?1:-1)*(ddn*nnt+sqrt(cos2t))))._norm(); 142 | double a=nt-nc, b=nt+nc, R0=a*a/(b*b), c = 1-(into?-ddn:tdir._dot(n)); 143 | double Re=R0+(1-R0)*c*c*c*c*c,Tr=1-Re,P=.25+.5*Re,RP=Re/P,TP=Tr/(1-P); 144 | colsum=colsum+mult(raycol,obj.e); 145 | raycol=mult(raycol,f); 146 | if(depth>2){ 147 | if(erand48(seed) > _agents; 164 | double _dt=1/_FPS*_SPEEDUP_RATE; 165 | double _cur_tim=0.0; 166 | template 167 | vector _Global_Filter(bool (*f)(_Agent*)){ 168 | vector ret; 169 | for(int i=0;i<_agents.size();i++){ 170 | if(f(_agents[i].second)){ 171 | ret.push_back((T*)_agents[i].second); 172 | } 173 | } 174 | return ret; 175 | } 176 | void _Plot(void){ 177 | glClear(GL_COLOR_BUFFER_BIT); 178 | for(int i=0;i<_agents.size();i++){ 179 | _agents[i].second->_plot(_cur_tim, _dt); 180 | } 181 | glutSwapBuffers(); 182 | printf("enter to continue:"); 183 | //getchar(); 184 | printf("go\n"); 185 | } 186 | void _Step_Time(int _time_value){ 187 | //printf("step time: %d\n",_time_value); 188 | for(int i=0;i<_agents.size();i++){ 189 | _agents[i].second->_step(_cur_tim,_dt,_agents[i].first); 190 | _agents[i].first->_copy_from(_agents[i].second); 191 | } 192 | _cur_tim+=_dt; 193 | glutPostRedisplay(); 194 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 195 | } 196 | 197 | 198 | int main(int argc, char *argv[]){ 199 | glutInit(&argc, argv); 200 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);//double buffer 201 | //todo: user shall be able to designate these things in AML 202 | glutInitWindowPosition(100, 100); 203 | glutInitWindowSize(1024/2,768/2); 204 | int glut_window = glutCreateWindow("AML"); 205 | 206 | int w=1024, h=768;//, samps = argc==2 ? atoi(argv[1])/4 : 1; // # samples 207 | int samps=4; 208 | _Set_Map_XY(w,h); 209 | _Set_Map_Center(w/2.0,h/2.0); 210 | Ray cam(vec3f(50,52,295.6), vec3f(0,-0.042612,-1)._norm()); // cam pos, dir 211 | vec3f cx=vec3f(w*.5135/h), cy=(cx._crossprd(cam.d))._norm()*.5135, r, *c=new vec3f[w*h]; 212 | for (int y=0; y 5 | #include 6 | #define gl_h_ 7 | #include 8 | // #include 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /lib/geolib.h: -------------------------------------------------------------------------------- 1 | #ifndef _GEOLIB_H 2 | #define _GEOLIB_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | typedef double Float; 11 | class vec2f{ 12 | public: 13 | Float x,y; 14 | vec2f(){} 15 | vec2f(Float _x,Float _y):x(_x),y(_y){} 16 | vec2f operator + (const vec2f &b){return vec2f(x+b.x,y+b.y);} 17 | vec2f operator - (const vec2f &b){return vec2f(x-b.x,y-b.y);} 18 | vec2f operator - (){return vec2f(-x,-y);} 19 | Float operator * (const vec2f &b){return x*b.x+y*b.y;} 20 | vec2f operator * (Float b){return vec2f(x*b,y*b);} 21 | vec2f operator / (Float b){return vec2f(x/b,y/b);} 22 | Float _sqrlen(void){return x*x+y*y;} 23 | Float _crossprd(const vec2f &b){return x*b.y-b.x*y;} 24 | }; 25 | class vec3f{ 26 | public: 27 | Float x,y,z; 28 | vec3f(){} 29 | vec3f(Float _x,Float _y,Float _z):x(_x),y(_y),z(_z){} 30 | vec3f operator + (const vec3f &b){return vec3f(x+b.x,y+b.y,z+b.z);} 31 | vec3f operator - (const vec3f &b){return vec3f(x-b.x,y-b.y,z-b.z);} 32 | Float operator * (const vec3f &b){return x*b.x+y*b.y+z*b.z;} 33 | vec3f operator * (Float b){return vec3f(x*b,y*b,z*b);} 34 | vec3f operator / (Float b){return vec3f(x/b,y/b,z/b);} 35 | Float _sqrlen(void){return x*x+y*y+z*z;} 36 | vec3f _crossprd(const vec3f &b){return vec3f(y*b.z-z*b.y,z*b.x-x*b.z,x*b.y-y*b.x);} 37 | }; 38 | 39 | const Float _PI=acos(-1.0); 40 | class angle{//[-PI,PI] 41 | private: 42 | angle(vec2f r){ 43 | p=r/sqrt(r._sqrlen()); 44 | } 45 | angle(Float _x,Float _y){ 46 | vec2f r(_x,_y); 47 | p=r/sqrt(r._sqrlen()); 48 | } 49 | public: 50 | vec2f p; 51 | angle(){} 52 | angle(Float r){ 53 | p=vec2f(cos(r),sin(r)); 54 | } 55 | operator Float(){ 56 | return atan2(p.y,p.x); 57 | } 58 | angle operator + (const angle &q){ 59 | //cos(a+b)=cos(a)cos(b)-sin(a)sin(b) 60 | //sin(a+b)=cos(a)sin(b)+sin(a)cos(b) 61 | return angle(p.x*q.p.x-p.y*q.p.y, p.x*q.p.y+p.y*q.p.x); 62 | } 63 | angle operator + (Float d){ 64 | return *this+angle(d); 65 | //return angle(p.x*cos(d)-p.y*sin(d), p.x*sin(d)+p.y*cos(d)); 66 | } 67 | angle operator - (const angle &q){ 68 | //cos(a-b)=cos(a)cos(b)+sin(a)sin(b) 69 | //sin(a-b)=sin(a)cos(b)-cos(a)sin(b) 70 | return angle(p.x*q.p.x+p.y*q.p.y, -p.x*q.p.y+p.y*q.p.x); 71 | } 72 | angle operator - (Float d){ 73 | return *this-angle(d); 74 | return angle(p.x*cos(d)+p.y*sin(d), -p.x*sin(d)+p.y*cos(d)); 75 | } 76 | bool operator < (const angle &b){ 77 | return p._crossprd(b.p)>0; 78 | } 79 | bool operator <= (const angle &b){ 80 | return p._crossprd(b.p)>=0; 81 | } 82 | bool operator > (const angle &b){ 83 | return p._crossprd(b.p)<0; 84 | } 85 | bool operator >= (const angle &b){ 86 | return p._crossprd(b.p)<=0; 87 | } 88 | bool operator == (const angle &b){ 89 | return p._crossprd(b.p)==0; 90 | } 91 | angle operator * (Float d){ 92 | Float r=atan2(p.y,p.x); 93 | return angle(r*d); 94 | } 95 | angle _approach(const angle &a_tgt,const angle &a_lw,const angle &a_hi){ 96 | vec2f tgt=a_tgt.p; 97 | vec2f lw=a_hi.p,hi=a_hi.p; 98 | assert(p._crossprd(lw) * p._crossprd(hi) <= 0.0); 99 | //instead of approaching this angle to tgt, "approach" tgt to this angle 100 | vec2f ret1,ret2;//ret1: countercloskwise, ret2: clockwise 101 | if(p._crossprd(tgt)>=0){ 102 | if(p._crossprd(lw)>=0&&lw._crossprd(tgt)>=0){ 103 | ret1=lw; 104 | ret2=hi; 105 | } 106 | else if(p._crossprd(hi)>=0&&hi._crossprd(tgt)>=0){ 107 | ret1=hi; 108 | ret2=lw; 109 | } 110 | else{ 111 | return a_tgt; 112 | } 113 | } 114 | else{ 115 | if(p._crossprd(lw)<=0&&lw._crossprd(tgt)<=0){ 116 | ret1=lw; 117 | ret2=hi; 118 | } 119 | else if(p._crossprd(hi)<=0&&hi._crossprd(tgt)<=0){ 120 | ret1=hi; 121 | ret2=lw; 122 | } 123 | else{ 124 | return a_tgt; 125 | } 126 | } 127 | if(fabs(ret1._crossprd(tgt))<=fabs(ret2._crossprd(tgt))){ 128 | return angle(ret1); 129 | } 130 | else{ 131 | return angle(ret2); 132 | } 133 | } 134 | }; 135 | #endif 136 | -------------------------------------------------------------------------------- /lib/header-agent.ml: -------------------------------------------------------------------------------- 1 | // Add the header for agent 2 | #include "cglib.h" 3 | 4 | double _FPS=60.0; 5 | // unit is "meter" for them 6 | double _MAP_X=20,_MAP_Y=20; 7 | vec2f _MAP_CENTER(0,0); 8 | double _SPEEDUP_RATE=1; 9 | 10 | //must allowed user-defination interfaces: 11 | void _Set_Map_XY(double _X,double _Y){ 12 | _MAP_X = _X; 13 | _MAP_Y = _Y; 14 | } 15 | void _Set_Map_Center(double _x,double _y){ 16 | _MAP_CENTER = vec2f(_x,_y); 17 | } 18 | void _Set_Speedup_Rate(double _rate){ 19 | _SPEEDUP_RATE=_rate; 20 | } 21 | void _SET_FPS(double _fps){ 22 | _FPS = _fps; 23 | } 24 | void _AML_Vertex2f(vec2f pos){ 25 | pos = pos - _MAP_CENTER; 26 | pos = (pos/_MAP_X*2.0, pos/_MAP_Y*2.0); 27 | glVertex2f(pos.x,pos.y); 28 | } 29 | class _Agent{ 30 | public: 31 | virtual void _plot(double,double)=0; 32 | virtual void _step(double,double,_Agent*)=0; 33 | virtual void _copy_from(_Agent*)=0; 34 | }; 35 | -------------------------------------------------------------------------------- /lib/header.ml: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | -------------------------------------------------------------------------------- /lib/tail.ml: -------------------------------------------------------------------------------- 1 | return 0; 2 | } 3 | -------------------------------------------------------------------------------- /src/ast.mli: -------------------------------------------------------------------------------- 1 | type identifier = string 2 | 3 | and exprs = expr list 4 | 5 | and stmts = stmt list 6 | 7 | and expr = 8 | | Bool of bool 9 | | Int of int 10 | | Float of float 11 | | String of string 12 | | Identifier of identifier 13 | | Unop of string * expr 14 | | Binop of string * expr * expr 15 | | Call of (identifier * exprs) 16 | | Println of exprs 17 | | Read of exprs 18 | | Ternary of (expr * expr * expr) 19 | | Assign of (identifier * expr) 20 | 21 | and stmt = 22 | | Stmts of stmts 23 | | Exprs of exprs 24 | | If of (expr * stmt) 25 | | IfElse of (expr * stmt * stmt) 26 | | For of (exprs * exprs * exprs * stmt) 27 | | Comment of string 28 | | Function of (identifier * identifier * exprs * stmt) 29 | | Empty 30 | 31 | and toplevel = 32 | | Agent of identifier * stmt 33 | | Stmt of stmt 34 | 35 | and t = toplevel list;; 36 | -------------------------------------------------------------------------------- /src/lexer.mll: -------------------------------------------------------------------------------- 1 | { 2 | open Parser 3 | 4 | exception SyntaxError of string 5 | } 6 | 7 | let ident = ['a'-'z' 'A'-'Z' '_'] ['a'-'z' 'A'-'Z' '0'-'9' '_' '[' ']' '-' '>' '.']* [''']* 8 | let int = '-'? ['0'-'9'] ['0'-'9']* 9 | let digit = ['0'-'9'] 10 | let frac = '.' digit* 11 | let exp = ['e' 'E'] ['-' '+']? digit+ 12 | let float = '-'? digit* frac+ exp? 13 | let white = [' ' '\t']+ 14 | let string = ['"'] ['a'-'z' 'A'-'Z' '0'-'9' '_' ' ']* ['"'] 15 | let newline = '\r' | '\n' | "\r\n" 16 | 17 | rule token = 18 | parse 19 | | newline { token lexbuf } 20 | | white { token lexbuf } 21 | | "/*" { read_comment (Buffer.create 128) lexbuf } 22 | | "//" { read_line_comment (Buffer.create 128) lexbuf } 23 | | "Agent" { AGENT } 24 | | "if" { IF } 25 | | "else" { ELSE } 26 | | "for" { FOR } 27 | | "true" { BOOL (true) } 28 | | "false" { BOOL (false) } 29 | | "println" { PRINTLN } 30 | | "read" { READ } 31 | | int { INT (int_of_string (Lexing.lexeme lexbuf)) } 32 | | float { FLOAT (float_of_string (Lexing.lexeme lexbuf)) } 33 | | string { STRING (Lexing.lexeme lexbuf) } 34 | | '=' { EQUAL } 35 | | '+' { PLUS } 36 | | '-' { MINUS } 37 | | '*' { TIMES } 38 | | '/' { DIV } 39 | | '%' { MOD } 40 | | '!' { NOT } 41 | | "++" { INC } 42 | | "--" { DEC } 43 | 44 | | "==" { SEQ } 45 | | "!=" { SNE } 46 | | '>' { GT } 47 | | '<' { LT } 48 | | ">=" { GE } 49 | | "<=" { LE } 50 | 51 | | ":==" { FSEQ } 52 | | ":!=" { FSNE } 53 | | ":>" { FGT } 54 | | ":<" { FLT } 55 | | ":>=" { FGE } 56 | | ":<=" { FLE } 57 | 58 | | "&&" { AND } 59 | | "||" { OR } 60 | | "&" { BINAND} 61 | | '|' { BINOR } 62 | | '^' { BINXOR } 63 | | '(' { LPAREN } 64 | | ')' { RPAREN } 65 | | '{' { LBRACE } 66 | | '}' { RBRACE } 67 | | '?' { QUESTION } 68 | | ':' { COLON } 69 | | ';' { SEMICOLON } 70 | | ',' { COMMA } 71 | | ident { IDENTIFIER (Lexing.lexeme lexbuf) } 72 | | eof { EOF } 73 | | _ { raise (SyntaxError ( 74 | "Unexpected char: " ^ Lexing.lexeme lexbuf)) } 75 | 76 | and read_comment buf = 77 | parse 78 | | "*/" { COMMENT (Buffer.contents buf) } 79 | | _ { Buffer.add_string buf (Lexing.lexeme lexbuf); 80 | read_comment buf lexbuf } 81 | 82 | and read_line_comment buf = 83 | parse 84 | | eof { COMMENT (Buffer.contents buf) } 85 | | "//" { COMMENT (Buffer.contents buf) } 86 | | newline { COMMENT (Buffer.contents buf) } 87 | | _ { Buffer.add_string buf (Lexing.lexeme lexbuf); 88 | read_line_comment buf lexbuf } 89 | -------------------------------------------------------------------------------- /src/main.ml: -------------------------------------------------------------------------------- 1 | open Ast 2 | open Printf 3 | open Pre 4 | 5 | exception Invalid_input;; 6 | 7 | let rec print_ind ind str= 8 | if (ind > 0) then ((" ") ^ (print_ind (ind - 1) str)) 9 | else str;; 10 | 11 | let rec print_stmt ind e = 12 | match e with 13 | | [] -> "" 14 | | [Empty] -> ";\n" 15 | 16 | | [Exprs([Call("register", identifiers)])] -> ( 17 | (print_stmt ind [register identifiers]) 18 | ) 19 | 20 | | [Exprs([Call("plotvec2f", identifiers)])] -> 21 | (print_stmt ind [Exprs([Call("_AML_Vertex2f", identifiers)])]) 22 | 23 | | [Function(t, "project", identifiers, stmt)] -> 24 | print_stmt ind [pre_main(Function(t, "main", identifiers, stmt))] 25 | 26 | | [Exprs([String(s)])] -> 27 | (print_ind ind (exprs [String(s)])) 28 | ^ "\n" 29 | 30 | | [Exprs([e])] -> 31 | (print_ind ind (exprs [e])) 32 | ^ ";\n" 33 | 34 | | [Exprs(h :: t)] -> 35 | (print_ind ind (exprs [h])) ^ ", " ^ (print_stmt 0 [Exprs(t)]); 36 | 37 | | [If(a, b)] -> 38 | (print_ind ind "if") 39 | ^ "(" ^ (exprs [a]) ^ ")" 40 | ^ " {\n" 41 | ^ (print_stmt (ind + 1) [b]) 42 | ^ (print_ind ind "}\n") 43 | | [IfElse(a, b, c)] -> 44 | (print_stmt ind [If(a, b)]) 45 | ^ (print_ind ind "else {\n") 46 | ^ (print_stmt (ind + 1) [c]) 47 | ^ (print_ind ind "}\n") 48 | | [For(a, b, c, d)] -> 49 | (print_ind ind "for(") 50 | ^ (exprs a) 51 | ^ "; " 52 | ^ (exprs b) 53 | ^ "; " 54 | ^ (exprs c) 55 | ^ ") {\n" 56 | ^ (print_stmt (ind + 1) [d]) 57 | ^ (print_ind ind "}\n") 58 | | [Function(t, identifier, identifiers, stmt)] -> 59 | if identifier = "__main__" then ((print_ind ind (t ^ " ")) 60 | ^ "main" 61 | ^ "(" ^ (exprs identifiers) ^ ")" 62 | ^ " {\n" 63 | ^ (print_stmt (ind+1) [stmt]) 64 | ^ (print_ind ind "}\n") 65 | ) 66 | else ( 67 | (* if identifier = "__main__" then identifier = "main"; *) 68 | ((print_ind ind (t ^ " ")) 69 | ^ identifier 70 | ^ "(" ^ (exprs identifiers) ^ ")" 71 | ^ " {\n" 72 | ^ (print_stmt (ind+1) [stmt]) 73 | ^ (print_ind ind "}\n") 74 | ) 75 | ) 76 | | [Comment(s)] -> 77 | (print_ind ind ("/*" ^ s ^ "*/")) 78 | ^ "\n" 79 | | [Stmts(x)] -> print_stmt ind x; 80 | | h :: t -> (print_stmt ind [h]) 81 | ^ (print_stmt ind t) 82 | 83 | and register e = 84 | match e with 85 | [con] -> Stmts([ 86 | Exprs([String("_Agent1 = new " ^ (exprs [con]) ^ ";")]); 87 | Exprs([String("_Agent2 = new " ^ (exprs [con]) ^ ";")]); 88 | Exprs([String("_agents.push_back(make_pair((_Agent*)_Agent1, (_Agent*)_Agent2));")]); 89 | ]) 90 | | other -> Stmts([Exprs([String("fault")])]) 91 | 92 | and print_io str e = 93 | match e with 94 | | [] -> "\"\"" 95 | | [x] -> exprs [x] 96 | | h :: t -> (exprs [h]) ^ " " ^ str ^ "' '" ^ str ^ " " ^ (print_io str t) 97 | 98 | and print_id x = 99 | if (is_diff x) then 100 | let _x = (remove_diff x) in 101 | ("(" ^ _x ^ " - (_last->" ^ _x ^ "))") 102 | else x 103 | 104 | and exprs e = 105 | match e with 106 | | [] -> "" 107 | | [Int(a)] -> string_of_int(a) 108 | | [Bool(a)] -> string_of_bool(a) 109 | | [Float(a)] -> string_of_float(a) 110 | | [String(s)] -> s 111 | | [Identifier(s)] -> s 112 | | [Assign(a, e)] -> 113 | a ^ " " ^ (exprs [e]) 114 | | [Binop("=", e1, Identifier(id))] -> 115 | (exprs [e1]) ^ " = " ^ (print_id id) 116 | | [Binop(op, e1, e2)] -> 117 | if (is_first_op op) then 118 | "(" 119 | ^ ("!(_last->" ^ (exprs [e1]) ^ " " ^ (remove_first_op op) ^ " " ^ (exprs [e2]) ^ ")") 120 | ^ "&&" 121 | ^ ("(" ^ (exprs [e1]) ^ " " ^ (remove_first_op op) ^ " " ^ (exprs [e2]) ^ ")") 122 | ^ ")" 123 | else if(op <> "=") then "(" ^ (exprs [e1]) ^ " " ^ op ^ " " ^ (exprs [e2]) ^ ")" 124 | else 125 | (exprs [e1]) ^ " " ^ op ^ " " ^ (exprs [e2]) 126 | | [Unop(op, e)] -> 127 | if op = "_++" then "(" ^ (exprs [e]) ^ "++)" 128 | else if op = "_++" then "(" ^ (exprs [e]) ^ "--)" 129 | else "(" ^ op ^ (exprs [e]) ^ ")" 130 | | [Call(identifier, identifiers)] -> 131 | (identifier ^ 132 | ( 133 | if (identifier = "return") then (" " ^ (exprs identifiers)) 134 | else ("(" ^ (exprs identifiers) ^ ")") 135 | )) 136 | | [Println(exprs)] -> 137 | "cout << " ^ print_io "<<" exprs ^ " << endl" 138 | | [Read(exprs)] -> 139 | "cin >> " ^ print_io ">>" exprs ^ " >> endl" 140 | | [Ternary(e1, e2, e3)] -> 141 | (exprs [e1]) ^ "? " ^ (exprs [e2]) ^ " : " ^ (exprs [e3]) 142 | | h :: t -> (exprs [h]) ^ ", " ^ (exprs t) 143 | 144 | let rec print_toplevel = function 145 | | Stmt(x) -> printf "%s" (print_stmt 0 [x]) 146 | | Agent(identifier, stmt) -> 147 | let x = pre_agent(Agent(identifier, stmt)) in 148 | printf "class %s: public _Agent{\n" identifier; 149 | printf "public:\n"; 150 | printf "%s" (print_stmt 1 [x]); 151 | printf("};\n");; 152 | 153 | let rec print_t = function 154 | | [] -> printf("") 155 | | [x] -> print_toplevel x; 156 | | h :: t -> print_toplevel h; 157 | print_t(t);; 158 | 159 | let addlib lib = 160 | let rec aux ic = 161 | try 162 | let line = input_line ic in 163 | print_endline line; 164 | aux ic; 165 | with e -> 166 | close_in_noerr ic 167 | in aux (open_in lib);; 168 | 169 | let read_all file = 170 | let rec aux ic = 171 | try 172 | let line = input_line ic in 173 | (line^(aux ic)); 174 | with e -> 175 | close_in_noerr ic; 176 | ""; 177 | in aux (open_in file);; 178 | 179 | let _ = 180 | if Array.length Sys.argv = 1 then printf "Expect ./main.native [file]\n" 181 | else let file = Sys.argv.(1) in 182 | addlib "lib/header.ml"; 183 | if Array.length Sys.argv > 2 then addlib "lib/header-agent.ml"; 184 | let tokens = Lexing.from_channel (open_in file) in 185 | let t = Parser.main Lexer.token tokens in 186 | print_t(t); 187 | 188 | (*addlib "lib/tail.ml";*) 189 | -------------------------------------------------------------------------------- /src/parser.mly: -------------------------------------------------------------------------------- 1 | %{ open Ast %} 2 | %token AGENT 3 | %token FLOAT 4 | %token STRING 5 | %token IDENTIFIER 6 | %token COMMENT 7 | %token INT 8 | %token BOOL 9 | %token IF 10 | %token ELSE 11 | %token FOR 12 | %token MOD 13 | %token READ 14 | %token PRINTLN 15 | %token PLUS MINUS TIMES DIV 16 | %token LPAREN RPAREN 17 | %token NOT 18 | %token INC 19 | %token DEC 20 | %token EQUAL 21 | %token EOF 22 | %token COMMA 23 | %token COLON 24 | %token QUESTION 25 | %token SEMICOLON 26 | %token LBRACE RBRACE 27 | %token AND 28 | %token OR 29 | %token BINAND 30 | %token BINXOR 31 | %token BINOR 32 | 33 | %token SEQ 34 | %token SNE 35 | %token GT 36 | %token LT 37 | %token GE 38 | %token LE 39 | 40 | %token FSEQ 41 | %token FSNE 42 | %token FGT 43 | %token FLT 44 | %token FGE 45 | %token FLE 46 | 47 | %left AND OR 48 | %left BINAND BINXOR BINOR 49 | %nonassoc SEQ SNE FSEQ FSNE 50 | %nonassoc GT LT GE LE FGT FLT FGE FLE 51 | %nonassoc NOT 52 | %nonassoc INC 53 | %nonassoc DEC 54 | %left PLUS MINUS 55 | %left DIV TIMES MOD 56 | 57 | %nonassoc IF 58 | %nonassoc ELSE 59 | 60 | %start main 61 | %type main 62 | 63 | %% 64 | main: 65 | toplevel_list EOF { $1 } 66 | 67 | toplevel: 68 | | AGENT IDENTIFIER stmt SEMICOLON { Agent($2, $3) } 69 | | stmt { Stmt($1) } 70 | 71 | toplevel_list: 72 | | { [] } 73 | | toplevel toplevel_list { $1 :: $2 } 74 | 75 | expr: 76 | | INT { Int($1) } 77 | | BOOL { Bool($1) } 78 | | STRING { String($1) } 79 | | FLOAT { Float($1) } 80 | | IDENTIFIER { Identifier($1) } 81 | | LPAREN expr RPAREN { $2 } 82 | | binary_op { $1 } 83 | | unary_op { $1 } 84 | | call_stmt { $1 } 85 | | expr QUESTION expr COLON expr { Ternary($1, $3, $5) } 86 | | IDENTIFIER IDENTIFIER EQUAL expr { Assign($1, Binop("=", Identifier($2), $4)) } 87 | | IDENTIFIER IDENTIFIER { Assign($1, Identifier($2)) } 88 | 89 | expr_list: 90 | | { [] } 91 | | expr { [$1] } 92 | | expr COMMA expr_list { $1 :: $3 } 93 | 94 | 95 | for_stmt: 96 | | FOR LPAREN expr_list SEMICOLON expr_list SEMICOLON expr_list RPAREN 97 | stmt 98 | { For($3, $5, $7, $9) } 99 | 100 | call_stmt: 101 | | IDENTIFIER LPAREN expr_list RPAREN { Call($1, $3) } 102 | | PRINTLN LPAREN expr_list RPAREN { Println($3) } 103 | | READ LPAREN expr_list RPAREN { Read($3) } 104 | 105 | if_stmt: 106 | | IF LPAREN expr RPAREN stmt 107 | %prec IF; 108 | { If($3, $5) } 109 | | IF LPAREN expr RPAREN stmt 110 | ELSE stmt 111 | { IfElse($3, $5, $7) } 112 | 113 | stmt: 114 | | SEMICOLON { Empty } 115 | | if_stmt { $1 } 116 | | for_stmt { $1 } 117 | | COMMENT { Comment($1) } 118 | | expr_list SEMICOLON { Exprs($1) } 119 | | IDENTIFIER IDENTIFIER LPAREN expr_list RPAREN LBRACE stmt_list RBRACE { Function($1, $2, $4, Stmts($7)) } 120 | | IDENTIFIER LPAREN expr_list RPAREN LBRACE stmt_list RBRACE { Function("", $1, $3, Stmts($6)) } 121 | | LBRACE stmt_list RBRACE { Stmts($2) } 122 | 123 | stmt_list: 124 | | { [] } 125 | | stmt stmt_list { $1 :: $2 } 126 | 127 | unary_op: 128 | | NOT expr { Unop ("!", $2) } 129 | | MINUS expr { Unop ("-", $2) } 130 | | INC expr { Unop ("++", $2) } 131 | | DEC expr { Unop ("--", $2) } 132 | | expr INC { Unop ("_++", $1) } 133 | | expr DEC { Unop ("_--", $1) } 134 | 135 | binary_op: 136 | | expr TIMES expr { Binop("*", $1, $3) } 137 | | expr DIV expr { Binop("/", $1, $3) } 138 | | expr PLUS expr { Binop("+", $1, $3) } 139 | | expr MINUS expr { Binop("-", $1, $3) } 140 | | expr EQUAL expr { Binop("=", $1, $3) } 141 | | expr MOD expr { Binop("%", $1, $3) } 142 | 143 | | expr GT expr { Binop(">", $1, $3) } 144 | | expr LT expr { Binop("<", $1, $3) } 145 | | expr GE expr { Binop(">=", $1, $3) } 146 | | expr LE expr { Binop("<=", $1, $3) } 147 | | expr SEQ expr { Binop("==", $1, $3) } 148 | | expr SNE expr { Binop("!=", $1, $3) } 149 | 150 | | expr FGT expr { Binop(":>", $1, $3) } 151 | | expr FLT expr { Binop(":<", $1, $3) } 152 | | expr FGE expr { Binop(":>=", $1, $3) } 153 | | expr FLE expr { Binop(":<=", $1, $3) } 154 | | expr FSEQ expr { Binop(":==", $1, $3) } 155 | | expr FSNE expr { Binop(":!=", $1, $3) } 156 | 157 | | expr AND expr { Binop("&&", $1, $3) } 158 | | expr OR expr { Binop("||", $1, $3) } 159 | | expr BINAND expr { Binop("&", $1, $3) } 160 | | expr BINOR expr { Binop("|", $1, $3) } 161 | | expr BINXOR expr { Binop("^", $1, $3) } 162 | -------------------------------------------------------------------------------- /src/pre.ml: -------------------------------------------------------------------------------- 1 | 2 | open Ast 3 | open Printf 4 | 5 | let remove_first_op x = 6 | let len = String.length x 7 | in 8 | if (x.[0] = ':') then (String.sub x 1 (len - 1)) 9 | else x 10 | 11 | let remove_diff x = 12 | let len = String.length x 13 | in 14 | if (x.[len - 1] = '\'') then (String.sub x 0 (len - 1)) 15 | else x 16 | 17 | let is_diff x = 18 | x != (remove_diff x) 19 | 20 | let is_first_op x = 21 | x != (remove_first_op x) 22 | 23 | let rec gather_diff list = function 24 | Exprs([Binop("=", Identifier(e1), Identifier(e2))]) -> 25 | if(is_diff e2) then (remove_diff e2) :: list 26 | else list 27 | | Stmts(h :: t) -> gather_diff (gather_diff list h) (Stmts t) 28 | | x -> list 29 | 30 | let rec pre_agent_fun indentifier = function 31 | Function(t, "step", identifiers, stmt) -> 32 | ( 33 | let _stmt = Stmts([ 34 | Exprs([String(indentifier ^"* _last = (" ^ indentifier ^ "*)_last_Agent;")]); 35 | stmt; 36 | ]) 37 | in Function(t, "_step", [String("double _tim, double _dtim, _Agent* _last_Agent")], _stmt) 38 | ) 39 | | Function(t, "plot", identifiers, stmt) -> 40 | let _stmt = Stmts([ 41 | stmt; 42 | Exprs([String("glFlush();\n")]); 43 | ]) 44 | in Function(t, "_plot", [String("double _tim, double _dtim")], 45 | _stmt) 46 | | Function(t, identifier, identifiers, stmt) -> 47 | Function(t, identifier, identifiers, stmt) 48 | | x -> x 49 | 50 | let pre_agent = function 51 | Agent(identifier, Stmts(stmt)) -> 52 | let _stmt = Stmts(List.concat [ 53 | (List.map (pre_agent_fun identifier) stmt); 54 | [ 55 | Exprs([String("void _copy_from(_Agent *_from_Agent){ 56 | " ^ identifier ^"* _from = (" ^ identifier ^"*)_from_Agent; 57 | *this = *_from; 58 | }")]) 59 | ]]) 60 | in _stmt 61 | | other -> Stmts([Exprs[String("fault")]]) 62 | 63 | let pre_main = function 64 | Function(t, identifier, identifiers, stmt) -> 65 | let _stmt = 66 | Stmts( 67 | [Exprs[(String("glutInit(&argc, argv); 68 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 69 | glutInitWindowPosition(100, 100); 70 | glutInitWindowSize(500, 500); 71 | int glut_window = glutCreateWindow(\"AML\"); 72 | void *_Agent1, *_Agent2;"))]; 73 | stmt; 74 | Exprs[(String("glutDisplayFunc(&_Plot); 75 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 76 | glutMainLoop();"))]; 77 | ]) 78 | and prev = 79 | Exprs([String(" 80 | vector > _agents; 81 | double _dt=1/_FPS*_SPEEDUP_RATE; 82 | double _cur_tim=0.0; 83 | void _Plot(void){ 84 | glClear(GL_COLOR_BUFFER_BIT); 85 | for(int i=0;i<_agents.size();i++){ 86 | _agents[i].second->_plot(_cur_tim, _dt); 87 | } 88 | glutSwapBuffers(); 89 | } 90 | void _Step_Time(int _time_value){ 91 | //printf(\"step time: %d\\n\",_time_value); 92 | for(int i=0;i<_agents.size();i++){ 93 | _agents[i].second->_step(_cur_tim,_dt,_agents[i].first); 94 | _agents[i].first->_copy_from(_agents[i].second); 95 | } 96 | _cur_tim+=_dt; 97 | glutPostRedisplay(); 98 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 99 | } 100 | ")]) 101 | in 102 | Stmts([prev; Function("int", "main", [String("int argc, char *argv[]")], _stmt)]) 103 | | x -> x;; 104 | -------------------------------------------------------------------------------- /tests/aml/arith.aml: -------------------------------------------------------------------------------- 1 | int main(){ 2 | println(false, true, 42), println(1 + (4 + 6) * 3); 3 | println(8 - 3 % 2); 4 | println(-9 - 9); 5 | println((2 + 8) / 3); 6 | println(2 == 2, 2 != 2, 3 > 2); 7 | println(4 < 5, 6 >= 2, 19 <= 30); 8 | println(!true), println(!false); 9 | println(!(2 - 1)); 10 | println(-1.0); 11 | return(0); 12 | } -------------------------------------------------------------------------------- /tests/aml/array.aml: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a[3]; 3 | a[0] = 1, a[1] = 2; 4 | print(a[0], a[1]); 5 | } -------------------------------------------------------------------------------- /tests/aml/block.aml: -------------------------------------------------------------------------------- 1 | int main() { 2 | //Level 0 Start 3 | println("Hello"); 4 | { 5 | //Level 1 Start 6 | println("Lo"); 7 | { 8 | //Level 2 Start 9 | println("and behold"); 10 | //Level 2 End 11 | } 12 | //Level 1 End 13 | } 14 | println("End"); 15 | //Level 0 End 16 | return(0); 17 | } -------------------------------------------------------------------------------- /tests/aml/comment.aml: -------------------------------------------------------------------------------- 1 | int main(){ 2 | a = 3; 3 | /* This is comment 1 */ 4 | a = a * 5; 5 | /* This is comment 2 */ 6 | println(a); 7 | /* This is comment 3 */ 8 | // 9 | } -------------------------------------------------------------------------------- /tests/aml/demo1.aml: -------------------------------------------------------------------------------- 1 | Agent Ball{ 2 | vec2f pos; 3 | vec2f vel; 4 | Ball(int _x, int _y){ 5 | pos = vec2f(_x, _y); 6 | } 7 | void plot(){ 8 | glColor3f(1.0, 1.0, 1.0); 9 | glBegin(GL_LINES); 10 | plotvec2f(pos); 11 | plotvec2f(pos + vel * 0.1 / _dtim); 12 | glEnd(); 13 | glColor3f(1.0, 0.0, 0.0); 14 | glBegin(GL_POINTS); 15 | plotvec2f(pos); 16 | glEnd(); 17 | } 18 | void step(){ 19 | pos = vec2f(pos.x, 10 - _tim * _tim * 4.9); // free fall 20 | vel = pos'; // first order difference 21 | } 22 | }; 23 | 24 | project(){ 25 | for(int i = 1; i <= 5; i++) 26 | register(Ball(-12 + 4 * i, 10)); 27 | } -------------------------------------------------------------------------------- /tests/aml/demo2.aml: -------------------------------------------------------------------------------- 1 | Agent Blinker{ 2 | angle theta; 3 | vec2f pos; 4 | int color; 5 | double PI; 6 | Blinker(angle _theta){ 7 | theta=_theta; 8 | color=0; 9 | PI=acos(-1.0); 10 | } 11 | void plot(){ 12 | vec2f a=pos+vec2f(0,1); 13 | vec2f b=pos+vec2f(-0.866,-0.5); 14 | vec2f c=pos+vec2f(0.866,-0.5); 15 | if(color==0){ 16 | glColor3f(1.0,0.0,0.0); 17 | } 18 | else{ 19 | glColor3f(0.0,0.0,1.0); 20 | } 21 | glBegin(GL_TRIANGLES); 22 | plotvec2f(a); 23 | plotvec2f(b); 24 | plotvec2f(c); 25 | glEnd(); 26 | } 27 | void step(){ 28 | double dlt=_dtim/10*2*PI; 29 | theta = theta + dlt; 30 | pos=vec2f(cos(theta),sin(theta))*5; 31 | if(theta :> angle(0)){ // operator :> means "when > holds true for the first time" 32 | color = color ^ 1; 33 | } 34 | } 35 | }; 36 | project(){ 37 | for(int i = 1; i <= 10; i++) 38 | register(Blinker(angle(i * acos(-1.0) / 5))); 39 | } -------------------------------------------------------------------------------- /tests/aml/function.aml: -------------------------------------------------------------------------------- 1 | int fibonacci(int num) { 2 | if (num == 0) { 3 | return(0); 4 | } else if(num == 1){ 5 | return(1); 6 | } else { 7 | return (fibonacci(num - 2) + fibonacci(num - 1)); 8 | } 9 | } 10 | int main() 11 | { 12 | println(fibonacci(3)); 13 | return(0); 14 | } 15 | -------------------------------------------------------------------------------- /tests/aml/global_variable.aml: -------------------------------------------------------------------------------- 1 | int a = 1; 2 | int b = 2; 3 | int c; 4 | int main(){ 5 | c = a + b; 6 | println(c); 7 | } 8 | -------------------------------------------------------------------------------- /tests/aml/if.aml: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a; 3 | a = (1 + 2 == 3? 5: 4); 4 | if (a > 2) 5 | println("Yes"); 6 | else 7 | println("No"); 8 | if(a >= 3) 9 | if(a <= 3) 10 | printfln("Yes"); 11 | else printfln("No"); 12 | if (2 < 10) 13 | { 14 | println("Yes"); 15 | } 16 | if (true) { 17 | if (false){ 18 | v=(4 + 1); 19 | } else { 20 | v = 2; 21 | } 22 | } else { 23 | } 24 | println(v); 25 | return(0); 26 | } -------------------------------------------------------------------------------- /tests/aml/io.aml: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int a; 4 | int b; 5 | read(a); 6 | read(b); 7 | println(a); 8 | println(b); 9 | read(a, b); 10 | println(a, b); 11 | return(0); 12 | } -------------------------------------------------------------------------------- /tests/aml/loop.aml: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int sum; 4 | for(int i = 1, j; i <= 10; i++) 5 | for(j = 1; j <= 10; j++) 6 | { 7 | sum = sum + i + j; 8 | } 9 | println(sum); 10 | return(0); 11 | } -------------------------------------------------------------------------------- /tests/aml/ternary.aml: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | int b = 2; 4 | int c = (a == b)?3 : 2; 5 | return(0); 6 | } -------------------------------------------------------------------------------- /tests/aml/variable.aml: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int i = 1; 3 | double f = 2; 4 | string s; 5 | bool b; 6 | ang d; 7 | vec2f v; 8 | s = "3"; 9 | s = ""; 10 | b = true; 11 | d = ang(3.0, 2.0); 12 | } 13 | -------------------------------------------------------------------------------- /tests/aml/vector.aml: -------------------------------------------------------------------------------- 1 | int main(){ 2 | println(vec2f(1, 2)); 3 | println(vec2f(0, 0) + vec2f(1, 1)); 4 | vec2f a = vec2f(3, 5); 5 | vec2f b = vec2f(1, 5); 6 | double d = 2.1; 7 | println(a + b); 8 | println(a - b); 9 | println(a * b); 10 | println(a / b); 11 | println(a * d); 12 | println(a / d); 13 | return(0); 14 | } -------------------------------------------------------------------------------- /tests/cpp/arith.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | cout << false <<' '<< true <<' '<< 42 << endl, cout << (1 + ((4 + 6) * 3)) << endl; 5 | cout << (8 - (3 % 2)) << endl; 6 | cout << (-9 - 9) << endl; 7 | cout << ((2 + 8) / 3) << endl; 8 | cout << (2 == 2) <<' '<< (2 != 2) <<' '<< (3 > 2) << endl; 9 | cout << (4 < 5) <<' '<< (6 >= 2) <<' '<< (19 <= 30) << endl; 10 | cout << (!true) << endl, cout << (!false) << endl; 11 | cout << (!(2 - 1)) << endl; 12 | cout << -1. << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /tests/cpp/array.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | int a[3]; 5 | a[0] = 1, a[1] = 2; 6 | print(a[0], a[1]); 7 | } 8 | -------------------------------------------------------------------------------- /tests/cpp/block.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | /*Level 0 Start*/ 5 | cout << "Hello" << endl; 6 | /*Level 1 Start*/ 7 | cout << "Lo" << endl; 8 | /*Level 2 Start*/ 9 | cout << "and behold" << endl; 10 | /*Level 2 End*/ 11 | /*Level 1 End*/ 12 | cout << "End" << endl; 13 | /*Level 0 End*/ 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /tests/cpp/comment.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | a = 3; 5 | /* This is comment 1 */ 6 | a = (a * 5); 7 | /* This is comment 2 */ 8 | cout << a << endl; 9 | /* This is comment 3 */ 10 | /**/ 11 | } 12 | -------------------------------------------------------------------------------- /tests/cpp/demo1.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | // Add the header for agent 4 | #include "cglib.h" 5 | 6 | double _FPS=60.0; 7 | // unit is "meter" for them 8 | double _MAP_X=20,_MAP_Y=20; 9 | vec2f _MAP_CENTER(0,0); 10 | double _SPEEDUP_RATE=1; 11 | 12 | //must allowed user-defination interfaces: 13 | void _Set_Map_XY(double _X,double _Y){ 14 | _MAP_X = _X; 15 | _MAP_Y = _Y; 16 | } 17 | void _Set_Map_Center(double _x,double _y){ 18 | _MAP_CENTER = vec2f(_x,_y); 19 | } 20 | void _Set_Speedup_Rate(double _rate){ 21 | _SPEEDUP_RATE=_rate; 22 | } 23 | void _SET_FPS(double _fps){ 24 | _FPS = _fps; 25 | } 26 | void _AML_Vertex2f(vec2f pos){ 27 | pos = pos - _MAP_CENTER; 28 | pos = (pos/_MAP_X*2.0, pos/_MAP_Y*2.0); 29 | glVertex2f(pos.x,pos.y); 30 | } 31 | class _Agent{ 32 | public: 33 | virtual void _plot(double,double)=0; 34 | virtual void _step(double,double,_Agent*)=0; 35 | virtual void _copy_from(_Agent*)=0; 36 | }; 37 | class Ball: public _Agent{ 38 | public: 39 | vec2f pos; 40 | vec2f vel; 41 | Ball(int _x, int _y) { 42 | pos = vec2f(_x, _y); 43 | } 44 | void _plot(double _tim, double _dtim) { 45 | glColor3f(1., 1., 1.); 46 | glBegin(GL_LINES); 47 | _AML_Vertex2f(pos); 48 | _AML_Vertex2f((pos + ((vel * 0.1) / _dtim))); 49 | glEnd(); 50 | glColor3f(1., 0., 0.); 51 | glBegin(GL_POINTS); 52 | _AML_Vertex2f(pos); 53 | glEnd(); 54 | glFlush(); 55 | 56 | } 57 | void _step(double _tim, double _dtim, _Agent* _last_Agent) { 58 | Ball* _last = (Ball*)_last_Agent; 59 | pos = vec2f(pos.x, (10 - ((_tim * _tim) * 4.9))); 60 | /* free fall*/ 61 | vel = (pos - (_last->pos)); 62 | /* first order difference*/ 63 | } 64 | void _copy_from(_Agent *_from_Agent){ 65 | Ball* _from = (Ball*)_from_Agent; 66 | *this = *_from; 67 | } 68 | }; 69 | 70 | vector > _agents; 71 | double _dt=1/_FPS*_SPEEDUP_RATE; 72 | double _cur_tim=0.0; 73 | void _Plot(void){ 74 | glClear(GL_COLOR_BUFFER_BIT); 75 | for(int i=0;i<_agents.size();i++){ 76 | _agents[i].second->_plot(_cur_tim, _dt); 77 | } 78 | glutSwapBuffers(); 79 | } 80 | void _Step_Time(int _time_value){ 81 | //printf("step time: %d\n",_time_value); 82 | for(int i=0;i<_agents.size();i++){ 83 | _agents[i].second->_step(_cur_tim,_dt,_agents[i].first); 84 | _agents[i].first->_copy_from(_agents[i].second); 85 | } 86 | _cur_tim+=_dt; 87 | glutPostRedisplay(); 88 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 89 | } 90 | 91 | int main(int argc, char *argv[]) { 92 | glutInit(&argc, argv); 93 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 94 | glutInitWindowPosition(100, 100); 95 | glutInitWindowSize(500, 500); 96 | int glut_window = glutCreateWindow("AML"); 97 | void *_Agent1, *_Agent2; 98 | for(int i = 1; (i <= 5); (i++)) { 99 | _Agent1 = new Ball((-12 + (4 * i)), 10); 100 | _Agent2 = new Ball((-12 + (4 * i)), 10); 101 | _agents.push_back(make_pair((_Agent*)_Agent1, (_Agent*)_Agent2)); 102 | } 103 | glutDisplayFunc(&_Plot); 104 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 105 | glutMainLoop(); 106 | } 107 | -------------------------------------------------------------------------------- /tests/cpp/demo2.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | // Add the header for agent 4 | #include "cglib.h" 5 | 6 | double _FPS=60.0; 7 | // unit is "meter" for them 8 | double _MAP_X=20,_MAP_Y=20; 9 | vec2f _MAP_CENTER(0,0); 10 | double _SPEEDUP_RATE=1; 11 | 12 | //must allowed user-defination interfaces: 13 | void _Set_Map_XY(double _X,double _Y){ 14 | _MAP_X = _X; 15 | _MAP_Y = _Y; 16 | } 17 | void _Set_Map_Center(double _x,double _y){ 18 | _MAP_CENTER = vec2f(_x,_y); 19 | } 20 | void _Set_Speedup_Rate(double _rate){ 21 | _SPEEDUP_RATE=_rate; 22 | } 23 | void _SET_FPS(double _fps){ 24 | _FPS = _fps; 25 | } 26 | void _AML_Vertex2f(vec2f pos){ 27 | pos = pos - _MAP_CENTER; 28 | pos = (pos/_MAP_X*2.0, pos/_MAP_Y*2.0); 29 | glVertex2f(pos.x,pos.y); 30 | } 31 | class _Agent{ 32 | public: 33 | virtual void _plot(double,double)=0; 34 | virtual void _step(double,double,_Agent*)=0; 35 | virtual void _copy_from(_Agent*)=0; 36 | }; 37 | class Blinker: public _Agent{ 38 | public: 39 | angle theta; 40 | vec2f pos; 41 | int color; 42 | double PI; 43 | Blinker(angle _theta) { 44 | theta = _theta; 45 | color = 0; 46 | PI = acos(-1.); 47 | } 48 | void _plot(double _tim, double _dtim) { 49 | vec2f a = (pos + vec2f(0, 1)); 50 | vec2f b = (pos + vec2f(-0.866, -0.5)); 51 | vec2f c = (pos + vec2f(0.866, -0.5)); 52 | if((color == 0)) { 53 | glColor3f(1., 0., 0.); 54 | } 55 | else { 56 | glColor3f(0., 0., 1.); 57 | } 58 | glBegin(GL_TRIANGLES); 59 | _AML_Vertex2f(a); 60 | _AML_Vertex2f(b); 61 | _AML_Vertex2f(c); 62 | glEnd(); 63 | glFlush(); 64 | 65 | } 66 | void _step(double _tim, double _dtim, _Agent* _last_Agent) { 67 | Blinker* _last = (Blinker*)_last_Agent; 68 | double dlt = (((_dtim / 10) * 2) * PI); 69 | theta = (theta + dlt); 70 | pos = (vec2f(cos(theta), sin(theta)) * 5); 71 | if((!(_last->theta > angle(0))&&(theta > angle(0)))) { 72 | /* operator :> means "when > holds true for the first time"*/ 73 | color = (color ^ 1); 74 | } 75 | } 76 | void _copy_from(_Agent *_from_Agent){ 77 | Blinker* _from = (Blinker*)_from_Agent; 78 | *this = *_from; 79 | } 80 | }; 81 | 82 | vector > _agents; 83 | double _dt=1/_FPS*_SPEEDUP_RATE; 84 | double _cur_tim=0.0; 85 | void _Plot(void){ 86 | glClear(GL_COLOR_BUFFER_BIT); 87 | for(int i=0;i<_agents.size();i++){ 88 | _agents[i].second->_plot(_cur_tim, _dt); 89 | } 90 | glutSwapBuffers(); 91 | } 92 | void _Step_Time(int _time_value){ 93 | //printf("step time: %d\n",_time_value); 94 | for(int i=0;i<_agents.size();i++){ 95 | _agents[i].second->_step(_cur_tim,_dt,_agents[i].first); 96 | _agents[i].first->_copy_from(_agents[i].second); 97 | } 98 | _cur_tim+=_dt; 99 | glutPostRedisplay(); 100 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 101 | } 102 | 103 | int main(int argc, char *argv[]) { 104 | glutInit(&argc, argv); 105 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 106 | glutInitWindowPosition(100, 100); 107 | glutInitWindowSize(500, 500); 108 | int glut_window = glutCreateWindow("AML"); 109 | void *_Agent1, *_Agent2; 110 | for(int i = 1; (i <= 10); (i++)) { 111 | _Agent1 = new Blinker(angle(((i * acos(-1.)) / 5))); 112 | _Agent2 = new Blinker(angle(((i * acos(-1.)) / 5))); 113 | _agents.push_back(make_pair((_Agent*)_Agent1, (_Agent*)_Agent2)); 114 | } 115 | glutDisplayFunc(&_Plot); 116 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 117 | glutMainLoop(); 118 | } 119 | -------------------------------------------------------------------------------- /tests/cpp/function.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int fibonacci(int num) { 4 | if((num == 0)) { 5 | return 0; 6 | } 7 | else { 8 | if((num == 1)) { 9 | return 1; 10 | } 11 | else { 12 | return (fibonacci((num - 2)) + fibonacci((num - 1))); 13 | } 14 | } 15 | } 16 | int main() { 17 | cout << fibonacci(3) << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /tests/cpp/global_variable.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int a = 1; 4 | int b = 2; 5 | int c; 6 | int main() { 7 | c = (a + b); 8 | cout << c << endl; 9 | } 10 | -------------------------------------------------------------------------------- /tests/cpp/if.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | int a; 5 | a = ((1 + 2) == 3? 5 : 4); 6 | if((a > 2)) { 7 | cout << "Yes" << endl; 8 | } 9 | else { 10 | cout << "No" << endl; 11 | } 12 | if((a >= 3)) { 13 | if((a <= 3)) { 14 | printfln("Yes"); 15 | } 16 | else { 17 | printfln("No"); 18 | } 19 | } 20 | if((2 < 10)) { 21 | cout << "Yes" << endl; 22 | } 23 | if(true) { 24 | if(false) { 25 | v = (4 + 1); 26 | } 27 | else { 28 | v = 2; 29 | } 30 | } 31 | else { 32 | } 33 | cout << v << endl; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /tests/cpp/io.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | int a; 5 | int b; 6 | cin >> a >> endl; 7 | cin >> b >> endl; 8 | cout << a << endl; 9 | cout << b << endl; 10 | cin >> a >>' '>> b >> endl; 11 | cout << a <<' '<< b << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /tests/cpp/loop.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | int sum; 5 | for(int i = 1, j; (i <= 10); (i++)) { 6 | for(j = 1; (j <= 10); (j++)) { 7 | sum = ((sum + i) + j); 8 | } 9 | } 10 | cout << sum << endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /tests/cpp/ternary.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | int a = 1; 5 | int b = 2; 6 | int c = (a == b)? 3 : 2; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /tests/cpp/variable.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | int i = 1; 5 | double f = 2; 6 | string s; 7 | bool b; 8 | ang d; 9 | vec2f v; 10 | s = "3"; 11 | s = ""; 12 | b = true; 13 | d = ang(3., 2.); 14 | } 15 | -------------------------------------------------------------------------------- /tests/cpp/vector.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | cout << vec2f(1, 2) << endl; 5 | cout << (vec2f(0, 0) + vec2f(1, 1)) << endl; 6 | vec2f a = vec2f(3, 5); 7 | vec2f b = vec2f(1, 5); 8 | double d = 2.1; 9 | cout << (a + b) << endl; 10 | cout << (a - b) << endl; 11 | cout << (a * b) << endl; 12 | cout << (a / b) << endl; 13 | cout << (a * d) << endl; 14 | cout << (a / d) << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /tests/std/arith.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | cout << false <<' '<< true <<' '<< 42 << endl, cout << (1 + ((4 + 6) * 3)) << endl; 5 | cout << (8 - (3 % 2)) << endl; 6 | cout << (-9 - 9) << endl; 7 | cout << ((2 + 8) / 3) << endl; 8 | cout << (2 == 2) <<' '<< (2 != 2) <<' '<< (3 > 2) << endl; 9 | cout << (4 < 5) <<' '<< (6 >= 2) <<' '<< (19 <= 30) << endl; 10 | cout << (!true) << endl, cout << (!false) << endl; 11 | cout << (!(2 - 1)) << endl; 12 | cout << -1. << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /tests/std/array.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | int a[3]; 5 | a[0] = 1, a[1] = 2; 6 | print(a[0], a[1]); 7 | } 8 | -------------------------------------------------------------------------------- /tests/std/block.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | /*Level 0 Start*/ 5 | cout << "Hello" << endl; 6 | /*Level 1 Start*/ 7 | cout << "Lo" << endl; 8 | /*Level 2 Start*/ 9 | cout << "and behold" << endl; 10 | /*Level 2 End*/ 11 | /*Level 1 End*/ 12 | cout << "End" << endl; 13 | /*Level 0 End*/ 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /tests/std/comment.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | a = 3; 5 | /* This is comment 1 */ 6 | a = (a * 5); 7 | /* This is comment 2 */ 8 | cout << a << endl; 9 | /* This is comment 3 */ 10 | /**/ 11 | } 12 | -------------------------------------------------------------------------------- /tests/std/demo1.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | // Add the header for agent 4 | #include "cglib.h" 5 | 6 | double _FPS=60.0; 7 | // unit is "meter" for them 8 | double _MAP_X=20,_MAP_Y=20; 9 | vec2f _MAP_CENTER(0,0); 10 | double _SPEEDUP_RATE=1; 11 | 12 | //must allowed user-defination interfaces: 13 | void _Set_Map_XY(double _X,double _Y){ 14 | _MAP_X = _X; 15 | _MAP_Y = _Y; 16 | } 17 | void _Set_Map_Center(double _x,double _y){ 18 | _MAP_CENTER = vec2f(_x,_y); 19 | } 20 | void _Set_Speedup_Rate(double _rate){ 21 | _SPEEDUP_RATE=_rate; 22 | } 23 | void _SET_FPS(double _fps){ 24 | _FPS = _fps; 25 | } 26 | void _AML_Vertex2f(vec2f pos){ 27 | pos = pos - _MAP_CENTER; 28 | pos = (pos/_MAP_X*2.0, pos/_MAP_Y*2.0); 29 | glVertex2f(pos.x,pos.y); 30 | } 31 | class _Agent{ 32 | public: 33 | virtual void _plot(double,double)=0; 34 | virtual void _step(double,double,_Agent*)=0; 35 | virtual void _copy_from(_Agent*)=0; 36 | }; 37 | class Ball: public _Agent{ 38 | public: 39 | vec2f pos; 40 | vec2f vel; 41 | Ball(int _x, int _y) { 42 | pos = vec2f(_x, _y); 43 | } 44 | void _plot(double _tim, double _dtim) { 45 | glColor3f(1., 1., 1.); 46 | glBegin(GL_LINES); 47 | _AML_Vertex2f(pos); 48 | _AML_Vertex2f((pos + ((vel * 0.1) / _dtim))); 49 | glEnd(); 50 | glColor3f(1., 0., 0.); 51 | glBegin(GL_POINTS); 52 | _AML_Vertex2f(pos); 53 | glEnd(); 54 | glFlush(); 55 | 56 | } 57 | void _step(double _tim, double _dtim, _Agent* _last_Agent) { 58 | Ball* _last = (Ball*)_last_Agent; 59 | pos = vec2f(pos.x, (10 - ((_tim * _tim) * 4.9))); 60 | /* free fall*/ 61 | vel = (pos - (_last->pos)); 62 | /* first order difference*/ 63 | } 64 | void _copy_from(_Agent *_from_Agent){ 65 | Ball* _from = (Ball*)_from_Agent; 66 | *this = *_from; 67 | } 68 | }; 69 | 70 | vector > _agents; 71 | double _dt=1/_FPS*_SPEEDUP_RATE; 72 | double _cur_tim=0.0; 73 | void _Plot(void){ 74 | glClear(GL_COLOR_BUFFER_BIT); 75 | for(int i=0;i<_agents.size();i++){ 76 | _agents[i].second->_plot(_cur_tim, _dt); 77 | } 78 | glutSwapBuffers(); 79 | } 80 | void _Step_Time(int _time_value){ 81 | //printf("step time: %d\n",_time_value); 82 | for(int i=0;i<_agents.size();i++){ 83 | _agents[i].second->_step(_cur_tim,_dt,_agents[i].first); 84 | _agents[i].first->_copy_from(_agents[i].second); 85 | } 86 | _cur_tim+=_dt; 87 | glutPostRedisplay(); 88 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 89 | } 90 | 91 | int main(int argc, char *argv[]) { 92 | glutInit(&argc, argv); 93 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 94 | glutInitWindowPosition(100, 100); 95 | glutInitWindowSize(500, 500); 96 | int glut_window = glutCreateWindow("AML"); 97 | void *_Agent1, *_Agent2; 98 | for(int i = 1; (i <= 5); (i++)) { 99 | _Agent1 = new Ball((-12 + (4 * i)), 10); 100 | _Agent2 = new Ball((-12 + (4 * i)), 10); 101 | _agents.push_back(make_pair((_Agent*)_Agent1, (_Agent*)_Agent2)); 102 | } 103 | glutDisplayFunc(&_Plot); 104 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 105 | glutMainLoop(); 106 | } 107 | -------------------------------------------------------------------------------- /tests/std/demo2.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | // Add the header for agent 4 | #include "cglib.h" 5 | 6 | double _FPS=60.0; 7 | // unit is "meter" for them 8 | double _MAP_X=20,_MAP_Y=20; 9 | vec2f _MAP_CENTER(0,0); 10 | double _SPEEDUP_RATE=1; 11 | 12 | //must allowed user-defination interfaces: 13 | void _Set_Map_XY(double _X,double _Y){ 14 | _MAP_X = _X; 15 | _MAP_Y = _Y; 16 | } 17 | void _Set_Map_Center(double _x,double _y){ 18 | _MAP_CENTER = vec2f(_x,_y); 19 | } 20 | void _Set_Speedup_Rate(double _rate){ 21 | _SPEEDUP_RATE=_rate; 22 | } 23 | void _SET_FPS(double _fps){ 24 | _FPS = _fps; 25 | } 26 | void _AML_Vertex2f(vec2f pos){ 27 | pos = pos - _MAP_CENTER; 28 | pos = (pos/_MAP_X*2.0, pos/_MAP_Y*2.0); 29 | glVertex2f(pos.x,pos.y); 30 | } 31 | class _Agent{ 32 | public: 33 | virtual void _plot(double,double)=0; 34 | virtual void _step(double,double,_Agent*)=0; 35 | virtual void _copy_from(_Agent*)=0; 36 | }; 37 | class Blinker: public _Agent{ 38 | public: 39 | angle theta; 40 | vec2f pos; 41 | int color; 42 | double PI; 43 | Blinker(angle _theta) { 44 | theta = _theta; 45 | color = 0; 46 | PI = acos(-1.); 47 | } 48 | void _plot(double _tim, double _dtim) { 49 | vec2f a = (pos + vec2f(0, 1)); 50 | vec2f b = (pos + vec2f(-0.866, -0.5)); 51 | vec2f c = (pos + vec2f(0.866, -0.5)); 52 | if((color == 0)) { 53 | glColor3f(1., 0., 0.); 54 | } 55 | else { 56 | glColor3f(0., 0., 1.); 57 | } 58 | glBegin(GL_TRIANGLES); 59 | _AML_Vertex2f(a); 60 | _AML_Vertex2f(b); 61 | _AML_Vertex2f(c); 62 | glEnd(); 63 | glFlush(); 64 | 65 | } 66 | void _step(double _tim, double _dtim, _Agent* _last_Agent) { 67 | Blinker* _last = (Blinker*)_last_Agent; 68 | double dlt = (((_dtim / 10) * 2) * PI); 69 | theta = (theta + dlt); 70 | pos = (vec2f(cos(theta), sin(theta)) * 5); 71 | if((!(_last->theta > angle(0))&&(theta > angle(0)))) { 72 | /* operator :> means "when > holds true for the first time"*/ 73 | color = (color ^ 1); 74 | } 75 | } 76 | void _copy_from(_Agent *_from_Agent){ 77 | Blinker* _from = (Blinker*)_from_Agent; 78 | *this = *_from; 79 | } 80 | }; 81 | 82 | vector > _agents; 83 | double _dt=1/_FPS*_SPEEDUP_RATE; 84 | double _cur_tim=0.0; 85 | void _Plot(void){ 86 | glClear(GL_COLOR_BUFFER_BIT); 87 | for(int i=0;i<_agents.size();i++){ 88 | _agents[i].second->_plot(_cur_tim, _dt); 89 | } 90 | glutSwapBuffers(); 91 | } 92 | void _Step_Time(int _time_value){ 93 | //printf("step time: %d\n",_time_value); 94 | for(int i=0;i<_agents.size();i++){ 95 | _agents[i].second->_step(_cur_tim,_dt,_agents[i].first); 96 | _agents[i].first->_copy_from(_agents[i].second); 97 | } 98 | _cur_tim+=_dt; 99 | glutPostRedisplay(); 100 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 101 | } 102 | 103 | int main(int argc, char *argv[]) { 104 | glutInit(&argc, argv); 105 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 106 | glutInitWindowPosition(100, 100); 107 | glutInitWindowSize(500, 500); 108 | int glut_window = glutCreateWindow("AML"); 109 | void *_Agent1, *_Agent2; 110 | for(int i = 1; (i <= 10); (i++)) { 111 | _Agent1 = new Blinker(angle(((i * acos(-1.)) / 5))); 112 | _Agent2 = new Blinker(angle(((i * acos(-1.)) / 5))); 113 | _agents.push_back(make_pair((_Agent*)_Agent1, (_Agent*)_Agent2)); 114 | } 115 | glutDisplayFunc(&_Plot); 116 | glutTimerFunc(1000/_FPS, _Step_Time, 1); 117 | glutMainLoop(); 118 | } 119 | -------------------------------------------------------------------------------- /tests/std/function.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int fibonacci(int num) { 4 | if((num == 0)) { 5 | return 0; 6 | } 7 | else { 8 | if((num == 1)) { 9 | return 1; 10 | } 11 | else { 12 | return (fibonacci((num - 2)) + fibonacci((num - 1))); 13 | } 14 | } 15 | } 16 | int main() { 17 | cout << fibonacci(3) << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /tests/std/global_variable.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int a = 1; 4 | int b = 2; 5 | int c; 6 | int main() { 7 | c = (a + b); 8 | cout << c << endl; 9 | } 10 | -------------------------------------------------------------------------------- /tests/std/if.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | int a; 5 | a = ((1 + 2) == 3? 5 : 4); 6 | if((a > 2)) { 7 | cout << "Yes" << endl; 8 | } 9 | else { 10 | cout << "No" << endl; 11 | } 12 | if((a >= 3)) { 13 | if((a <= 3)) { 14 | printfln("Yes"); 15 | } 16 | else { 17 | printfln("No"); 18 | } 19 | } 20 | if((2 < 10)) { 21 | cout << "Yes" << endl; 22 | } 23 | if(true) { 24 | if(false) { 25 | v = (4 + 1); 26 | } 27 | else { 28 | v = 2; 29 | } 30 | } 31 | else { 32 | } 33 | cout << v << endl; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /tests/std/io.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | int a; 5 | int b; 6 | cin >> a >> endl; 7 | cin >> b >> endl; 8 | cout << a << endl; 9 | cout << b << endl; 10 | cin >> a >>' '>> b >> endl; 11 | cout << a <<' '<< b << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /tests/std/loop.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | int sum; 5 | for(int i = 1, j; (i <= 10); (i++)) { 6 | for(j = 1; (j <= 10); (j++)) { 7 | sum = ((sum + i) + j); 8 | } 9 | } 10 | cout << sum << endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /tests/std/ternary.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | int a = 1; 5 | int b = 2; 6 | int c = (a == b)? 3 : 2; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /tests/std/variable.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | int i = 1; 5 | double f = 2; 6 | string s; 7 | bool b; 8 | ang d; 9 | vec2f v; 10 | s = "3"; 11 | s = ""; 12 | b = true; 13 | d = ang(3., 2.); 14 | } 15 | -------------------------------------------------------------------------------- /tests/std/vector.cpp: -------------------------------------------------------------------------------- 1 | // Produced by AML 2 | #include "geolib.h" 3 | int main() { 4 | cout << vec2f(1, 2) << endl; 5 | cout << (vec2f(0, 0) + vec2f(1, 1)) << endl; 6 | vec2f a = vec2f(3, 5); 7 | vec2f b = vec2f(1, 5); 8 | double d = 2.1; 9 | cout << (a + b) << endl; 10 | cout << (a - b) << endl; 11 | cout << (a * b) << endl; 12 | cout << (a / b) << endl; 13 | cout << (a * d) << endl; 14 | cout << (a / d) << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /tests/test.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | tests = ["arith", "if", "loop", "variable", "comment", "function", "block", "vector", "io", "global_variable", "array", "ternary"] 4 | tests_agent = ["demo1", "demo2"] 5 | 6 | l = len(tests) 7 | for i, test in zip(range(l), tests): 8 | os.system("./main.native tests/aml/{0}.aml > tests/cpp/{0}.cpp".format(test)) 9 | if (os.system("diff tests/cpp/{0}.cpp tests/std/{0}.cpp".format(test)) == 0): 10 | print("Pass the test {0} ({0} / {1}) ({2})".format(i + 1, l, test)) 11 | else : 12 | print("Fail the test {0} ({0} / {1}) ({2})".format(i + 1, l, test)) 13 | 14 | l = len(tests_agent) 15 | for i, test in zip(range(len(tests_agent)), tests_agent): 16 | os.system("./main.native tests/aml/{0}.aml --agent > tests/cpp/{0}.cpp".format(test)) 17 | if (os.system("diff tests/cpp/{0}.cpp tests/std/{0}.cpp".format(test)) == 0): 18 | print("Pass the demo {0} ({0} / {1}) ({2})".format(i + 1, l, test)) 19 | else : 20 | print("Fail the demo {0} ({0} / {1}) ({2})".format(i + 1, l, test)) 21 | --------------------------------------------------------------------------------