مترجم نص بايثون بسيط في شبيبة
مرحبا! بشكل عام ، أبلغ من العمر 14 عامًا ، وأخذت في الاعتبار جميع عمليات الإشراف السابقة وبالتالي حاولت مرة أخرى. المترجم مكتوب بلغة Python ╕
ما أعمل عليه:
1. py-to-js
2. ryno
3. محرر الشفرة
بادئ ذي بدء ، سأقول على الفور أن هذا رمز غير مكتمل باسم متغير غبي ، وأخطاء ، وما إلى ذلك. الآن دعنا نذهب ، ننشئ ملفًا interp.py
أو أي شيء آخر ونكتب هذا هناك:pyfile = open('testpy.py', 'r', encoding = 'UTF-8')
count = -1
with open('open.js', 'w', encoding = 'UTF-8') as f:
py = pyfile.readlines()
cc, c, tab, local = 1, 0, 0, 0
variables = []
f.write('"use strict";' + '\n' * 2)
f.write('''// Thanks for using py-to-js
// I develop this with hearth''' + '\n' * 2)
for num in py:
count += 1
tab = py[count].count('\t')
if '=' in num and not 'print' in num and not '==' in num and not '!=' in num:
name = num.replace('\n', '')
let = 'let '
if name.startswith('\t'):
let = '\tlet '
name = name.replace('\t', '')
if name in variables:
let = ''
variables.append(name)
f.write( let + f"{name}" + ';' + '\n' * 2)
elif 'if' in num:
condition = num.replace('\n', '').replace('if', '').replace(':', '')
if_ = 'if '
if condition.startswith('\t'):
condition = condition.replace('\t', '')
if_ = '\tif '
cc += 1
else:
cc = 1
f.write( if_ + '(' + f'{condition}' + ' )' + ' {' + '\n')
elif 'def' in num:
con = num.replace('\n', '').replace('def', '').replace(':', '')
def_ = 'function'
if con.startswith('\t'):
con = condition.replace('\t', '')
def_ = '\tfunction'
cc += 1
else:
cc = 1
f.write( def_ + f'{con}' + ' {' + '\n')
elif 'print' in num:
command = num.replace('\n', '')
if command.startswith('\t'):
command = command.replace('\t', '')
f.write('\t' + f'{ command.replace("print", "console.log")}'+ ';' + '\n')
try:
if py[count + 1].count('\t') < tab and py[count + 1] != '\n':
if local == 0:
f.write('}' + '\n')
c += 1
elif local > 0:
f.write('}' * local + '\n')
c += 1 * local
local = 0
elif py[count + 1].count('\t') >= tab and py[count + 1].count('\t') != 0 and py[count + 1] != '\n':
if 'def' in py[count + 1] or 'if' in py[count + 1]:
local += 1
except:
pass
try:
if '\t' in py[count] and not '\t' in py[count + 1] and py[count + 1] != '\n':
f.write('}' * (cc - c) + '\n' * 2)
c = 0
except:
f.write('}' * (cc - c))
c = 0
print('''Programm has been compiled..
1.def tests
2.add class''')
في الوقت الحالي ، فقط للاختبار سنكتب ملفًا ثابتًا في الحقل المفتوح. بالنسبة لأولئك الذين لا يفهمون ما هو مكتوب هنا ، يمكنني أن أشرح في التعليقات. بالمناسبة ، في النهاية ، كتبت ما أعتقد أنه يجب إصلاحه في المنعطف الأول ، وعندها فقط أنهي إنشاء المتغيرات ، إلخ. إذا كان هذا الرمز يمكن أن يتناسب مع عدد أقل من الأسطر ، اكتب في التعليقات. قم بإنشاء testpy.py
برنامج نصي ووضعه فيه:i = 0
b = 0
c = 0
if i == 0:
print('i = 0')
if b == 0:
print('b = 0')
if c == 0:
print('c = 0')
if b != 0:
print('b != 0')
if c != 0:
print('c != 0')
أطلقنا برامجنا من py-to-js ونحصل على هذه النتيجة:"use strict";
let i = 0;
let b = 0;
let c = 0;
if (i == 0) {
console.log('i = 0');
if (b == 0) {
console.log('b = 0');
if (c == 0) {
console.log('c = 0');
}
}
if (b != 0) {
console.log('b != 0');
if (c != 0) {
console.log('c != 0');
}
}
}