This commit is contained in:
Valk Richard Li 2020-02-05 22:47:23 +08:00 committed by GitHub
parent 0e86e5f3c3
commit 6175e274c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 351 additions and 6 deletions

View File

@ -26,7 +26,8 @@ class abstract_syntax_tree
// main functions
// print
void print_tree(const int);
void print_tree();
void print_tree_block(const int);
// set
void set_clear();
@ -96,7 +97,14 @@ abstract_syntax_tree& abstract_syntax_tree::operator=(const abstract_syntax_tree
return *this;
}
void abstract_syntax_tree::print_tree(const int n)
void abstract_syntax_tree::print_tree()
{
std::cout<<">> [Abstract-syntax-tree] get tree root: "<<(this)<<""<<std::endl;
print_tree_block(1);
return;
}
void abstract_syntax_tree::print_tree_block(const int n)
{
std::string __str="";
for(int i=0;i<n;++i)
@ -109,15 +117,18 @@ void abstract_syntax_tree::print_tree(const int n)
case __string: std::cout<<": "<<var_string;break;
case __id:
case __dynamic_id:
std::cout<<": "<<var_name<<" (sym_num:"<<symbol_number<<"["<<(symbol_is_global? "global":"local")<<"])";
break;
case __call_vector:
case __call_hash:
case __call_function:std::cout<<": "<<var_name<<" (sym_num: "<<symbol_number<<"("<<(symbol_is_global? "global":"local")<<"))";break;
case __call_function:break;
default:break;
}
std::cout<<std::endl;
if(!children.empty())
{
for(std::list<abstract_syntax_tree>::iterator i=children.begin();i!=children.end();++i)
i->print_tree(n+1);
i->print_tree_block(n+1);
}
return;
}

194
version2.0/lib/base.nas Normal file
View File

@ -0,0 +1,194 @@
# nasal lib base.nas
# 2020/2/4
# this file is used to avoid name confliction
# and is used to avoid name undefined
# before running this file will be translated to abstract syntax tree
# and this ast will be linked before main ast as main-ast's beginning
# append
# The first argument specifies a vector.
# Appends the remaining arguments to the end of the vector.
var append=func(vector,elements...)
{
var call_inline_push_back=func(vector,elements){};
call_inline_push_back(vector,elements);
return;
}
# setsize
# Sets the size of a vector.
# The first argument specifies a vector, the second a number representing the desired size of that vector.
# If the vector is currently larger than the specified size,it is truncated.
# If it is smaller, it is padded with nil entries.Returns the vector operated upon.
var setsize=func(vector,__size)
{
var call_inline_push_null=func(vector,__size){};
call_inline_push_null(vector,__size);
return;
}
# subvec
# Returns a sub-range of a vector.
# The first argument specifies a vector, the second a starting index,
# and the optional third argument indicates a length (the default is to the end of the vector).
var subvec=func(vector,start,length=nil)
{
var call_inline_subvec=func(vector,start,length=nil){};
return call_inline_subvec(vector,start,length);
}
# contains
# The first argument specifies a hash, the second must be a scalar.
# Returns 1 if the hash contains the scalar as a key, 0 if not.
var contains=func(hash,key)
{
var call_inline_contains=func(hash,key){};
return call_inline_contains(hash,key);
}
# delete
# The first argument specifies a hash, the second must be a scalar key.
# Deletes the key from the hash if it exists.
# Operationally, this is identical to setting the hash value specified by the key to nil,
# but this variant potentially frees storage by deleting the reference to the key and by shrinking the hash.
var delete=func(hash,key)
{
var call_inline_delete=func(hash,key){};
call_inline_delete(hash,key);
return;
}
# int
# Returns the integer part of the numeric value of the single argument, or nil if none exists.
# Truncates towards zero, not negative infinity (i.e. it's implemented in C as a double tointeger typecast).
var int=func(value)
{
var inline_trans_int=func(value){};
return inline_trans_int(value);
}
# num
# Returns the numeric value of the single argument, or nil if none exists.
var num=func(value)
{
var inline_trans_num=func(value){};
return inline_trans_num(value);
}
# keys
# Returns a vector containing the list of keys found in the single hash argument.
var keys=func(hash)
{
var inline_get_keys=func(hash){};
return inline_get_keys(hash);
}
# pop
# Removes and returns the last element of the single vector argument.
var pop=func(vector)
{
var inline_get_back=func(vector){};
var inline_pop_back=func(vector){};
var ret=inline_get_back(vector);
inline_pop_back(vector);
return ret;
}
# size
# Returns the size of the single argument.
# For strings, this is the length in bytes.
# For vectors, this is the number of elements.
# For hashes, it is the number of key/value pairs.
# Returns nil for number and nil arguments.
var size=func(object)
{
var call_inline_sizeof=func(object){};
return call_inline_sizeof(object);
}
# streq
# Tests the string values of the two arguments for equality.
# Needed because the == operator in Nasal tests for numeric equality, as in perl.
# So "0" == "0.0" is true,but streq("0", "0.0") is false.
# This is rarely required in typical code.
var streq=func(__a,__b)
{
var inline_str_cmp_equal=func(str1,str2){};
return inline_str_cmp_equal(__a,__b);
}
# cmp
# Compares two strings, returning -1 if a is less than b, 0 if theyare identical, and 1 if a is greater than b.
var cmp=func(__a,__b)
{
var inline_cmp=func(var1,var2){};
return inline_cmp(__a,__b);
}
# sort
# Creates a new vector containing the elements in the input vector sorted in ascending order according to the rule givenby function,
# which takes two arguments (elements of the input vector) and should return less than zero, zero, or greater than zero if the first argument is,
# respectively, less than, equal to, or greater than the second argument. Despite being implemented with ANSI C qsort(),
# the sort is stable; "equal" elements in the output vector will appear in the same relative order as they do in the input.
var sort=func(vector,function)
{
var call_cpp_sort=func(vector,function){};
call_cpp_sort(vector,function);
return;
}
# substr
# Computes a substring.
# The first argument specifes a string, the second is an integer index of the start of a substring,
# the optional third argument specifies a length (the default is to return the remaining string).
# Example: substr("abcde", 1, 3) returns "bcd".
var substr=func(__string,start,length=nil)
{
var call_inline_substr=func(__string,start,length){};
return call_inline_substr(__string,start,length);
}
# sprintf
# Creates and returns a string formatted as per ANSI C sprintf().
var sprintf=func(__format,var_args...)
{
var call_inline_sprintf=func(__format,var_args){};
return call_inline_sprintf(__format,var_args);
}
# find
# Finds and returns the index of the first occurence of the string needle in the string haystack, or -1 if no such occurence was found.
var find=func(needle,haystack)
{
var inline_find_first_occur=func(needle,haystack){};
return inline_find_first_occur(needle,haystack);
}
# split
# Splits the input string into a vector of substrings bounded by occurences of the delimeter substring.
var split=func(delimeter,__string)
{
var inline_split=func(delimeter,__string){};
return inline_split(delimeter,__string);
}
# rand
# Returns a random number in the range [0:1) (that is, 0.0 is a possible return value. 1.0 is not).
# If a numeric argument is specified, it is used as a seed instead and the function returns nil.
# Implemented in terms of the C library's rand/srand functions;
# the result should have a full double-precision number's worth of randomness even on systems with a 15 bit rand().
var rand=func(seed=nil)
{
var inline_rand=func(seed){};
return inline_rand(seed);
}
# id
# Returns a unique string identifying the object.
# Two id strings are equal if and only if the two references point to the same object in memory.
# Numbers don't have id's and will cause a runtime error if passed to id().
var id=func(thing)
{
var inline_getid=func(thing){};
return inline_getid(thing);
}

36
version2.0/lib/io.nas Normal file
View File

@ -0,0 +1,36 @@
# nasal lib io.nas
# 2020/2/4
# this file is used to avoid name confliction
# and is used to avoid name undefined
# before running this file will be translated to abstract syntax tree
# and this ast will be linked before main ast as main-ast's beginning
var input=func(filename="")
{
if(filename=="")
return __system_call_cpp_istream();
else
return __system_call_cpp_ifstream(filename);
};
var print=func(dyn...)
{
forindex(var i;dyn)
__system_call_c_puts(dyn[i]);
return nil;
};
var fprint=func(arr,filename="")
{
if(filename=="")
{
print("__fprint didn't get a correct file name\n");
return false;
}
else
{
__system_call_cpp_ofstream(arr,filename);
return true;
}
return;
}

97
version2.0/lib/math.nas Normal file
View File

@ -0,0 +1,97 @@
# nasal lib math.nas
# 2020/2/4
# this file is used to avoid name confliction
# and is used to avoid name undefined
# before running this file will be translated to abstract syntax tree
# and this ast will be linked before main ast as main-ast's beginning
var math=
{
e:2.7182818284590452354,
pi:3.14159265358979323846,
sin:func(x)
{
var call_inline_sin=func(x){};
return call_inline_sin(x);
},
cos:func(x)
{
var call_inline_cos=func(x){};
return call_inline_cos(x);
},
tan:func(x){return me.sin(x)/me.cos(x);},
exp:func(x)
{
var call_inline_pow=func(num,x){};
return call_inline_pow(me.e,x);
}
};
var __e=2.7182818284590452354;
var __pi=3.14159265358979323846;
var __ln_2=0.69314718055994530942;
var __ln_10=2.30258509299404568402;
var abs=func(number)
{
if(number>0)
return number;
else
return -1*number;
}
var sin=func(number)
{
return number;
}
var cos=func(number)
{
return number;
}
var tan=func(number)
{
return sin(number)/cos(number);
}
var cot=func(number)
{
return cos(number)/sin(number);
}
var exp=func(number)
{
var int_num=int(number);
var f_num=number-int_num;
var __res_exp=exp(int_num);
var pw=1;
for(var i=1;i<6;i+=1)
{
__res_exp+=pw/i;
pw*=f_num;
}
return __res_exp;
}
var ln=func(number)
{
return number;
}
var sqrt=func(number)
{
var temp = number/8 + 0.5 + 2*number/(4+number);
var cnt = 10;
while(cnt!=0)
{
cnt-=1;
temp = (temp+number/temp)/2;
}
return temp;
}
var atan2=func(x,y)
{
return y/x;
}

View File

@ -0,0 +1,7 @@
#nasal-strict-lib thread
var thread=func(__function)
{
__system_call_cpp_new_thread(__function);
return 0;
}