diff --git a/README.md b/README.md index 097aa53..4789315 100644 --- a/README.md +++ b/README.md @@ -291,6 +291,9 @@ a(x:0,y:1,z:2); ``` ## closure + +Use closure to OOP. + ```javascript var f=func() { @@ -298,6 +301,21 @@ var f=func() return func(){return a;}; } print(f()()); + +var student=func(name,age) +{ + var val={ + name:name, + age:age + }; + return { + print_info:func(){println(val.name,' ',val.age);}, + set_age: func(age){val.age=age;}, + get_age: func(){return val.age;}, + set_name: func(name){val.name=name;}, + get_name: func(){return val.name;} + }; +} ``` ## built-in functions @@ -319,13 +337,13 @@ nasal_val* builtin_chr(std::unordered_map&,nasal_gc&); Then complete this function using C++: ```C++ -nasal_val* builtin_print(std::unordered_map& local_scope_addr,nasal_gc& gc) +nasal_val* builtin_print(std::unordered_map& local_scope,nasal_gc& gc) { - // get arguments by using in_builtin_find - nasal_val* vector_value_addr=in_builtin_find("elements"); + // get arguments by using builtin_find + nasal_val* vector_value=builtin_find("elements"); // main process - // also check type here,if get a type error,use builtin_error_occurred and return nullptr - nasal_vec& ref_vec=vector_value_addr->get_vector(); + // also check number of arguments and type here,if get a type error,use builtin_err and return nullptr + nasal_vec& ref_vec=vector_value->get_vector(); int size=ref_vec.size(); for(int i=0;i& local_scope_addr,na } } // if a nasal value is not in use,use gc::del_reference to delete it - // if get a new reference of a nasal value,use gc::add_reference // generate return value,use gc::gc_alloc(type) to make a new value + // or use reserved reference nil_addr/one_addr/zero_addr return nil_addr; } ``` @@ -352,16 +370,16 @@ After that, write the built-in function's name(in nasal) and the function's poin ```C++ struct FUNC_TABLE { - std::string func_name; - nasal_val* (*func_pointer)(std::unordered_map&,nasal_gc&); -} builtin_func_table[]= + std::string name; + nasal_val* (*func)(std::unordered_map&,nasal_gc&); +} builtin_func[]= { {"__builtin_std_cout",builtin_print}, - {"",NULL} + {"", NULL } }; ``` -At last,warp the 'nasal_call_builtin_std_cout' in a nasal file: +At last,warp the '__builtin_std_cout' in a nasal file: ```javascript var print=func(elements...) @@ -373,4 +391,4 @@ var print=func(elements...) In version 5.0,if you don't warp built-in function in a normal nasal function,this built-in function may cause a fault when searching arguments,which will cause SIGSEGV segmentation error(maybe). -Use import("") to get the nasal file including your biult-in functions,then you could use it. \ No newline at end of file +Use import("") to get the nasal file including your built-in functions,then you could use it. \ No newline at end of file diff --git a/test/class.nas b/test/class.nas index 542c978..2f024c9 100644 --- a/test/class.nas +++ b/test/class.nas @@ -12,8 +12,8 @@ var student=func(name,age) get_age: func(){return val.age;}, set_name: func(name){val.name=name;}, get_name: func(){return val.name;} - } -}; + }; +} var s=student('valk',24); s.print_info(); println(s.get_age(),' ',s.get_name()); diff --git a/test/mandelbrot.nas b/test/mandelbrot.nas new file mode 100644 index 0000000..08e031b --- /dev/null +++ b/test/mandelbrot.nas @@ -0,0 +1,31 @@ +import("lib.nas"); +var yMin=-0.2; +var yMax=0.2; +var xMin=-1.5; +var xMax=-1.0; +for(var yPixel=0;yPixel<24;yPixel+=1) +{ + var y=(yPixel/24)*(yMax-yMin)+yMin; + for(var xPixel=0;xPixel<80;xPixel+=1) + { + var x=(xPixel/80)*(xMax-xMin)+xMin; + var pixel=" "; + var x0=x; + var y0=y; + for(var iter=0;iter<80;iter+=1) + { + var x1=(x0*x0)-(y0*y0)+x; + var y1=2*x0*y0+y; + x0=x1; + y0=y1; + var d=(x0*x0)+(y0*y0); + if(d>4) + { + pixel=chr(" .:;+=xX$&"[int(iter/8)]); + break; + } + } + print(pixel); + } + print('\n'); +} \ No newline at end of file