2020-10-23 14:53:04 +08:00
|
|
|
#ifndef __NASAL_H__
|
|
|
|
#define __NASAL_H__
|
2022-05-19 20:09:23 +08:00
|
|
|
#define __nasver "10.0"
|
2022-04-16 19:45:55 +08:00
|
|
|
|
2022-07-16 01:02:33 +08:00
|
|
|
#ifndef _MSC_VER
|
2020-10-23 14:53:04 +08:00
|
|
|
#include <unistd.h>
|
2022-07-16 01:02:33 +08:00
|
|
|
#include <dirent.h>
|
2022-07-18 23:54:44 +08:00
|
|
|
#else
|
2022-07-16 16:53:11 +08:00
|
|
|
#include <io.h>
|
|
|
|
#include <direct.h>
|
|
|
|
#endif
|
|
|
|
|
2022-05-15 20:35:20 +08:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdio>
|
2020-10-23 14:53:04 +08:00
|
|
|
#include <iostream>
|
2022-07-09 23:36:14 +08:00
|
|
|
#include <iomanip>
|
2020-10-23 14:53:04 +08:00
|
|
|
#include <fstream>
|
2020-12-14 23:43:00 +08:00
|
|
|
#include <sstream>
|
2020-10-23 14:53:04 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cmath>
|
|
|
|
#include <list>
|
|
|
|
#include <stack>
|
|
|
|
#include <queue>
|
|
|
|
#include <vector>
|
2021-03-01 15:54:58 +08:00
|
|
|
#include <unordered_map>
|
2022-07-07 14:35:10 +08:00
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
2020-10-23 14:53:04 +08:00
|
|
|
|
2021-10-27 23:05:25 +08:00
|
|
|
#include <sys/stat.h>
|
2021-10-29 19:52:49 +08:00
|
|
|
#include <fcntl.h>
|
2021-10-27 23:05:25 +08:00
|
|
|
|
2021-12-03 19:31:03 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
2022-03-16 18:44:38 +08:00
|
|
|
#include <sys/wait.h>
|
2021-12-03 19:31:03 +08:00
|
|
|
#endif
|
|
|
|
|
2022-07-23 12:35:21 +08:00
|
|
|
using i32=std::int32_t;
|
|
|
|
using i64=std::int64_t;
|
2022-07-23 17:00:25 +08:00
|
|
|
using u8=std::uint8_t;
|
2022-07-23 12:35:21 +08:00
|
|
|
using u16=std::uint16_t;
|
|
|
|
using u32=std::uint32_t;
|
|
|
|
using u64=std::uint64_t;
|
|
|
|
using usize=std::size_t;
|
|
|
|
using f64=double;
|
2022-07-23 17:00:25 +08:00
|
|
|
using std::string;
|
2022-07-23 12:35:21 +08:00
|
|
|
|
|
|
|
const u32 STACK_DEPTH=2048;
|
|
|
|
|
|
|
|
inline f64 hex_to_double(const char* str)
|
2020-12-14 23:43:00 +08:00
|
|
|
{
|
2022-07-23 12:35:21 +08:00
|
|
|
f64 ret=0;
|
2021-08-09 21:30:18 +08:00
|
|
|
for(;*str;++str)
|
|
|
|
{
|
|
|
|
ret*=16;
|
|
|
|
if('0'<=*str && *str<='9')
|
|
|
|
ret+=(*str-'0');
|
|
|
|
else if('a'<=*str && *str<='f')
|
|
|
|
ret+=(*str-'a'+10);
|
|
|
|
else if('A'<=*str && *str<='F')
|
|
|
|
ret+=(*str-'A'+10);
|
|
|
|
else
|
|
|
|
return nan("");
|
|
|
|
}
|
|
|
|
return ret;
|
2020-12-14 23:43:00 +08:00
|
|
|
}
|
2022-07-23 12:35:21 +08:00
|
|
|
inline f64 oct_to_double(const char* str)
|
2020-12-14 23:43:00 +08:00
|
|
|
{
|
2022-07-23 12:35:21 +08:00
|
|
|
f64 ret=0;
|
2021-08-09 21:30:18 +08:00
|
|
|
for(;*str;++str)
|
|
|
|
{
|
|
|
|
ret*=8;
|
|
|
|
if('0'<=*str && *str<'8')
|
|
|
|
ret+=(*str-'0');
|
|
|
|
else
|
|
|
|
return nan("");
|
|
|
|
}
|
|
|
|
return ret;
|
2020-12-14 23:43:00 +08:00
|
|
|
}
|
2022-07-23 12:35:21 +08:00
|
|
|
inline f64 dec_to_double(const char* str)
|
2020-12-14 23:43:00 +08:00
|
|
|
{
|
2022-07-23 12:35:21 +08:00
|
|
|
f64 ret=0,negative=1,num_pow=0;
|
2021-08-09 21:30:18 +08:00
|
|
|
while('0'<=*str && *str<='9')
|
|
|
|
ret=ret*10+(*str++-'0');
|
|
|
|
if(!*str) return ret;
|
|
|
|
if(*str=='.')
|
|
|
|
{
|
|
|
|
if(!*++str) return nan("");
|
|
|
|
num_pow=0.1;
|
|
|
|
while('0'<=*str && *str<='9')
|
|
|
|
{
|
|
|
|
ret+=num_pow*(*str++-'0');
|
|
|
|
num_pow*=0.1;
|
|
|
|
}
|
|
|
|
if(!*str) return ret;
|
|
|
|
}
|
|
|
|
if(*str!='e' && *str!='E')
|
|
|
|
return nan("");
|
|
|
|
if(!*++str) return nan("");
|
|
|
|
if(*str=='-' || *str=='+')
|
|
|
|
negative=(*str++=='-'? -1:1);
|
|
|
|
if(!*str) return nan("");
|
|
|
|
num_pow=0;
|
|
|
|
for(;*str;++str)
|
|
|
|
{
|
|
|
|
if('0'<=*str && *str<='9')
|
|
|
|
num_pow=num_pow*10+(*str-'0');
|
|
|
|
else
|
|
|
|
return nan("");
|
|
|
|
}
|
|
|
|
return ret*std::pow(10,negative*num_pow);
|
2020-12-14 23:43:00 +08:00
|
|
|
}
|
2022-07-23 12:35:21 +08:00
|
|
|
f64 str2num(const char* str)
|
2020-12-14 23:43:00 +08:00
|
|
|
{
|
2022-07-08 23:00:36 +08:00
|
|
|
bool negative=false;
|
2022-07-23 12:35:21 +08:00
|
|
|
f64 res=0;
|
2021-08-09 21:30:18 +08:00
|
|
|
if(*str=='-' || *str=='+')
|
2022-07-08 23:00:36 +08:00
|
|
|
negative=(*str++=='-');
|
2021-08-09 21:30:18 +08:00
|
|
|
if(!*str)
|
|
|
|
return nan("");
|
|
|
|
if(str[0]=='0' && str[1]=='x')
|
2022-07-08 23:00:36 +08:00
|
|
|
res=hex_to_double(str+2);
|
2021-08-09 21:30:18 +08:00
|
|
|
else if(str[0]=='0' && str[1]=='o')
|
2022-07-08 23:00:36 +08:00
|
|
|
res=oct_to_double(str+2);
|
2021-08-09 21:30:18 +08:00
|
|
|
else
|
2022-07-08 23:00:36 +08:00
|
|
|
res=dec_to_double(str);
|
|
|
|
return negative?-res:res;
|
2020-12-14 23:43:00 +08:00
|
|
|
}
|
|
|
|
|
2022-07-08 18:16:00 +08:00
|
|
|
int utf8_hdchk(const char head)
|
2022-05-07 16:50:13 +08:00
|
|
|
{
|
2022-07-08 00:42:56 +08:00
|
|
|
// RFC-2279 but now we use RFC-3629 so nbytes is less than 4
|
2022-07-23 12:35:21 +08:00
|
|
|
const u8 c=(u8)head;
|
2022-05-07 16:50:13 +08:00
|
|
|
if((c>>5)==0x06) // 110x xxxx (10xx xxxx)^1
|
2022-07-08 18:16:00 +08:00
|
|
|
return 1;
|
2022-05-07 16:50:13 +08:00
|
|
|
if((c>>4)==0x0e) // 1110 xxxx (10xx xxxx)^2
|
2022-07-08 18:16:00 +08:00
|
|
|
return 2;
|
2022-05-07 16:50:13 +08:00
|
|
|
if((c>>3)==0x1e) // 1111 0xxx (10xx xxxx)^3
|
2022-07-08 18:16:00 +08:00
|
|
|
return 3;
|
|
|
|
return 0;
|
2022-05-07 16:50:13 +08:00
|
|
|
}
|
|
|
|
|
2022-07-23 17:00:25 +08:00
|
|
|
string chrhex(const char c)
|
2022-06-30 23:15:10 +08:00
|
|
|
{
|
2022-07-23 17:00:25 +08:00
|
|
|
const char hextbl[]="0123456789abcdef";
|
|
|
|
return {hextbl[(c&0xf0)>>4],hextbl[c&0x0f]};
|
2022-06-30 23:15:10 +08:00
|
|
|
}
|
|
|
|
|
2022-07-23 17:00:25 +08:00
|
|
|
string rawstr(const string& str,const usize maxlen=0)
|
2021-07-21 00:20:25 +08:00
|
|
|
{
|
2022-07-23 17:00:25 +08:00
|
|
|
string ret("");
|
2021-08-09 21:30:18 +08:00
|
|
|
for(auto i:str)
|
2022-03-23 22:54:07 +08:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
// windows ps or cmd doesn't output unicode normally
|
2022-07-06 22:21:01 +08:00
|
|
|
// if 'chcp65001' is not enabled, we output the hex
|
2022-03-23 22:54:07 +08:00
|
|
|
if(i<=0)
|
|
|
|
{
|
2022-07-08 00:42:56 +08:00
|
|
|
ret+="\\x"+chrhex(i);
|
2022-03-23 22:54:07 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2021-08-09 21:30:18 +08:00
|
|
|
switch(i)
|
|
|
|
{
|
2022-05-07 02:22:49 +08:00
|
|
|
case '\0': ret+="\\0"; break;
|
|
|
|
case '\a': ret+="\\a"; break;
|
|
|
|
case '\b': ret+="\\b"; break;
|
|
|
|
case '\t': ret+="\\t"; break;
|
|
|
|
case '\n': ret+="\\n"; break;
|
|
|
|
case '\v': ret+="\\v"; break;
|
|
|
|
case '\f': ret+="\\f"; break;
|
|
|
|
case '\r': ret+="\\r"; break;
|
2022-07-16 01:02:33 +08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
case '\033':ret+="\\e";break;
|
|
|
|
#else
|
2022-07-06 22:21:01 +08:00
|
|
|
case '\e': ret+="\\e"; break;
|
2022-07-16 01:02:33 +08:00
|
|
|
#endif
|
2022-05-07 02:22:49 +08:00
|
|
|
case '\"': ret+="\\\"";break;
|
2022-07-06 22:21:01 +08:00
|
|
|
case '\'': ret+="\\\'";break;
|
|
|
|
case '\\': ret+="\\\\";break;
|
2022-05-07 02:22:49 +08:00
|
|
|
default: ret+=i; break;
|
2021-08-09 21:30:18 +08:00
|
|
|
}
|
2022-03-23 22:54:07 +08:00
|
|
|
}
|
2022-07-08 00:42:56 +08:00
|
|
|
if(maxlen && ret.length()>maxlen)
|
|
|
|
ret=ret.substr(0,maxlen)+"...";
|
2021-10-14 23:22:28 +08:00
|
|
|
return ret;
|
2021-07-21 00:20:25 +08:00
|
|
|
}
|
2021-12-23 21:15:50 +08:00
|
|
|
#include "nasal_err.h"
|
2020-10-23 14:53:04 +08:00
|
|
|
#include "nasal_lexer.h"
|
|
|
|
#include "nasal_ast.h"
|
|
|
|
#include "nasal_parse.h"
|
|
|
|
#include "nasal_import.h"
|
2022-01-22 00:41:08 +08:00
|
|
|
#include "nasal_opt.h"
|
2020-10-23 14:53:04 +08:00
|
|
|
#include "nasal_gc.h"
|
|
|
|
#include "nasal_builtin.h"
|
2020-12-06 21:07:40 +08:00
|
|
|
#include "nasal_codegen.h"
|
2021-02-10 00:12:22 +08:00
|
|
|
#include "nasal_vm.h"
|
2021-12-20 21:33:22 +08:00
|
|
|
#include "nasal_dbg.h"
|
2020-10-23 14:53:04 +08:00
|
|
|
|
|
|
|
#endif
|