34 lines
1004 B
C++
34 lines
1004 B
C++
#include <iostream>
|
|
#include "../nasal.h"
|
|
|
|
double fibonaci(double x){
|
|
if(x<=2)
|
|
return x;
|
|
return fibonaci(x-1)+fibonaci(x-2);
|
|
}
|
|
extern "C" var fib(std::vector<var>& args,gc& ngc){
|
|
std::cout<<"[mod] this is the first test module of nasal\n";
|
|
if(!args.size())
|
|
return nas_err("fib","lack arguments");
|
|
var num=args[0];
|
|
if(num.type!=vm_num)
|
|
return nas_err("extern_fib","\"num\" must be number");
|
|
return {vm_num,fibonaci(num.tonum())};
|
|
}
|
|
extern "C" var quick_fib(std::vector<var>& args,gc& ngc){
|
|
std::cout<<"[mod] this is the first test module of nasal\n";
|
|
if(!args.size())
|
|
return nas_err("fib","lack arguments");
|
|
var num=args[0];
|
|
if(num.type!=vm_num)
|
|
return nas_err("extern_quick_fib","\"num\" must be number");
|
|
if(num.num()<2)
|
|
return num;
|
|
double a=1,b=1,res=0;
|
|
for(double i=1;i<num.num();i+=1){
|
|
res=a+b;
|
|
a=b;
|
|
b=res;
|
|
}
|
|
return {vm_num,res};
|
|
} |