Skip to content

Commit

Permalink
Updates for v0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
PerceptronV committed Dec 17, 2020
1 parent 3c9625b commit 6983682
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 105 deletions.
2 changes: 1 addition & 1 deletion build/lib/scierra/compile/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def build(filedir, filename, outname):
a = os.system(cmd)

if a != 0:
print(a)
print('Compilation terminated with return code {}'.format(a))
return False
else:
return True
Expand Down
142 changes: 98 additions & 44 deletions build/lib/scierra/sim/sim.py
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__()
4 changes: 4 additions & 0 deletions build/lib/scierra/utils/__init__.py
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
70 changes: 65 additions & 5 deletions build/lib/scierra/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os, random

WHITESPACES = [' ', '\r', '\t', '\n']


def rndstr(l):
s = ''
Expand All @@ -19,19 +21,42 @@ def working_dir():
return os.getcwd()


def count(str, k, syntax=False):
def str_equal(str, segments, idx):
l = len(segments)
if idx is None:
return False
if l == 1:
str_len = len(segments[0])
return str[idx:idx+str_len] == segments[0]
else:
str_len = len(segments[0])
if str[idx:idx+str_len] != segments[0]:
return False
return str_equal(str, segments[1:], cross_whitespaces(str, idx+str_len-1))


def count(str, k, syntax=False, exp = False):
'''
syntax & exp ARGS CANNOT BOTH BE TRUE!!!
OTHERWISE PROGRAM WILL BE UNPREDICTABLE
'''
if exp:
segments = k.split('*')
else:
segments = k

r = 0
l = len(k)

if str[:l] == k:
if str_equal(str, segments, 0):
if syntax:
if not str[l].isalnum():
r += 1
else:
r += 1

for i in range(1, len(str)):
if str[i:i+l] == k:
if str_equal(str, segments, i):
if syntax:
if not str[i - 1].isalnum() and not str[i + l].isalnum():
r += 1
Expand All @@ -42,9 +67,9 @@ def count(str, k, syntax=False):


def skip_whitespaces(str, index):
WHITESPACES = [' ', '\r', '\t', '\n']

i = index + 1
if i == len(str):
return None
while str[i] in WHITESPACES:
i += 1
if i == len(str):
Expand All @@ -53,6 +78,18 @@ def skip_whitespaces(str, index):
return str[i]


def cross_whitespaces(str, index):
i = index + 1
if i == len(str):
return None
while str[i] in WHITESPACES:
i += 1
if i == len(str):
return None

return i


def count_fors(str):
r = 0
for i in range(len(str) - 3):
Expand All @@ -62,6 +99,29 @@ def count_fors(str):
return r


def min_semis(proc):
fors = count_fors(proc)
if fors > 0:
ret = fors * 2
elif '#' in proc or count(proc, 'template', syntax=True) > 0:
ret = 0
else:
ret = 1
return ret


def isblock(proc):
ret = True

if count(proc, '{') > count(proc, '}'):
ret = False

if count(proc, ';') < min_semis(proc):
ret = False

return ret


def empty_couts(str):
proc, dictionary = replace_string(str)
ret = ''
Expand Down
Binary file modified dist/scierra-0.1-py3-none-any.whl
Binary file not shown.
Binary file modified dist/scierra-0.1.tar.gz
Binary file not shown.
Loading

0 comments on commit 6983682

Please sign in to comment.