-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.py
135 lines (133 loc) · 3.73 KB
/
compile.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
output = []
aput = []
ac = ''
fncs = []
ignore = []
var = []
tpe = []
dt = ['str', 'int', 'dec']
def strp(inp):
a = ''
for i in inp:
if i == ':':
a += '('
elif i == ';':
a += ')'
elif i == '!':
a += '(int)'
elif i == '*':
a += '(float)'
else:
a += i
return a
def comln(inp):
global ac
global dt
global tpe
args = inp.split(' ')
if args[0] in dt:
tp = args[0]
name = args[1]
del args[0]
del args[0]
var.append(name)
tpe.append(tp)
if tp == 'str':
if len(args) == 1:
if len(args[0]) == 1:
output.append(f'char {name} = {strp(args[0])};')
else:
output.append(f'char {name}[] = {strp(" ".join(args))};')
elif len(args) == 0:
output.append(f'char {name}[] = {strp(" ".join(args))};')
else:
output.append(f'char {name}[ac];')
elif tp == 'int':
if len(args) == 1:
if len(args[0]) == 1:
output.append(f'int {name} = {args[0]};')
else:
output.append(f'int {name}[] = {strp(" ".join(args))};')
elif len(args) == 0:
output.append(f'int {name}[] = {strp(" ".join(args))};')
else:
output.append(f'int {name}[ac];')
elif tp == 'dec':
if len(args) == 1:
if len(args[0]) == 1:
output.append(f'float {name} = {args[0]};')
else:
output.append(f'float {name}[] = {strp(" ".join(args))};')
elif len(args) == 0:
output.append(f'float {name}[] = {strp(" ".join(args))};')
else:
output.append(f'float {name}[ac];')
args = ['']
elif args[0] == 'func':
name = args[1]
del args[0]
del args[0]
output.insert(0, f'void {name}({" ".join(args)});')
aput.append(f'void {name}({" ".join(args)})')
fncs.append(name)
args = [args[len(args) - 1]]
elif args[0] == ':':
output.append('{')
elif args[0] == ';':
output.append('}')
elif args[0] == 'on':
if args[2] == '=':
if tpe[var.index(args[1])] == 'str':
output.append(f'if (strcmp({args[1]},{args[3]})==0)')
else:
output.append(f'if ({args[1]} == {args[3]})')
elif args[2] == '>':
output.append(f'if ({args[1]} > {args[3]})')
elif args[2] == '<':
output.append(f'if ({args[1]} < {args[3]})')
elif args[2] == '!':
output.append(f'if ({args[1]} != {args[3]})')
elif args[0] == 'or':
if args[2] == '=':
if tpe(var.index(args[1])) == 'str':
output.append(f'else if (strcmp({args[1]},{args[3]})==0)')
else:
output.append(f'else if ({args[1]} == {args[3]})')
elif args[2] == '>':
output.append(f'else if ({args[1]} > {args[3]})')
elif args[2] == '<':
output.append(f'else if ({args[1]} < {args[3]})')
elif args[2] == '!':
output.append(f'else if ({args[1]} != {args[3]})')
elif args[0] == '!on':
output.append('else')
elif args[0] in fncs:
name = args[0]
del args[0]
output.append(f'{name}({strp(" ".join(args))});')
args = ['']
elif args[0] == 'sac':
output.append('ac = {args[1]};')
elif args[0] in var:
name = args[0]
del args[0]
output.append(f'{name} = {strp(" ".join(args))}')
args = ['']
elif args[0] == '#':
pass
elif args[0] == 'outln':
del args[0]
output.append(f'puts({" ".join(args)});')
args = ['']
elif args[0] == 'in':
var.append(args[1])
tpe.append('str')
output.append(f'char {args[1]}[{args[2]}];')
output.append(f'scanf("%{args[2]}s", {args[1]});')
elif args[0] == 'outs':
output.append(f'printf("%s", {args[1]});')
elif args[0] == 'outi':
output.append(f'printf("%d", {args[1]});')
else:
if not args[0] == '':
print(f'Error while compiling: "{args[0]}" is not an included variable or function.')