-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwenyan.y
129 lines (122 loc) · 4.29 KB
/
wenyan.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
%{
#include <stdio.h>
#include <stdlib.h>
#include "AST.h"
#include "ChineseConvert.h"
#include "ErrorLog.h"
#include "lex.yy.c"
#include "token.h"
int yyerror(const std::string &msg)
{
wyLog(log_error)<<msg<<" "<<yytext;
return 1;
}
extern std::string token_name[2048];
Node *p, *root;
%}
%token NUMBER NAME INF CSTRING
%token FUNCTION FUNC_BEGIN FUNC_END FUNC_PARAM USE_FUNC USE_TO RETURN RETURN_IT
%token THIS_IS DEF NAMED_AS DEF_AS DEFNAMED_AS
%token TYPE_NUMBER TYPE_STRING
%token PRINT_IT PRINT
%token WHILE_TRUE DO TIMES END BREAK FORIN CONTAINS
%token LOGIC_EQUAL LOGIC_LESS LOGIC_GREATER
%token IF_BEGIN IF_END IF_STAT IF_ELSE
%token GET IT LENGTH
%token ARI_ADD ARI_SUB ARI_MUL ARI_DIV ARI_MOD
%token EVAL ROUND_IT FLOOR_IT
%token LIST_E LIST_CONCAT LIST_SLICE TO WITH
%%
file : sections {root = $$;};
sections: section | sections section;
section : function | sentencei ;
sentences : sentence | sentences sentencei ;
sentence : control_sentence | print_sentence | do_it_sentence | function_sentence
| loop_sentence | if_sentence
| concat_sentence | slice_sentence
| index_sentence | eval_sentence | define_sentence;
| comment_sentence
sentencei: sentence | assign_sentence;
value : func_use | NAME | NUMBER | LIST_E | INF | CSTRING;
valuei : IT | value;
valueref : IT | NAME;
expression_3 : valuei | valuei single_operator ;
expression_2 : expression_3 ari_operator_1 expression_2 | expression_3;
expression_1 : expression_2 ari_operator_0 expression_1 | expression_2;
expression_0 : expression_1 logic_operator expression_0 | expression_1;
logic_operator : LOGIC_EQUAL | LOGIC_LESS | LOGIC_GREATER ;
single_operator : LENGTH;
ari_operator_0 : ARI_ADD | ARI_SUB;
ari_operator_1 : ARI_MUL | ARI_DIV | ARI_MOD ;
func_use : USE_FUNC NAME params ;
control_sentence : return_sentence | BREAK ;
do_it_sentence : PRINT_IT | RETURN_IT | ROUND_IT | FLOOR_IT;
return_sentence : RETURN expression_0;
print_sentence : PRINT expression_0;
function_sentence : func_use;
loop_sentence : loop_statment sentences END ;
loop_statment : while_true_loop | do_times_loop | for_in_loop;
while_true_loop : WHILE_TRUE ;
do_times_loop : DO value TIMES ;
for_in_loop : FORIN expression_0 CONTAINS NAME IF_STAT;
param : NAME | NUMBER | IT | CSTRING;
params : params USE_TO param | USE_TO param ;
function : func_def sentences func_end ;
func_def : DEF FUNCTION DEFNAMED_AS NAME func_params ;
func_params : FUNC_PARAM func_param_packs ;
func_param_packs : func_param_packs func_param_pack | func_param_pack ;
func_param_pack : NUMBER type name_defs ;
func_end : THIS_IS NAME FUNC_END ;
type : TYPE_NUMBER | TYPE_STRING | LIST_E;
name_defs : name_defs name_def | name_def ;
name_def : NAMED_AS NAME ;
if_end : IF_END | END;
if_sentence : if_statment sentences if_end | if_statment sentences if_end IF_ELSE sentences if_end;
if_statment : IF_BEGIN expression_0 IF_STAT ;
index_sentence : GET expression_0 IT value;
eval_sentence : EVAL expression_0 | GET expression_0;
assign_sentence : NAMED_AS NAME;
define_sentence : DEF_AS NAME IF_END | DEFNAMED_AS NAME;
concat_sentence : LIST_CONCAT valueref WITH expression_0;
slice_sentence : LIST_SLICE value TO value;
comment_sentence : CSTRING;
%%
int main(int argc, char **argv)
{
if (argc < 2)
{
puts("Usage: ./wenyan <target>.wy");
return 0;
}
std::string fn = argv[1];
if (fn.size() < 3)
{
printf("Filename '%s' not valid\n", fn.c_str());
return 1;
}
init_token();
extern FILE* yyin;
ChineseConverter chinese_converter;
chinese_converter.loadConfig("parse.config");
FILE *fin = fopen(fn.c_str(),"r");
FILE *fout= fopen((fn.substr(0, fn.size() - 3) + ".js").c_str(),"w");
char c;
std::vector<char> str;
while((c=fgetc(fin))!=EOF){
str.push_back(c);
}
str.push_back(0);
auto p1 = chinese_converter.convertString(std::string(str.data()));
std::cout<<"ascii code:"<<std::endl;
std::cout<<p1<<std::endl;
FILE * filetmp = fopen("tmp.wytmp","w");
fwrite(p1.c_str(), p1.size(), 1, filetmp);
fclose(filetmp);
yyin = fopen("tmp.wytmp","r");
yyparse();
// std::cout<<"\nAST:\n"; root->printAll();
std::string js_code = root->codeGenerate(0, 0);
std::cout<<"code:"<<std::endl<<js_code<<std::endl;
fwrite(js_code.c_str(), 1, js_code.size(), fout);
return 0;
}