This commit is contained in:
Valk Richard Li 2020-03-28 17:02:29 +08:00 committed by GitHub
parent 8dc04bf95d
commit c0f58cc2b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 396 additions and 97 deletions

View File

@ -10,9 +10,8 @@
# 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;
nasal_call_inline_push_back(vector,elements);
return nil;
}
# setsize
@ -22,9 +21,8 @@ var append=func(vector,elements...)
# 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;
nasal_call_inline_push_null(vector,__size);
return nil;
}
# subvec
@ -33,8 +31,7 @@ var setsize=func(vector,__size)
# 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);
return nasal_call_inline_subvec(vector,start,length);
}
# contains
@ -42,8 +39,7 @@ var subvec=func(vector,start,length=nil)
# 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);
return nasal_call_inline_contains(hash,key);
}
# delete
@ -53,8 +49,7 @@ var contains=func(hash,key)
# 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);
nasal_call_inline_delete(hash,key);
return;
}
@ -63,35 +58,28 @@ var delete=func(hash,key)
# 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);
return nasal_call_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);
return nasal_call_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);
return nasal_call_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;
return nasal_call_inline_pop_back(vector);
}
# size
@ -102,8 +90,7 @@ var pop=func(vector)
# Returns nil for number and nil arguments.
var size=func(object)
{
var call_inline_sizeof=func(object){};
return call_inline_sizeof(object);
return nasal_call_inline_sizeof(object);
}
# streq
@ -113,16 +100,14 @@ var size=func(object)
# 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);
return nasal_call_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);
return nasal_call_inline_cmp(__a,__b);
}
# sort
@ -132,8 +117,7 @@ var cmp=func(__a,__b)
# 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);
nasal_call_inline_cpp_sort(vector,function);
return;
}
@ -144,32 +128,28 @@ var sort=func(vector,function)
# 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);
return nasal_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);
return nasal_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);
return nasal_call_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);
return nasal_call_inline_split(delimeter,__string);
}
# rand
@ -179,8 +159,7 @@ var split=func(delimeter,__string)
# 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);
return nasal_call_inline_rand(seed);
}
# id
@ -189,6 +168,5 @@ var rand=func(seed=nil)
# 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);
return nasal_call_inline_get_id(thing);
}

View File

@ -14,27 +14,23 @@ var bits=
# The last bit is the low bit of the last byte in the string.
fld:func(__string,startbit,length)
{
var call_built_in_bitcalc=func(__str,__start,__len){};
return call_built_in_bitcalc(__string,startbit,length);
return nasal_call_built_in_bitcalc(__string,startbit,length);
},
# As bits.fld(), but interprets the result as a 2's complement signed value.
sfld:func(__string,startbit,length)
{
var call_built_in_sbitcalc=func(__str,__start,__len){};
return call_built_in_sbitcalc(__string,startbit,length);
return nasal_call_built_in_sbitcalc(__string,startbit,length);
},
# Sets the specified value into the bit string at the specified position.
# The string must be mutable: either the result of a runtime concatenation (the ~ operator) or a call to bits.buf()(see below).
# Attempts to modify immutable strings (e.g. compile time constants) will produce a runtime error.
setfld:func(__string,startbit,length,value)
{
var call_built_in_setbit=func(__str,__start,__len,__val){};
return call_built_in_setbit(__string,startbit,length,value);
return nasal_call_built_in_setbit(__string,startbit,length,value);
},
# Returns a zero-filled mutable string of the specified length.
buf:func(length)
{
var call_built_in_null_string_gen=func(__len){};
return call_built_in_null_string_gen(length);
return nasal_call_built_in_null_string_gen(length);
},
};

View File

@ -11,14 +11,12 @@ var io=
# Failures are thrown as runtime errors as per die().
open:func(filename,mode="r")
{
var call_c_fopen=func(__file,__mode){};
return call_c_fopen(filename,mode);
return nasal_call_inline_c_fopen(filename,mode);
},
# Closes the specified file as per ANSI fclose().
close:func(filehandle)
{
var call_c_fclose=func(__filehandle){};
call_c_fclose(filehandle);
nasal_call_inline_c_fclose(filehandle);
return;
},
# Attempts to read length bytes from the filehandle into the beginning of the mutable string buf.
@ -26,16 +24,14 @@ var io=
# Returns the number of bytes successfully read.
read:func(filehandle,buf,length)
{
var call_c_read=func(__filehandle,__buf,__len){};
return call_c_read(filehandle,buf,length);
return nasal_call_inline_c_read(filehandle,buf,length);
},
# Attempts to write the entirety of the specified string to the filehandle.
# Failures are thrown as runtime errors as per die().
# Returns the number of bytes successfully written.
write:func(filehandle,str)
{
var call_c_write=func(__filehandle,__str){};
return call_c_write(filehandle,str);
return nasal_call_inline_c_write(filehandle,str);
},
# As ANSI fseek().
# Attempts to seek to the specified position based on the whence value
@ -45,15 +41,13 @@ var io=
SEEK_END:3,
seek:func(filehandle,position,whence)
{
var call_c_seek=func(__filehandle,__position,__whence){};
call_c_seek(filehandle,position,whence);
nasal_call_inline_c_seek(filehandle,position,whence);
return;
},
# Returns the current seek position of the filehandle.
tell:func(filehandle)
{
var call_c_tell=func(__filehandle){};
return call_c_tell(filehandle);
return nasal_call_inline_c_tell(filehandle);
},
# Reads and returns a single text line from the filehandle.
# Interprets both "\n" and "\r\n" as end of line markers,
@ -61,8 +55,7 @@ var io=
# End offile or error is signaled by returning nil.
readln:func(filehandle)
{
var call_builtin_c_getline=func(__filehandle){};
return call_builtin_c_getline(filehandle);
return nasal_call_inline_builtin_c_getline(filehandle);
},
# Calls unix or win32 stat() on the specified file name and
# returns a seven element array whose contents are,
@ -70,15 +63,13 @@ var io=
# Errors are signaled as exceptions as per die().
stat:func(filename)
{
var call_builtin_stat=func(__filename){};
return call_builtin_stat(filename);
return nasal_call_inline_builtin_stat(filename);
},
};
var print=func(dyn...)
{
var __system_call_c_std_puts=func(__str){};
forindex(var i;dyn)
__system_call_c_std_puts(dyn[i]);
nasal_call_inline_c_std_puts(dyn[i]);
return nil;
};

View File

@ -13,40 +13,37 @@ var math=
# Returns the sine of the single argument
sin:func(x)
{
var call_inline_sin=func(__x){};
return call_inline_sin(x);
return nasal_call_inline_sin(x);
},
# Returns the cosine of the single argument
cos:func(x)
{
var call_inline_cos=func(__x){};
return call_inline_cos(x);
return nasal_call_inline_cos(x);
},
# you know what the f*ck this is
tan:func(x){return me.sin(x)/me.cos(x);},
tan:func(x)
{
return nasal_call_inline_tan(x);
},
# Returns e (Euler's constant) raised to the power specified by the single argument
exp:func(x)
{
var call_inline_pow=func(__num,__x){};
return call_inline_pow(me.e,x);
return nasal_call_inline_pow(me.e,x);
},
# Returns the natural logarithm of the single argument.
ln:func(x)
{
var call_inline_cpp_math_ln=func(__x){};
return call_inline_cpp_math_ln(x);
return nasal_call_inline_cpp_math_ln(x);
},
# Returns the square root of the single argument.
sqrt:func(x)
{
var call_inline_cpp_math_sqrt=func(__x){};
return call_inline_cpp_math_sqrt(x);
return nasal_call_inline_cpp_math_sqrt(x);
},
# Returns the arctangent of y/x, with the correct sign for the quadrant.
# Wraps the ANSI C function of the same name.
atan2:func(x,y)
{
var call_inline_cpp_atan2=func(__num1,__num2){};
return call_inline_cpp_atan2(x,y);
return nasal_call_inline_cpp_atan2(x,y);
},
};

View File

@ -20,4 +20,4 @@
#include "nasal_gc.h"
#include "nasal_runtime.h"
#endif
#endif

View File

@ -192,6 +192,7 @@ class gc_manager
if(!memory[addr].refcnt)
{
// if refcnt is 0,then starting the destructor
// std::cout<<">> [Gc] collected ";prt_hex(addr);std::cout<<std::endl;
memory[addr].collected=true;
switch(memory[addr].elem.get_type())
{
@ -203,6 +204,7 @@ class gc_manager
default:break;
}
memory[addr].elem.set_type(scalar_nil);
free_space.push_back(addr);
}
}
else

View File

@ -1,5 +1,41 @@
#ifndef __NASAL_RUNTIME_H__
#define __NASAL_RUNTIME_H__
#define nas_lib_func_num 29
std::string inline_func_name[nas_lib_func_num]=
{
//base.nas
"nasal_call_inline_push_null",
"nasal_call_inline_subvec",
"nasal_call_inline_contains",
"nasal_call_inline_delete",
"nasal_call_inline_trans_int",
"nasal_call_inline_trans_num",
"nasal_call_inline_get_keys",
"nasal_call_inline_pop_back",
"nasal_call_inline_sizeof",
"nasal_call_inline_str_cmp_equal",
"nasal_call_inline_cmp",
"nasal_call_inline_cpp_sort",
"nasal_call_inline_substr",
"nasal_call_inline_sprintf",
"nasal_call_inline_find_first_occur",
"nasal_call_inline_split",
"nasal_call_inline_rand",
"nasal_call_inline_get_id",
//bits.nas
"nasal_call_built_in_bitcalc",
"nasal_call_built_in_sbitcalc",
"nasal_call_built_in_setbit",
"nasal_call_built_in_null_string_gen",
//math.nas
"nasal_call_inline_sin",
"nasal_call_inline_cos",
"nasal_call_inline_tan",
"nasal_call_inline_pow",
"nasal_call_inline_cpp_math_ln",
"nasal_call_inline_cpp_math_sqrt",
"nasal_call_inline_cpp_atan2"
};
class nasal_runtime
{
@ -42,7 +78,7 @@ class nasal_runtime
__special_call_vector_negative_value,
__special_call_vector_too_large_value,
__normal_call_vector_too_large_value,
__error_call_type_when_getting_address,
__function_returned_value_be_assigned,
__call_function_lack_para,
__forindex_foreach_not_vector,
__break_not_used_in_loop,
@ -67,6 +103,7 @@ class nasal_runtime
int conditional (std::list<std::map<std::string,int> >&,abstract_syntax_tree&);// checked
int block_proc (std::list<std::map<std::string,int> >&,abstract_syntax_tree&);// checked
int func_proc (std::list<std::map<std::string,int> >&,abstract_syntax_tree&,abstract_syntax_tree&,abstract_syntax_tree&,int);// checked
int inline_function (std::list<std::map<std::string,int> >&,abstract_syntax_tree&,int);
public:
nasal_runtime()
{
@ -124,8 +161,8 @@ void nasal_runtime::error_interrupt(const int type,const int line)
std::cout<<"the number used to call the sub-vector is too large(over 0x7fffffff)."<<std::endl;break;
case __normal_call_vector_too_large_value:
std::cout<<"the number used to call the vector is too large(over 0x7fffffff)."<<std::endl;break;
case __error_call_type_when_getting_address:
std::cout<<"this type of calling identifier is not allowed here."<<std::endl;break;
case __function_returned_value_be_assigned:
std::cout<<"cannot assigned a value that function returns."<<std::endl;break;
case __call_function_lack_para:
std::cout<<"lack parameter(s) when calling a function."<<std::endl;break;
case __forindex_foreach_not_vector:
@ -1799,10 +1836,296 @@ int nasal_runtime::calculation(std::list<std::map<std::string,int> >& local_scop
}
int nasal_runtime::assignment(std::list<std::map<std::string,int> >& local_scope,abstract_syntax_tree& node,int data_addr)
{
int ret_addr=-1;
return ret_addr;
int* assigned_addr=NULL;
std::string tmp_id_name=node.get_var_name();
if(global_scope.find(tmp_id_name)!=global_scope.end())
assigned_addr=&(global_scope[tmp_id_name]);
for(std::list<std::map<std::string,int> >::iterator iter=local_scope.begin();iter!=local_scope.end();++iter)
if(iter->find(tmp_id_name)!=iter->end())
assigned_addr=&((*iter)[tmp_id_name]);
if(!assigned_addr)
return -1;
int assigned_value_addr=*assigned_addr;
for(std::list<abstract_syntax_tree>::iterator iter=node.get_children().begin();iter!=node.get_children().end();++iter)
{
// call vector/special call hash/subvec
// the special type of calling hash like a["name"] is also generated as calling vector
if(iter->get_node_type()==__call_vector)
{
// check the scalar type of called identifier here
int called_type=nasal_gc.get_scalar(assigned_value_addr).get_type();
if(called_type!=scalar_vector && called_type!=scalar_hash)
{
error_interrupt(__error_value_type,iter->get_node_line());
return -1;
}
if(iter->get_children().front().get_node_type()==__sub_vector)
{
if(called_type==scalar_hash)
{
error_interrupt(__not_callable_vector,iter->get_node_line());
return -1;
}
int num1_addr=-1;
int num2_addr=-1;
// identifier[num1:];
if(iter->get_children().front().get_children().size()==1)
{
num1_addr=calculation(local_scope,iter->get_children().front().get_children().front());
if(num1_addr<0)
return -1;
}
// identifier[num1:num2];
else
{
num1_addr=calculation(local_scope,iter->get_children().front().get_children().front());
num2_addr=calculation(local_scope,iter->get_children().front().get_children().back());
if(num1_addr<0 || num2_addr<0)
return -1;
}
if(nasal_gc.get_scalar(num1_addr).get_type()!=scalar_number
&& nasal_gc.get_scalar(num1_addr).get_type()!=scalar_string)
{
error_interrupt(__error_value_type_when_calling_vector,iter->get_children().front().get_children().front().get_node_line());
return -1;
}
if(num2_addr>=0
&& (nasal_gc.get_scalar(num2_addr).get_type()!=scalar_number
&& nasal_gc.get_scalar(num2_addr).get_type()!=scalar_string
&& nasal_gc.get_scalar(num2_addr).get_type()!=scalar_nil))
{
error_interrupt(__error_value_type_when_calling_vector,iter->get_children().front().get_children().back().get_node_line());
return -1;
}
if(nasal_gc.get_scalar(num1_addr).get_type()==scalar_string)
{
if(check_numerable_string(nasal_gc.get_scalar(num1_addr).get_string().get_string()))
{
double tmp_num=trans_string_to_number(nasal_gc.get_scalar(num1_addr).get_string().get_string());
if(tmp_num<0)
{
error_interrupt(__special_call_vector_negative_value,iter->get_children().front().get_children().front().get_node_line());
return -1;
}
nasal_gc.reference_delete(num1_addr);
num1_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(num1_addr).set_type(scalar_number);
nasal_gc.get_scalar(num1_addr).get_number().set_number(tmp_num);
}
else
{
error_interrupt(__not_numerable_str,iter->get_children().front().get_children().front().get_node_line());
return -1;
}
}
if(num2_addr>=0 && nasal_gc.get_scalar(num2_addr).get_type()==scalar_string)
{
if(check_numerable_string(nasal_gc.get_scalar(num2_addr).get_string().get_string()))
{
double tmp_num=trans_string_to_number(nasal_gc.get_scalar(num2_addr).get_string().get_string());
if(tmp_num<0)
{
error_interrupt(__special_call_vector_negative_value,iter->get_children().front().get_children().back().get_node_line());
return -1;
}
nasal_gc.reference_delete(num2_addr);
num2_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(num2_addr).set_type(scalar_number);
nasal_gc.get_scalar(num2_addr).get_number().set_number(tmp_num);
}
else
{
error_interrupt(__not_numerable_str,iter->get_children().front().get_children().back().get_node_line());
return -1;
}
}
if(nasal_gc.get_scalar(num1_addr).get_number().get_number()>2147483647)
{
error_interrupt(__special_call_vector_too_large_value,iter->get_children().front().get_children().front().get_node_line());
return -1;
}
if(num2_addr>=0 && nasal_gc.get_scalar(num2_addr).get_number().get_number()>2147483647)
{
error_interrupt(__special_call_vector_too_large_value,iter->get_children().front().get_children().back().get_node_line());
return -1;
}
int begin_num=(int)nasal_gc.get_scalar(num1_addr).get_number().get_number();
int end_num=0;
if(num2_addr<0 || nasal_gc.get_scalar(num2_addr).get_type()==scalar_nil)
end_num=nasal_gc.get_scalar(assigned_value_addr).get_vector().get_size();
else
end_num=(int)nasal_gc.get_scalar(num2_addr).get_number().get_number();
if(num1_addr>=0)
nasal_gc.reference_delete(num1_addr);
if(num2_addr>=0)
nasal_gc.reference_delete(num2_addr);
std::vector<int> subvec_result;
for(int i=begin_num;i<end_num;++i)
{
// addr used here
int tmp_data_addr=nasal_gc.get_scalar(assigned_value_addr).get_vector().get_elem(i);
int new_addr=-1;
if(tmp_data_addr<0)
{
error_interrupt(__invalid_vector_member,iter->get_children().front().get_children().front().get_node_line());
return -1;
}
switch(nasal_gc.get_scalar(tmp_data_addr).get_type())
{
case scalar_nil:
new_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(new_addr).set_type(scalar_nil);
break;
case scalar_number:
new_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(new_addr).set_type(scalar_number);
nasal_gc.get_scalar(new_addr).get_number().deep_copy(nasal_gc.get_scalar(tmp_data_addr).get_number());
break;
case scalar_string:
new_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(new_addr).set_type(scalar_string);
nasal_gc.get_scalar(new_addr).get_string().deep_copy(nasal_gc.get_scalar(tmp_data_addr).get_string());
break;
case scalar_function:
new_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(new_addr).set_type(scalar_function);
nasal_gc.get_scalar(new_addr).get_function().deep_copy(nasal_gc.get_scalar(tmp_data_addr).get_function());
break;
case scalar_vector:
case scalar_hash:
new_addr=tmp_data_addr;
nasal_gc.reference_add(new_addr);
break;
}
nasal_gc.reference_delete(tmp_data_addr);
subvec_result.push_back(new_addr);
}
int tmp_addr=assigned_value_addr;
assigned_value_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(assigned_value_addr).set_type(scalar_vector);
for(int i=0;i<subvec_result.size();++i)
nasal_gc.get_scalar(assigned_value_addr).get_vector().vec_push(subvec_result[i]);
nasal_gc.reference_delete(tmp_addr);
assigned_addr=NULL;
}// end sub-vector
else
{
// normal vector/hash calling
int data_addr=calculation(local_scope,iter->get_children().front());
if(data_addr<0)
return -1;
if(nasal_gc.get_scalar(data_addr).get_type()!=scalar_number && nasal_gc.get_scalar(data_addr).get_type()!=scalar_string)
{
error_interrupt(__error_value_type_when_calling_vector,iter->get_children().front().get_node_line());
return -1;
}
if(called_type==scalar_vector)
{
double place_num=0;
if(nasal_gc.get_scalar(data_addr).get_type()==scalar_string)
{
if(check_numerable_string(nasal_gc.get_scalar(data_addr).get_string().get_string()))
place_num=(int)trans_string_to_number(nasal_gc.get_scalar(data_addr).get_string().get_string());
else
{
error_interrupt(__not_numerable_str,iter->get_children().front().get_node_line());
return -1;
}
}
if(place_num>2147483647 || place_num<-2147483648)
{
error_interrupt(__normal_call_vector_too_large_value,iter->get_children().front().get_node_line());
return -1;
}
int tmp_addr=assigned_value_addr;
assigned_addr=nasal_gc.get_scalar(tmp_addr).get_vector().get_elem_addr((int)place_num);
if(!assigned_addr)
{
error_interrupt(__invalid_vector_member,iter->get_children().front().get_children().front().get_node_line());
return -1;
}
assigned_value_addr=*assigned_addr;
nasal_gc.reference_add(assigned_value_addr);
nasal_gc.reference_delete(tmp_addr);
}
else if(called_type==scalar_hash)
{
if(nasal_gc.get_scalar(data_addr).get_type()!=scalar_string)
{
error_interrupt(__error_value_type_when_calling_hash,iter->get_children().front().get_node_line());
return -1;
}
int tmp_addr=assigned_value_addr;
assigned_addr=nasal_gc.get_scalar(tmp_addr).get_hash().get_hash_member_addr(nasal_gc.get_scalar(data_addr).get_string().get_string());
if(!assigned_addr)
{
error_interrupt(__invalid_hash_member,iter->get_children().front().get_node_line());
return -1;
}
assigned_value_addr=*assigned_addr;
nasal_gc.reference_add(assigned_value_addr);
nasal_gc.reference_delete(tmp_addr);
}
nasal_gc.reference_delete(data_addr);
}
}// end call vector
// call hash identifier.identifier
else if(iter->get_node_type()==__call_hash)
{
if(nasal_gc.get_scalar(assigned_value_addr).get_type()!=scalar_hash)
{
error_interrupt(__not_callable_hash,iter->get_node_line());
return -1;
}
int tmp_addr=assigned_value_addr;
assigned_addr=nasal_gc.get_scalar(assigned_value_addr).get_hash().get_hash_member_addr(iter->get_var_name());
if(!assigned_addr)
{
error_interrupt(__invalid_hash_member,iter->get_node_line());
return -1;
}
assigned_value_addr=*assigned_addr;
nasal_gc.reference_add(assigned_value_addr);
nasal_gc.reference_delete(tmp_addr);
}// end call hash
// call function identifier(...)
else if(iter->get_node_type()==__call_function)
{
error_interrupt(__function_returned_value_be_assigned,iter->get_node_line());
return -1;
}
}
switch(nasal_gc.get_scalar(data_addr).get_type())
{
case scalar_nil:
*assigned_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(*assigned_addr).set_type(scalar_nil);
break;
case scalar_number:
*assigned_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(*assigned_addr).set_type(scalar_number);
nasal_gc.get_scalar(*assigned_addr).get_number().deep_copy(nasal_gc.get_scalar(data_addr).get_number());
break;
case scalar_string:
*assigned_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(*assigned_addr).set_type(scalar_string);
nasal_gc.get_scalar(*assigned_addr).get_string().deep_copy(nasal_gc.get_scalar(data_addr).get_string());
break;
case scalar_function:
*assigned_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(*assigned_addr).set_type(scalar_function);
nasal_gc.get_scalar(*assigned_addr).get_function().deep_copy(nasal_gc.get_scalar(data_addr).get_function());
break;
case scalar_vector:
case scalar_hash:
*assigned_addr=data_addr;
nasal_gc.reference_add(data_addr);
break;
}
nasal_gc.reference_delete(assigned_value_addr);
// data_addr is only a parameter here,and it's refcnt has not been changed when using it here
nasal_gc.reference_add(*assigned_addr);
return *assigned_addr;
}
int nasal_runtime::call_identifier(std::list<std::map<std::string,int> >& local_scope,abstract_syntax_tree& node)
{
@ -1814,12 +2137,16 @@ int nasal_runtime::call_identifier(std::list<std::map<std::string,int> >& local_
if(iter->find(tmp_id_name)!=iter->end())
addr=(*iter)[tmp_id_name];
if(addr<0)
{
for(int i=0;i<nas_lib_func_num;++i)
if(inline_func_name[i]==tmp_id_name)
addr=inline_function(local_scope,node,-1);
}
if(addr<0)
{
error_interrupt(__undefined_identifier,node.get_node_line());
return -1;
}
//addr refcnt+1
nasal_gc.reference_add(addr);
int last_hash_addr=-1;
for(std::list<abstract_syntax_tree>::iterator iter=node.get_children().begin();iter!=node.get_children().end();++iter)
{
@ -2288,7 +2615,9 @@ int nasal_runtime::loop_expr(std::list<std::map<std::string,int> >& local_scope,
for(int i=0;i<nasal_gc.get_scalar(vec_addr).get_vector().get_size();++i)
{
int now_step_elem_addr=nasal_gc.get_scalar(vec_addr).get_vector().get_elem(i);
assignment(local_scope,assignment_ast,now_step_elem_addr);
int tmp_val=assignment(local_scope,assignment_ast,now_step_elem_addr);
if(tmp_val>=0)
nasal_gc.reference_delete(tmp_val);
int state=block_proc(local_scope,*iter);
if(state==__state_break)
break;
@ -2301,6 +2630,7 @@ int nasal_runtime::loop_expr(std::list<std::map<std::string,int> >& local_scope,
else if(state==__state_no_operation)
;
}
nasal_gc.reference_delete(vec_addr);
}
else if(loop_type==__forindex)
{
@ -2335,7 +2665,9 @@ int nasal_runtime::loop_expr(std::list<std::map<std::string,int> >& local_scope,
int tmp_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(tmp_addr).set_type(scalar_number);
nasal_gc.get_scalar(tmp_addr).get_number().set_number((double)i);
assignment(local_scope,assignment_ast,tmp_addr);
int tmp_val=assignment(local_scope,assignment_ast,tmp_addr);
if(tmp_val>=0)
nasal_gc.reference_delete(tmp_val);
nasal_gc.reference_delete(tmp_addr);
int state=block_proc(local_scope,*iter);
if(state==__state_break)
@ -2349,6 +2681,7 @@ int nasal_runtime::loop_expr(std::list<std::map<std::string,int> >& local_scope,
else if(state==__state_no_operation)
;
}
nasal_gc.reference_delete(vec_addr);
}
else if(loop_type==__for)
{
@ -2365,6 +2698,7 @@ int nasal_runtime::loop_expr(std::list<std::map<std::string,int> >& local_scope,
while(check_condition(local_scope,*condition_iterator))
{
int state=block_proc(local_scope,*block_proc_iterator);
if(state==__state_break)
break;
else if(state==__state_continue)
@ -2692,13 +3026,12 @@ void nasal_runtime::main_proc(abstract_syntax_tree& root)
{
time_t begin_time,end_time;
begin_time=std::time(NULL);
// initializing global scope and nasal_gc
// runtime_error_exit_mark is set to -1,if runtime_error_exit_mark >=0,this means an error occurred
global_scope.clear();
main_local_scope.clear();
nasal_gc.gc_init();
runtime_error_exit_mark=-1;
if(root.get_node_type()!=__root)
{
error_interrupt(__incorrect_head_of_tree,root.get_node_line());
@ -2772,9 +3105,11 @@ void nasal_runtime::main_proc(abstract_syntax_tree& root)
else if(state==__state_error)
break;
}
end_time=std::time(NULL);
std::cout<<">> [Runtime] process exited after "<<end_time-begin_time<<" s ."<<std::endl;
global_scope.clear();
main_local_scope.clear();
nasal_gc.gc_init();
return;
}