add test file & update README
This commit is contained in:
parent
0700ce14f7
commit
a7d6518bff
42
README.md
42
README.md
|
@ -291,6 +291,9 @@ a(x:0,y:1,z:2);
|
||||||
```
|
```
|
||||||
|
|
||||||
## closure
|
## closure
|
||||||
|
|
||||||
|
Use closure to OOP.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var f=func()
|
var f=func()
|
||||||
{
|
{
|
||||||
|
@ -298,6 +301,21 @@ var f=func()
|
||||||
return func(){return a;};
|
return func(){return a;};
|
||||||
}
|
}
|
||||||
print(f()());
|
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
|
## built-in functions
|
||||||
|
@ -319,13 +337,13 @@ nasal_val* builtin_chr(std::unordered_map<int,nasal_val*>&,nasal_gc&);
|
||||||
Then complete this function using C++:
|
Then complete this function using C++:
|
||||||
|
|
||||||
```C++
|
```C++
|
||||||
nasal_val* builtin_print(std::unordered_map<int,nasal_val*>& local_scope_addr,nasal_gc& gc)
|
nasal_val* builtin_print(std::unordered_map<int,nasal_val*>& local_scope,nasal_gc& gc)
|
||||||
{
|
{
|
||||||
// get arguments by using in_builtin_find
|
// get arguments by using builtin_find
|
||||||
nasal_val* vector_value_addr=in_builtin_find("elements");
|
nasal_val* vector_value=builtin_find("elements");
|
||||||
// main process
|
// main process
|
||||||
// also check type here,if get a type error,use builtin_error_occurred and return nullptr
|
// 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_addr->get_vector();
|
nasal_vec& ref_vec=vector_value->get_vector();
|
||||||
int size=ref_vec.size();
|
int size=ref_vec.size();
|
||||||
for(int i=0;i<size;++i)
|
for(int i=0;i<size;++i)
|
||||||
{
|
{
|
||||||
|
@ -341,8 +359,8 @@ nasal_val* builtin_print(std::unordered_map<int,nasal_val*>& local_scope_addr,na
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// if a nasal value is not in use,use gc::del_reference to delete it
|
// 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
|
// 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;
|
return nil_addr;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -352,16 +370,16 @@ After that, write the built-in function's name(in nasal) and the function's poin
|
||||||
```C++
|
```C++
|
||||||
struct FUNC_TABLE
|
struct FUNC_TABLE
|
||||||
{
|
{
|
||||||
std::string func_name;
|
std::string name;
|
||||||
nasal_val* (*func_pointer)(std::unordered_map<int,nasal_val*>&,nasal_gc&);
|
nasal_val* (*func)(std::unordered_map<int,nasal_val*>&,nasal_gc&);
|
||||||
} builtin_func_table[]=
|
} builtin_func[]=
|
||||||
{
|
{
|
||||||
{"__builtin_std_cout",builtin_print},
|
{"__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
|
```javascript
|
||||||
var print=func(elements...)
|
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).
|
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.
|
Use import("") to get the nasal file including your built-in functions,then you could use it.
|
|
@ -12,8 +12,8 @@ var student=func(name,age)
|
||||||
get_age: func(){return val.age;},
|
get_age: func(){return val.age;},
|
||||||
set_name: func(name){val.name=name;},
|
set_name: func(name){val.name=name;},
|
||||||
get_name: func(){return val.name;}
|
get_name: func(){return val.name;}
|
||||||
}
|
};
|
||||||
};
|
}
|
||||||
var s=student('valk',24);
|
var s=student('valk',24);
|
||||||
s.print_info();
|
s.print_info();
|
||||||
println(s.get_age(),' ',s.get_name());
|
println(s.get_age(),' ',s.get_name());
|
||||||
|
|
|
@ -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');
|
||||||
|
}
|
Loading…
Reference in New Issue