2020-10-23 14:53:04 +08:00
|
|
|
#ifndef __NASAL_H__
|
|
|
|
#define __NASAL_H__
|
2020-12-14 23:43:00 +08:00
|
|
|
|
2021-05-31 19:10:59 +08:00
|
|
|
#include <stdint.h>
|
2020-10-23 14:53:04 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <iostream>
|
|
|
|
#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>
|
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-31 23:11:04 +08:00
|
|
|
#include <dirent.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>
|
|
|
|
#endif
|
|
|
|
|
2021-06-05 20:42:58 +08:00
|
|
|
inline double hex_to_double(const char* str)
|
2020-12-14 23:43:00 +08:00
|
|
|
{
|
2021-08-09 21:30:18 +08:00
|
|
|
double ret=0;
|
|
|
|
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
|
|
|
}
|
2021-06-05 20:42:58 +08:00
|
|
|
inline double oct_to_double(const char* str)
|
2020-12-14 23:43:00 +08:00
|
|
|
{
|
2021-08-09 21:30:18 +08:00
|
|
|
double ret=0;
|
|
|
|
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
|
|
|
}
|
2021-06-05 20:42:58 +08:00
|
|
|
inline double dec_to_double(const char* str)
|
2020-12-14 23:43:00 +08:00
|
|
|
{
|
2021-08-09 21:30:18 +08:00
|
|
|
double ret=0,negative=1,num_pow=0;
|
|
|
|
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
|
|
|
}
|
2021-06-05 20:42:58 +08:00
|
|
|
double str2num(const char* str)
|
2020-12-14 23:43:00 +08:00
|
|
|
{
|
2021-08-09 21:30:18 +08:00
|
|
|
bool is_negative=false;
|
|
|
|
double ret_num=0;
|
|
|
|
if(*str=='-' || *str=='+')
|
|
|
|
is_negative=(*str++=='-');
|
|
|
|
if(!*str)
|
|
|
|
return nan("");
|
|
|
|
if(str[0]=='0' && str[1]=='x')
|
|
|
|
ret_num=hex_to_double(str+2);
|
|
|
|
else if(str[0]=='0' && str[1]=='o')
|
|
|
|
ret_num=oct_to_double(str+2);
|
|
|
|
else
|
|
|
|
ret_num=dec_to_double(str);
|
|
|
|
return is_negative?-ret_num:ret_num;
|
2020-12-14 23:43:00 +08:00
|
|
|
}
|
|
|
|
|
2021-10-27 23:05:25 +08:00
|
|
|
std::string rawstr(const std::string& str)
|
2021-07-21 00:20:25 +08:00
|
|
|
{
|
2021-10-14 23:22:28 +08:00
|
|
|
std::string ret("");
|
2021-08-09 21:30:18 +08:00
|
|
|
for(auto i:str)
|
|
|
|
switch(i)
|
|
|
|
{
|
2021-10-14 23:22:28 +08:00
|
|
|
case '\a': ret+="\\a";break;
|
|
|
|
case '\b': ret+="\\b";break;
|
|
|
|
case '\f': ret+="\\f";break;
|
|
|
|
case '\n': ret+="\\n";break;
|
|
|
|
case '\r': ret+="\\r";break;
|
|
|
|
case '\t': ret+="\\t";break;
|
|
|
|
case '\v': ret+="\\v";break;
|
|
|
|
case '\0': ret+="\\0";break;
|
|
|
|
default: ret+=i; break;
|
2021-08-09 21:30:18 +08:00
|
|
|
}
|
2021-10-14 23:22:28 +08:00
|
|
|
return ret;
|
2021-07-21 00:20:25 +08:00
|
|
|
}
|
2020-10-23 14:53:04 +08:00
|
|
|
#include "nasal_lexer.h"
|
|
|
|
#include "nasal_ast.h"
|
|
|
|
#include "nasal_parse.h"
|
|
|
|
#include "nasal_import.h"
|
|
|
|
#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"
|
2020-10-23 14:53:04 +08:00
|
|
|
|
|
|
|
#endif
|