diff --git a/README.md b/README.md index f5a72e8..5fd0fd0 100644 --- a/README.md +++ b/README.md @@ -337,9 +337,8 @@ and the debugger will print this: ```javascript source code: ---> var fib=func(x) - { - if(x<2) return x; +--> var fib = func(x) { + if (x<2) return x; return fib(x-1)+fib(x-2); } for(var i=0;i<31;i+=1) @@ -347,16 +346,16 @@ source code: next bytecode: - 0x000848 4a 00 00 01 callfv 0x1(std/lib.nas:427) - 0x000849 3d 00 00 00 pop 0x0(std/lib.nas:427) - 0x00084a 07 00 00 00 pnil 0x0(std/lib.nas:423) - 0x00084b 56 00 00 00 ret 0x0(std/lib.nas:423) - 0x00084c 03 00 00 5e loadg 0x5e(std/lib.nas:423) ---> 0x00084d 0b 00 08 51 newf 0x851(test/fib.nas:1) - 0x00084e 02 00 00 03 intl 0x3(test/fib.nas:1) - 0x00084f 0d 00 00 08 para 0x8 (x)(test/fib.nas:1) + 0x0003a8 07:00 00 00 00 00 00 00 00 pnil 0x0 (std/lib.nas:413) + 0x0003a9 56:00 00 00 00 00 00 00 00 ret 0x0 (std/lib.nas:413) + 0x0003aa 03:00 00 00 00 00 00 00 56 loadg 0x56 (std/lib.nas:413) +--> 0x0003ab 0b:00 00 00 00 00 00 03 af newf 0x3af (test/fib.nas:1) + 0x0003ac 02:00 00 00 00 00 00 00 03 intl 0x3 (test/fib.nas:1) + 0x0003ad 0d:00 00 00 00 00 00 00 22 para 0x22 (x) (test/fib.nas:1) + 0x0003ae 3e:00 00 00 00 00 00 03 be jmp 0x3be (test/fib.nas:1) + 0x0003af 45:00 00 00 00 00 00 00 01 calll 0x1 (test/fib.nas:2) -stack (0x55ccd0a1b9d0, limit 10, total 0) +vm stack (0x7fca7e9f1010, limit 16, total 0) >> ``` @@ -371,9 +370,8 @@ This will help you debugging or learning how the vm works: ```javascript source code: - var fib=func(x) - { ---> if(x<2) return x; + var fib = func(x) { +--> if (x<2) return x; return fib(x-1)+fib(x-2); } for(var i=0;i<31;i+=1) @@ -381,24 +379,24 @@ source code: next bytecode: - 0x000850 3e 00 08 60 jmp 0x860(test/fib.nas:1) ---> 0x000851 45 00 00 01 calll 0x1(test/fib.nas:3) - 0x000852 39 00 00 07 lessc 0x7 (2)(test/fib.nas:3) - 0x000853 40 00 08 56 jf 0x856(test/fib.nas:3) - 0x000854 45 00 00 01 calll 0x1(test/fib.nas:3) - 0x000855 56 00 00 00 ret 0x0(test/fib.nas:3) - 0x000856 44 00 00 5f callg 0x5f(test/fib.nas:4) - 0x000857 45 00 00 01 calll 0x1(test/fib.nas:4) + 0x0003a8 07:00 00 00 00 00 00 00 00 pnil 0x0 (std/lib.nas:413) + 0x0003a9 56:00 00 00 00 00 00 00 00 ret 0x0 (std/lib.nas:413) + 0x0003aa 03:00 00 00 00 00 00 00 56 loadg 0x56 (std/lib.nas:413) + 0x0003ab 0b:00 00 00 00 00 00 03 af newf 0x3af (test/fib.nas:1) + 0x0003ac 02:00 00 00 00 00 00 00 03 intl 0x3 (test/fib.nas:1) + 0x0003ad 0d:00 00 00 00 00 00 00 22 para 0x22 (x) (test/fib.nas:1) + 0x0003ae 3e:00 00 00 00 00 00 03 be jmp 0x3be (test/fib.nas:1) +--> 0x0003af 45:00 00 00 00 00 00 00 01 calll 0x1 (test/fib.nas:2) -stack (0x55ccd0a1b9d0, limit 10, total 8) - 0x000007 | pc | 0x869 +vm stack (0x7fca7e9f1010, limit 16, total 8) + 0x000007 | pc | 0x3c7 0x000006 | addr | 0x0 0x000005 | nil | 0x000004 | nil | 0x000003 | num | 0 0x000002 | nil | 0x000001 | nil | - 0x000000 | func | <0x55ccd0a58fa0> entry:0x487 + 0x000000 | func | <0x5573f66ef5f0> func(elems...) {..} >> ``` diff --git a/doc/README_zh.md b/doc/README_zh.md index 2f655e9..77f6480 100644 --- a/doc/README_zh.md +++ b/doc/README_zh.md @@ -322,9 +322,8 @@ local (0x55dcb5b43190 <+7>) ```javascript source code: ---> var fib=func(x) - { - if(x<2) return x; +--> var fib = func(x) { + if (x<2) return x; return fib(x-1)+fib(x-2); } for(var i=0;i<31;i+=1) @@ -332,16 +331,16 @@ source code: next bytecode: - 0x000848 4a 00 00 01 callfv 0x1(std/lib.nas:427) - 0x000849 3d 00 00 00 pop 0x0(std/lib.nas:427) - 0x00084a 07 00 00 00 pnil 0x0(std/lib.nas:423) - 0x00084b 56 00 00 00 ret 0x0(std/lib.nas:423) - 0x00084c 03 00 00 5e loadg 0x5e(std/lib.nas:423) ---> 0x00084d 0b 00 08 51 newf 0x851(test/fib.nas:1) - 0x00084e 02 00 00 03 intl 0x3(test/fib.nas:1) - 0x00084f 0d 00 00 08 para 0x8 (x)(test/fib.nas:1) + 0x0003a8 07:00 00 00 00 00 00 00 00 pnil 0x0 (std/lib.nas:413) + 0x0003a9 56:00 00 00 00 00 00 00 00 ret 0x0 (std/lib.nas:413) + 0x0003aa 03:00 00 00 00 00 00 00 56 loadg 0x56 (std/lib.nas:413) +--> 0x0003ab 0b:00 00 00 00 00 00 03 af newf 0x3af (test/fib.nas:1) + 0x0003ac 02:00 00 00 00 00 00 00 03 intl 0x3 (test/fib.nas:1) + 0x0003ad 0d:00 00 00 00 00 00 00 22 para 0x22 (x) (test/fib.nas:1) + 0x0003ae 3e:00 00 00 00 00 00 03 be jmp 0x3be (test/fib.nas:1) + 0x0003af 45:00 00 00 00 00 00 00 01 calll 0x1 (test/fib.nas:2) -stack (0x55ccd0a1b9d0, limit 10, total 0) +vm stack (0x7fca7e9f1010, limit 16, total 0) >> ``` @@ -356,9 +355,8 @@ stack (0x55ccd0a1b9d0, limit 10, total 0) ```javascript source code: - var fib=func(x) - { ---> if(x<2) return x; + var fib = func(x) { +--> if (x<2) return x; return fib(x-1)+fib(x-2); } for(var i=0;i<31;i+=1) @@ -366,24 +364,24 @@ source code: next bytecode: - 0x000850 3e 00 08 60 jmp 0x860(test/fib.nas:1) ---> 0x000851 45 00 00 01 calll 0x1(test/fib.nas:3) - 0x000852 39 00 00 07 lessc 0x7 (2)(test/fib.nas:3) - 0x000853 40 00 08 56 jf 0x856(test/fib.nas:3) - 0x000854 45 00 00 01 calll 0x1(test/fib.nas:3) - 0x000855 56 00 00 00 ret 0x0(test/fib.nas:3) - 0x000856 44 00 00 5f callg 0x5f(test/fib.nas:4) - 0x000857 45 00 00 01 calll 0x1(test/fib.nas:4) + 0x0003a8 07:00 00 00 00 00 00 00 00 pnil 0x0 (std/lib.nas:413) + 0x0003a9 56:00 00 00 00 00 00 00 00 ret 0x0 (std/lib.nas:413) + 0x0003aa 03:00 00 00 00 00 00 00 56 loadg 0x56 (std/lib.nas:413) + 0x0003ab 0b:00 00 00 00 00 00 03 af newf 0x3af (test/fib.nas:1) + 0x0003ac 02:00 00 00 00 00 00 00 03 intl 0x3 (test/fib.nas:1) + 0x0003ad 0d:00 00 00 00 00 00 00 22 para 0x22 (x) (test/fib.nas:1) + 0x0003ae 3e:00 00 00 00 00 00 03 be jmp 0x3be (test/fib.nas:1) +--> 0x0003af 45:00 00 00 00 00 00 00 01 calll 0x1 (test/fib.nas:2) -stack (0x55ccd0a1b9d0, limit 10, total 8) - 0x000007 | pc | 0x869 +vm stack (0x7fca7e9f1010, limit 16, total 8) + 0x000007 | pc | 0x3c7 0x000006 | addr | 0x0 0x000005 | nil | 0x000004 | nil | 0x000003 | num | 0 0x000002 | nil | 0x000001 | nil | - 0x000000 | func | <0x55ccd0a58fa0> entry:0x487 + 0x000000 | func | <0x5573f66ef5f0> func(elems...) {..} >> ``` diff --git a/std/runtime.nas b/std/runtime.nas index b6edeaf..bad3203 100644 --- a/std/runtime.nas +++ b/std/runtime.nas @@ -1,13 +1,29 @@ -# runtime.nas -# 2023 by ValKmjolnir -# runtime gives us some functions that we could manage it manually. - -# command line arguments -var argv = func() { - return globals.arg; -} - -var gc = { - extend: func(type) {return __gcextd;}, - info: func() {return __gcinfo;} -}; +# runtime.nas +# 2023 by ValKmjolnir +# runtime gives us some functions that we could manage it manually. + +# command line arguments +var argv = func() { + return globals.arg; +} + +var _gc_extend = func(type) { + return __gcextd; +} + +var gc = { + extend: func(type, times = 1) { + if (times<=0) { + return nil; + } + if (times>16) { + times = 16; + } + + for(var i = 0; iedge; -} - -var vec3abs = func(v) { - return [abs(v[0]),abs(v[1]),abs(v[2])]; -} -var vec3sign = func(v) { - return [sign(v[0]),sign(v[1]),sign(v[2])]; -} -var vec3step = func(edge,v) { - return [step(edge[0],v[0]),step(edge[1],v[1]),step(edge[2],v[2])]; -} -var vec3reflect = func(rd,n) { - var d=vec3dot(n,rd); - return vec3sub(rd,vec3mul(n,vec3mul([2,2,2],[d,d,d]))); -} - -var sphere = func(ro,rd,r) { - var b=vec3dot(ro,rd); - var c=vec3dot(ro,ro)-r*r; - var h=b*b-c; - if (h<0.0) return [-1.0,-1.0]; - h=sqrt(h); - return [-b-h,-b+h]; -} - -var box = func(ro,rd,boxSize,outNormal) { - var m=vec3div([1.0,1.0,1.0],rd); - var n=vec3mul(m,ro); - var k=vec3mul(vec3abs(m),boxSize); - var t1=vec3sub(vec3neg(n),k); - var t2=vec3add(vec3neg(n),k); - var tN=max(max(t1[0],t1[1]),t1[2]); - var tF=min(min(t2[0],t2[1]),t2[2]); - if (tN>tF or tF<0.0) return [-1.0,-1.0]; - var yzx=[t1[1],t1[2],t1[0]]; - var zxy=[t1[2],t1[0],t1[1]]; - var tmp=vec3mul(vec3mul(vec3neg(vec3sign(rd)), vec3step(yzx,t1)),vec3step(zxy,t1)); - outNormal[0]=tmp[0]; - outNormal[1]=tmp[1]; - outNormal[2]=tmp[2]; - return [tN, tF]; -} - -var plane = func(ro,rd,p,w) { - return -(vec3dot(ro,p)+w)/vec3dot(rd,p); -} - -var main = func(frame) { - - var height=15*2; - var width=int(height*1/0.618)*2; - - var aspect=width/height; - var pixelAspect=11.0/24.0; - - var gradient=split(""," .:!/r(l1Z4H9W8$"); - var gradientSize=size(gradient)-1; - - var screen=[]; - setsize(screen,width*height); - - var light=vec3norm([-0.5,0.5,-1.0]); - var spherePos=[0,3,0]; - var vec2_2_2=[2,2]; - var vec2_1_1=[1,1]; - var vec3_000=[0,0,0]; - var vec3_00n1=[0,0,-1]; - var vec3_111=[1,1,1]; - - print("\e[2J"); - var stamp=maketimestamp(); - for(var t=0;t0) { - var itPoint=vec3add(vec3sub(ro,spherePos),vec3mul(rd,[intersection[0],intersection[0],intersection[0]])); - minIt=intersection[0]; - n=vec3norm(itPoint); - } - var boxN=[0,0,0]; - intersection=box(ro,rd,vec3_111,boxN); - if (intersection[0]>0 and intersection[0]0 and intersection[0]edge; +} + +var vec3abs = func(v) { + return [abs(v[0]),abs(v[1]),abs(v[2])]; +} +var vec3sign = func(v) { + return [sign(v[0]),sign(v[1]),sign(v[2])]; +} +var vec3step = func(edge,v) { + return [step(edge[0],v[0]),step(edge[1],v[1]),step(edge[2],v[2])]; +} +var vec3reflect = func(rd,n) { + var d=vec3dot(n,rd); + return vec3sub(rd,vec3mul(n,vec3mul([2,2,2],[d,d,d]))); +} + +var sphere = func(ro,rd,r) { + var b=vec3dot(ro,rd); + var c=vec3dot(ro,ro)-r*r; + var h=b*b-c; + if (h<0.0) return [-1.0,-1.0]; + h=sqrt(h); + return [-b-h,-b+h]; +} + +var box = func(ro,rd,boxSize,outNormal) { + var m=vec3div([1.0,1.0,1.0],rd); + var n=vec3mul(m,ro); + var k=vec3mul(vec3abs(m),boxSize); + var t1=vec3sub(vec3neg(n),k); + var t2=vec3add(vec3neg(n),k); + var tN=max(max(t1[0],t1[1]),t1[2]); + var tF=min(min(t2[0],t2[1]),t2[2]); + if (tN>tF or tF<0.0) return [-1.0,-1.0]; + var yzx=[t1[1],t1[2],t1[0]]; + var zxy=[t1[2],t1[0],t1[1]]; + var tmp=vec3mul(vec3mul(vec3neg(vec3sign(rd)), vec3step(yzx,t1)),vec3step(zxy,t1)); + outNormal[0]=tmp[0]; + outNormal[1]=tmp[1]; + outNormal[2]=tmp[2]; + return [tN, tF]; +} + +var plane = func(ro,rd,p,w) { + return -(vec3dot(ro,p)+w)/vec3dot(rd,p); +} + +var main = func(frame) { + + var height=15*2; + var width=int(height*1/0.618)*2; + + var aspect=width/height; + var pixelAspect=11.0/24.0; + + var gradient=split(""," .:!/r(l1Z4H9W8$"); + var gradientSize=size(gradient)-1; + + var screen=[]; + setsize(screen,width*height); + + var light=vec3norm([-0.5,0.5,-1.0]); + var spherePos=[0,3,0]; + var vec2_2_2=[2,2]; + var vec2_1_1=[1,1]; + var vec3_000=[0,0,0]; + var vec3_00n1=[0,0,-1]; + var vec3_111=[1,1,1]; + + print("\e[2J"); + var stamp=maketimestamp(); + for(var t=0;t0) { + var itPoint=vec3add(vec3sub(ro,spherePos),vec3mul(rd,[intersection[0],intersection[0],intersection[0]])); + minIt=intersection[0]; + n=vec3norm(itPoint); + } + var boxN=[0,0,0]; + intersection=box(ro,rd,vec3_111,boxN); + if (intersection[0]>0 and intersection[0]0 and intersection[0]