Skip to content

Commit a91084c

Browse files
authored
Merge pull request #5 from covscript/repl
Support REPL
2 parents 1eab1a8 + 95e2665 commit a91084c

File tree

6 files changed

+258
-64
lines changed

6 files changed

+258
-64
lines changed

csbuild/ecs_bootstrap.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
"Name": "ecs_bootstrap",
44
"Info": "Extended CovScript(ECS Lang) Bootstrap",
55
"Author": "Michael Lee",
6-
"Version": "1.4.3",
6+
"Version": "1.5.4",
77
"Target": "imports/ecs_bootstrap.csp",
88
"Dependencies": [
99
"parsergen",
1010
"ecs_parser",
1111
"ecs_generator",
12+
"sdk_extension",
1213
"codec",
1314
"regex"
1415
]

csbuild/ecs_generator.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"Name": "ecs_generator",
44
"Info": "Extended CovScript(ECS Lang) Generator",
55
"Author": "Michael Lee",
6-
"Version": "4.0.0a-230405",
6+
"Version": "4.0.0b-230502",
77
"Target": "imports/ecs_generator.csp",
88
"Dependencies": [
99
"parsergen",

csbuild/ecs_parser.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"Name": "ecs_parser",
44
"Info": "Extended CovScript(ECS Lang) Parser",
55
"Author": "Michael Lee",
6-
"Version": "1.2.8",
6+
"Version": "1.3.0",
77
"Target": "imports/ecs_parser.csp",
88
"Dependencies": [
99
"parsergen",

imports/ecs_bootstrap.csp

Lines changed: 175 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Bootstrap of Extended Covariant Script Generator v1.4.3
1+
# Bootstrap of Extended Covariant Script Generator v1.5.4
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -21,16 +21,121 @@
2121
package ecs_bootstrap
2222

2323
import parsergen, ecs_parser, ecs_generator, codec, regex
24+
import sdk_extension as sdk
2425

25-
var wrapper_ver = "1.4.3"
26+
var wrapper_ver = "1.5.4"
2627

27-
function show_version()
28+
function show_version_simple()
2829
@begin
2930
system.out.println(
3031
"Version: " + ecs_generator.ecs_info.version + ", Wrapper " + wrapper_ver + "\n" +
3132
"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" +
34139
" STD Version: " + ecs_generator.ecs_info.std_version + "\n"
35140
)
36141
@end
@@ -40,21 +145,27 @@ end
40145
function show_help()
41146
@begin
42147
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" +
48152
" -f Disable compile cache\n" +
49153
" -m Disable beautify\n" +
50154
" -c Check grammar only\n" +
51155
" -g Generate cSYM info\n" +
52156
" -d Run debugger\n" +
53-
" -u <CHARSET> Set unicode charset\n" +
54-
" CHARSET = {\"UTF8\", \"GBK\"}\n" +
55-
" -i <PATH> Set import path\n" +
56157
" -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"
58169
)
59170
@end
60171
system.exit(0)
@@ -63,15 +174,18 @@ end
63174
var compiler_args = new string
64175
var file_name = new string
65176
var arguments = new string
177+
var args_arr = new array
66178
var executor = "cs "
67179
var no_hash = false
68180
var csx_path = null
69181
var unicode = null
70182
var minmal = false
71183
var no_run = false
184+
var silent = false
72185
var splash = null
73186
var output = null
74187
var csym = false
188+
var repl = false
75189

76190
function process_args(cmd_args)
77191
var index = 1
@@ -96,6 +210,14 @@ function process_args(cmd_args)
96210
case "-c"
97211
no_run = true
98212
end
213+
case "-s"
214+
silent = true
215+
end
216+
case "-r"
217+
repl = true
218+
++index
219+
break
220+
end
99221
case "-g"
100222
no_hash = true
101223
csym = true
@@ -116,7 +238,10 @@ function process_args(cmd_args)
116238
system.out.println("Error: Option \"-u\" not completed. Usage: \"ecs -u <CHARSET>\"")
117239
system.exit(0)
118240
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
120245
end
121246
case "-i"
122247
if index == cmd_args.size - 1
@@ -144,12 +269,14 @@ function process_args(cmd_args)
144269
++index
145270
end
146271
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++]
149276
end
150-
file_name = cmd_args[index++]
151277
while index < cmd_args.size
152-
arguments += " " + cmd_args[index++]
278+
arguments += " " + cmd_args[index]
279+
args_arr.push_back(cmd_args[index++])
153280
end
154281
end
155282

@@ -183,6 +310,34 @@ end
183310

184311
function main(cmd_args)
185312
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
186341
if !system.file.exist(file_name) || !match(regex.build(ecs_parser.grammar.ext), file_name)
187342
system.out.println("Error: invalid input file.")
188343
system.exit(0)
@@ -217,7 +372,7 @@ function main(cmd_args)
217372
system.exit(0)
218373
end
219374
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)
221376
end
222377
parser.add_grammar("ecs-lang", ecs_parser.grammar)
223378
parser.from_file(file_name)

0 commit comments

Comments
 (0)