diff --git a/version1.1/ast.h b/version1.1/ast.h new file mode 100644 index 0000000..ac12ba0 --- /dev/null +++ b/version1.1/ast.h @@ -0,0 +1,407 @@ +#ifndef __NASAL_ABSTRACT_SYNTAX_TREE_RUN_H__ +#define __NASAL_ABSTRACT_SYNTAX_TREE_RUN_H__ + +class abstract_syntax_tree +{ + protected: + int ast_node_type; + std::list statement_list; + std::list children; + + // for definition and assignment + std::list var_list; + std::list var_changed_list; + std::list var_content_list; + + // for number and string leaf + double var_number; + std::string var_string; + std::string id_name; + public: + abstract_syntax_tree() + { + ast_node_type=0; + statement_list.clear(); + children.clear(); + + var_list.clear(); + var_changed_list.clear(); + var_content_list.clear(); + + var_number=0; + var_string=""; + id_name="ukn"; + } + abstract_syntax_tree(const abstract_syntax_tree& temp) + { + ast_node_type=temp.ast_node_type; + if(temp.statement_list.empty()) + statement_list.clear(); + else + statement_list=temp.statement_list; + if(temp.children.empty()) + children.clear(); + else + children=temp.children; + if(temp.var_list.empty()) + var_list.clear(); + else + var_list=temp.var_list; + if(temp.var_changed_list.empty()) + var_changed_list.clear(); + else + var_changed_list=temp.var_changed_list; + if(temp.var_content_list.empty()) + var_content_list.clear(); + else + var_content_list=temp.var_content_list; + var_number=temp.var_number; + var_string=temp.var_string; + id_name=temp.id_name; + } + abstract_syntax_tree& operator=(const abstract_syntax_tree temp) + { + ast_node_type=temp.ast_node_type; + if(temp.statement_list.empty()) + statement_list.clear(); + else + statement_list=temp.statement_list; + if(temp.children.empty()) + children.clear(); + else + children=temp.children; + if(temp.var_list.empty()) + var_list.clear(); + else + var_list=temp.var_list; + if(temp.var_changed_list.empty()) + var_changed_list.clear(); + else + var_changed_list=temp.var_changed_list; + if(temp.var_content_list.empty()) + var_content_list.clear(); + else + var_content_list=temp.var_content_list; + var_number=temp.var_number; + var_string=temp.var_string; + id_name=temp.id_name; + return *this; + } + void tree_set_clear() + { + statement_list.clear(); + children.clear(); + var_list.clear(); + var_changed_list.clear(); + var_content_list.clear(); + return; + } + void print_ast_node(int tab_num) + { + std::string indent=""; + for(int i=0;i::iterator i=var_list.begin();i!=var_list.end();++i) + i->print_ast_node(tab_num+1); + std::cout<::iterator i=var_content_list.begin();i!=var_content_list.end();++i) + i->print_ast_node(tab_num+1); + return; + } + std::cout<<"{ Type:"; + print_token(ast_node_type); + std::cout<::iterator i=children.begin();i!=children.end();++i) + i->print_ast_node(tab_num+1); + } + if(!statement_list.empty()) + { + std::cout<::iterator i=statement_list.begin();i!=statement_list.end();++i) + i->print_ast_node(tab_num+1); + } + std::cout< name_list,std::list var_content) + { + ast_node_type=__definition; + var_list=name_list; + var_content_list=var_content; + return; + } + // for assignment + void set_assignment_expr(std::list to_be_changed,std::list var_content) + { + ast_node_type=__assignment; + var_changed_list=to_be_changed; + var_content_list=var_content; + return; + } + // for choose block + void set_choose_block(std::list if_else_statements) + { + ast_node_type=__ifelse; + statement_list=if_else_statements; + return; + } + // for if + void set_if_expr(abstract_syntax_tree condition,abstract_syntax_tree statements) + { + ast_node_type=__if; + children.push_back(condition); + children.push_back(statements); + return; + } + // for elsif + void set_elsif_expr(abstract_syntax_tree condition,abstract_syntax_tree statements) + { + ast_node_type=__elsif; + children.push_back(condition); + children.push_back(statements); + return; + } + // for else + void set_else_expr(abstract_syntax_tree statements) + { + ast_node_type=__else; + children.push_back(statements); + return; + } + // for statement block + void set_block() + { + ast_node_type=__block; + return; + } + void add_statement(abstract_syntax_tree statement_head) + { + statement_list.push_back(statement_head); + return; + } + // for return expr + void set_node_to_ret(abstract_syntax_tree child) + { + ast_node_type=__return; + children.push_back(child); + return; + } + // for loop + void set_node_to_loop() + { + ast_node_type=__loop; + return; + } + void set_node_to_continue_break(const int type) + { + ast_node_type=type; + return; + } + // for function expr + void set_node_to_function(std::list parameters,abstract_syntax_tree block) + { + ast_node_type=__func; + children=parameters; + statement_list=block.statement_list; + return; + } + // for sub-tree node operator + void set_two_operator(const int operator_type,abstract_syntax_tree f_child,abstract_syntax_tree s_child) + { + ast_node_type=operator_type; + children.push_back(f_child); + children.push_back(s_child); + return; + } + void set_one_operator(const int operator_type,abstract_syntax_tree f_child) + { + ast_node_type=operator_type; + children.push_back(f_child); + return; + } + // for leaf node call + void set_node_to_call_function() + { + ast_node_type=__call_function; + return; + } + void set_node_to_list_search() + { + ast_node_type=__list_search; + return; + } + void set_node_to_hash_search() + { + ast_node_type=__hash_search; + return; + } + // for leaf node list + void set_node_to_list(std::list list_members) + { + ast_node_type=__list; + children=list_members; + return; + } + // for leaf node hash + void set_node_to_hash(std::list hash_members) + { + ast_node_type=__hash; + children=hash_members; + return; + } + // for hash member + void set_node_to_hashmember(std::string& name,abstract_syntax_tree mem_var) + { + ast_node_type=__hash_member; + id_name=name; + children.clear(); + children.push_back(mem_var); + return; + } + // for leaf node hash + void set_node_to_hash() + { + ast_node_type=__hash; + return; + } + // for identifier node + void set_node_to_id(std::string& name) + { + ast_node_type=__id; + id_name=name; + return; + } + // for dynamic identifier + void set_node_to_dynid(std::string& name) + { + ast_node_type=__dynamic_id; + id_name=""; + for(int i=0;i<(int)name.length()-3;++i) + id_name+=name[i]; + return; + } + // for leaf node string + void set_node_to_string(std::string& str) + { + ast_node_type=__string; + var_string=str; + return; + } + std::string get_string() + { + return var_string; + } + // for leaf node number + void set_node_to_number(std::string& str) + { + ast_node_type=__number; + if(str=="nil") + { + var_number=0; + return; + } + if((int)str.length()>2 && (str[1]=='x' || str[1]=='o')) + { + if(str[1]=='x') + { + int num=0; + int pw=1; + for(int i=(int)str.length()-1;i>1;--i) + { + if('0'<=str[i] && str[i]<='9') + num+=(str[i]-'0')*pw; + else if('a'<=str[i] && str[i]<='f') + num+=(10+str[i]-'a')*pw; + else if('A'<=str[i] && str[i]<='F') + num+=(10+str[i]-'A')*pw; + pw<<=4; + } + var_number=(double)num; + } + else + { + int num=0; + int pw=1; + for(int i=(int)str.length()-1;i>1;--i) + { + num+=(str[i]-'0')*pw; + pw<<=3; + } + var_number=(double)num; + } + return; + } + int dot_place=-1; + for(int i=0;i<(int)str.length();++i) + if(str[i]=='.') + { + dot_place=i; + break; + } + if(dot_place==-1) + { + var_number=0; + double pw=1; + for(int i=(int)str.length()-1;i>=0;--i) + { + var_number+=(str[i]-'0')*pw; + pw*=10; + } + } + else + { + var_number=0; + double pw=0.1; + for(int i=dot_place+1;i<(int)str.length();++i) + { + var_number+=(str[i]-'0')*pw; + pw/=10; + } + pw=1; + for(int i=dot_place-1;i>=0;--i) + { + var_number+=(str[i]-'0')*pw; + pw*=10; + } + } + return; + } + double get_number() + { + return var_number; + } +}; + + +#endif diff --git a/version1.1/ast_generator.h b/version1.1/ast_generator.h new file mode 100644 index 0000000..41a523f --- /dev/null +++ b/version1.1/ast_generator.h @@ -0,0 +1,1112 @@ +#ifndef __AST_GENERATOR_H__ +#define __AST_GENERATOR_H__ + +class generator +{ + private: + abstract_syntax_tree root; + std::stack parse; + token this_token; + public: + generator() + { + this_token.type=0; + root.set_root(); + } + void get_token() + { + if(parse.empty()) + { + this_token.type=0; + return; + } + this_token=parse.top(); + parse.pop(); + return; + } + void set_ast_empty() + { + root.tree_set_clear(); + return; + } + void gen_process(std::list& lexer) + { + while(!parse.empty()) + parse.pop(); + std::stack temp; + for(std::list::iterator i=lexer.begin();i!=lexer.end();++i) + { + token temp_parse; + temp_parse=*i; + temp.push(temp_parse);//push this into stack + } + if(temp.empty()) + { + std::cout<<">>[Abstract-syntax-tree] warning: empty lexer list."<>\n[Abstract-syntax-tree]"< parameters; + get_token(); + if(this_token.type==__left_brace) + parse.push(this_token); + else if(this_token.type==__left_curve) + { + get_token(); + while(this_token.type!=__right_curve) + { + if(this_token.type==__id) + { + abstract_syntax_tree param; + param.set_node_to_id(this_token.content); + parameters.push_back(param); + get_token(); + if(this_token.type==__right_curve) + parse.push(this_token); + } + else if(this_token.type==__dynamic_id) + { + abstract_syntax_tree param; + param.set_node_to_dynid(this_token.content); + parameters.push_back(param); + get_token(); + parse.push(this_token); + } + get_token(); + } + } + node.set_node_to_function(parameters,statements_block()); + return node; +} +abstract_syntax_tree generator::list_generate_expr() +{ + abstract_syntax_tree node; + std::list list_members; + get_token(); + while(this_token.type!=__right_bracket) + { + switch(this_token.type) + { + case __number:list_members.push_back(number_begin_expr());break; + case __string:list_members.push_back(string_begin_expr());break; + case __id:list_members.push_back(identifier_begin_expr());break; + case __left_bracket:list_members.push_back(list_generate_expr());break; + case __left_brace:list_members.push_back(hash_generate_expr());break; + case __left_curve:list_members.push_back(in_curve_calc_expr());break; + default:break; + } + get_token(); + if(this_token.type==__comma) + get_token(); + } + node.set_node_to_list(list_members); + return node; +} +abstract_syntax_tree generator::hash_generate_expr() +{ + abstract_syntax_tree node; + std::list hashmember; + abstract_syntax_tree elem; + std::string id_name; + get_token(); + while(this_token.type!=__right_brace) + { + switch(this_token.type) + { + // case __number:number_begin_expr();break; + // case __string:string_begin_expr();break; + case __id:id_name=this_token.content;break; + default:break; + } + get_token(); + get_token(); + switch(this_token.type) + { + case __number:elem.set_node_to_hashmember(id_name,number_begin_expr());break; + case __string:elem.set_node_to_hashmember(id_name,string_begin_expr());break; + case __id:elem.set_node_to_hashmember(id_name,identifier_begin_expr());break; + case __func:elem.set_node_to_hashmember(id_name,function_generate_expr());break; + case __left_bracket:elem.set_node_to_hashmember(id_name,list_generate_expr());break; + case __left_brace:elem.set_node_to_hashmember(id_name,hash_generate_expr());break; + case __left_curve:elem.set_node_to_hashmember(id_name,in_curve_calc_expr());break; + default:break; + } + hashmember.push_back(elem); + get_token(); + if(this_token.type==__comma) + get_token(); + } + node.set_node_to_hash(hashmember); + return node; +} +abstract_syntax_tree generator::definition_expr() +{ + abstract_syntax_tree node; + abstract_syntax_tree id; + std::list name_list; + std::list var_content; + + get_token(); + if(this_token.type==__left_curve) + { + while(this_token.type!=__right_curve) + { + get_token(); + id.set_node_to_id(this_token.content); + name_list.push_back(id); + get_token(); + if(this_token.type==__right_curve) + break; + } + } + else if(this_token.type==__id) + { + id.set_node_to_id(this_token.content); + name_list.push_back(id); + } + get_token(); + if(this_token.type==__semi) + { + parse.push(this_token); + node.set_definition_expr(name_list,var_content); + return node; + } + get_token(); + token t; + t.line=this_token.line; + t.type=__semi; + + switch(this_token.type) + { + case __number:var_content.push_back(number_begin_expr());break; + case __string:var_content.push_back(string_begin_expr());break; + case __id:var_content.push_back(identifier_begin_expr());break; + case __func:var_content.push_back(function_generate_expr());parse.push(t);break; + case __add_operator: + case __sub_operator: + case __nor_operator:var_content.push_back(one_operator_expr());break; + case __left_bracket:var_content.push_back(list_generate_expr());break; + case __left_brace:var_content.push_back(hash_generate_expr());break; + case __left_curve:var_content.push_back(in_curve_calc_expr());break; + default:break; + } + node.set_definition_expr(name_list,var_content); + return node; +} +abstract_syntax_tree generator::assignment_expr() +{ + abstract_syntax_tree node; + std::list to_be_changed; + std::list var_content; + get_token(); + switch(this_token.type) + { + case __number:number_begin_expr();break; + case __string:string_begin_expr();break; + case __id:identifier_begin_expr();break; + case __func:function_generate_expr();break; + case __add_operator: + case __sub_operator: + case __nor_operator:one_operator_expr();break; + case __left_bracket:list_generate_expr();break; + case __left_brace:hash_generate_expr();break; + case __left_curve:in_curve_calc_expr();break; + default:break; + } + node.set_assignment_expr(to_be_changed,var_content); + return node; +} +bool generator::else_if_check() +{ + token temp=this_token; + if(this_token.type!=__else) + return false; + else + { + get_token(); + if(this_token.type!=__if) + { + parse.push(this_token); + this_token=temp;// to avoid when recognizing 'else' without 'if' + + return false; + } + } + return true; +} +abstract_syntax_tree generator::if_else_expr() +{ + abstract_syntax_tree node; + get_token(); + get_token(); + get_token(); + switch(this_token.type) + { + case __add_operator: + case __sub_operator: + case __nor_operator:one_operator_expr();break; + case __number:number_begin_expr();break; + case __string:string_begin_expr();break; + case __id:identifier_begin_expr();break; + case __left_curve:in_curve_calc_expr();break; + default:break; + } + get_token(); + get_token(); + if(this_token.type==__left_brace) + { + // if without { then only one statement is behind it + parse.push(this_token); + statements_block(); + } + else + { + token t; + switch(this_token.type) + { + case __var:definition_expr();check_semi_at_end();break; + case __id:identifier_begin_expr();check_semi_at_end();break; + case __number:number_begin_expr();check_semi_at_end();break; + case __string:string_begin_expr();check_semi_at_end();break; + case __if:parse.push(this_token);if_else_expr();break; + case __left_curve: + t=this_token; + get_token(); + if(this_token.type==__var) + { + parse.push(t); + definition_expr(); + } + else + { + in_curve_calc_expr(); + } + check_semi_at_end(); + break; + case __add_operator: + case __sub_operator: + case __nor_operator:one_operator_expr();break; + case __while: + case __for: + case __foreach: + case __forindex:parse.push(this_token);loop_expr();break; + case __continue: + case __break:check_semi_at_end();break; + case __semi:break; + case __return:return_expr();check_semi_at_end();break; + default:break; + } + } + if(parse.empty()) + return node; + get_token(); + while(this_token.type==__elsif || else_if_check()) + { + get_token(); + get_token(); + switch(this_token.type) + { + case __add_operator: + case __sub_operator: + case __nor_operator:one_operator_expr();break; + case __number:number_begin_expr();break; + case __string:string_begin_expr();break; + case __id:identifier_begin_expr();break; + default:break; + } + get_token(); + get_token(); + if(this_token.type==__left_brace) + { + // if without { then only one statement is behind it + parse.push(this_token); + statements_block(); + } + else + { + token t; + switch(this_token.type) + { + case __var:definition_expr();check_semi_at_end();break; + case __id:identifier_begin_expr();check_semi_at_end();break; + case __number:number_begin_expr();check_semi_at_end();break; + case __string:string_begin_expr();check_semi_at_end();break; + case __if:parse.push(this_token);if_else_expr();break; + case __left_curve: + t=this_token; + get_token(); + if(this_token.type==__var) + { + parse.push(t); + definition_expr(); + } + else + { + in_curve_calc_expr(); + } + check_semi_at_end(); + break; + case __add_operator: + case __sub_operator: + case __nor_operator:one_operator_expr();break; + case __while: + case __for: + case __foreach: + case __forindex:parse.push(this_token);loop_expr();break; + case __continue: + case __break:check_semi_at_end();break; + case __semi:break; + case __return:return_expr();check_semi_at_end();break; + default:break; + } + } + if(parse.empty()) + return node; + get_token(); + } + if(this_token.type==__else) + { + get_token(); + if(this_token.type==__left_brace) + { + // if without { then only one statement is behind it + parse.push(this_token); + statements_block(); + } + else + { + token t; + switch(this_token.type) + { + case __var:definition_expr();check_semi_at_end();break; + case __id:identifier_begin_expr();check_semi_at_end();break; + case __number:number_begin_expr();check_semi_at_end();break; + case __string:string_begin_expr();check_semi_at_end();break; + case __if:parse.push(this_token);if_else_expr();break; + case __left_curve: + t=this_token; + get_token(); + if(this_token.type==__var) + { + parse.push(t); + definition_expr(); + } + else + { + in_curve_calc_expr(); + } + check_semi_at_end(); + break; + case __add_operator: + case __sub_operator: + case __nor_operator:one_operator_expr();break; + case __while: + case __for: + case __foreach: + case __forindex:parse.push(this_token);loop_expr();break; + case __continue: + case __break:check_semi_at_end();break; + case __semi:break; + case __return:return_expr();check_semi_at_end();break; + default:break; + } + } + } + else + parse.push(this_token); + return node; +} +abstract_syntax_tree generator::loop_expr() +{ + abstract_syntax_tree node; + get_token(); + if(this_token.type==__while) + { + get_token(); + get_token(); + switch(this_token.type) + { + case __add_operator: + case __sub_operator: + case __nor_operator:one_operator_expr();break; + case __number:number_begin_expr();break; + case __string:string_begin_expr();break; + case __id:identifier_begin_expr();break; + case __left_curve:in_curve_calc_expr();break; + default:break; + } + get_token(); + get_token(); + if(this_token.type==__left_brace) + { + // if without { then only one statement is behind it + parse.push(this_token); + statements_block(); + } + else + { + token t; + switch(this_token.type) + { + case __var:definition_expr();check_semi_at_end();break; + case __id:identifier_begin_expr();check_semi_at_end();break; + case __number:number_begin_expr();check_semi_at_end();break; + case __string:string_begin_expr();check_semi_at_end();break; + case __if:parse.push(this_token);if_else_expr();break; + case __left_curve: + t=this_token; + get_token(); + if(this_token.type==__var) + { + parse.push(t); + definition_expr(); + } + else + { + in_curve_calc_expr(); + } + check_semi_at_end(); + break; + case __add_operator: + case __sub_operator: + case __nor_operator:one_operator_expr();break; + case __while: + case __for: + case __foreach: + case __forindex:parse.push(this_token);loop_expr();break; + case __continue: + case __break:check_semi_at_end();break; + case __semi:break; + case __return:return_expr();check_semi_at_end();break; + default:break; + } + } + } + else if(this_token.type==__for) + { + get_token(); + get_token(); + switch(this_token.type) + { + case __var:definition_expr();break; + case __id:identifier_begin_expr();break; + case __semi:parse.push(this_token);break; + default:break; + } + check_semi_at_end(); + get_token(); + switch(this_token.type) + { + case __add_operator: + case __sub_operator: + case __nor_operator:one_operator_expr();break; + case __id:identifier_begin_expr();break; + case __number:number_begin_expr();break; + case __string:string_begin_expr();break; + case __left_curve:in_curve_calc_expr();break; + case __semi:parse.push(this_token);break; + default:break; + } + check_semi_at_end(); + get_token(); + switch(this_token.type) + { + case __id:identifier_begin_expr();break; + case __number:number_begin_expr();break; + case __string:string_begin_expr();break; + case __left_curve:in_curve_calc_expr();break; + case __right_curve:parse.push(this_token);break; + default:break; + } + get_token(); + get_token(); + if(this_token.type==__left_brace) + { + // if without { then only one statement is behind it + parse.push(this_token); + statements_block(); + } + else + { + token t; + switch(this_token.type) + { + case __var:definition_expr();check_semi_at_end();break; + case __id:identifier_begin_expr();check_semi_at_end();break; + case __number:number_begin_expr();check_semi_at_end();break; + case __string:string_begin_expr();check_semi_at_end();break; + case __if:parse.push(this_token);if_else_expr();break; + case __left_curve: + t=this_token; + get_token(); + if(this_token.type==__var) + { + parse.push(t); + definition_expr(); + } + else + { + in_curve_calc_expr(); + } + check_semi_at_end(); + break; + case __add_operator: + case __sub_operator: + case __nor_operator:one_operator_expr();break; + case __while: + case __for: + case __foreach: + case __forindex:parse.push(this_token);loop_expr();break; + case __continue: + case __break:check_semi_at_end();break; + case __semi:break; + case __return:return_expr();check_semi_at_end();break; + default:break; + } + } + } + else if(this_token.type==__forindex || this_token.type==__foreach) + { + get_token(); + get_token(); + switch(this_token.type) + { + case __var:definition_expr();break; + case __id:identifier_begin_expr();break; + case __semi:parse.push(this_token);break; + default:break; + } + check_semi_at_end(); + get_token(); + switch(this_token.type) + { + case __id:identifier_begin_expr();break; + case __number:number_begin_expr();break; + case __string:string_begin_expr();break; + case __left_curve:in_curve_calc_expr();break; + case __left_bracket:list_generate_expr();break; + case __right_curve:parse.push(this_token);break; + default:break; + } + get_token(); + get_token(); + if(this_token.type==__left_brace) + { + // if without { then only one statement is behind it + parse.push(this_token); + statements_block(); + } + else + { + token t; + switch(this_token.type) + { + case __var:definition_expr();check_semi_at_end();break; + case __id:identifier_begin_expr();check_semi_at_end();break; + case __number:number_begin_expr();check_semi_at_end();break; + case __string:string_begin_expr();check_semi_at_end();break; + case __if:parse.push(this_token);if_else_expr();break; + case __left_curve: + t=this_token; + get_token(); + if(this_token.type==__var) + { + parse.push(t); + definition_expr(); + } + else + { + in_curve_calc_expr(); + } + check_semi_at_end(); + break; + case __add_operator: + case __sub_operator: + case __nor_operator:one_operator_expr();break; + case __while: + case __for: + case __foreach: + case __forindex:parse.push(this_token);loop_expr();break; + case __continue: + case __break:check_semi_at_end();break; + case __semi:break; + case __return:return_expr();check_semi_at_end();break; + default:break; + } + } + } + node.set_node_to_loop(); + return node; +} +abstract_syntax_tree generator::continue_break_expr() +{ + abstract_syntax_tree node; + node.set_node_to_continue_break(this_token.type); + return node; +} +abstract_syntax_tree generator::add_sub_operator_expr() +{ + abstract_syntax_tree node; + get_token(); + switch(this_token.type) + { + case __number:node=number_begin_expr();break; + case __string:node=string_begin_expr();break; + case __id:node=identifier_begin_expr();break; + case __left_curve:node=in_curve_calc_expr();break; + default:break; + } + return node; +} +abstract_syntax_tree generator::mul_div_operator_expr() +{ + abstract_syntax_tree node; + get_token(); + switch(this_token.type) + { + case __number:node=number_begin_expr();break; + case __string:node=string_begin_expr();break; + case __id:node=identifier_begin_expr();break; + case __left_curve:node=in_curve_calc_expr();break; + default:break; + } + return node; +} +abstract_syntax_tree generator::link_operator_expr() +{ + abstract_syntax_tree node; + get_token(); + switch(this_token.type) + { + case __number:node=number_begin_expr();break; + case __string:node=string_begin_expr();break; + case __id:node=identifier_begin_expr();break; + case __left_curve:node=in_curve_calc_expr();break; + default:break; + } + return node; +} +abstract_syntax_tree generator::compare_operator_expr() +{ + abstract_syntax_tree node; + get_token(); + switch(this_token.type) + { + case __number:node=number_begin_expr();break; + case __string:node=string_begin_expr();break; + case __id:node=identifier_begin_expr();break; + case __left_curve:node=in_curve_calc_expr();break; + case __add_operator: + case __sub_operator: + case __nor_operator:node=one_operator_expr();break; + default:break; + } + return node; +} +abstract_syntax_tree generator::one_operator_expr() +{ + abstract_syntax_tree node; + int type=this_token.type; + get_token(); + switch(this_token.type) + { + case __number:node.set_one_operator(type,number_begin_expr());;break; + case __string:node.set_one_operator(type,string_begin_expr());break; + case __id:node.set_one_operator(type,identifier_begin_expr());break; + case __left_curve:node.set_one_operator(type,in_curve_calc_expr());break; + default:break; + } + return node; +} +abstract_syntax_tree generator::in_curve_calc_expr() +{ + abstract_syntax_tree node; + get_token(); + switch(this_token.type) + { + case __add_operator: + case __sub_operator: + case __nor_operator:node=one_operator_expr();break; + case __number:node=number_begin_expr();break; + case __string:node=string_begin_expr();break; + case __id:node=identifier_begin_expr();break; + case __left_curve:node=in_curve_calc_expr();break; + default:parse.push(this_token);break; + } + get_token(); + get_token(); + int temp_type=this_token.type; + switch(this_token.type) + { + case __add_operator: + case __sub_operator:node.set_two_operator(temp_type,node,add_sub_operator_expr());break; + case __mul_operator: + case __div_operator:node.set_two_operator(temp_type,node,mul_div_operator_expr());break; + case __link_operator:node.set_two_operator(temp_type,node,link_operator_expr());break; + case __and_operator: + case __or_operator: + case __cmp_equal: + case __cmp_not_equal: + case __cmp_less: + case __cmp_more: + case __cmp_less_or_equal: + case __cmp_more_or_equal:node.set_two_operator(temp_type,node,compare_operator_expr());break; + default:parse.push(this_token);break; + } + return node; +} +abstract_syntax_tree generator::number_begin_expr() +{ + abstract_syntax_tree node; + node.set_node_to_number(this_token.content); + get_token(); + int temp_type=this_token.type; + switch(this_token.type) + { + case __add_operator: + case __sub_operator:node.set_two_operator(temp_type,node,add_sub_operator_expr());break; + case __mul_operator: + case __div_operator:node.set_two_operator(temp_type,node,mul_div_operator_expr());break; + case __link_operator:node.set_two_operator(temp_type,node,link_operator_expr());break; + case __and_operator: + case __or_operator: + case __cmp_equal: + case __cmp_not_equal: + case __cmp_less: + case __cmp_more: + case __cmp_less_or_equal: + case __cmp_more_or_equal:node.set_two_operator(temp_type,node,compare_operator_expr());break; + default:parse.push(this_token);break; + } + return node; +} +abstract_syntax_tree generator::string_begin_expr() +{ + abstract_syntax_tree node; + node.set_node_to_string(this_token.content); + get_token(); + int temp_type=this_token.type; + switch(this_token.type) + { + case __add_operator: + case __sub_operator:node.set_two_operator(temp_type,node,add_sub_operator_expr());break; + case __mul_operator: + case __div_operator:node.set_two_operator(temp_type,node,mul_div_operator_expr());break; + case __link_operator:node.set_two_operator(temp_type,node,link_operator_expr());break; + case __and_operator: + case __or_operator: + case __cmp_equal: + case __cmp_not_equal: + case __cmp_less: + case __cmp_more: + case __cmp_less_or_equal: + case __cmp_more_or_equal:node.set_two_operator(temp_type,node,compare_operator_expr());break; + default:parse.push(this_token);break; + } + return node; +} +abstract_syntax_tree generator::call_list_expr() +{ + abstract_syntax_tree node; + get_token(); + switch(this_token.type) + { + case __number:number_begin_expr();break; + case __string:string_begin_expr();break; + case __id:identifier_begin_expr();break; + case __left_curve:in_curve_calc_expr();break; + default:break; + } + get_token(); + if(this_token.type==__colon)// to check if there is [number:number] or [number:] + { + get_token(); + switch(this_token.type) + { + case __number:number_begin_expr();break; + case __string:string_begin_expr();break; + case __id:identifier_begin_expr();break; + case __left_curve:in_curve_calc_expr();break; + case __right_bracket:break;// this is [number:] + } + get_token(); + get_token(); + switch(this_token.type) + { + case __left_bracket:call_list_expr();break; + case __left_curve:call_function_expr();break; + case __dot:call_hash_expr();break; + default:parse.push(this_token);break; + } + } + else if(this_token.type==__right_bracket) + { + get_token(); + switch(this_token.type) + { + case __left_bracket:call_list_expr();break; + case __left_curve:call_function_expr();break; + case __dot:call_hash_expr();break; + default:parse.push(this_token);break; + } + } + node.set_node_to_list_search(); + return node; +} +abstract_syntax_tree generator::call_function_expr() +{ + abstract_syntax_tree node; + get_token(); + while(this_token.type!=__right_curve) + { + switch(this_token.type) + { + case __number:number_begin_expr();break; + case __string:string_begin_expr();break; + case __id:identifier_begin_expr();break; + case __left_bracket:list_generate_expr();break; + case __left_brace:hash_generate_expr();break; + case __left_curve:in_curve_calc_expr();break; + case __func:function_generate_expr();break; + default:break; + } + get_token(); + if(this_token.type==__comma) + get_token(); + } + get_token(); + switch(this_token.type) + { + case __left_bracket:call_list_expr();break; + case __left_curve:call_function_expr();break; + case __dot:call_hash_expr();break; + default:parse.push(this_token);break; + } + + node.set_node_to_call_function(); + return node; +} +abstract_syntax_tree generator::call_hash_expr() +{ + get_token(); + return identifier_begin_expr(); +} +abstract_syntax_tree generator::identifier_begin_expr() +{ + // __id has been checked + abstract_syntax_tree node; + + node.set_node_to_id(this_token.content); + + get_token(); + int temp_type=this_token.type; + switch(this_token.type) + { + case __left_bracket:node=call_list_expr();break; + case __left_curve:node=call_function_expr();break; + case __dot:node=call_hash_expr();break; + case __add_operator: + case __sub_operator:node.set_two_operator(temp_type,node,add_sub_operator_expr());break; + case __mul_operator: + case __div_operator:node.set_two_operator(temp_type,node,mul_div_operator_expr());break; + case __link_operator:node.set_two_operator(temp_type,node,link_operator_expr());break; + case __and_operator: + case __or_operator: + case __cmp_equal: + case __cmp_not_equal: + case __cmp_less: + case __cmp_more: + case __cmp_less_or_equal: + case __cmp_more_or_equal:node.set_two_operator(temp_type,node,compare_operator_expr());break; + default:parse.push(this_token);break; + } + get_token(); + temp_type=this_token.type; + switch(this_token.type) + { + case __add_operator: + case __sub_operator:node.set_two_operator(temp_type,node,add_sub_operator_expr());break; + case __mul_operator: + case __div_operator:node.set_two_operator(temp_type,node,mul_div_operator_expr());break; + case __link_operator:node.set_two_operator(temp_type,node,link_operator_expr());break; + case __and_operator: + case __or_operator: + case __cmp_equal: + case __cmp_not_equal: + case __cmp_less: + case __cmp_more: + case __cmp_less_or_equal: + case __cmp_more_or_equal:node.set_two_operator(temp_type,node,compare_operator_expr());break; + case __equal: + case __add_equal: + case __sub_equal: + case __mul_equal: + case __div_equal: + case __link_equal:node=assignment_expr();break; + default:parse.push(this_token);break; + } + return node; +} +void generator::gen_main_work() +{ + root.tree_set_clear(); + + while(!parse.empty()) + { + get_token(); + token t; + switch(this_token.type) + { + case __var:root.add_statement(definition_expr());check_semi_at_end();break; + case __id:root.add_statement(identifier_begin_expr());check_semi_at_end();break; + case __number:root.add_statement(number_begin_expr());check_semi_at_end();break; + case __string:root.add_statement(string_begin_expr());check_semi_at_end();break; + case __left_curve: + t=this_token; + get_token(); + if(this_token.type==__var) + { + parse.push(t); + root.add_statement(definition_expr()); + } + else + { + parse.push(this_token); + root.add_statement(in_curve_calc_expr()); + } + check_semi_at_end(); + break; + case __if:parse.push(this_token);root.add_statement(if_else_expr());break; + case __add_operator: + case __sub_operator: + case __nor_operator:root.add_statement(one_operator_expr());break; + case __while: + case __for: + case __foreach: + case __forindex:parse.push(this_token);root.add_statement(loop_expr());break; + case __semi:break; + default:break; + } + } + std::cout<<">>[Abstract-syntax-tree] complete generation."<> Nasal interpreter by ValKmjolnir"<> Input [help] to find help."<> "; + std::getline(std::cin,command); + if(command=="help") + { + std::cout<<">> Nasal interpreter by ValKmjolnir"<> 1. [ ] |input file name to load the file."<> 2. [cls ] |clear the screen."<> 3. [exit ] |shut down the interpreter."<> 4. [lexer ] |run and show the lexer. (-lexer)"<> 5. [parser] |run parser and see parse stack & parse result(s). (-parser)"<> 6. [ast ] |print the abstract syntax tree."<> 7. [del ] |delete program in memory."<> 8. [run ] |run the programme in stack. (-lexer -parser)"<> 9. [rs ] |check the source program."<>[Delete] Complete."< +#include +#include +#include +#include +#include + +#include "nasal_token_type.h" + + +#include "nasal_lexer.h" +#include "nasal_parser.h" +#include "ast.h" +#include "ast_generator.h" + +#endif diff --git a/version1.1/nasal_lexer.h b/version1.1/nasal_lexer.h new file mode 100644 index 0000000..26f88de --- /dev/null +++ b/version1.1/nasal_lexer.h @@ -0,0 +1,535 @@ +#ifndef __NASAL_LEXER_H__ +#define __NASAL_LEXER_H__ + +#include +#include +#include +#include + +#define OPERATOR 1 // operator +#define IDENTIFIER 2 // id +#define NUMBER 3 // number +#define RESERVEWORD 4 // reserve word +#define STRING 5 // string +#define DYNAMIC_ID 6 // id... +#define FAIL -1 //fail +#define SCANEND -2 //complete scanning +#define ERRORFOUND -3 //error occurred + +std::string reserve_word[15]= +{ + "for","foreach","forindex","while", + "var","func","break","continue","return", + "if","else","elsif","nil","and","or" +}; + +int isReserveWord(std::string &p) +{ + for(int i=0;i<15;++i) + if(reserve_word[i]==p) + return i+1; + return FAIL; +} + +bool isLetter(char t) +{ + return (('a'<=t) && (t<='z') || ('A'<=t) && (t<='Z')); +} + +bool isNumber(char t) +{ + return (('0'<=t) && (t<='9')); +} + +bool isHex(char t) +{ + return ((('0'<=t) && (t<='9')) || (('a'<=t) && (t<='f'))); +} + +bool isOct(char t) +{ + return (('0'<=t) && (t<='7')); +} + +class resource_programme_process +{ + private: + char *resource; + public: + resource_programme_process() + { + resource=NULL; + resource=new char[16777216]; + } + ~resource_programme_process() + { + if(resource) + delete []resource; + } + char* use_file() + { + return resource; + } + void input_file(std::string& filename) + { + std::ifstream fin(filename); + if(fin.fail()) + { + std::cout<<">>[Error] Cannot load file: "< lexer; + public: + void scanner(int &syn,const char* source,std::string &__token,int &ptr,int &line) + { + char temp; + temp=source[ptr]; + while(temp==' ' || temp=='\n' || temp=='\t' || temp=='\r' || temp<0 || temp>127) + { + ++ptr; + if(temp=='\n') + ++line; + temp=source[ptr]; + } + __token=""; + if(isLetter(temp) || temp=='_') + { + __token+=temp; + ++ptr; + temp=source[ptr]; + while(isLetter(temp) || isNumber(temp) || temp=='_') + { + __token+=temp; + ++ptr; + temp=source[ptr]; + } + syn=isReserveWord(__token); + if(syn==FAIL) + syn=IDENTIFIER; + else + syn=RESERVEWORD; + if((syn==IDENTIFIER) && source[ptr]=='.' && source[ptr+1]=='.' && source[ptr+2]=='.') + { + __token+="..."; + syn=DYNAMIC_ID; + ptr+=3; + } + } + else if(isNumber(temp)) + { + if((source[ptr]=='0') && (source[ptr+1]=='x')) + { + __token+=source[ptr]; + __token+=source[ptr+1]; + ptr+=2; + temp=source[ptr]; + while(isNumber(temp) || isHex(temp)) + { + __token+=temp; + ++ptr; + temp=source[ptr]; + } + if((int)__token.length()==2) + { + std::cout<<">>[Lexer-warning] lexer: expect a hex-number string after '0x' ."<>[Lexer-warning] lexer: expect a oct-number string after '0o' ."<' || temp=='~') + { + syn=OPERATOR; + __token+=temp; + ++ptr; + temp=source[ptr]; + if(temp=='=') + { + __token+=temp; + ++ptr; + } + } + else if(temp=='\\') + { + syn=OPERATOR; + __token+=temp; + ++ptr; + temp=source[ptr]; + if(temp=='=' || temp=='n' || temp=='t' || temp=='r' || temp=='\\' || temp=='\'' || temp=='\"') + { + __token+=temp; + ++ptr; + } + } + else if(temp=='\"') + { + syn=STRING; + __token+=temp; + ++ptr; + temp=source[ptr]; + while(temp!='\"') + { + if(temp=='\\') + { + __token+=temp; + + ++ptr; + temp=source[ptr]; + __token+=temp; + + ++ptr; + temp=source[ptr]; + } + else + { + __token+=temp; + ++ptr; + temp=source[ptr]; + } + if(temp==0 || temp=='\n') + break; + } + //add the last char \" + if(temp=='\"') + { + __token+=temp; + ++ptr; + } + else + __token+=" __missing_end_of_string"; + } + else if(temp==0) + { + syn=SCANEND; + return; + } + else + { + syn=FAIL; + std::cout<<">>[Error] unexpected error occurred: "<>[Error] cannot identify "<>[Lexer] max size: "<0)//all Syn type is larger than zero + { + temp.line=line; + temp.type=syn; + temp.content=__token; + lexer.push_back(temp); + } + } + std::cout<<">>[Lexer] complete scanning."<::iterator i=lexer.begin();i!=lexer.end();++i) + { + temp=*i; + std::cout<<"line "<::iterator i=lexer.begin();i!=lexer.end();++i) + { + if((*i).type==RESERVEWORD) + { + if((*i).content=="var") + (*i).type=__var; + else if((*i).content=="func") + (*i).type=__func; + else if((*i).content=="return") + (*i).type=__return; + else if((*i).content=="nil") + (*i).type=__number; + else if((*i).content=="continue") + (*i).type=__continue; + else if((*i).content=="break") + (*i).type=__break; + else if((*i).content=="and") + (*i).type=__and_operator; + else if((*i).content=="or") + (*i).type=__or_operator; + else if((*i).content=="for") + (*i).type=__for; + else if((*i).content=="forindex") + (*i).type=__forindex; + else if((*i).content=="foreach") + (*i).type=__foreach; + else if((*i).content=="while") + (*i).type=__while; + else if((*i).content=="if") + (*i).type=__if; + else if((*i).content=="else") + (*i).type=__else; + else if((*i).content=="elsif") + (*i).type=__elsif; + } + else if(((*i).content=="==") || ((*i).content=="!=") || ((*i).content==">") || ((*i).content==">=") || ((*i).content=="<") || ((*i).content=="<=")) + { + if((*i).content=="==") + (*i).type=__cmp_equal; + else if((*i).content=="!=") + (*i).type=__cmp_not_equal; + else if((*i).content==">") + (*i).type=__cmp_more; + else if((*i).content==">=") + (*i).type=__cmp_more_or_equal; + else if((*i).content=="<") + (*i).type=__cmp_less; + else if((*i).content=="<=") + (*i).type=__cmp_less_or_equal; + } + else if(((*i).content==";") || ((*i).content==",") || ((*i).content=="=") || ((*i).content==":") || ((*i).content==".") || ((*i).content=="?") || ((*i).content=="%") || ((*i).content=="$") || ((*i).content=="`") || ((*i).content=="^") || ((*i).content=="@")) + { + char c=(*i).content[0]; + switch(c) + { + case ';':(*i).type=__semi;break; + case ',':(*i).type=__comma;break; + case '=':(*i).type=__equal;break; + case ':':(*i).type=__colon;break; + case '.':(*i).type=__dot;break; + default:(*i).type=__unknown_operator;break; + } + } + else if(((*i).type==NUMBER) || ((*i).type==STRING) || ((*i).type==IDENTIFIER) || ((*i).type==DYNAMIC_ID)) + { + int t=(*i).type; + switch(t) + { + case NUMBER:(*i).type=__number;break; + case STRING:(*i).type=__string;break; + case IDENTIFIER:(*i).type=__id;break; + case DYNAMIC_ID:(*i).type=__dynamic_id;break; + } + } + else if(((*i).content=="+") || ((*i).content=="-") || ((*i).content=="*") || ((*i).content=="/") || ((*i).content=="~") || ((*i).content=="!")) + { + char c=(*i).content[0]; + switch(c) + { + case '+':(*i).type=__add_operator;break; + case '-':(*i).type=__sub_operator;break; + case '*':(*i).type=__mul_operator;break; + case '/':(*i).type=__div_operator;break; + case '~':(*i).type=__link_operator;break; + case '!':(*i).type=__nor_operator;break; + } + } + else if(((*i).content=="+=") || ((*i).content=="-=") || ((*i).content=="*=") || ((*i).content=="/=") || ((*i).content=="~=")) + { + char c=(*i).content[0]; + switch(c) + { + case '+':(*i).type=__add_equal;break; + case '-':(*i).type=__sub_equal;break; + case '*':(*i).type=__mul_equal;break; + case '/':(*i).type=__div_equal;break; + case '~':(*i).type=__link_equal;break; + } + } + else if(((*i).content=="(") || ((*i).content==")") || ((*i).content=="[") || ((*i).content=="]") || ((*i).content=="{") || ((*i).content=="}")) + { + char c=(*i).content[0]; + switch(c) + { + case '(':(*i).type=__left_curve;break; + case ')':(*i).type=__right_curve;break; + case '[':(*i).type=__left_bracket;break; + case ']':(*i).type=__right_bracket;break; + case '{':(*i).type=__left_brace;break; + case '}':(*i).type=__right_brace;break; + } + } + } + return; + } + std::list& return_list() + { + return lexer; + } +}; + + + + +#endif diff --git a/version1.1/nasal_parser.h b/version1.1/nasal_parser.h new file mode 100644 index 0000000..ce4bcfa --- /dev/null +++ b/version1.1/nasal_parser.h @@ -0,0 +1,1393 @@ +#ifndef __NASAL_PARSER_H__ +#define __NASAL_PARSER_H__ + +class nasal_parser +{ + private: + std::stack parse; + token this_token; + int error; + int warning; + public: + nasal_parser() + { + error=0; + warning=0; + this_token.type=0; + } + void get_token() + { + if(parse.empty()) + { + this_token.type=0; + return; + } + this_token=parse.top(); + parse.pop(); + return; + } + int get_error_num() + { + return error; + } + void print_parser_stack() + { + if(parse.empty()) + return; + int line=0; + std::stack temp; + while(!parse.empty()) + { + temp.push(parse.top()); + if(line!=temp.top().line) + { + if(line+1==temp.top().line) + { + line=temp.top().line; + if(line==1) + std::cout<<"line "< "; + parse.pop(); + } + while(!temp.empty()) + { + parse.push(temp.top()); + temp.pop(); + } + std::cout<& lexer) + { + while(!parse.empty()) + parse.pop(); + std::stack temp; + for(std::list::iterator i=lexer.begin();i!=lexer.end();++i) + { + token temp_parse; + temp_parse=*i; + temp.push(temp_parse);//push this into stack + } + if(temp.empty()) + { + std::cout<<">>[Parse] warning: empty lexer list."<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Error] line "<>[Parse] complete generation."< + +enum token_type +{ + __stack_end=1, + __equal, // = + __cmp_equal,__cmp_not_equal, // == != + __cmp_less,__cmp_less_or_equal, // < <= + __cmp_more,__cmp_more_or_equal, // > >= + __and_operator,__or_operator,__nor_operator, // and or ! + __add_operator,__sub_operator, // + - + __mul_operator,__div_operator,__link_operator, // * / ~ + __add_equal,__sub_equal, // += -= + __mul_equal,__div_equal,__link_equal, // *= /= ~= + __left_brace,__right_brace, // {} + __left_bracket,__right_bracket, // [] + __left_curve,__right_curve, // () + __semi,__comma,__colon,__dot, // ; , : . + __unknown_operator, + __var,__func,__return, + __if,__elsif,__else, + __continue,__break, + __for,__forindex,__foreach,__while, + //operators & reserve words + + __number,__string,__id,__dynamic_id, + //basic elements + + __root, + __list,__hash, + __hash_member, + __call_function,__list_search,__hash_search, + __block, + __definition,__assignment, + __function,__loop,__ifelse +}; + +void print_token(int type) +{ + std::string context=""; + switch(type) + { + case __stack_end: context="#"; break; + case __equal: context="="; break; + case __cmp_equal: context="==";break; + case __cmp_not_equal: context="!=";break; + case __cmp_less: context="<"; break; + case __cmp_less_or_equal: context="<=";break; + case __cmp_more: context=">"; break; + case __cmp_more_or_equal: context=">=";break; + case __and_operator: context="and";break; + case __or_operator: context="or"; break; + case __nor_operator: context="!"; break; + case __add_operator: context="+"; break; + case __sub_operator: context="-"; break; + case __mul_operator: context="*"; break; + case __div_operator: context="/"; break; + case __link_operator: context="~"; break; + case __add_equal: context="+=";break; + case __sub_equal: context="-=";break; + case __mul_equal: context="*=";break; + case __div_equal: context="/=";break; + case __link_equal: context="~=";break; + case __left_brace: context="{"; break; + case __right_brace: context="}"; break; + case __left_bracket: context="["; break; + case __right_bracket: context="]"; break; + case __left_curve: context="("; break; + case __right_curve: context=")"; break; + case __semi: context=";"; break; + case __comma: context=","; break; + case __colon: context=":"; break; + case __dot: context="."; break; + case __unknown_operator: context="unknown_operator";break; + case __var: context="var"; break; + case __func: context="func";break; + case __continue: context="continye"; break; + case __break: context="break"; break; + case __for: context="for"; break; + case __forindex: context="forindex";break; + case __foreach: context="foreach";break; + case __while: context="while";break; + case __if: context="if";break; + case __elsif: context="elsif";break; + case __else: context="else";break; + case __return: context="return";break; + + case __id: context="id";break; + case __dynamic_id: context="id...";break; + case __number: context="num";break; + case __string: context="str";break; + + case __root: context="root";break; + case __list: context="list";break; + case __hash: context="hash";break; + case __hash_member: context="hash_member";break; + case __call_function: context="call_func";break; + case __list_search: context="call_list";break; + case __hash_search: context="call_hash";break; + case __block: context="block";break; + case __definition: context="definition";break; + case __assignment: context="assignment";break; + case __function: context="function";break; + case __loop: context="loop";break; + case __ifelse: context="if-else";break; + default: context="unknown_token";break; + } + std::cout<180) + position_val += value-360; + else if(position_val+value<-180) + position_val += value+360; + else + position_val += value; + return position_val; +} +var road_check_func = func(){ + + var lat = props.getNode("/position/latitude-deg",1).getValue(); + var lon = props.getNode("/position/longitude-deg",1).getValue(); + var position_info = geodinfo(lat,lon); + var position_names = position_info[1].names; + # the friction_factor of freeway runway and road is 1 + + if((position_names[0]=="Freeway") or (position_names[0]=="Road")) + { + var car_heading = 0; + var lat_change = 0; + var lon_change = 0; + var left_range = 0; + var right_range = 0; + + for(var i=0;i>-0.00005;i-=0.000001) + { + car_heading = props.getNode("/orientation/heading-deg",1).getValue(); + lat_change = math.sin(math.pi*car_heading/180); + lon_change = -math.cos(math.pi*car_heading/180); + lat = props.getNode("/position/latitude-deg",1).getValue()+0.0001*math.cos(math.pi*car_heading/180); + lon = props.getNode("/position/longitude-deg",1).getValue()+0.0001*math.sin(math.pi*car_heading/180); + var other_position_info = geodinfo(position_change(lat,i*lat_change),position_change(lon,i*lon_change)); + var other_names = other_position_info[1].names; + if((other_names[0]=="Freeway") or (other_names[0]=="Road")) + right_range += 1; + else + break; + } + for(var i=0;i<0.00005;i+=0.000001) + { + car_heading = props.getNode("/orientation/heading-deg",1).getValue(); + lat_change = math.sin(math.pi*car_heading/180); + lon_change = -math.cos(math.pi*car_heading/180); + lat = props.getNode("/position/latitude-deg",1).getValue()+0.0001*math.cos(math.pi*car_heading/180); + lon = props.getNode("/position/longitude-deg",1).getValue()+0.0001*math.sin(math.pi*car_heading/180); + var other_position_info = geodinfo(position_change(lat,i*lat_change),position_change(lon,i*lon_change)); + var other_names = other_position_info[1].names; + if((other_names[0]=="Freeway") or (other_names[0]=="Road")) + left_range+=1; + else + break; + } + #if(left_range>right_range) + #{ + # setprop("/controls/flight/rudder",-(right_range-left_range)*(right_range-left_range)/10000); + # print("right ",right_range); + #} + #else if(left_range=i;i+=1) +{ + print(i); +} +foreach(var i;[1+(1+1),2,3,4]) +{ + print(i); +} +forindex(var i=list;[1,2,3,4]) +{ + print(i[0]); +} +while(!id) +{ + print("yes"); +} \ No newline at end of file