From 94b6e84693fa1d802899a0820dcec088cf9de116 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Tue, 11 Jul 2023 00:05:20 +0800 Subject: [PATCH] :memo: optimize code --- module/fib.cpp | 47 ++++++++++++++++++++++---------------------- src/nasal.h | 2 +- src/nasal_import.cpp | 18 ++++++++--------- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/module/fib.cpp b/module/fib.cpp index 1e6ae92..41c1449 100644 --- a/module/fib.cpp +++ b/module/fib.cpp @@ -16,7 +16,7 @@ var fib(var* args, usize size, gc* ngc) { if (!size) { return nas_err("fib","lack arguments"); } - var num=args[0]; + var num = args[0]; return var::num(fibonaci(num.tonum())); } @@ -24,15 +24,15 @@ var quick_fib(var* args, usize size, gc* ngc) { if (!size) { return nas_err("quick_fib","lack arguments"); } - double num=args[0].tonum(); + double num = args[0].tonum(); if (num<2) { return var::num(num); } - double a=1,b=1,res=0; - for(double i=1;ialloc(vm_obj); + var res = ngc->alloc(vm_obj); res.obj().set(ghost_for_test, new u32, &ngc->global_ghost_type_table); return res; } var set_new_ghost(var* args, usize size, gc* ngc) { - var res=args[0]; + var res = args[0]; if (!res.objchk(ghost_for_test)) { - std::cout<<"set_new_ghost: not ghost for test type.\n"; + std::cout << "set_new_ghost: not ghost for test type.\n"; return nil; } - f64 num=args[1].num(); - *((u32*)res.obj().ptr)=static_cast(num); - std::cout<<"set_new_ghost: successfully set ghost = "<(num); + std::cout << "set_new_ghost: successfully set ghost = " << num << "\n"; return nil; } var print_new_ghost(var* args, usize size, gc* ngc) { - var res=args[0]; + var res = args[0]; if (!res.objchk(ghost_for_test)) { - std::cout<<"print_new_ghost: not ghost for test type.\n"; + std::cout << "print_new_ghost: not ghost for test type.\n"; return nil; } - std::cout<<"print_new_ghost: "<exists("fib_for_test")) { - nasal_fib_module::ghost_for_test=table->get_ghost_type_index("fib_for_test"); + nasal_fib_module::ghost_for_test = table->get_ghost_type_index("fib_for_test"); return nasal_fib_module::func_tbl; } - nasal_fib_module::ghost_for_test=table->register_ghost_type( + nasal_fib_module::ghost_for_test = table->register_ghost_type( "fib_for_test", nasal_fib_module::ghost_for_test_destructor ); diff --git a/src/nasal.h b/src/nasal.h index f12c9a4..bf97efc 100644 --- a/src/nasal.h +++ b/src/nasal.h @@ -32,7 +32,7 @@ using u64 = std::uint64_t; using usize = std::size_t; using f64 = double; -const u32 STACK_DEPTH=4096; +const u32 STACK_DEPTH = 4096; f64 hex2f(const char*); f64 oct2f(const char*); diff --git a/src/nasal_import.cpp b/src/nasal_import.cpp index 2658305..dd283d0 100644 --- a/src/nasal_import.cpp +++ b/src/nasal_import.cpp @@ -1,17 +1,17 @@ #include "nasal_import.h" -linker::linker(error& e): show_path(false), lib_loaded(false), err(e) { - char sep=is_windows()? ';':':'; - std::string PATH=getenv("PATH"); - usize last=0; - usize pos=PATH.find(sep, 0); +linker::linker(error& e): + show_path(false), lib_loaded(false), err(e) { + char sep = is_windows()? ';':':'; + std::string PATH = getenv("PATH"); + usize last = 0, pos = PATH.find(sep, 0); while(pos!=std::string::npos) { - std::string dirpath=PATH.substr(last, pos-last); + std::string dirpath = PATH.substr(last, pos-last); if (dirpath.length()) { envpath.push_back(dirpath); } - last=pos+1; - pos=PATH.find(sep, last); + last = pos+1; + pos = PATH.find(sep, last); } if (last!=PATH.length()) { envpath.push_back(PATH.substr(last)); @@ -132,7 +132,7 @@ bool linker::exist(const std::string& file) { void linker::link(code_block* new_tree_root, code_block* old_tree_root) { // add children of add_root to the back of root - for(auto& i:old_tree_root->get_expressions()) { + for(auto& i : old_tree_root->get_expressions()) { new_tree_root->add_expression(i); } // clean old root