-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstart.py
More file actions
110 lines (97 loc) · 3.75 KB
/
Copy pathstart.py
File metadata and controls
110 lines (97 loc) · 3.75 KB
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
from toTs import ToTs
from toTaskNew import ToTaskNew
import streamlit as st
import os
from io import BytesIO
import subprocess
import json
st.title('LLM-Based Java Concurrent Program to ArkTS Converter')
filename = "ThreadBridge.ets"
with open(filename, 'r') as file:
Ku = file.read()
file_bytes = Ku.encode('utf-8')
st.download_button(label='download ThreadBridge', data=BytesIO(file_bytes), file_name=filename)
uploaded_file = st.file_uploader('Upload a file')
name = ' '
if uploaded_file is not None:
st.write('File uploaded successfully!')
file_content = uploaded_file.read() # Display file content
name = uploaded_file.name[:-5]
with open('Tmp.java', 'w') as new_file:
new_file.write(file_content.decode())
def run_jar_with_input():
result = subprocess.run(['java', '-jar', 'java_target/maven-demo.jar'], capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError("Java environment not detected, terminating the program.")
return result.stdout
def deleteZhu(mycode):
if mycode[0] == '`':
lines = mycode.splitlines()
# 使用切片操作去掉第一行和最后一行
lines = lines[1:-1]
if lines[-1][0] == '`':
lines = lines[0:-1]
# 将处理后的列表重新拼接为字符串
result_string = "\n".join(lines)
return result_string
else:
return mycode
if st.button('run'):
ast_result = run_jar_with_input()
st.write(ast_result)
file_path = 'result.json'
with open(file_path, 'r') as file:
result = json.load(file)
code = result['ast_output']
origin_code = code
flag = result['flag']
flag = flag % 4
if flag / 2 == 1:
code = ''
refer = ("import { SynStart, SynEnd, wait, notify, SharedBoolean, SharedString, SharedNumber, Syc, isMainThread, addFunc, Runnable, Thread } from "
"'./ThreadBridge';\n\n") + code
for c in result['classes']:
class_code = c['class_code']
class_flag = c['class_flag']
tmp = ToTaskNew(class_flag, refer, origin_code).run(class_code) + '\n\n\n'
tmp = deleteZhu(tmp)
code += tmp
# st.code(tmp)
else:
code = ToTs().getStart(code)
if flag / 2 == 1:
refer = ("import { SynStart, SynEnd, wait, notify, SharedBoolean, SharedString, SharedNumber, Syc, isMainThread, addFunc, Runnable, Thread } from "
"'./ThreadBridge';\n\n")
wash = deleteZhu(ToTaskNew().addWash(refer + code))
code = refer + wash + '\n\n' + code
code = ToTs().getClean(code)
code = deleteZhu(code)
main_code = """if (isMainThread()) {
// You can put the entry of your code here to test.
}"""
code = code + '\n\n' + main_code
last_code = code
error_flag = 0
for _ in range(3):
with open('Output.ts', 'w') as file:
file.write(code)
process = subprocess.Popen(['tsc', 'Output.ts'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
if len(stdout.decode().splitlines()) <= 1:
error_flag = 0
break
# st.code(stdout.decode())
error_flag = 1
st.code(stdout.decode())
code = deleteZhu(ToTs().getFix(code, stdout.decode()))
# st.code(code)
if error_flag == 1:
code = last_code
print(code)
with open('Output.ts', 'w') as file:
file.write(code)
st.code(code)
st.code("convert finish")
filename = "Index.ets"
file_bytes = code.encode('utf-8')
st.download_button(label='Download File', data=BytesIO(file_bytes), file_name=filename)