improved interpreter and added support for doWhile
This commit is contained in:
@@ -130,13 +130,9 @@ class while_instruction_back(while_instruction_front):
|
||||
while_instruction_front.__init__(self, condition, instructions)
|
||||
|
||||
def to_image(self, x:int, y:int, x_sz: int):
|
||||
children_x, children_y, children_sz_x, children_sz_y = cti.draw_while_loop_back(self.instruction_text, x, y, x_sz, y_sz)
|
||||
blk_size = self.draw_children(children_x, children_y, children_sz_x)
|
||||
return x
|
||||
|
||||
def draw_children(self, x:float, y:float, x_sz:float):
|
||||
for instruction in self.child_instructions:
|
||||
x, y = instruction.to_image(x, y, x_sz)
|
||||
children_x, children_y, children_sz_x = cti.draw_while_loop_back(self.instruction_text, x, y, x_sz, self.getblksize())
|
||||
self.draw_children(children_x, children_y, children_sz_x)
|
||||
return x, y + self.getblksize()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -90,21 +90,21 @@ def draw_while_loop_back(condition: str, x: int, y: int, xsize: int, ysize: int)
|
||||
if not output_img:
|
||||
raise Exception("Output image was not initialized! Make sure to call NSD_init first")
|
||||
|
||||
#ole #TODO
|
||||
text_y_sz = get_text_size(condition)[1]
|
||||
|
||||
#the box
|
||||
output_img.line((x,y) + (x + xsize * .1, y), fill=0)
|
||||
output_img.line((x + xsize * .1, y) + (x + xsize * .1, y + ysize * .9), fill=0)
|
||||
output_img.line((x + xsize * .1, y + ysize * .9) + (x + xsize, y + ysize * .9), fill=0)
|
||||
output_img.line((x + xsize, y + ysize * .9) + (x + xsize, y + ysize), fill=0)
|
||||
output_img.line((x + xsize * .1, y) + (x + xsize * .1, y + ysize - text_y_sz), fill=0)
|
||||
output_img.line((x + xsize * .1, y + ysize - text_y_sz) + (x + xsize, y + ysize - text_y_sz), fill=0)
|
||||
output_img.line((x + xsize, y + ysize - text_y_sz) + (x + xsize, y + ysize), fill=0)
|
||||
output_img.line((x,y + ysize) + (x + xsize, y + ysize), fill=0)
|
||||
output_img.line((x,y) + (x, y + ysize), fill=0)
|
||||
|
||||
#the text
|
||||
output_img.text((x + xsize * .1, y + ysize * .95), condition, font = font, fill = (0), anchor="lm")
|
||||
output_img.text((x + xsize * .1, y + ysize - text_y_sz * .5), condition, font = font, fill = (0), anchor="lm")
|
||||
|
||||
#the x, y offset then the x,y draw size (the canvas)
|
||||
return x + xsize * .1, y, xsize * .9, ysize * .9
|
||||
return x + xsize * .1, y, xsize * .9
|
||||
|
||||
def NSD_save(filename: str):
|
||||
"""Save the created file"""
|
||||
|
||||
@@ -5,6 +5,7 @@ from typing import List, Tuple
|
||||
from draw.Iinstruction import *
|
||||
|
||||
COMMENT_REGEX = r"""^//|^#|^COMMENT|^--"""
|
||||
WHILE_TAG = "solange " #german for 'while'. Change this depending on your language
|
||||
|
||||
class JavaSyntaxError(Exception):
|
||||
pass
|
||||
@@ -52,7 +53,7 @@ def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[
|
||||
return outer_scope, i
|
||||
|
||||
if line.startswith("while("):
|
||||
logging.debug("Found while instruction in line: %i", i+1)
|
||||
logging.debug("Found while construct in line: %i", i+1)
|
||||
|
||||
bracket_idx = line.rindex(')') # throws if while contruct is illformed
|
||||
|
||||
@@ -60,10 +61,10 @@ def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[
|
||||
brace_offset = get_scope_start_offset(src, i)
|
||||
child_instructions, i = get_instructions_in_scope(src, i+brace_offset)
|
||||
|
||||
outer_scope.append(while_instruction_front(('while ' + instruction_txt), child_instructions))
|
||||
outer_scope.append(while_instruction_front((WHILE_TAG + instruction_txt), child_instructions))
|
||||
|
||||
elif line.startswith("if("):
|
||||
logging.debug("Found if instruction in line: %i", i+1)
|
||||
logging.debug("Found if construct in line: %i", i+1)
|
||||
|
||||
bracket_idx = line.rindex(')') # throws if the contruct is illformed
|
||||
|
||||
@@ -73,16 +74,14 @@ def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[
|
||||
|
||||
false_instructions = None
|
||||
if src[i].__contains__("else"): #if there is an else statement, check it
|
||||
logging.debug("found else construct in line: %i", i+1)
|
||||
brace_offset = get_scope_start_offset(src, i)
|
||||
false_instructions, i = get_instructions_in_scope(src, i+2)
|
||||
|
||||
outer_scope.append(if_instruction(instruction_txt, true_instructions, false_instructions))
|
||||
|
||||
elif line.startswith("do{"):
|
||||
#construct:
|
||||
#do{
|
||||
#...
|
||||
#}while(...);
|
||||
logging.debug("Found start of do-while instruction in line: %i", i)
|
||||
elif line.startswith("do"):
|
||||
logging.debug("Found start of do-while construct in line: %i", i+1)
|
||||
|
||||
brace_offset = get_scope_start_offset(src, i)
|
||||
child_instructions, i = get_instructions_in_scope(src, i+brace_offset)
|
||||
@@ -91,7 +90,7 @@ def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[
|
||||
bracket_idx = end_line.rindex(");")
|
||||
instruction_txt = end_line[7: bracket_idx]
|
||||
|
||||
outer_scope.append(while_instruction_back(('do while' + instruction_txt), child_instructions))
|
||||
outer_scope.append(while_instruction_back((WHILE_TAG + instruction_txt), child_instructions))
|
||||
|
||||
else:
|
||||
logging.debug("Found generic instruction in line: %i", i+1)
|
||||
|
||||
@@ -25,30 +25,96 @@
|
||||
// niet22();
|
||||
// }while(bool23);
|
||||
|
||||
//the following code was heavily distorted in order to test the interpreter. Sorry to everyone who has to read this
|
||||
|
||||
drehe("links");
|
||||
while(huegelVorhanden("rechts"))
|
||||
{
|
||||
gesteinSammeln();
|
||||
fahre();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
drehe("rechts");
|
||||
gesteinSammeln();
|
||||
|
||||
|
||||
|
||||
|
||||
gesteinSammeln();
|
||||
|
||||
|
||||
|
||||
fahre();
|
||||
while(huegelVorhanden("rechts"))
|
||||
|
||||
|
||||
while(huegelVorhanden("rechts"))
|
||||
{
|
||||
gesteinSammeln();
|
||||
fahre();
|
||||
}
|
||||
drehe("rechts");
|
||||
gesteinSammeln();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
gesteinSammeln();
|
||||
fahre();
|
||||
while(huegelVorhanden("rechts"))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
while(huegelVorhanden("rechts"))
|
||||
{
|
||||
gesteinSammeln();
|
||||
if(!huegelVorhanden("vorne"))
|
||||
|
||||
|
||||
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
fahre();
|
||||
} else
|
||||
|
||||
|
||||
{
|
||||
fahre();
|
||||
}
|
||||
}
|
||||
drehe("rechts");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
drehe("rechts");
|
||||
|
||||
do
|
||||
|
||||
|
||||
|
||||
|
||||
{
|
||||
insideDoWhile();
|
||||
insideDoWhile();
|
||||
|
||||
|
||||
insideDoWhile();
|
||||
|
||||
|
||||
|
||||
insideDoWhile();
|
||||
}while( !huegelVorhanden( "vorne" ) ) ;
|
||||
Reference in New Issue
Block a user