This commit is contained in:
oleting
2021-01-17 10:34:02 +00:00
parent b5ace33119
commit 10eef59ea2
3 changed files with 19 additions and 16 deletions

View File

@@ -6,7 +6,10 @@ datei_endung = ".png"
img = None img = None
output_img = None output_img = None
_bkp_font = ImageFont.truetype("res/fonts/NotoSans-Regular.ttf", 12) #in case set_font does funky stuff, backup the original font
_bkp_font = ImageFont.load_default()
#in case set_font does funky stuff, backup the original font
font = _bkp_font font = _bkp_font
@@ -77,7 +80,7 @@ def draw_if_statement(condition: str, x: int, y: int, true_sz: int, false_sz: in
return x, y + text_y_size, true_sz, ysize - text_y_size, x + true_sz, y + text_y_size, false_sz, ysize - text_y_size return x, y + text_y_size, true_sz, ysize - text_y_size, x + true_sz, y + text_y_size, false_sz, ysize - text_y_size
def draw_while_loop_front(condition: str, x: int, y: int, xsize: int, ysize: int): def draw_while_loop_front(condition: str, x: int, y: int, xsize: int, ysize: int):
if not output_img: if not output_img:
raise Exception("Output image was not initialized! Make sure to call NSD_init first") raise Exception("Output image was not initialized! Make sure to call NSD_init first")
@@ -98,7 +101,7 @@ def draw_while_loop_front(condition: str, x: int, y: int, xsize: int, ysize: int
return x + xsize * .1, y + text_y_sz, xsize * .9 return x + xsize * .1, y + text_y_sz, xsize * .9
def draw_while_loop_back(condition: str, x: int, y: int, xsize: int, ysize: int): def draw_while_loop_back(condition: str, x: int, y: int, xsize: int, ysize: int):
if not output_img: if not output_img:
raise Exception("Output image was not initialized! Make sure to call NSD_init first") raise Exception("Output image was not initialized! Make sure to call NSD_init first")

View File

@@ -118,7 +118,7 @@ class JavaInterpreter:
def _get_scope_start_offset(self, i: int) -> int: def _get_scope_start_offset(self, i: int) -> int:
if self._check_src(i, "{"): if self._check_src(i, "{"):
return 1 return 1
elif self._check_src(i+1, "{"): elif self._check_src(i+1, "{"):
return 2 return 2
raise ScopeNotFoundException("Unable to find scope start. Is the program ill-formed?") raise ScopeNotFoundException("Unable to find scope start. Is the program ill-formed?")
@@ -128,7 +128,7 @@ class JavaInterpreter:
instruction_txt = line[6:bracket_idx] instruction_txt = line[6:bracket_idx]
child_instructions, idx = self._get_subscope(idx) child_instructions, idx = self._get_subscope(idx)
return while_instruction_front(("while" + instruction_txt), child_instructions), idx return while_instruction_front(("while" + instruction_txt), child_instructions), idx
def _get_subscope(self, idx: int): def _get_subscope(self, idx: int):
brace_offset = self._get_scope_start_offset(idx) brace_offset = self._get_scope_start_offset(idx)
return self._get_instructions_in_scope(idx+brace_offset) return self._get_instructions_in_scope(idx+brace_offset)
@@ -144,7 +144,7 @@ class JavaInterpreter:
else: else:
logging.debug("found else construct in line: %i", idx+1) logging.debug("found else construct in line: %i", idx+1)
instructions, idx = self._get_subscope(idx) instructions, idx = self._get_subscope(idx)
elif self._check_line_start(idx+1, "else"): elif self._check_line_start(idx+1, "else"):
if self._check_src(idx+1, "if("): if self._check_src(idx+1, "if("):
logging.debug("found else if construct in line: %i", idx+2) logging.debug("found else if construct in line: %i", idx+2)
@@ -155,7 +155,7 @@ class JavaInterpreter:
logging.debug("found else construct in line: %i", idx+2) logging.debug("found else construct in line: %i", idx+2)
instructions, idx = self._get_subscope(idx+1) instructions, idx = self._get_subscope(idx+1)
return instructions, idx return instructions, idx
def _handle_if(self, line: str, idx: int): def _handle_if(self, line: str, idx: int):
bracket_idx = line.rindex(')') # throws if the contruct is illformed bracket_idx = line.rindex(')') # throws if the contruct is illformed
@@ -164,7 +164,7 @@ class JavaInterpreter:
true_instructions, idx = self._get_subscope(idx) true_instructions, idx = self._get_subscope(idx)
false_instructions, idx = self._get_else_scope(idx) false_instructions, idx = self._get_else_scope(idx)
return if_instruction(instruction_txt, true_instructions, false_instructions), idx return if_instruction(instruction_txt, true_instructions, false_instructions), idx
def _handle_do_while(self, line: str, idx: int): def _handle_do_while(self, line: str, idx: int):
@@ -193,7 +193,7 @@ class JavaInterpreter:
var = segments[0][4:] var = segments[0][4:]
cond = segments[1] cond = segments[1]
inc = segments[2][:-2] inc = segments[2][:-2]
instructions = [] instructions = []
if cond == "": #did you know test expressions where optional and defaulted to true? Me neither if cond == "": #did you know test expressions where optional and defaulted to true? Me neither
@@ -202,16 +202,16 @@ class JavaInterpreter:
if var != "": if var != "":
variable_instruction = self._handle_variable(var, idx)[0] variable_instruction = self._handle_variable(var, idx)[0]
instructions.append(variable_instruction) instructions.append(variable_instruction)
brace_offset = self._get_scope_start_offset(idx) brace_offset = self._get_scope_start_offset(idx)
child_instructions, idx = self._get_instructions_in_scope(idx+brace_offset) child_instructions, idx = self._get_instructions_in_scope(idx+brace_offset)
if inc != "": if inc != "":
increment_instruction = generic_instruction(inc) increment_instruction = generic_instruction(inc)
child_instructions.append(increment_instruction) child_instructions.append(increment_instruction)
instructions.append(for_instruction("while " + cond, child_instructions)) instructions.append(for_instruction("while " + cond, child_instructions))
return instructions, idx return instructions, idx
except IndexError: except IndexError:
@@ -232,7 +232,7 @@ class JavaInterpreter:
if line.startswith("while("): if line.startswith("while("):
logging.debug("Found while construct in line: %i", idx+1) logging.debug("Found while construct in line: %i", idx+1)
return self._handle_while(line, idx) return self._handle_while(line, idx)
elif line.startswith("if("): elif line.startswith("if("):
logging.debug("Found if construct in line: %i", idx+1) logging.debug("Found if construct in line: %i", idx+1)
return self._handle_if(line, idx) return self._handle_if(line, idx)
@@ -258,7 +258,7 @@ class JavaInterpreter:
i = idx i = idx
while i < len(self._lines): while i < len(self._lines):
line = self._lines[i] line = self._lines[i]
if self._check_src(i, '}'): if self._check_src(i, '}'):
break break
@@ -270,7 +270,7 @@ class JavaInterpreter:
i += 1 i += 1
return scope, i return scope, i
def _remove_keywords(self): def _remove_keywords(self):
self.src = self._src.replace(' ', '') self.src = self._src.replace(' ', '')
self.src = self._comment_pattern.sub('', self.src) self.src = self._comment_pattern.sub('', self.src)

View File

@@ -1,6 +1,6 @@
from typing import Optional
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import FileField, SubmitField from wtforms import FileField, SubmitField
from wtforms.validators import Optional
from flask_wtf.file import FileAllowed from flask_wtf.file import FileAllowed
from wtforms.fields.core import StringField from wtforms.fields.core import StringField