From e8bd4664b3d7215792d839223e82c3326d16857f Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Sat, 2 Jul 2022 18:08:41 +0800 Subject: [PATCH] :zap: optimize codes --- nasal_gc.h | 62 +++++++++++++++++---------------------------------- nasal_lexer.h | 13 +++++------ nasal_parse.h | 4 ++-- 3 files changed, 28 insertions(+), 51 deletions(-) diff --git a/nasal_gc.h b/nasal_gc.h index 9bfe8b7..223957a 100644 --- a/nasal_gc.h +++ b/nasal_gc.h @@ -1,8 +1,7 @@ #ifndef __NASAL_GC_H__ #define __NASAL_GC_H__ -enum nasal_type -{ +enum vm_type:std::uint32_t{ /* none-gc object */ vm_none=0, vm_cnt, @@ -20,43 +19,27 @@ enum nasal_type vm_co, vm_type_size }; - +const uint32_t gc_obj_size=vm_type_size-vm_str; // change parameters here to make your own efficient gc // better set bigger number on vm_vec -const uint32_t initialize[vm_type_size]= +const uint32_t initialize[gc_obj_size]= { - /* none-gc object */ - 0, // vm_none, error type - 0, // vm_count, used in foreach/forindex - 0, // vm_addr, used to store local address pointers - 0, // vm_ret, used to store call-return address - 0, // vm_nil - 0, // vm_num - /* gc object */ 128, // vm_str 512, // vm_func 128, // vm_vec - 64, // vm_hash + 32, // vm_hash 512, // vm_upval - 16, // vm_obj + 0, // vm_obj 0 // vm_co }; -const uint32_t increment[vm_type_size]= +const uint32_t increment[gc_obj_size]= { - /* none-gc object */ - 0, // vm_none, error type - 0, // vm_count, used in foreach/forindex - 0, // vm_addr, used to store local address pointers - 0, // vm_ret, used to store call-return address - 0, // vm_nil - 0, // vm_num - /* gc object */ - 1024,// vm_str + 256, // vm_str 512, // vm_func - 8192,// vm_vec - 1024,// vm_hash + 512, // vm_vec + 512, // vm_hash 128, // vm_upval - 256, // vm_obj + 128, // vm_obj 16 // vm_co }; @@ -68,7 +51,6 @@ struct nasal_obj; // special objects struct nasal_co; // coroutine struct nasal_val; // nasal_val includes gc-managed types - struct nasal_ref { uint8_t type; @@ -480,8 +462,8 @@ struct nasal_gc std::vector env_argv; // command line arguments /* values for analysis */ - uint64_t size[vm_type_size]; - uint64_t count[vm_type_size]; + uint64_t size[gc_obj_size]; + uint64_t count[gc_obj_size]; nasal_gc( uint32_t& _pc, nasal_ref*& _localr, @@ -594,10 +576,10 @@ void nasal_gc::init(const std::vector& s,const std::vector'||c=='~') #define NOTE(c) (c=='#') -enum token_type -{ +enum tok:std::uint32_t{ tok_null=0, // null token (default token type) tok_num, // number basic token type tok_str, // string basic token type @@ -36,12 +35,10 @@ enum token_type tok_eof // end of token list }; -struct -{ +struct{ const char* str; const uint32_t tok_type; -}token_table[]= -{ +}token_table[]={ {"for" ,tok_for }, {"forindex",tok_forindex }, {"foreach" ,tok_foreach }, @@ -93,13 +90,13 @@ struct struct token { uint32_t line; - uint32_t column; + uint32_t col; uint32_t type; std::string str; token(uint32_t l=0,uint32_t c=0,uint32_t t=tok_null,const std::string& s=""):str(s) { line=l; - column=c; + col=c; type=t; } }; diff --git a/nasal_parse.h b/nasal_parse.h index 1d68591..c253a1f 100644 --- a/nasal_parse.h +++ b/nasal_parse.h @@ -123,13 +123,13 @@ void nasal_parse::compile(const nasal_lexer& lexer) } void nasal_parse::die(uint32_t line,std::string info,bool report_prev=false) { - int col=(int)tokens[ptr].column-(int)tokens[ptr].str.length(); + int col=(int)tokens[ptr].col-(int)tokens[ptr].str.length(); if(tokens[ptr].type==tok_str) col-=2; // tok_str's str has no \" if(report_prev && ptr-1>=0) // used to report lack of ',' ';' { line=tokens[ptr-1].line; - col=tokens[ptr-1].column+1; + col=tokens[ptr-1].col+1; } nerr.err("parse",line,col<0?0:col,info); }