🎨 add namespace

This commit is contained in:
ValKmjolnir 2023-09-08 00:33:57 +08:00
parent d89f290a8a
commit cd4e0c1716
27 changed files with 127 additions and 26 deletions

View File

@ -2,6 +2,8 @@
#include <iostream>
namespace nasal {
bool ast_dumper::visit_null_expr(null_expr* node) {
dump_indent();
std::cout << "null" << format_location(node->get_location());
@ -478,4 +480,6 @@ bool ast_dumper::visit_return_expr(return_expr* node) {
pop_indent();
}
return true;
}
}
}

View File

@ -6,6 +6,8 @@
#include <cstring>
#include <sstream>
namespace nasal {
class ast_dumper:public ast_visitor {
private:
std::vector<std::string> indent;
@ -78,4 +80,6 @@ public:
void dump(code_block* root) {
root->accept(this);
}
};
};
}

View File

@ -1,5 +1,7 @@
#include "ast_visitor.h"
namespace nasal {
bool ast_visitor::visit_expr(expr* node) {
node->accept(this);
return true;
@ -231,4 +233,6 @@ bool ast_visitor::visit_return_expr(return_expr* node) {
node->get_value()->accept(this);
}
return true;
}
}
}

View File

@ -2,6 +2,8 @@
#include "nasal_ast.h"
namespace nasal {
class ast_visitor {
public:
virtual bool visit_expr(expr*);
@ -40,4 +42,6 @@ public:
virtual bool visit_continue_expr(continue_expr*);
virtual bool visit_break_expr(break_expr*);
virtual bool visit_return_expr(return_expr*);
};
};
}

View File

@ -89,7 +89,7 @@ std::ostream& version(std::ostream& out) {
num = (num+rand())*(1.0/(RAND_MAX+1.0));
}
if (num<0.01) {
parse::easter_egg();
nasal::parse::easter_egg();
}
out << "nasal interpreter version " << __nasver;
out << " (" << __DATE__ << " " << __TIME__ << ")\n";
@ -112,10 +112,10 @@ void execute(
using clk = std::chrono::high_resolution_clock;
const auto den = clk::duration::period::den;
lexer lex;
parse parse;
linker ld;
codegen gen;
nasal::lexer lex;
nasal::parse parse;
nasal::linker ld;
nasal::codegen gen;
// lexer scans file to get tokens
lex.scan(file).chkerr();
@ -123,7 +123,7 @@ void execute(
// parser gets lexer's token list to compile
parse.compile(lex).chkerr();
if (cmd&VM_RAW_AST) {
auto dumper = std::unique_ptr<ast_dumper>(new ast_dumper);
auto dumper = std::unique_ptr<nasal::ast_dumper>(new nasal::ast_dumper);
dumper->dump(parse.tree());
}
@ -131,10 +131,10 @@ void execute(
ld.link(parse, file, cmd&VM_DETAIL).chkerr();
// optimizer does simple optimization on ast
auto opt = std::unique_ptr<optimizer>(new optimizer);
auto opt = std::unique_ptr<nasal::optimizer>(new nasal::optimizer);
opt->do_optimization(parse.tree());
if (cmd&VM_AST) {
auto dumper = std::unique_ptr<ast_dumper>(new ast_dumper);
auto dumper = std::unique_ptr<nasal::ast_dumper>(new nasal::ast_dumper);
dumper->dump(parse.tree());
}
@ -150,10 +150,10 @@ void execute(
// run
auto start = clk::now();
if (cmd&VM_DEBUG) {
auto debugger = std::unique_ptr<dbg>(new dbg);
auto debugger = std::unique_ptr<nasal::dbg>(new nasal::dbg);
debugger->run(gen, ld, argv, cmd&VM_PROFILE, cmd&VM_PROF_ALL);
} else if (cmd&VM_TIME || cmd&VM_EXEC) {
auto runtime = std::unique_ptr<vm>(new vm);
auto runtime = std::unique_ptr<nasal::vm>(new nasal::vm);
runtime->run(gen, ld, argv, cmd&VM_DETAIL);
}
@ -180,8 +180,8 @@ i32 main(i32 argc, const char* argv[]) {
} else if (s=="-v" || s=="--version") {
std::clog << version;
} else if (s=="-r" || s=="--repl") {
auto repl_module = std::unique_ptr<repl::repl>(new repl::repl);
repl_module->execute();
auto repl = std::unique_ptr<nasal::repl::repl>(new nasal::repl::repl);
repl->execute();
} else if (s[0]!='-') {
execute(s, {}, VM_EXEC);
} else {

View File

@ -1,6 +1,8 @@
#include "nasal_ast.h"
#include "ast_visitor.h"
namespace nasal {
void expr::accept(ast_visitor* visitor) {
visitor->visit_expr(this);
}
@ -365,4 +367,6 @@ return_expr::~return_expr() {
void return_expr::accept(ast_visitor* visitor) {
visitor->visit_return_expr(this);
}
}
}

View File

@ -6,6 +6,8 @@
#include <vector>
#include <unordered_map>
namespace nasal {
enum class expr_type:u32 {
ast_null = 0, // null node
ast_block, // code block
@ -669,3 +671,5 @@ public:
expr* get_value() {return value;}
void accept(ast_visitor*) override;
};
}

View File

@ -1,5 +1,7 @@
#include "nasal_codegen.h"
namespace nasal {
void codegen::init_file_map(const std::vector<std::string>& file_list) {
file_map = {};
for(usize i = 0; i<file_list.size(); ++i) {
@ -1255,3 +1257,5 @@ void codegen::symbol_dump(std::ostream& out) const {
}
}
}
}

View File

@ -28,6 +28,8 @@
#pragma warning (disable:4267)
#endif
namespace nasal {
class codegen {
private:
error err;
@ -136,3 +138,5 @@ public:
void print(std::ostream&);
void symbol_dump(std::ostream&) const;
};
}

View File

@ -1,5 +1,7 @@
#include "nasal_dbg.h"
namespace nasal {
void debug_prof_data::init_counter() {
for(usize i = 0; i<debug_prof_data::operand_size; ++i) {
operand_counter[i] = 0;
@ -276,3 +278,5 @@ void dbg::run(
imm.clear();
return;
}
}

View File

@ -9,6 +9,8 @@
#include <algorithm>
#include <unordered_map>
namespace nasal {
class debug_prof_data {
private:
static const usize operand_size = op_code_type::op_ret + 1;
@ -167,3 +169,5 @@ public:
bool
);
};
}

View File

@ -1,6 +1,8 @@
#include "nasal_err.h"
#include "repl.h"
namespace nasal {
#ifdef _WIN32
#include <windows.h> // use SetConsoleTextAttribute
struct for_reset {
@ -184,4 +186,6 @@ void error::err(
}
}
std::cerr << "\n\n";
}
}
}

View File

@ -8,6 +8,8 @@
#include "nasal.h"
namespace nasal {
struct span {
u32 begin_line;
u32 begin_column;
@ -65,3 +67,5 @@ public:
}
u32 geterr() const {return cnt;}
};
}

View File

@ -1,6 +1,8 @@
#include "nasal_import.h"
#include "symbol_finder.h"
namespace nasal {
linker::linker():
show_path(false), lib_loaded(false),
this_file(""), lib_path("") {
@ -346,3 +348,5 @@ const error& linker::link(
delete old_tree_root;
return err;
}
}

View File

@ -20,7 +20,9 @@
#include <vector>
class linker{
namespace nasal {
class linker {
private:
bool show_path;
bool lib_loaded;
@ -31,6 +33,7 @@ private:
std::vector<std::string> module_load_stack;
std::vector<std::string> envpath;
private:
bool import_check(expr*);
bool exist(const std::string&);
u16 find(const std::string&);
@ -53,3 +56,5 @@ public:
const auto& get_this_file() const {return this_file;}
const auto& get_lib_path() const {return lib_path;}
};
}

View File

@ -7,6 +7,8 @@
#include "nasal_lexer.h"
#include "repl.h"
namespace nasal {
bool lexer::skip(char c) {
return c==' ' || c=='\n' || c=='\t' || c=='\r' || c==0;
}
@ -372,3 +374,5 @@ const error& lexer::scan(const std::string& file) {
res = "";
return err;
}
}

View File

@ -19,6 +19,8 @@
#define S_ISREG(m) (((m)&0xF000)==0x8000)
#endif
namespace nasal {
enum class tok:u32 {
null=0, // null token (default token type)
num, // number literal
@ -182,3 +184,5 @@ public:
const error& scan(const std::string&);
const std::vector<token>& result() const {return toks;}
};
}

View File

@ -1,6 +1,8 @@
#include "nasal_ast.h"
#include "nasal_parse.h"
namespace nasal {
const error& parse::compile(const lexer& lexer) {
toks=lexer.result().data();
ptr=in_func=in_loop=0;
@ -1057,3 +1059,5 @@ return_expr* parse::return_expression() {
update_location(node);
return node;
}
}

View File

@ -7,6 +7,8 @@
#include "nasal_lexer.h"
#include "nasal_err.h"
namespace nasal {
class parse {
#define thisspan (toks[ptr].loc)
@ -155,4 +157,6 @@ public:
}
const error& compile(const lexer&);
static void easter_egg();
};
};
}

View File

@ -1,5 +1,7 @@
#include "nasal_vm.h"
namespace nasal {
void vm::init(
const std::vector<std::string>& strs,
const std::vector<f64>& nums,
@ -442,3 +444,5 @@ mcallh: exec_nodie(o_mcallh); // -0
ret: exec_nodie(o_ret ); // -2
#endif
}
}

View File

@ -13,6 +13,8 @@
#pragma warning (disable:4102)
#endif
namespace nasal {
class vm {
protected:
@ -954,4 +956,6 @@ inline void vm::o_ret() {
if (!ctx.pc) {
ngc.ctxreserve();
}
}
}
}

View File

@ -1,5 +1,7 @@
#include "optimizer.h"
namespace nasal {
void optimizer::const_string(
binary_operator* node,
string_literal* left_node,
@ -129,4 +131,6 @@ bool optimizer::visit_unary_operator(unary_operator* node) {
void optimizer::do_optimization(code_block* root) {
root->accept(this);
}
}
}

View File

@ -5,6 +5,8 @@
#include "nasal_ast.h"
#include "ast_visitor.h"
namespace nasal {
class optimizer:public ast_visitor {
private:
void const_string(binary_operator*, string_literal*, string_literal*);
@ -18,3 +20,5 @@ public:
public:
void do_optimization(code_block*);
};
}

View File

@ -6,6 +6,7 @@
#include "nasal_codegen.h"
#include "nasal_vm.h"
namespace nasal {
namespace repl {
std::string repl::readline(std::string prompt = ">>> ") {
@ -142,4 +143,5 @@ void repl::execute() {
}
}
}
}
}

View File

@ -8,6 +8,7 @@
#include <cstring>
#include <sstream>
namespace nasal {
namespace repl {
struct info {
@ -37,4 +38,5 @@ public:
void execute();
};
}
}
}

View File

@ -1,5 +1,7 @@
#include "symbol_finder.h"
namespace nasal {
bool symbol_finder::visit_definition_expr(definition_expr* node) {
if (node->get_variable_name()) {
symbols.push_back({
@ -40,4 +42,6 @@ const std::vector<symbol_finder::symbol_info>& symbol_finder::do_find(code_block
symbols.clear();
root->accept(this);
return symbols;
}
}
}

View File

@ -7,6 +7,8 @@
#include <sstream>
#include <vector>
namespace nasal {
class symbol_finder:public ast_visitor {
public:
struct symbol_info {
@ -22,4 +24,6 @@ public:
bool visit_function(function*) override;
bool visit_iter_expr(iter_expr*) override;
const std::vector<symbol_finder::symbol_info>& do_find(code_block*);
};
};
}