📝 fix MSVC warning in nasal_builtin.h & improve error output.
This commit is contained in:
parent
6c04487319
commit
a13e419518
|
@ -1,6 +1,5 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "../nasal.h"
|
#include "../nasal.h"
|
||||||
#include "../nasal_gc.h"
|
|
||||||
|
|
||||||
double fibonaci(double x){
|
double fibonaci(double x){
|
||||||
if(x<=2)
|
if(x<=2)
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#include "../nasal.h"
|
#include "../nasal.h"
|
||||||
#include "../nasal_gc.h"
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
@ -63,7 +62,7 @@ public:
|
||||||
peek_char=-1;
|
peek_char=-1;
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
read(0,&ch,1);
|
ssize_t tmp=read(0,&ch,1);
|
||||||
return ch;
|
return ch;
|
||||||
#else
|
#else
|
||||||
return getch();
|
return getch();
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#include "../nasal.h"
|
#include "../nasal.h"
|
||||||
#include "../nasal_gc.h"
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
28
nasal.h
28
nasal.h
|
@ -130,19 +130,19 @@ string rawstr(const string& str,const usize maxlen=0)
|
||||||
#endif
|
#endif
|
||||||
switch(i)
|
switch(i)
|
||||||
{
|
{
|
||||||
case '\0': ret+="\\0"; break;
|
case '\0': ret+="\\0"; break;
|
||||||
case '\a': ret+="\\a"; break;
|
case '\a': ret+="\\a"; break;
|
||||||
case '\b': ret+="\\b"; break;
|
case '\b': ret+="\\b"; break;
|
||||||
case '\t': ret+="\\t"; break;
|
case '\t': ret+="\\t"; break;
|
||||||
case '\n': ret+="\\n"; break;
|
case '\n': ret+="\\n"; break;
|
||||||
case '\v': ret+="\\v"; break;
|
case '\v': ret+="\\v"; break;
|
||||||
case '\f': ret+="\\f"; break;
|
case '\f': ret+="\\f"; break;
|
||||||
case '\r': ret+="\\r"; break;
|
case '\r': ret+="\\r"; break;
|
||||||
case '\033':ret+="\\e";break;
|
case '\033':ret+="\\e"; break;
|
||||||
case '\"': ret+="\\\"";break;
|
case '\"': ret+="\\\"";break;
|
||||||
case '\'': ret+="\\\'";break;
|
case '\'': ret+="\\\'";break;
|
||||||
case '\\': ret+="\\\\";break;
|
case '\\': ret+="\\\\";break;
|
||||||
default: ret+=i; break;
|
default: ret+=i; break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(maxlen && ret.length()>maxlen)
|
if(maxlen && ret.length()>maxlen)
|
||||||
|
@ -150,4 +150,6 @@ string rawstr(const string& str,const usize maxlen=0)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "nasal_gc.h" // declarations of nas_ref and nasal_gc
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -279,11 +279,11 @@ nas_ref builtin_u32or(nas_ref* local,nasal_gc& gc)
|
||||||
}
|
}
|
||||||
nas_ref builtin_u32nand(nas_ref* local,nasal_gc& gc)
|
nas_ref builtin_u32nand(nas_ref* local,nasal_gc& gc)
|
||||||
{
|
{
|
||||||
return {vm_num,(f64)(~(u32(local[1].num())&u32(local[2].num())))};
|
return {vm_num,(f64)(u32)(~(u32(local[1].num())&u32(local[2].num())))};
|
||||||
}
|
}
|
||||||
nas_ref builtin_u32not(nas_ref* local,nasal_gc& gc)
|
nas_ref builtin_u32not(nas_ref* local,nasal_gc& gc)
|
||||||
{
|
{
|
||||||
return {vm_num,(f64)(~u32(local[1].num()))};
|
return {vm_num,(f64)(u32)(~u32(local[1].num()))};
|
||||||
}
|
}
|
||||||
nas_ref builtin_pow(nas_ref* local,nasal_gc& gc)
|
nas_ref builtin_pow(nas_ref* local,nasal_gc& gc)
|
||||||
{
|
{
|
||||||
|
|
|
@ -436,7 +436,7 @@ void nasal_codegen::func_gen(const nasal_ast& ast)
|
||||||
in_iterloop.pop();
|
in_iterloop.pop();
|
||||||
code[lsize].num=local.back().size();
|
code[lsize].num=local.back().size();
|
||||||
if(local.back().size()>=STACK_DEPTH)
|
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());
|
||||||
local.pop_back();
|
local.pop_back();
|
||||||
|
|
||||||
if(!block.size() || block.child().back().type()!=ast_ret)
|
if(!block.size() || block.child().back().type()!=ast_ret)
|
||||||
|
@ -472,7 +472,7 @@ void nasal_codegen::call_id(const nasal_ast& ast)
|
||||||
{
|
{
|
||||||
gen(op_callb,i,ast.line());
|
gen(op_callb,i,ast.line());
|
||||||
if(local.empty())
|
if(local.empty())
|
||||||
die("builtin functions work in a local scope.",ast.line());
|
die("should warp native functions in local scope",ast.line());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
i32 index;
|
i32 index;
|
||||||
|
@ -491,7 +491,7 @@ void nasal_codegen::call_id(const nasal_ast& ast)
|
||||||
gen(op_callg,index,ast.line());
|
gen(op_callg,index,ast.line());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
die("undefined symbol \""+str+"\".",ast.line());
|
die("undefined symbol \""+str+"\"",ast.line());
|
||||||
}
|
}
|
||||||
|
|
||||||
void nasal_codegen::call_hash(const nasal_ast& ast)
|
void nasal_codegen::call_hash(const nasal_ast& ast)
|
||||||
|
@ -588,7 +588,7 @@ void nasal_codegen::mcall_id(const nasal_ast& ast)
|
||||||
for(u32 i=0;builtin[i].name;++i)
|
for(u32 i=0;builtin[i].name;++i)
|
||||||
if(builtin[i].name==str)
|
if(builtin[i].name==str)
|
||||||
{
|
{
|
||||||
die("cannot change builtin function.",ast.line());
|
die("cannot modify native function",ast.line());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
i32 index;
|
i32 index;
|
||||||
|
@ -607,7 +607,7 @@ void nasal_codegen::mcall_id(const nasal_ast& ast)
|
||||||
gen(op_mcallg,index,ast.line());
|
gen(op_mcallg,index,ast.line());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
die("undefined symbol \""+str+"\".",ast.line());
|
die("undefined symbol \""+str+"\"",ast.line());
|
||||||
}
|
}
|
||||||
|
|
||||||
void nasal_codegen::mcall_vec(const nasal_ast& ast)
|
void nasal_codegen::mcall_vec(const nasal_ast& ast)
|
||||||
|
@ -1235,7 +1235,7 @@ void nasal_codegen::compile(const nasal_parse& parse,const nasal_import& import)
|
||||||
block_gen(parse.ast()); // generate main block
|
block_gen(parse.ast()); // generate main block
|
||||||
gen(op_exit,0,0);
|
gen(op_exit,0,0);
|
||||||
if(global.size()>=STACK_DEPTH)
|
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);
|
||||||
nerr.chkerr();
|
nerr.chkerr();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -142,12 +142,12 @@ void nasal_lexer::open(const string& file)
|
||||||
struct stat buffer;
|
struct stat buffer;
|
||||||
if(stat(file.c_str(),&buffer)==0 && !S_ISREG(buffer.st_mode))
|
if(stat(file.c_str(),&buffer)==0 && !S_ISREG(buffer.st_mode))
|
||||||
{
|
{
|
||||||
nerr.err("lexer","<"+file+"> is not a regular file.");
|
nerr.err("lexer","<"+file+"> is not a regular file");
|
||||||
nerr.chkerr();
|
nerr.chkerr();
|
||||||
}
|
}
|
||||||
std::ifstream fin(file,std::ios::binary);
|
std::ifstream fin(file,std::ios::binary);
|
||||||
if(fin.fail())
|
if(fin.fail())
|
||||||
nerr.err("lexer","failed to open <"+file+">.");
|
nerr.err("lexer","failed to open <"+file+">");
|
||||||
else
|
else
|
||||||
nerr.load(file);
|
nerr.load(file);
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
@ -182,7 +182,7 @@ string nasal_lexer::utf8_gen()
|
||||||
string utf_info="0x"+chrhex(tmp[0]);
|
string utf_info="0x"+chrhex(tmp[0]);
|
||||||
for(u32 i=1;i<tmp.size();++i)
|
for(u32 i=1;i<tmp.size();++i)
|
||||||
utf_info+=" 0x"+chrhex(tmp[i]);
|
utf_info+=" 0x"+chrhex(tmp[i]);
|
||||||
die("invalid utf-8 character `"+utf_info+"`, make sure it is utf8-text file.");
|
die("invalid utf-8 character `"+utf_info+"`, make sure it is utf8-text file");
|
||||||
std::exit(1);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
str+=tmp;
|
str+=tmp;
|
||||||
|
@ -224,7 +224,7 @@ string nasal_lexer::num_gen()
|
||||||
str+=res[ptr++];
|
str+=res[ptr++];
|
||||||
column+=str.length();
|
column+=str.length();
|
||||||
if(str.length()<3)// "0x"
|
if(str.length()<3)// "0x"
|
||||||
die("invalid number `"+str+"`.");
|
die("invalid number `"+str+"`");
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
// generate oct number
|
// generate oct number
|
||||||
|
@ -236,7 +236,7 @@ string nasal_lexer::num_gen()
|
||||||
str+=res[ptr++];
|
str+=res[ptr++];
|
||||||
column+=str.length();
|
column+=str.length();
|
||||||
if(str.length()<3)// "0o"
|
if(str.length()<3)// "0o"
|
||||||
die("invalid number `"+str+"`.");
|
die("invalid number `"+str+"`");
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
// generate dec number
|
// generate dec number
|
||||||
|
@ -253,7 +253,7 @@ string nasal_lexer::num_gen()
|
||||||
if(str.back()=='.')
|
if(str.back()=='.')
|
||||||
{
|
{
|
||||||
column+=str.length();
|
column+=str.length();
|
||||||
die("invalid number `"+str+"`.");
|
die("invalid number `"+str+"`");
|
||||||
return "0";
|
return "0";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -268,7 +268,7 @@ string nasal_lexer::num_gen()
|
||||||
if(str.back()=='e' || str.back()=='E' || str.back()=='-' || str.back()=='+')
|
if(str.back()=='e' || str.back()=='E' || str.back()=='-' || str.back()=='+')
|
||||||
{
|
{
|
||||||
column+=str.length();
|
column+=str.length();
|
||||||
die("invalid number `"+str+"`.");
|
die("invalid number `"+str+"`");
|
||||||
return "0";
|
return "0";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -317,12 +317,12 @@ string nasal_lexer::str_gen()
|
||||||
// check if this string ends with a " or '
|
// check if this string ends with a " or '
|
||||||
if(ptr++>=res.size())
|
if(ptr++>=res.size())
|
||||||
{
|
{
|
||||||
die("get EOF when generating string.");
|
die("get EOF when generating string");
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
++column;
|
++column;
|
||||||
if(begin=='`' && str.length()!=1)
|
if(begin=='`' && str.length()!=1)
|
||||||
die("\'`\' is used for string that includes one character.");
|
die("\'`\' is used for string that includes one character");
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -369,7 +369,7 @@ void nasal_lexer::scan(const string& file)
|
||||||
++column;
|
++column;
|
||||||
u32 type=get_type(str);
|
u32 type=get_type(str);
|
||||||
if(!type)
|
if(!type)
|
||||||
die("invalid operator `"+str+"`.");
|
die("invalid operator `"+str+"`");
|
||||||
tokens.push_back({line,column,type,str});
|
tokens.push_back({line,column,type,str});
|
||||||
++ptr;
|
++ptr;
|
||||||
}
|
}
|
||||||
|
@ -397,7 +397,7 @@ void nasal_lexer::scan(const string& file)
|
||||||
{
|
{
|
||||||
++column;
|
++column;
|
||||||
char c=res[ptr++];
|
char c=res[ptr++];
|
||||||
die("invalid character 0x"+chrhex(c)+".");
|
die("invalid character 0x"+chrhex(c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tokens.push_back({line,column,tok_eof,"eof"});
|
tokens.push_back({line,column,tok_eof,"eof"});
|
||||||
|
|
|
@ -880,7 +880,7 @@ inline void nasal_vm::o_mcallv()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
die("cannot get memory space in other types");
|
die("cannot get memory space in this type");
|
||||||
}
|
}
|
||||||
inline void nasal_vm::o_mcallh()
|
inline void nasal_vm::o_mcallh()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue