diff --git a/balloon/abstract_syntax_tree.cpp b/balloon/abstract_syntax_tree.cpp index 8dae46d..cf853b1 100644 --- a/balloon/abstract_syntax_tree.cpp +++ b/balloon/abstract_syntax_tree.cpp @@ -4,8 +4,9 @@ int exit_type=0; -std::stack ret_stack;// for function ret use -std::list parameter;// for function call use +std::stack ret_stack; // for function ret use +std::list parameter; // for function call use + var abstract_syntax_tree::calculation() { @@ -30,12 +31,23 @@ var abstract_syntax_tree::calculation() } else if(this->type==__array) { - temp=this->array_generation(); + temp.set_type(__var_array); + if(!children.empty()) + for(std::list::iterator i=children.begin();i!=children.end();++i) + temp.append_array(i->calculation()); return temp; } else if(this->type==__hash) { - temp=this->hash_generation(); + temp.set_type(__var_hash); + if(!children.empty()) + for(std::list::iterator i=children.begin();i!=children.end();++i) + { + var t; + t=i->children.front().calculation(); + t.set_name(i->name); + temp.append_hash(t); + } return temp; } else if(this->type==__function) @@ -477,31 +489,6 @@ var abstract_syntax_tree::call_identifier() return temp; } -var abstract_syntax_tree::array_generation() -{ - var new_var; - new_var.set_type(__var_array); - if(!children.empty()) - for(std::list::iterator i=children.begin();i!=children.end();++i) - new_var.append_array(i->calculation()); - return new_var; -} - -var abstract_syntax_tree::hash_generation() -{ - var new_var; - new_var.set_type(__var_hash); - if(!children.empty()) - for(std::list::iterator i=children.begin();i!=children.end();++i) - { - var temp; - temp=i->children.front().calculation(); - temp.set_name(i->name); - new_var.append_hash(temp); - } - return new_var; -} - var* abstract_syntax_tree::get_var_addr() { var* addr=&error_var; @@ -697,6 +684,8 @@ void abstract_syntax_tree::run_root() } end_time=time(NULL); std::cout<<"------------------------------------------------------------------------------"<>[Runtime] process exited after "< #include #include @@ -10,13 +20,34 @@ #include #include -#include "balloon_type.h" -#include "abstract_syntax_tree.h" -#include "balloon_var.h" -#include "balloon_lexer.h" -#include "balloon_parse.h" -#include "balloon_scope.h" - -#include "abstract_syntax_tree.cpp" +void alert_sound() +{ + printf("\a"); + return; +} +#include "balloon_type.h" // place some enums +#include "abstract_syntax_tree.h" // ast +#include "balloon_var.h" // var +/* global varia in balloon_var.h : + var error_var; // give an error_var to get +*/ +#include "balloon_lexer.h" // lexer +/* global varia in balloon_lexer.h : + struct token; // used to get token from resource codes + std::string reserve_word[15]; // place many reserve words + int is_reserve_word(std::string str); // check the string is a reserve word or not and return the number of reserve word + bool check_number(std::string str); // check the string can be processed to number or not +*/ +#include "balloon_parse.h" // parser +#include "balloon_scope.h" // place to store vars +/* global varia in balloon_scope.h : + balloon_scope scope; // make a place to vars +*/ +#include "abstract_syntax_tree.cpp" // runtime +/* global varia in abstract_syntax_tree.cpp : + int exit_type; // record the state of runtime + std::stack ret_stack; // for function ret use + std::list parameter; // for function call use +*/ #endif diff --git a/balloon/balloon_lexer.h b/balloon/balloon_lexer.h index dcb6ca4..79b124f 100644 --- a/balloon/balloon_lexer.h +++ b/balloon/balloon_lexer.h @@ -1,12 +1,17 @@ #ifndef __BALLOON_LEXER_H__ #define __BALLOON_LEXER_H__ +/* + reserve words are those below + and they are also the reserve words of nasal +*/ std::string reserve_word[15]= { "for","foreach","forindex","while", "var","func","break","continue","return", "if","else","elsif","nil","and","or" }; + int is_reserve_word(std::string str) { for(int i=0;i<15;++i) @@ -14,6 +19,10 @@ int is_reserve_word(std::string str) return __reserve_word; return __token_identifier; } + +/* + check if the generated string can be put to number +*/ bool check_number(std::string str) { if(str.length()==1) @@ -63,7 +72,12 @@ bool check_number(std::string str) return false; } - +/* + use std::list to store resource codes + and if you continue adding files + the codes will be added behind files that have + been added in before +*/ class resource_file { private: @@ -108,6 +122,8 @@ class resource_file { if(32<=*i && *i<128 || *i=='\n') std::cout<<*i; + else if(*i=='\t') + std::cout<<" "; if(*i=='\n') { ++line; @@ -125,7 +141,15 @@ struct token int type; std::string str; }; - +/* + lexer can recognize: + number: 100(int) 0.001(double) 0xdeadbeef(hex) 0o1701(oct) + string: "str" 'str' + identifier + reserve word + normal identifier: ID and id is different + operator +*/ class balloon_lexer { private: diff --git a/balloon/balloon_parse.h b/balloon/balloon_parse.h index a518fd6..5e5e09d 100644 --- a/balloon/balloon_parse.h +++ b/balloon/balloon_parse.h @@ -81,26 +81,58 @@ class balloon_parse root.run_root(); return; } + /* + ret: return expression + choose: if-else expression + loop: while/for/forindex/foreach expression + definition + assignment + block: set of expressions + generate: + array + hash + number + string + function + block + scalar: + calculation: + calculation_or: + calculation_and: + calculation_cmp: + calculation_additive: + calculation_multive: + call_identifier + unary expression + ->generate + semi: check if the statements is ended with semi + parse_main: main process + */ abstract_syntax_tree ret(); abstract_syntax_tree choose(); abstract_syntax_tree loop(); abstract_syntax_tree definition(); abstract_syntax_tree assignment(); + abstract_syntax_tree block(); + abstract_syntax_tree array_generate(); abstract_syntax_tree hash_generate(); + abstract_syntax_tree func_generate(); abstract_syntax_tree check_number(); abstract_syntax_tree check_string(); - abstract_syntax_tree check_unary(); - abstract_syntax_tree block(); - abstract_syntax_tree func_generate(); + + abstract_syntax_tree scalar(); + abstract_syntax_tree call_identifier(); + abstract_syntax_tree check_unary(); + abstract_syntax_tree calculation(); abstract_syntax_tree calculation_or(); abstract_syntax_tree calculation_and(); abstract_syntax_tree calculation_cmp(); abstract_syntax_tree calculation_additive(); abstract_syntax_tree calculation_multive(); - abstract_syntax_tree scalar(); + void check_semi(); void parse_main(); }; @@ -489,7 +521,7 @@ abstract_syntax_tree balloon_parse::block() { abstract_syntax_tree new_node; abstract_syntax_tree temp; - new_node.set_type(__block); + new_node.set_type(__normal_block); get_token(); if(this_token.type!=__left_brace) { diff --git a/balloon/balloon_scope.h b/balloon/balloon_scope.h index aeb09c9..22a5a2d 100644 --- a/balloon/balloon_scope.h +++ b/balloon/balloon_scope.h @@ -6,6 +6,14 @@ class balloon_scope private: std::list global; std::list > > scope_list; + /* + example: + std::list global // global scope + std::list > > // total scope + -> std::list > // block scope + ->std::list // local scope + -> var -> var -> var -> var + */ public: void set_clear() { diff --git a/balloon/balloon_type.h b/balloon/balloon_type.h index ff282a9..c099ed3 100644 --- a/balloon/balloon_type.h +++ b/balloon/balloon_type.h @@ -51,7 +51,7 @@ enum parse_type //basic elements __null_node, - __block, + __normal_block, __array, __hash, __root, @@ -65,6 +65,7 @@ enum parse_type __call_hash, __call_function }; + void print_detail_token(int type) { std::string context=""; @@ -119,7 +120,7 @@ void print_detail_token(int type) case __string: context="str";break; case __null_node: context="null node";break; - case __block: context="block";break; + case __normal_block: context="block";break; case __array: context="array";break; case __hash: context="hash";break; case __root: context="root";break; @@ -138,6 +139,34 @@ void print_detail_token(int type) return; } +enum var_type +{ + __null_type, + __var_number, + __var_string, + __var_array, + __var_hash, + __var_function +}; + +void print_scalar(int type) +{ + std::string str=""; + switch(type) + { + case __null_type: str="null";break; + case __var_number: str="number";break; + case __var_string: str="string";break; + case __var_array: str="array";break; + case __var_hash: str="hash";break; + case __var_function:str="function";break; + default: str="unknown";break; + } + std::cout<>[Lexer] error(s) found,stop."<>[Parse] error(s) found,stop."<>[Lexer] error(s) found,stop."<>[Parse] error(s) found,stop."<>[Lexer] error(s) found,stop."<