From 972ad49a4f44e0c6fa2fa98e4d46d2ecc6b7e318 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Mon, 5 Sep 2022 00:41:36 +0800 Subject: [PATCH] :zap: improve error output info generated by codegen. --- nasal_ast.h | 10 ++++-- nasal_codegen.h | 23 ++++++------ nasal_err.h | 18 +++++++--- nasal_import.h | 10 +++--- nasal_parse.h | 94 +++++++++++++++++++++++++------------------------ 5 files changed, 88 insertions(+), 67 deletions(-) diff --git a/nasal_ast.h b/nasal_ast.h index c114d89..e533727 100644 --- a/nasal_ast.h +++ b/nasal_ast.h @@ -129,12 +129,13 @@ class nasal_ast { private: u32 _line; + u32 _column; u32 _type; f64 _num; string _str; std::vector _child; public: - nasal_ast(const u32 l=0,const u32 t=ast_null):_line(l),_type(t),_num(0){} + nasal_ast(const u32 l,const u32 c,const u32 t):_line(l),_column(c),_type(t),_num(0){} nasal_ast(const nasal_ast&); nasal_ast(nasal_ast&&); void tree(); @@ -155,6 +156,7 @@ public: void set_num(const f64 n){_num=n;} inline u32 line() const {return _line;} + inline u32 col() const {return _column;} inline u32 type() const {return _type;} inline f64 num() const {return _num;} inline const string& str() const {return _str;} @@ -166,6 +168,7 @@ nasal_ast::nasal_ast(const nasal_ast& tmp): _str(tmp._str),_child(tmp._child) { _line=tmp._line; + _column=tmp._column; _type=tmp._type; _num =tmp._num; } @@ -173,6 +176,7 @@ nasal_ast::nasal_ast(const nasal_ast& tmp): nasal_ast::nasal_ast(nasal_ast&& tmp) { _line=tmp._line; + _column=tmp._column; _type=tmp._type; _num =tmp._num; _str.swap(tmp._str); @@ -182,6 +186,7 @@ nasal_ast::nasal_ast(nasal_ast&& tmp) nasal_ast& nasal_ast::operator=(const nasal_ast& tmp) { _line=tmp._line; + _column=tmp._column; _type=tmp._type; _num=tmp._num; _str=tmp._str; @@ -192,6 +197,7 @@ nasal_ast& nasal_ast::operator=(const nasal_ast& tmp) nasal_ast& nasal_ast::operator=(nasal_ast&& tmp) { _line=tmp._line; + _column=tmp._column; _type=tmp._type; _num=tmp._num; _str.swap(tmp._str); @@ -201,7 +207,7 @@ nasal_ast& nasal_ast::operator=(nasal_ast&& tmp) void nasal_ast::clear() { - _line=0; + _line=_column=0; _num=0; _str=""; _type=ast_null; diff --git a/nasal_codegen.h b/nasal_codegen.h index 81ae647..595ba22 100644 --- a/nasal_codegen.h +++ b/nasal_codegen.h @@ -209,7 +209,7 @@ private: std::stack fbstk; std::stack festk; - void die(const string&,const u32); + void die(const string&,const u32,const u32); void regist_num(const f64); void regist_str(const string&); void find_symbol(const nasal_ast&); @@ -260,10 +260,13 @@ public: const std::vector& codes() const {return code;} }; -void nasal_codegen::die(const string& info,const u32 line) +void nasal_codegen::die(const string& info,const u32 line,const u32 col) { nerr.load(file[fileindex]); - nerr.err("code",line,info); + if(col) + nerr.err("code",line,col,info); + else + nerr.err("code",line,info); } void nasal_codegen::regist_num(const f64 num) @@ -409,7 +412,7 @@ void nasal_codegen::func_gen(const nasal_ast& ast) { const string& str=tmp.str(); if(str=="me") - die("\"me\" should not be a parameter",tmp.line()); + die("\"me\" should not be a parameter",tmp.line(),tmp.col()); regist_str(str); switch(tmp.type()) { @@ -436,7 +439,7 @@ void nasal_codegen::func_gen(const nasal_ast& ast) in_iterloop.pop(); code[lsize].num=local.back().size(); if(local.back().size()>=STACK_DEPTH) - die("too many local variants: "+std::to_string(local.back().size()),block.line()); + die("too many local variants: "+std::to_string(local.back().size()),block.line(),0); local.pop_back(); if(!block.size() || block.child().back().type()!=ast_ret) @@ -472,7 +475,7 @@ void nasal_codegen::call_id(const nasal_ast& ast) { gen(op_callb,i,ast.line()); if(local.empty()) - die("should warp native functions in local scope",ast.line()); + die("should warp native functions in local scope",ast.line(),ast.col()); return; } i32 index; @@ -491,7 +494,7 @@ void nasal_codegen::call_id(const nasal_ast& ast) gen(op_callg,index,ast.line()); return; } - die("undefined symbol \""+str+"\"",ast.line()); + die("undefined symbol \""+str+"\"",ast.line(),ast.col()); } void nasal_codegen::call_hash(const nasal_ast& ast) @@ -588,7 +591,7 @@ void nasal_codegen::mcall_id(const nasal_ast& ast) for(u32 i=0;builtin[i].name;++i) if(builtin[i].name==str) { - die("cannot modify native function",ast.line()); + die("cannot modify native function",ast.line(),ast.col()); return; } i32 index; @@ -607,7 +610,7 @@ void nasal_codegen::mcall_id(const nasal_ast& ast) gen(op_mcallg,index,ast.line()); return; } - die("undefined symbol \""+str+"\"",ast.line()); + die("undefined symbol \""+str+"\"",ast.line(),ast.col()); } void nasal_codegen::mcall_vec(const nasal_ast& ast) @@ -1235,7 +1238,7 @@ void nasal_codegen::compile(const nasal_parse& parse,const nasal_import& import) block_gen(parse.ast()); // generate main block gen(op_exit,0,0); if(global.size()>=STACK_DEPTH) - die("too many global variants: "+std::to_string(global.size()),0); + die("too many global variants: "+std::to_string(global.size()),0,0); nerr.chkerr(); } diff --git a/nasal_err.h b/nasal_err.h index b59020c..2b11d03 100644 --- a/nasal_err.h +++ b/nasal_err.h @@ -123,8 +123,13 @@ public: std::cerr< "< "<