1
- # Bootstrap of Extended Covariant Script Generator v1.4.3
1
+ # Bootstrap of Extended Covariant Script Generator v1.5.4
2
2
#
3
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
4
# you may not use this file except in compliance with the License.
21
21
package ecs_bootstrap
22
22
23
23
import parsergen, ecs_parser, ecs_generator, codec, regex
24
+ import sdk_extension as sdk
24
25
25
- var wrapper_ver = "1.4.3 "
26
+ var wrapper_ver = "1.5.4 "
26
27
27
- function show_version ()
28
+ function show_version_simple ()
28
29
@begin
29
30
system.out.println(
30
31
"Version: " + ecs_generator.ecs_info.version + ", Wrapper " + wrapper_ver + "\n" +
31
32
"Copyright (C) 2017-2023 Michael Lee. All rights reserved.\n" +
32
- "Please visit http://covscript.org.cn/ for more information.\n\n" +
33
- "Metadata:\n" +
33
+ "Please visit http://covscript.org.cn/ for more information."
34
+ )
35
+ @end
36
+ end
37
+
38
+ class repl_instance
39
+ var codegen = new ecs_generator.generator
40
+ var parser = new parsergen.partial_parser_type
41
+ var unicode_cvt = null
42
+ var code_buff = new array
43
+ var repl_impl = null
44
+ var silent = false
45
+ function on_eof_hook(parser)
46
+ var tokens = null
47
+ loop
48
+ tokens = this.readline(".. ")
49
+ until tokens != null
50
+ foreach it in tokens do parser.lex.push_back(it)
51
+ return true
52
+ end
53
+ function initialize()
54
+ parser.on_eof_hook = on_eof_hook
55
+ codegen.code_buff := code_buff
56
+ codegen.file_name = "<REPL_ENV>"
57
+ codegen.minmal = true
58
+ end
59
+ function readline(prompt)
60
+ var line = null
61
+ loop
62
+ if !silent
63
+ system.out.print(prompt)
64
+ end
65
+ line = repl_impl.readline()
66
+ until line != null
67
+ code_buff.push_back(line)
68
+ if line == "@exit"
69
+ system.exit(0)
70
+ end
71
+ line += "\n"
72
+ var lexer = null
73
+ if unicode_cvt != null
74
+ lexer = new parsergen.unicode_lexer_type
75
+ lexer.cvt = unicode_cvt
76
+ else
77
+ lexer = new parsergen.lexer_type
78
+ end
79
+ lexer.pos[1] = code_buff.size - 1
80
+ var tokens = lexer.run(ecs_parser.grammar.lex, line)
81
+ if !lexer.error_log.empty()
82
+ parsergen.print_error("<REPL_ENV>", code_buff, lexer.error_log)
83
+ return new array
84
+ else
85
+ return tokens
86
+ end
87
+ end
88
+ function run(...args)
89
+ if !silent
90
+ system.out.println("Extended Covariant Script Interpreter REPL")
91
+ show_version_simple()
92
+ end
93
+ repl_impl = sdk.repl.create(args)
94
+ var header = codegen.repl_header().split({'\n'})
95
+ repl_impl.echo(false)
96
+ foreach line in header do repl_impl.exec(line)
97
+ repl_impl.echo(!silent)
98
+ loop
99
+ var tokens = null
100
+ loop
101
+ tokens = this.readline(">> ")
102
+ until tokens != null
103
+ if parser.run(ecs_parser.grammar.stx, tokens)
104
+ var ast = parser.production()
105
+ if ast != null
106
+ var code = codegen.repl_run(ast)
107
+ if code != null
108
+ code = code.split({'\n'})
109
+ try
110
+ foreach line in code
111
+ if !repl_impl.exec(line)
112
+ repl_impl.reset()
113
+ break
114
+ end
115
+ end
116
+ if repl_impl.has_exited()
117
+ system.exit(0)
118
+ end
119
+ catch e
120
+ system.out.println(e.what)
121
+ repl_impl.reset()
122
+ end
123
+ end
124
+ end
125
+ else
126
+ var err = parser.get_log(0)
127
+ parsergen.print_error("<REPL_ENV>", {code_buff..., ""}, err)
128
+ repl_impl.reset()
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ function show_version()
135
+ show_version_simple()
136
+ @begin
137
+ system.out.println(
138
+ "\nMetadata:\n" +
34
139
" STD Version: " + ecs_generator.ecs_info.std_version + "\n"
35
140
)
36
141
@end
40
145
function show_help()
41
146
@begin
42
147
system.out.println(
43
- "Usage: ecs [options...] <FILE> [arguments...]\n\n" +
44
- "Options:\n" +
45
- " Option Function\n" +
46
- " -h Show help information\n" +
47
- " -v Show version infomation\n" +
148
+ "Usage:\n" +
149
+ " ecs [options...] <FILE> [arguments...]\n" +
150
+ " ecs [options...]\n\n" +
151
+ "Interpreter Options:\n" +
48
152
" -f Disable compile cache\n" +
49
153
" -m Disable beautify\n" +
50
154
" -c Check grammar only\n" +
51
155
" -g Generate cSYM info\n" +
52
156
" -d Run debugger\n" +
53
- " -u <CHARSET> Set unicode charset\n" +
54
- " CHARSET = {\"UTF8\", \"GBK\"}\n" +
55
- " -i <PATH> Set import path\n" +
56
157
" -o <PATH> Set output path\n" +
57
- " -- <ARGS> Pass parameters to CovScript\n"
158
+ " -- <ARGS> Pass parameters to CovScript\n\n" +
159
+ "Interpreter REPL Options:\n" +
160
+ " -s Close the command prompt\n" +
161
+ " -r <ARGS...> Set arguments for REPL\n\n" +
162
+ "Common Options:\n" +
163
+ " Option Function\n" +
164
+ " -h Show help information\n" +
165
+ " -v Show version infomation\n" +
166
+ " -u <CHARSET> Set unicode charset\n" +
167
+ " CHARSET = {\"AUTO\", \"UTF8\", \"GBK\"}\n" +
168
+ " -i <PATH> Append import path\n"
58
169
)
59
170
@end
60
171
system.exit(0)
63
174
var compiler_args = new string
64
175
var file_name = new string
65
176
var arguments = new string
177
+ var args_arr = new array
66
178
var executor = "cs "
67
179
var no_hash = false
68
180
var csx_path = null
69
181
var unicode = null
70
182
var minmal = false
71
183
var no_run = false
184
+ var silent = false
72
185
var splash = null
73
186
var output = null
74
187
var csym = false
188
+ var repl = false
75
189
76
190
function process_args(cmd_args)
77
191
var index = 1
@@ -96,6 +210,14 @@ function process_args(cmd_args)
96
210
case "-c"
97
211
no_run = true
98
212
end
213
+ case "-s"
214
+ silent = true
215
+ end
216
+ case "-r"
217
+ repl = true
218
+ ++index
219
+ break
220
+ end
99
221
case "-g"
100
222
no_hash = true
101
223
csym = true
@@ -116,7 +238,10 @@ function process_args(cmd_args)
116
238
system.out.println("Error: Option \"-u\" not completed. Usage: \"ecs -u <CHARSET>\"")
117
239
system.exit(0)
118
240
end
119
- unicode = cmd_args[++index]
241
+ unicode = cmd_args[++index].toupper()
242
+ if unicode == "AUTO"
243
+ unicode = system.is_platform_windows()?"GBK":"UTF8"
244
+ end
120
245
end
121
246
case "-i"
122
247
if index == cmd_args.size - 1
@@ -144,12 +269,14 @@ function process_args(cmd_args)
144
269
++index
145
270
end
146
271
if index == cmd_args.size
147
- system.out.println("Error: no input file.")
148
- system.exit(0)
272
+ repl = true
273
+ end
274
+ if !repl
275
+ file_name = cmd_args[index++]
149
276
end
150
- file_name = cmd_args[index++]
151
277
while index < cmd_args.size
152
- arguments += " " + cmd_args[index++]
278
+ arguments += " " + cmd_args[index]
279
+ args_arr.push_back(cmd_args[index++])
153
280
end
154
281
end
155
282
183
310
184
311
function main(cmd_args)
185
312
process_args(cmd_args)
313
+ if repl
314
+ var instance = new repl_instance
315
+ instance.silent = silent
316
+ if unicode != null
317
+ var unicode_ext = context.import(runtime.get_import_path(), "unicode")
318
+ if unicode_ext == null
319
+ system.out.println("Error: unicode extension not installed yet.")
320
+ system.out.println("Run \'cspkg install extension --yes\' to enable unicode support.")
321
+ system.exit(0)
322
+ end
323
+ var cvt_name = unicode.toupper()
324
+ if cvt_name == "AUTO"
325
+ cvt_name = system.is_platform_windows()?"GBK":"UTF8"
326
+ end
327
+ if !codecvt_map.exist(cvt_name)
328
+ system.out.println("Error: unknown unicode charset \"" + cvt_name + "\".")
329
+ system.exit(0)
330
+ end
331
+ instance.unicode_cvt = codecvt_map.at(cvt_name)(unicode_ext)
332
+ ecs_parser.grammar.lex = ecs_parser.get_lexical([](str)->unicode_ext.build_wregex(instance.unicode_cvt.local2wide(str)), cvt_name)
333
+ end
334
+ if csx_path != null
335
+ sdk.set_import_path(runtime.get_import_path() + system.path.delimiter + csx_path)
336
+ instance.codegen.ecsx_path = csx_path.split({system.path.delimiter})
337
+ end
338
+ instance.run(args_arr...)
339
+ system.exit(0)
340
+ end
186
341
if !system.file.exist(file_name) || !match(regex.build(ecs_parser.grammar.ext), file_name)
187
342
system.out.println("Error: invalid input file.")
188
343
system.exit(0)
@@ -217,7 +372,7 @@ function main(cmd_args)
217
372
system.exit(0)
218
373
end
219
374
parser.unicode_cvt = codecvt_map.at(cvt_name)(unicode_ext)
220
- ecs_parser.grammar.lex = ecs_parser.get_lexical([](str)->unicode_ext.build_wregex(parser.unicode_cvt.local2wide(str)))
375
+ ecs_parser.grammar.lex = ecs_parser.get_lexical([](str)->unicode_ext.build_wregex(parser.unicode_cvt.local2wide(str)), cvt_name )
221
376
end
222
377
parser.add_grammar("ecs-lang", ecs_parser.grammar)
223
378
parser.from_file(file_name)
0 commit comments