-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c9625b
commit 6983682
Showing
12 changed files
with
516 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,138 @@ | ||
from scierra.compile import build, run | ||
from scierra.utils import gen_name, working_dir, count, replace_string, replace_comment, count_fors, empty_couts | ||
from scierra.utils import gen_name, working_dir, count, replace_string, replace_comment, empty_couts, isblock | ||
import os | ||
|
||
OUT_SUPPORT = [ | ||
'using', | ||
'#include', | ||
'typedef', | ||
'#define', | ||
'void', | ||
'return', | ||
'class' | ||
] | ||
import platform | ||
|
||
KEYWORDS = { | ||
'exp_prep':[ | ||
'#*include', | ||
'#*define' | ||
], | ||
'prep': [ | ||
'typedef', | ||
'using' | ||
], | ||
'glob': [ | ||
'class', | ||
'struct', | ||
'return', | ||
'void', | ||
'template', | ||
'typename' | ||
] | ||
} | ||
|
||
FILE_EXTENSION = { | ||
'Windows': '.exe', | ||
'Linux': '.out', | ||
'Darwin': '.out' | ||
} | ||
|
||
|
||
class Simulator(object): | ||
def __init__(self): | ||
self.wdir = working_dir() | ||
self.bef_mains = "" | ||
self.preprocs = "" | ||
self.globals = "" | ||
self.mains = "" | ||
self.buff = "" | ||
self.TEMPLATE = "#include<iostream>\n#include<sstream>\n#include<fstream>\n#include<vector>\n#include<string>\nusing namespace std;\n{}\n{}\n" | ||
self.TEMPLATE = "#include<iostream>\n#include<sstream>\n#include<fstream>\n#include<vector>\n#include<string>\nusing namespace std;\n{}\n{}\n{}\n" | ||
|
||
def gen_code(self, preps=None, globs=None, mains=None): | ||
if preps is None: | ||
preps = self.preprocs | ||
if globs is None: | ||
globs = self.globals | ||
if mains is None: | ||
mains = self.mains | ||
|
||
code = self.TEMPLATE.format( | ||
preps, globs, 'int main(){\n' + mains + '\nreturn 0;\n}' | ||
) | ||
|
||
return code | ||
|
||
def addline(self, line): | ||
self.buff += line | ||
proc, str_dict = replace_string(self.buff) | ||
proc, com_dict = replace_comment(proc) | ||
|
||
if (count(proc, '{') <= count(proc, '}')) and \ | ||
(count(proc, ';') >= (count_fors(proc) * 2 + 1) or '#' in proc): | ||
out = False | ||
for i in OUT_SUPPORT: | ||
if count(proc, i, syntax=True) > 0: | ||
out = True | ||
|
||
if out: | ||
if self.invoke(empty_couts(self.bef_mains) + self.buff, empty_couts(self.mains)): | ||
self.bef_mains += self.buff | ||
|
||
if isblock(proc): | ||
section = 'main' | ||
|
||
if '<prep>' in proc: | ||
section = 'prep' | ||
proc = proc.replace('<prep>', '') | ||
elif '<glob>' in proc: | ||
section = 'glob' | ||
proc = proc.replace('<glob>', '') | ||
elif '<main>' in proc: | ||
section = 'main' | ||
proc = proc.replace('<main>', '') | ||
else: | ||
if self.invoke(empty_couts(self.bef_mains), empty_couts(self.mains) + self.buff): | ||
for i in KEYWORDS['glob']: | ||
if count(proc, i, syntax=True) > 0: | ||
section = 'glob' | ||
|
||
for i in KEYWORDS['prep']: | ||
if count(proc, i, syntax=True) > 0: | ||
section = 'prep' | ||
|
||
for i in KEYWORDS['exp_prep']: | ||
if count(proc, i, exp=True) > 0: | ||
section = 'prep' | ||
|
||
for i in str_dict: | ||
proc = proc.replace(i, str_dict[i]) | ||
for i in com_dict: | ||
proc = proc.replace(i, com_dict[i]) | ||
self.buff = proc | ||
|
||
if section == 'prep': | ||
if self.invoke(self.preprocs + self.buff, self.globals, empty_couts(self.mains)): | ||
self.preprocs += self.buff | ||
elif section == 'glob': | ||
if self.invoke(self.preprocs, self.globals + self.buff, empty_couts(self.mains)): | ||
self.globals += self.buff | ||
elif section == 'main': | ||
if self.invoke(self.preprocs, self.globals, empty_couts(self.mains) + self.buff): | ||
self.mains += self.buff | ||
|
||
self.buff = "" | ||
return True | ||
|
||
elif proc[0] == '<' and proc[:6] not in ['<prep>', '<glob>', '<main>']: | ||
self.buff = 'cout << ' + self.buff[1:] + ';' | ||
self.invoke(self.preprocs, self.globals, empty_couts(self.mains) + self.buff) | ||
self.buff = "" | ||
return True | ||
|
||
else: | ||
return False | ||
|
||
def invoke(self, bef_mains, mains, src_name = None, exe_name = None): | ||
code = "#include<iostream>\nusing namespace std;\n{}\n{}\n".format( | ||
bef_mains, 'int main(){\n' + mains + '\nreturn 0;}' | ||
) | ||
def invoke(self, preps, globs, mains, src_name=None, out_name=None): | ||
code = self.gen_code(preps, globs, mains) | ||
|
||
if src_name == None: | ||
if src_name is None: | ||
src_name = gen_name(self.wdir, '.cpp') | ||
if exe_name == None: | ||
exe_name = gen_name(self.wdir, '.exe') | ||
cin_name = gen_name(self.wdir, '.cin') | ||
if out_name is None: | ||
out_name = gen_name(self.wdir, FILE_EXTENSION[platform.system()]) | ||
|
||
f = open(src_name, 'w') | ||
f.write(code) | ||
f.close() | ||
|
||
ret = build(self.wdir, src_name, exe_name) | ||
ret = build(self.wdir, src_name, out_name) | ||
if ret: | ||
ret = run(self.wdir, exe_name) == 0 | ||
os.remove(os.path.join(self.wdir, exe_name)) | ||
ret = run(self.wdir, out_name) == 0 | ||
os.remove(os.path.join(self.wdir, out_name)) | ||
|
||
os.remove(os.path.join(self.wdir, src_name)) | ||
|
||
return ret | ||
|
||
def print_code(self): | ||
code = self.TEMPLATE.format( | ||
self.bef_mains, 'int main(){\n' + self.mains + '\nreturn 0;}' | ||
) | ||
print(code) | ||
print(self.gen_code()) | ||
|
||
def restart(self): | ||
self.wdir = working_dir() | ||
self.bef_mains = "" | ||
self.mains = "" | ||
self.buff = "" | ||
self.__init__() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
from scierra.utils.utils import rndstr | ||
from scierra.utils.utils import gen_name | ||
from scierra.utils.utils import working_dir | ||
from scierra.utils.utils import str_equal | ||
from scierra.utils.utils import count | ||
from scierra.utils.utils import skip_whitespaces | ||
from scierra.utils.utils import cross_whitespaces | ||
from scierra.utils.utils import count_fors | ||
from scierra.utils.utils import min_semis | ||
from scierra.utils.utils import isblock | ||
from scierra.utils.utils import empty_couts | ||
from scierra.utils.utils import replace_string | ||
from scierra.utils.utils import replace_comment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Oops, something went wrong.