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
# -*- coding: utf-8 -*-
import sys
header = \
u'''
/****************************************************************************
**
** Copyright (C) 2021 @scriptiot
**
** EVM是一款通用化设计的虚拟机引擎,拥有语法解析前端接口、编译器、虚拟机和虚拟机扩展接口框架。
** 支持js、python、qml、lua等多种脚本语言,纯C开发,零依赖,支持主流 ROM > 50KB, RAM > 2KB的MCU;
** 自带垃圾回收(GC)先进的内存管理,采用最复杂的压缩算法,无内存碎片(大部分解释器都存在内存碎片)
** Version : 3.0
** Email : scriptiot@aliyun.com
** Website : https://github.com/scriptiot
** Licence: MIT Licence
****************************************************************************/
'''
def cstr_encode(text, splitLines=True, escapePercent=False):
output = "\""
count = len(text)
for i in range(count):
if text[i] == '\f':
output += "\\f"
elif text[i] == '\n':
if splitLines:
output += "\\n\"\n\""
else:
output += "\\n";
elif text[i] == '\r':
output += "\\r"
elif text[i] == '\t':
output += "\\t"
elif text[i] == '\"':
output += "\\\""
elif text[i] == '\\':
output += "\\\\"
elif text[i] == '%':
if escapePercent:
output += "%%"
else:
output += "%"
else:
output += text[i]
output += "\""
return output
def convert(fpath):
with open(fpath, "r") as f:
content = f.read()
ret = cstr_encode(content)
ccode = "%s\nconst char * appjs_content=\\\n%s;" % (header, ret)
with open("appjs.c", "w", encoding="utf-8") as f:
f.write(ccode)
return ccode
def convert_string(string):
return "%s\nconst char * appjs_content=\\\n%s;" % (header, cstr_encode(string))
if __name__ == '__main__':
ret = convert(sys.argv[1])
print(ret)