update parser to LL(1)

This commit is contained in:
Valk Richard Li 2021-02-13 13:28:20 +08:00
parent 944f713ee9
commit 996ac59c79
4 changed files with 202 additions and 553 deletions

119
README.md
View File

@ -261,127 +261,16 @@ a[-1,1,0:2,0:,:3,:,nil:8,3:nil,nil:nil];
## special function call ## special function call
This is of great use but is not very efficient.
```javascript ```javascript
a(x:0,y:1,z:2); a(x:0,y:1,z:2);
``` ```
## often used builtin functions ## built-in functions
Must import lib.nas or has these functions' definitions inside your code. Must import lib.nas or has these functions' definitions inside your code.
Also you could add builtin functions of your own(written in C/C++) to help you calculate things more quickly.(Advanced usage) Also you could add builtin functions of your own(written in C/C++) to help you calculate things more quickly.(Advanced usage)
```javascript Check built-in functions in lib.nas!
var import=func(filename)
{
nasal_call_import(filename);
return nil;
}
var print=func(elements...)
{
nasal_call_builtin_std_cout(elements);
return nil;
};
var append=func(vector,elements...)
{
nasal_call_builtin_push_back(vector,elements);
return nil;
}
var setsize=func(vector,size)
{
nasal_call_builtin_set_size(vector,size);
return nil;
}
var split=func(delimeter,string)
{
return nasal_call_builtin_split(delimeter,string);
}
var rand=func(seed=nil)
{
return nasal_call_builtin_rand(seed);
}
var id=func(thing)
{
return nasal_call_builtin_get_id(thing);
}
var int=func(value)
{
return nasal_call_builtin_trans_int(value);
}
var num=func(value)
{
return nasal_call_builtin_trans_num(value);
}
var pop=func(vector)
{
return nasal_call_builtin_pop_back(vector);
}
var str=func(number)
{
return nasal_call_builtin_trans_str(number);
}
var size=func(object)
{
return nasal_call_builtin_size(object);
}
var contains=func(hash,key)
{
return nasal_call_builtin_contains(hash,key);
}
var delete=func(hash,key)
{
nasal_call_builtin_delete(hash,key);
return;
}
var keys=func(hash)
{
return nasal_call_builtin_get_keys(hash);
}
var die=func(str)
{
nasal_call_builtin_die(str);
return nil;
}
var typeof=func(object)
{
return nasal_call_builtin_type(object);
}
var substr=func(str,begin,length)
{
return nasal_call_builtin_substr(str,begin,length);
}
var math=
{
e:2.7182818284590452354,
pi:3.14159265358979323846264338327950288,
sin:func(x)
{
return nasal_call_builtin_sin(x);
},
cos:func(x)
{
return nasal_call_builtin_cos(x);
},
tan:func(x)
{
return nasal_call_builtin_tan(x);
},
exp:func(x)
{
return nasal_call_builtin_exp(x);
},
ln:func(x)
{
return nasal_call_builtin_cpp_math_ln(x);
},
sqrt:func(x)
{
return nasal_call_builtin_cpp_math_sqrt(x);
},
atan2:func(x,y)
{
return nasal_call_builtin_cpp_atan2(x,y);
},
};
```

View File

@ -5,16 +5,16 @@ enum ast_node
{ {
ast_null=0,ast_root,ast_block, ast_null=0,ast_root,ast_block,
ast_nil,ast_num,ast_str,ast_id,ast_func,ast_hash,ast_vec, ast_nil,ast_num,ast_str,ast_id,ast_func,ast_hash,ast_vec,
ast_hashmember,ast_call,ast_call_hash,ast_call_vec,ast_call_func,ast_subvec, ast_hashmember,ast_call,ast_callh,ast_callv,ast_callf,ast_subvec,
ast_args,ast_default_arg,ast_dynamic_id, ast_args,ast_default_arg,ast_dynamic_id,
ast_and,ast_or, ast_and,ast_or,
ast_equal,ast_add_equal,ast_sub_equal,ast_mult_equal,ast_div_equal,ast_link_equal, ast_equal,ast_addeq,ast_subeq,ast_multeq,ast_diveq,ast_lnkeq,
ast_cmp_equal,ast_cmp_not_equal, ast_cmpeq,ast_neq,
ast_less,ast_leq, ast_less,ast_leq,
ast_grt,ast_geq, ast_grt,ast_geq,
ast_add,ast_sub,ast_mult,ast_div,ast_link, ast_add,ast_sub,ast_mult,ast_div,ast_link,
ast_unary_sub,ast_unary_not, ast_neg,ast_not,
ast_trinocular, ast_trino,
ast_for,ast_forindex,ast_foreach,ast_while,ast_new_iter, ast_for,ast_forindex,ast_foreach,ast_while,ast_new_iter,
ast_conditional,ast_if,ast_elsif,ast_else, ast_conditional,ast_if,ast_elsif,ast_else,
ast_multi_id,ast_multi_scalar, ast_multi_id,ast_multi_scalar,
@ -38,9 +38,9 @@ std::string ast_name(int type)
case ast_vec: return "vector"; case ast_vec: return "vector";
case ast_hashmember: return "hashmember"; case ast_hashmember: return "hashmember";
case ast_call: return "call"; case ast_call: return "call";
case ast_call_hash: return "callh"; case ast_callh: return "callh";
case ast_call_vec: return "callv"; case ast_callv: return "callv";
case ast_call_func: return "callf"; case ast_callf: return "callf";
case ast_subvec: return "subvec"; case ast_subvec: return "subvec";
case ast_args: return "args"; case ast_args: return "args";
case ast_default_arg: return "deflt_arg"; case ast_default_arg: return "deflt_arg";
@ -48,13 +48,13 @@ std::string ast_name(int type)
case ast_and: return "and"; case ast_and: return "and";
case ast_or: return "or"; case ast_or: return "or";
case ast_equal: return "="; case ast_equal: return "=";
case ast_add_equal: return "+="; case ast_addeq: return "+=";
case ast_sub_equal: return "-="; case ast_subeq: return "-=";
case ast_mult_equal: return "*="; case ast_multeq: return "*=";
case ast_div_equal: return "/="; case ast_diveq: return "/=";
case ast_link_equal: return "~="; case ast_lnkeq: return "~=";
case ast_cmp_equal: return "=="; case ast_cmpeq: return "==";
case ast_cmp_not_equal:return "!="; case ast_neq: return "!=";
case ast_less: return "<"; case ast_less: return "<";
case ast_leq: return "<="; case ast_leq: return "<=";
case ast_grt: return ">"; case ast_grt: return ">";
@ -64,9 +64,9 @@ std::string ast_name(int type)
case ast_mult: return "*"; case ast_mult: return "*";
case ast_div: return "/"; case ast_div: return "/";
case ast_link: return "~"; case ast_link: return "~";
case ast_unary_sub: return "unary-"; case ast_neg: return "unary-";
case ast_unary_not: return "unary!"; case ast_not: return "unary!";
case ast_trinocular: return "trino"; case ast_trino: return "trino";
case ast_for: return "for"; case ast_for: return "for";
case ast_forindex: return "forindex"; case ast_forindex: return "forindex";
case ast_foreach: return "foreach"; case ast_foreach: return "foreach";
@ -218,7 +218,7 @@ void nasal_ast::print_ast(int depth)
for(int i=0;i<depth;++i) indentation+="| "; for(int i=0;i<depth;++i) indentation+="| ";
indentation+=ast_name(this->type); indentation+=ast_name(this->type);
std::cout<<indentation; std::cout<<indentation;
if(this->type==ast_str || this->type==ast_id || this->type==ast_dynamic_id || this->type==ast_call_hash) if(this->type==ast_str || this->type==ast_id || this->type==ast_dynamic_id || this->type==ast_callh)
std::cout<<":"<<this->str; std::cout<<":"<<this->str;
else if(this->type==ast_num) else if(this->type==ast_num)
std::cout<<":"<<this->num; std::cout<<":"<<this->num;

View File

@ -27,7 +27,8 @@ enum token_type
tok_colon,tok_add,tok_sub,tok_mult,tok_div,tok_link,tok_not, tok_colon,tok_add,tok_sub,tok_mult,tok_div,tok_link,tok_not,
tok_eq, tok_eq,
tok_addeq,tok_subeq,tok_multeq,tok_diveq,tok_lnkeq, tok_addeq,tok_subeq,tok_multeq,tok_diveq,tok_lnkeq,
tok_cmpeq,tok_neq,tok_less,tok_leq,tok_grt,tok_geq tok_cmpeq,tok_neq,tok_less,tok_leq,tok_grt,tok_geq,
tok_eof
}; };
struct struct
@ -382,6 +383,8 @@ void nasal_lexer::scanner()
die("["+line_code+"_] unknown character.",line,line_code.length()); die("["+line_code+"_] unknown character.",line,line_code.length());
} }
} }
token tk(line,tok_eof,"");
token_list.push_back(tk);
return; return;
} }

File diff suppressed because it is too large Load Diff