📝 change span data from u32 to u64

This commit is contained in:
ValKmjolnir 2024-05-12 19:34:05 +08:00
parent eed59cfe07
commit 8e38764df0
17 changed files with 67 additions and 56 deletions

View File

@ -2,7 +2,6 @@
<img src="./doc/pic/header.png" style="width:600px"></img> <img src="./doc/pic/header.png" style="width:600px"></img>
![GitHub code size](https://img.shields.io/github/languages/code-size/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
![GitHub release(latest by date)](https://img.shields.io/github/v/release/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github) ![GitHub release(latest by date)](https://img.shields.io/github/v/release/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
[![license](https://img.shields.io/badge/license-GPLv2-green?style=flat-square&logo=github)](./LICENSE) [![license](https://img.shields.io/badge/license-GPLv2-green?style=flat-square&logo=github)](./LICENSE)
![downloads](https://img.shields.io/github/downloads/ValKmjolnir/Nasal-Interpreter/total.svg?style=flat-square&logo=github) ![downloads](https://img.shields.io/github/downloads/ValKmjolnir/Nasal-Interpreter/total.svg?style=flat-square&logo=github)

View File

@ -2,7 +2,6 @@
<img src="../doc/pic/header.png" style="width:600px"></img> <img src="../doc/pic/header.png" style="width:600px"></img>
![GitHub code size](https://img.shields.io/github/languages/code-size/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
![GitHub release(latest by date)](https://img.shields.io/github/v/release/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github) ![GitHub release(latest by date)](https://img.shields.io/github/v/release/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
[![license](https://img.shields.io/badge/license-GPLv2-green?style=flat-square&logo=github)](../LICENSE) [![license](https://img.shields.io/badge/license-GPLv2-green?style=flat-square&logo=github)](../LICENSE)
![downloads](https://img.shields.io/github/downloads/ValKmjolnir/Nasal-Interpreter/total.svg?style=flat-square&logo=github) ![downloads](https://img.shields.io/github/downloads/ValKmjolnir/Nasal-Interpreter/total.svg?style=flat-square&logo=github)

View File

@ -53,7 +53,9 @@ struct ghost_obj {
void ghost_for_test_destructor(void* ptr) { void ghost_for_test_destructor(void* ptr) {
std::cout << "ghost_for_test::destructor (0x"; std::cout << "ghost_for_test::destructor (0x";
std::cout << std::hex << reinterpret_cast<u64>(ptr) << std::dec << ") {\n"; std::cout << std::hex << reinterpret_cast<u64>(ptr) << std::dec << ") {\n";
delete static_cast<ghost_obj*>(ptr); delete static_cast<ghost_obj*>(ptr);
std::cout << " delete 0x" << std::hex; std::cout << " delete 0x" << std::hex;
std::cout << reinterpret_cast<u64>(ptr) << std::dec << ";\n"; std::cout << reinterpret_cast<u64>(ptr) << std::dec << ";\n";
std::cout << "}\n"; std::cout << "}\n";
@ -62,7 +64,9 @@ void ghost_for_test_destructor(void* ptr) {
void ghost_for_test_gc_marker(void* ptr, std::vector<var>* bfs_queue) { void ghost_for_test_gc_marker(void* ptr, std::vector<var>* bfs_queue) {
std::cout << "ghost_for_test::mark (0x"; std::cout << "ghost_for_test::mark (0x";
std::cout << std::hex << reinterpret_cast<u64>(ptr) << std::dec << ") {\n"; std::cout << std::hex << reinterpret_cast<u64>(ptr) << std::dec << ") {\n";
bfs_queue->push_back(static_cast<ghost_obj*>(ptr)->test_string); bfs_queue->push_back(static_cast<ghost_obj*>(ptr)->test_string);
std::cout << " mark 0x" << std::hex; std::cout << " mark 0x" << std::hex;
std::cout << reinterpret_cast<u64>(ptr) << std::dec << "->test_string;\n"; std::cout << reinterpret_cast<u64>(ptr) << std::dec << "->test_string;\n";
std::cout << "}\n"; std::cout << "}\n";
@ -86,8 +90,10 @@ var set_new_ghost(var* args, usize size, gc* ngc) {
return nil; return nil;
} }
f64 num = args[1].num(); f64 num = args[1].num();
reinterpret_cast<ghost_obj*>(res.ghost().pointer)->number = static_cast<u32>(num); reinterpret_cast<ghost_obj*>(res.ghost().pointer)->number = static_cast<u32>(num);
std::cout << "set_new_ghost: successfully set ghost.number = " << num << "\n"; std::cout << "set_new_ghost: successfully set ghost.number = " << num << "\n";
reinterpret_cast<ghost_obj*>(res.ghost().pointer)->test_string = ngc->newstr("just for test"); reinterpret_cast<ghost_obj*>(res.ghost().pointer)->test_string = ngc->newstr("just for test");
std::cout << "set_new_ghost: successfully set ghost.test_string = just for test\n"; std::cout << "set_new_ghost: successfully set ghost.test_string = just for test\n";
return nil; return nil;

View File

@ -35,8 +35,7 @@ private:
std::string format_location(const span& location) { std::string format_location(const span& location) {
std::stringstream ss; std::stringstream ss;
ss << " -> "; ss << " -> ";
ss << location.file << ":"; location.dump_begin(ss);
ss << location.begin_line << ":" << location.begin_column + 1;
ss << "\n"; ss << "\n";
return ss.str(); return ss.str();
} }

View File

@ -8,7 +8,7 @@
namespace nasal { namespace nasal {
enum class expr_type: u32 { enum class expr_type {
ast_null = 0, // null node ast_null = 0, // null node
ast_use, // use statement ast_use, // use statement
ast_block, // code block ast_block, // code block
@ -65,13 +65,13 @@ public:
expr(const span& location, expr_type node_type): expr(const span& location, expr_type node_type):
nd_loc(location), nd_type(node_type) {} nd_loc(location), nd_type(node_type) {}
virtual ~expr() = default; virtual ~expr() = default;
void set_begin(u32 line, u32 column) { void set_begin(u64 line, u64 column) {
nd_loc.begin_line = line; nd_loc.begin_line = line;
nd_loc.begin_column = column; nd_loc.begin_column = column;
} }
const span& get_location() const {return nd_loc;} const auto& get_location() const { return nd_loc; }
const u32 get_line() const {return nd_loc.begin_line;} const auto get_line() const { return nd_loc.begin_line; }
expr_type get_type() const {return nd_type;} auto get_type() const { return nd_type; }
void update_location(const span& location) { void update_location(const span& location) {
nd_loc.end_line = location.end_line; nd_loc.end_line = location.end_line;
nd_loc.end_column = location.end_column; nd_loc.end_column = location.end_column;

View File

@ -260,7 +260,7 @@ void dbg::run(
std::vector<u32> code; std::vector<u32> code;
std::vector<u16> code_file_index; std::vector<u16> code_file_index;
std::vector<u32> code_line; std::vector<u32> code_line;
for(auto& i : gen.codes()) { for(const auto& i : gen.codes()) {
code.push_back(i.op); code.push_back(i.op);
code_file_index.push_back(i.fidx); code_file_index.push_back(i.fidx);
code_line.push_back(i.line); code_line.push_back(i.line);

View File

@ -127,15 +127,15 @@ void error::err(
++cnt; ++cnt;
std::cerr std::cerr << red << stage << ": " << white << info << reset << "\n";
<< red << stage << ": " << white << info << reset << "\n" << cyan << " --> " std::cerr << cyan << " --> " << red;
<< red << loc.file << ":" << loc.begin_line << ":" << loc.begin_column+1 loc.dump_begin(std::cerr);
<< reset << "\n"; std::cerr << reset << "\n";
const usize maxlen = std::to_string(loc.end_line).length(); const usize maxlen = std::to_string(loc.end_line).length();
const std::string iden = identation(maxlen); const std::string iden = identation(maxlen);
for(u32 line = loc.begin_line; line<=loc.end_line; ++line) { for(u64 line = loc.begin_line; line<=loc.end_line; ++line) {
// skip line 0 // skip line 0
if (!line) { if (!line) {
continue; continue;
@ -164,25 +164,25 @@ void error::err(
// output underline // output underline
std::cerr << cyan << iden << " | " << reset; std::cerr << cyan << iden << " | " << reset;
if (loc.begin_line==loc.end_line) { if (loc.begin_line==loc.end_line) {
for(u32 i = 0; i<loc.begin_column; ++i) { for(u64 i = 0; i<loc.begin_column; ++i) {
std::cerr << char(" \t"[code[i]=='\t']); std::cerr << char(" \t"[code[i]=='\t']);
} }
for(u32 i = loc.begin_column; i<loc.end_column; ++i) { for(u64 i = loc.begin_column; i<loc.end_column; ++i) {
std::cerr << red << (code[i]=='\t'? "^^^^":"^") << reset; std::cerr << red << (code[i]=='\t'? "^^^^":"^") << reset;
} }
} else if (line==loc.begin_line) { } else if (line==loc.begin_line) {
for(u32 i = 0; i<loc.begin_column; ++i) { for(u64 i = 0; i<loc.begin_column; ++i) {
std::cerr << char(" \t"[code[i]=='\t']); std::cerr << char(" \t"[code[i]=='\t']);
} }
for(u32 i = loc.begin_column; i<code.size(); ++i) { for(u64 i = loc.begin_column; i<code.size(); ++i) {
std::cerr << red << (code[i]=='\t'? "^^^^":"^") << reset; std::cerr << red << (code[i]=='\t'? "^^^^":"^") << reset;
} }
} else if (loc.begin_line<line && line<loc.end_line) { } else if (loc.begin_line<line && line<loc.end_line) {
for(u32 i = 0; i<code.size(); ++i) { for(u64 i = 0; i<code.size(); ++i) {
std::cerr << red << (code[i]=='\t'? "^^^^":"^"); std::cerr << red << (code[i]=='\t'? "^^^^":"^");
} }
} else { } else {
for(u32 i = 0; i<loc.end_column; ++i) { for(u64 i = 0; i<loc.end_column; ++i) {
std::cerr << red << (code[i]=='\t'? "^^^^":"^"); std::cerr << red << (code[i]=='\t'? "^^^^":"^");
} }
} }

View File

@ -11,11 +11,15 @@
namespace nasal { namespace nasal {
struct span { struct span {
u32 begin_line; u64 begin_line;
u32 begin_column; u64 begin_column;
u32 end_line; u64 end_line;
u32 end_column; u64 end_column;
std::string file; std::string file;
void dump_begin(std::ostream& out) const {
out << file << ":" << begin_line << ":" << begin_column + 1;
}
}; };
std::ostream& back_white(std::ostream&); std::ostream& back_white(std::ostream&);
@ -46,7 +50,7 @@ private:
std::string identation(usize len) { std::string identation(usize len) {
return std::string(len, ' '); return std::string(len, ' ');
} }
std::string leftpad(u32 num, usize len) { std::string leftpad(u64 num, usize len) {
auto tmp = std::to_string(num); auto tmp = std::to_string(num);
while(tmp.length()<len) { while(tmp.length()<len) {
tmp = " "+tmp; tmp = " "+tmp;
@ -65,7 +69,7 @@ public:
std::exit(1); std::exit(1);
} }
} }
u32 geterr() const {return cnt;} auto geterr() const { return cnt; }
}; };
} }

View File

@ -96,7 +96,7 @@ void lexer::open(const std::string& file) {
} }
tok lexer::get_type(const std::string& str) { tok lexer::get_type(const std::string& str) {
return typetbl.count(str)? typetbl.at(str):tok::null; return token_mapper.count(str)? token_mapper.at(str):tok::null;
} }
std::string lexer::utf8_gen() { std::string lexer::utf8_gen() {
@ -138,8 +138,8 @@ std::string lexer::utf8_gen() {
} }
token lexer::id_gen() { token lexer::id_gen() {
u32 begin_line = line; u64 begin_line = line;
u32 begin_column = column; u64 begin_column = column;
std::string str = ""; std::string str = "";
while(ptr<res.size() && (is_id(res[ptr]) || is_dec(res[ptr]))) { while(ptr<res.size() && (is_id(res[ptr]) || is_dec(res[ptr]))) {
if (res[ptr]<0) { // utf-8 if (res[ptr]<0) { // utf-8
@ -157,8 +157,8 @@ token lexer::id_gen() {
} }
token lexer::num_gen() { token lexer::num_gen() {
u32 begin_line = line; u64 begin_line = line;
u32 begin_column = column; u64 begin_column = column;
// generate hex number // generate hex number
if (ptr+1<res.size() && res[ptr]=='0' && res[ptr+1]=='x') { if (ptr+1<res.size() && res[ptr]=='0' && res[ptr+1]=='x') {
std::string str = "0x"; std::string str = "0x";
@ -239,8 +239,8 @@ token lexer::num_gen() {
} }
token lexer::str_gen() { token lexer::str_gen() {
u32 begin_line = line; u64 begin_line = line;
u32 begin_column = column; u64 begin_column = column;
std::string str = ""; std::string str = "";
const char begin = res[ptr]; const char begin = res[ptr];
++column; ++column;
@ -298,8 +298,8 @@ token lexer::str_gen() {
} }
token lexer::single_opr() { token lexer::single_opr() {
u32 begin_line = line; u64 begin_line = line;
u32 begin_column = column; u64 begin_column = column;
std::string str(1, res[ptr]); std::string str(1, res[ptr]);
++column; ++column;
tok type = get_type(str); tok type = get_type(str);
@ -314,8 +314,8 @@ token lexer::single_opr() {
} }
token lexer::dots() { token lexer::dots() {
u32 begin_line = line; u64 begin_line = line;
u32 begin_column = column; u64 begin_column = column;
std::string str = "."; std::string str = ".";
if (ptr+2<res.size() && res[ptr+1]=='.' && res[ptr+2]=='.') { if (ptr+2<res.size() && res[ptr+1]=='.' && res[ptr+2]=='.') {
str += ".."; str += "..";
@ -326,8 +326,8 @@ token lexer::dots() {
} }
token lexer::calc_opr() { token lexer::calc_opr() {
u32 begin_line = line; u64 begin_line = line;
u32 begin_column = column; u64 begin_column = column;
// get calculation operator // get calculation operator
std::string str(1, res[ptr++]); std::string str(1, res[ptr++]);
if (ptr<res.size() && res[ptr]=='=') { if (ptr<res.size() && res[ptr]=='=') {

View File

@ -88,8 +88,8 @@ struct token {
class lexer { class lexer {
private: private:
u32 line; u64 line;
u32 column; u64 column;
usize ptr; usize ptr;
std::string filename; std::string filename;
std::string res; std::string res;
@ -98,7 +98,7 @@ private:
u64 invalid_char; u64 invalid_char;
std::vector<token> toks; std::vector<token> toks;
const std::unordered_map<std::string, tok> typetbl { const std::unordered_map<std::string, tok> token_mapper = {
{"use" ,tok::use }, {"use" ,tok::use },
{"true" ,tok::tktrue }, {"true" ,tok::tktrue },
{"false" ,tok::tkfalse }, {"false" ,tok::tkfalse },

View File

@ -101,7 +101,7 @@ struct opcode {
u8 op; // opcode u8 op; // opcode
u16 fidx; // source code file index u16 fidx; // source code file index
u32 num; // immediate num u32 num; // immediate num
u32 line; // location line of source code u64 line; // location line of source code
opcode() = default; opcode() = default;
opcode(const opcode&) = default; opcode(const opcode&) = default;
opcode& operator=(const opcode&) = default; opcode& operator=(const opcode&) = default;

View File

@ -105,7 +105,7 @@ bool parse::check_comma(const tok* panic_set) {
} }
bool parse::check_tuple() { bool parse::check_tuple() {
u32 check_ptr=ptr, curve=1, bracket=0, brace=0; u64 check_ptr = ptr, curve = 1, bracket = 0, brace = 0;
while(toks[++check_ptr].type!=tok::eof && curve) { while(toks[++check_ptr].type!=tok::eof && curve) {
switch(toks[check_ptr].type) { switch(toks[check_ptr].type) {
case tok::lcurve: ++curve; break; case tok::lcurve: ++curve; break;
@ -157,7 +157,7 @@ bool parse::check_in_curve_multi_definition() {
bool parse::check_special_call() { bool parse::check_special_call() {
// special call means like this: function_name(a:1,b:2,c:3); // special call means like this: function_name(a:1,b:2,c:3);
u32 check_ptr = ptr, curve = 1, bracket = 0, brace = 0; u64 check_ptr = ptr, curve = 1, bracket = 0, brace = 0;
while(toks[++check_ptr].type!=tok::eof && curve) { while(toks[++check_ptr].type!=tok::eof && curve) {
switch(toks[check_ptr].type) { switch(toks[check_ptr].type) {
case tok::lcurve: ++curve; break; case tok::lcurve: ++curve; break;

View File

@ -15,9 +15,9 @@ class parse {
#define prevspan (ptr!=0? toks[ptr-1].loc:toks[ptr].loc) #define prevspan (ptr!=0? toks[ptr-1].loc:toks[ptr].loc)
private: private:
u32 ptr; u64 ptr;
u32 in_func; // count function block u64 in_func; // count function block
u32 in_loop; // count loop block u64 in_loop; // count loop block
const token* toks; const token* toks;
code_block* root; code_block* root;
error err; error err;

View File

@ -451,7 +451,7 @@ void vm::run(
&&mcallv, &&mcallh, &&ret &&mcallv, &&mcallh, &&ret
}; };
std::vector<const void*> code; std::vector<const void*> code;
for(auto& i : gen.codes()) { for(const auto& i : gen.codes()) {
code.push_back(oprs[i.op]); code.push_back(oprs[i.op]);
imm.push_back(i.num); imm.push_back(i.num);
} }
@ -506,7 +506,7 @@ void vm::run(
&vm::o_ret &vm::o_ret
}; };
std::vector<nafunc> code; std::vector<nafunc> code;
for(auto& i : gen.codes()) { for(const auto& i : gen.codes()) {
code.push_back(oprs[i.op]); code.push_back(oprs[i.op]);
imm.push_back(i.num); imm.push_back(i.num);
} }

View File

@ -339,6 +339,7 @@ var builtin_left(context* ctx, gc* ngc) {
auto local = ctx->localr; auto local = ctx->localr;
var str = local[1]; var str = local[1];
var len = local[2]; var len = local[2];
if (!str.is_str()) { if (!str.is_str()) {
return nas_err("left", "\"string\" must be string"); return nas_err("left", "\"string\" must be string");
} }
@ -355,20 +356,23 @@ var builtin_right(context* ctx, gc* ngc) {
auto local = ctx->localr; auto local = ctx->localr;
var str = local[1]; var str = local[1];
var len = local[2]; var len = local[2];
if (!str.is_str()) { if (!str.is_str()) {
return nas_err("right", "\"string\" must be string"); return nas_err("right", "\"string\" must be string");
} }
if (!len.is_num()) { if (!len.is_num()) {
return nas_err("right", "\"length\" must be number"); return nas_err("right", "\"length\" must be number");
} }
i32 length = static_cast<i32>(len.num()); i32 length = static_cast<i32>(len.num());
i32 srclen = str.str().length(); i32 srclen = static_cast<i32>(str.str().length());
if (length>srclen) { if (length>srclen) {
length = srclen; length = srclen;
} }
if (length<0) { if (length<0) {
length = 0; length = 0;
} }
return ngc->newstr(str.str().substr(srclen-length, srclen)); return ngc->newstr(str.str().substr(srclen-length, srclen));
} }
@ -566,12 +570,12 @@ public:
} }
f64 elapsed_milliseconds() { f64 elapsed_milliseconds() {
auto duration = std::chrono::high_resolution_clock::now() - stamp; const auto duration = std::chrono::high_resolution_clock::now() - stamp;
return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(); return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
} }
f64 elapsed_microseconds() { f64 elapsed_microseconds() {
auto duration = std::chrono::high_resolution_clock::now() - stamp; const auto duration = std::chrono::high_resolution_clock::now() - stamp;
return std::chrono::duration_cast<std::chrono::microseconds>(duration).count(); return std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
} }
}; };

View File

@ -66,10 +66,12 @@ var builtin_values(context*, gc*);
var builtin_sleep(context*, gc*); var builtin_sleep(context*, gc*);
var builtin_platform(context*, gc*); var builtin_platform(context*, gc*);
var builtin_arch(context*, gc*); var builtin_arch(context*, gc*);
// md5 related functions // md5 related functions
std::string tohex(u32); std::string tohex(u32);
std::string md5(const std::string&); std::string md5(const std::string&);
var builtin_md5(context*, gc*); var builtin_md5(context*, gc*);
var builtin_maketimestamp(context*, gc*); var builtin_maketimestamp(context*, gc*);
var builtin_time_stamp(context*, gc*); var builtin_time_stamp(context*, gc*);
var builtin_elapsed_millisecond(context*, gc*); var builtin_elapsed_millisecond(context*, gc*);

View File

@ -4,8 +4,6 @@ import os
import platform import platform
import shutil import shutil
nasal_version = "11.2"
build_directory = pathlib.Path("build") build_directory = pathlib.Path("build")
if not os.path.exists(build_directory): if not os.path.exists(build_directory):
print("pack binaries failed: build directory not found") print("pack binaries failed: build directory not found")