interpreter improvements and error handling in NSD image saving
This commit is contained in:
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@@ -8,7 +8,7 @@
|
|||||||
"name": "Python: Aktuelle Datei",
|
"name": "Python: Aktuelle Datei",
|
||||||
"type": "python",
|
"type": "python",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "run.py",
|
"program": "debug.py",
|
||||||
"console": "integratedTerminal"
|
"console": "integratedTerminal"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -66,15 +66,20 @@ class NassiShneidermanDiagram:
|
|||||||
filepath = self.check_conflicts(filepath, on_conflict)
|
filepath = self.check_conflicts(filepath, on_conflict)
|
||||||
if filepath is not None:
|
if filepath is not None:
|
||||||
logging.info(f"Saving NSD to {filepath}...")
|
logging.info(f"Saving NSD to {filepath}...")
|
||||||
print(f"Saving... to {filepath}")
|
|
||||||
|
|
||||||
image_y_sz = self._get_image_height(i)
|
image_y_sz = self._get_image_height(i)
|
||||||
with NSD_writer(filepath, x_size, image_y_sz):
|
try:
|
||||||
scope = self.function_scopes[i].contents
|
with NSD_writer(filepath, x_size, image_y_sz):
|
||||||
x, y = 0, 0
|
scope = self.function_scopes[i].contents
|
||||||
for instruction in scope:
|
x, y = 0, 0
|
||||||
x, y = instruction.to_image(x, y, x_size)
|
for instruction in scope:
|
||||||
logging.info("Done!")
|
x, y = instruction.to_image(x, y, x_size)
|
||||||
|
logging.info("Done!")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Failed to save image {filepath} with error '{e}'")
|
||||||
|
except:
|
||||||
|
logging.error(f"Failed to save image {filepath}. Unknown error")
|
||||||
|
raise
|
||||||
|
|
||||||
def load_from_file(self, filepath:str):
|
def load_from_file(self, filepath:str):
|
||||||
self.function_scopes = itp.load_scoped_instructions(filepath)
|
self.function_scopes = itp.load_scoped_instructions(filepath)
|
||||||
@@ -7,7 +7,7 @@ from typing import List, Match, Tuple
|
|||||||
from errors.custom import InterpreterException, JavaSyntaxError, ScopeNotFoundException
|
from errors.custom import InterpreterException, JavaSyntaxError, ScopeNotFoundException
|
||||||
from draw.Iinstruction import *
|
from draw.Iinstruction import *
|
||||||
|
|
||||||
logging.warning("""As the Interpreter is still WIP, some Java language features are not supported. These include:
|
logging.warning("""Because the Interpreter is still WIP, some Java language features are not supported. These include:
|
||||||
*else if statements
|
*else if statements
|
||||||
*for loops
|
*for loops
|
||||||
Please remove these features from the source code as they will result in incorrect behaviour""")
|
Please remove these features from the source code as they will result in incorrect behaviour""")
|
||||||
@@ -23,7 +23,7 @@ class Function_scope(Iterable):
|
|||||||
return self.contents.__iter__()
|
return self.contents.__iter__()
|
||||||
|
|
||||||
COMMENT_PATTERN = re.compile(r"""^//|^/\*\*|^\*|^--""")
|
COMMENT_PATTERN = re.compile(r"""^//|^/\*\*|^\*|^--""")
|
||||||
REMOVE_KEYWORDS = [' ', "public", "private", "final"]
|
REMOVE_KEYWORDS = [' ', ';', "public", "private", "final", "protected"]
|
||||||
VARIABLE_TAGS = ["byte", "short", "int", "long", "float", "double", "boolean", "char", "String"]
|
VARIABLE_TAGS = ["byte", "short", "int", "long", "float", "double", "boolean", "char", "String"]
|
||||||
FUNCTION_IDENTIFIERS = ["void"]
|
FUNCTION_IDENTIFIERS = ["void"]
|
||||||
FUNCTION_IDENTIFIERS.extend(VARIABLE_TAGS)
|
FUNCTION_IDENTIFIERS.extend(VARIABLE_TAGS)
|
||||||
@@ -34,14 +34,15 @@ REPLACE = dict((re.escape(k), '') for k in REMOVE_KEYWORDS)
|
|||||||
remove_pattern = re.compile("|".join(REPLACE.keys()))
|
remove_pattern = re.compile("|".join(REPLACE.keys()))
|
||||||
|
|
||||||
variable_regex = "^("
|
variable_regex = "^("
|
||||||
for kw in FUNCTION_IDENTIFIERS:
|
for kw in VARIABLE_TAGS:
|
||||||
variable_regex += fr"""{kw}|"""
|
variable_regex += fr"""{kw}|"""
|
||||||
variable_pattern = re.compile(variable_regex[0:-1]+")(.*)")
|
variable_pattern = re.compile(variable_regex[0:-1]+")(.*=|.*;)(.*)")
|
||||||
|
print(variable_pattern)
|
||||||
|
|
||||||
function_regex = "^("
|
function_regex = "^("
|
||||||
for kw in FUNCTION_IDENTIFIERS:
|
for kw in FUNCTION_IDENTIFIERS:
|
||||||
function_regex += fr"""{kw}|"""
|
function_regex += fr"""{kw}|"""
|
||||||
function_regex = function_regex[0:-1]+ r""").*([(].*[)].*)"""
|
function_regex = function_regex[0:-1]+ ").*([(].*[)].*)"
|
||||||
|
|
||||||
function_pattern = re.compile(function_regex)
|
function_pattern = re.compile(function_regex)
|
||||||
|
|
||||||
@@ -150,8 +151,12 @@ def handle_do_while(line: str, src: List[str], i: int) -> Tuple[Iinstruction, in
|
|||||||
def handle_variable(line:str, src: List[str], i: int) -> Tuple[Iinstruction, int]:
|
def handle_variable(line:str, src: List[str], i: int) -> Tuple[Iinstruction, int]:
|
||||||
groups = variable_pattern.match(line).groups()
|
groups = variable_pattern.match(line).groups()
|
||||||
var_type = groups[0]
|
var_type = groups[0]
|
||||||
var_name = groups[1]
|
var_name = groups[1][:-1]
|
||||||
return generic_instruction(f"{var_type} {var_name}"), i
|
var_value = groups[2]
|
||||||
|
if var_value == "":
|
||||||
|
return generic_instruction(f"declare variable '{var_name}' of type {var_type}"), i
|
||||||
|
return generic_instruction(f"declare variable '{var_name}' of type {var_type} with value {var_value}"), i
|
||||||
|
|
||||||
|
|
||||||
def handle_instruction(line: str, src: List[str], i: int) -> Tuple[Iinstruction, int]:
|
def handle_instruction(line: str, src: List[str], i: int) -> Tuple[Iinstruction, int]:
|
||||||
if line.startswith("while("):
|
if line.startswith("while("):
|
||||||
@@ -166,9 +171,9 @@ def handle_instruction(line: str, src: List[str], i: int) -> Tuple[Iinstruction,
|
|||||||
logging.debug("Found do-while construct in line: %i", i+1)
|
logging.debug("Found do-while construct in line: %i", i+1)
|
||||||
return handle_do_while(line, src, i)
|
return handle_do_while(line, src, i)
|
||||||
|
|
||||||
# elif variable_pattern.match(line):
|
elif variable_pattern.match(line):
|
||||||
# logging.debug("Found variable in line %i", i+1)
|
logging.debug("Found variable in line %i", i+1)
|
||||||
# return handle_variable(line, src, i)
|
return handle_variable(line, src, i)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logging.debug("found generic instruction in line %i", i+1)
|
logging.debug("found generic instruction in line %i", i+1)
|
||||||
|
|||||||
@@ -274,7 +274,6 @@ public class Rover extends Actor
|
|||||||
|
|
||||||
protected void addedToWorld(World world)
|
protected void addedToWorld(World world)
|
||||||
{
|
{
|
||||||
|
|
||||||
setImage("images/rover.png");
|
setImage("images/rover.png");
|
||||||
world = getWorld();
|
world = getWorld();
|
||||||
anzeige = new Display();
|
anzeige = new Display();
|
||||||
@@ -285,7 +284,6 @@ public class Rover extends Actor
|
|||||||
setLocation(getX(),1);
|
setLocation(getX(),1);
|
||||||
}
|
}
|
||||||
anzeige.anzeigen("Ich bin bereit");
|
anzeige.anzeigen("Ich bin bereit");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Display extends Actor
|
class Display extends Actor
|
||||||
|
|||||||
249
res/input/input_parsed.java
Normal file
249
res/input/input_parsed.java
Normal file
@@ -0,0 +1,249 @@
|
|||||||
|
//except for this line, this is what interpret_source.load_src returns
|
||||||
|
importgreenfoot.*;//(World,Actor,GreenfootImage,GreenfootandMouseInfo)
|
||||||
|
classRoverextendsActor
|
||||||
|
{
|
||||||
|
Displayanzeige;
|
||||||
|
voidact()
|
||||||
|
{
|
||||||
|
S66Nr3(7);
|
||||||
|
}
|
||||||
|
voidfahreUmHuegel(Stringrichtung)
|
||||||
|
{
|
||||||
|
Stringpri;
|
||||||
|
Stringsec;
|
||||||
|
if(richtung.equals("Hoch")){
|
||||||
|
pri="links";
|
||||||
|
sec="rechts";
|
||||||
|
}else{
|
||||||
|
if(richtung.equals("Runter")){
|
||||||
|
pri="rechts";
|
||||||
|
sec="links";
|
||||||
|
}else{
|
||||||
|
nachricht("JUNGEDUSPAST!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
drehe(pri);
|
||||||
|
fahre();
|
||||||
|
drehe(sec);
|
||||||
|
fahre();
|
||||||
|
fahre();
|
||||||
|
drehe(sec);
|
||||||
|
fahre();
|
||||||
|
drehe(pri);
|
||||||
|
}
|
||||||
|
voidfahreBisHuegel()
|
||||||
|
{
|
||||||
|
while(!huegelVorhanden("vorne"))
|
||||||
|
{
|
||||||
|
fahre();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
voidfahreZeileDreheHoch()
|
||||||
|
{
|
||||||
|
fahreBisHuegel();
|
||||||
|
fahreUmHuegel("Hoch");
|
||||||
|
fahreBisHuegel();
|
||||||
|
drehe("um");
|
||||||
|
fahreBisHuegel();
|
||||||
|
fahreUmHuegel("Runter");
|
||||||
|
fahreBisHuegel();
|
||||||
|
drehe("rechts");
|
||||||
|
fahre();
|
||||||
|
drehe("rechts");
|
||||||
|
}
|
||||||
|
voidfahreZeileDreheRunter(booleangeheInNächsteZeile)
|
||||||
|
{
|
||||||
|
fahreBisHuegel();
|
||||||
|
fahreUmHuegel("Runter");
|
||||||
|
fahreBisHuegel();
|
||||||
|
drehe("um");
|
||||||
|
fahreBisHuegel();
|
||||||
|
fahreUmHuegel("Hoch");
|
||||||
|
fahreBisHuegel();
|
||||||
|
if(geheInNächsteZeile){
|
||||||
|
drehe("rechts");
|
||||||
|
fahre();
|
||||||
|
drehe("rechts");
|
||||||
|
}else{
|
||||||
|
drehe("um");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
voidS66Nr3(intanzahlZeilen)
|
||||||
|
{
|
||||||
|
if(anzahlZeilen<3){
|
||||||
|
nachricht("IchmussmindestensdreiZeilenfahren!:(");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fahreZeileDreheHoch();
|
||||||
|
for(inti=1;i<anzahlZeilen-1;i++){
|
||||||
|
fahreZeileDreheRunter(true);
|
||||||
|
}
|
||||||
|
fahreZeileDreheRunter(false);
|
||||||
|
}
|
||||||
|
voidfahre()
|
||||||
|
{
|
||||||
|
intposX=getX();
|
||||||
|
intposY=getY();
|
||||||
|
if(huegelVorhanden("vorne"))
|
||||||
|
{
|
||||||
|
nachricht("Zusteil!");
|
||||||
|
}
|
||||||
|
elseif(getRotation()==270&&getY()==1)
|
||||||
|
{
|
||||||
|
nachricht("Ichkannmichnichtbewegen");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
move(1);
|
||||||
|
Greenfoot.delay(1);
|
||||||
|
}
|
||||||
|
if(posX==getX()&&posY==getY()&&!huegelVorhanden("vorne"))
|
||||||
|
{
|
||||||
|
nachricht("Ichkannmichnichtbewegen");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
voiddrehe(Stringrichtung)
|
||||||
|
{
|
||||||
|
if(richtung.equals("rechts")){
|
||||||
|
setRotation(getRotation()+90);
|
||||||
|
}elseif(richtung.equals("links")){
|
||||||
|
setRotation(getRotation()-90);
|
||||||
|
}elseif(richtung.equals("um")){
|
||||||
|
setRotation(getRotation()+180);
|
||||||
|
}else{
|
||||||
|
nachricht("KeinenKorrekteRichtunggegeben!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
booleangesteinVorhanden()
|
||||||
|
{
|
||||||
|
if(getOneIntersectingObject(Gestein.class)!=null)
|
||||||
|
{
|
||||||
|
nachricht("Gesteingefunden!");
|
||||||
|
returntrue;
|
||||||
|
}
|
||||||
|
returnfalse;
|
||||||
|
}
|
||||||
|
booleanhuegelVorhanden(Stringrichtung)
|
||||||
|
{
|
||||||
|
introt=getRotation();
|
||||||
|
if(richtung=="vorne"&&rot==0||richtung=="rechts"&&rot==270||richtung=="links"&&rot==90)
|
||||||
|
{
|
||||||
|
if(getOneObjectAtOffset(1,0,Huegel.class)!=null&&((Huegel)getOneObjectAtOffset(1,0,Huegel.class)).getSteigung()>30)
|
||||||
|
{
|
||||||
|
returntrue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(richtung=="vorne"&&rot==180||richtung=="rechts"&&rot==90||richtung=="links"&&rot==270)
|
||||||
|
{
|
||||||
|
if(getOneObjectAtOffset(-1,0,Huegel.class)!=null&&((Huegel)getOneObjectAtOffset(-1,0,Huegel.class)).getSteigung()>30)
|
||||||
|
{
|
||||||
|
returntrue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(richtung=="vorne"&&rot==90||richtung=="rechts"&&rot==0||richtung=="links"&&rot==180)
|
||||||
|
{
|
||||||
|
if(getOneObjectAtOffset(0,1,Huegel.class)!=null&&((Huegel)getOneObjectAtOffset(0,1,Huegel.class)).getSteigung()>30)
|
||||||
|
{
|
||||||
|
returntrue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(richtung=="vorne"&&rot==270||richtung=="rechts"&&rot==180||richtung=="links"&&rot==0)
|
||||||
|
{
|
||||||
|
if(getOneObjectAtOffset(0,-1,Huegel.class)!=null&&((Huegel)getOneObjectAtOffset(0,-1,Huegel.class)).getSteigung()>30)
|
||||||
|
{
|
||||||
|
returntrue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(richtung!="vorne"&&richtung!="links"&&richtung!="rechts")
|
||||||
|
{
|
||||||
|
nachricht("Befehlnichtkorrekt!");
|
||||||
|
}
|
||||||
|
returnfalse;
|
||||||
|
}
|
||||||
|
voidanalysiereGestein()
|
||||||
|
{
|
||||||
|
if(gesteinVorhanden())
|
||||||
|
{
|
||||||
|
nachricht("Gesteinuntersucht!Wassergehaltist"+((Gestein)getOneIntersectingObject(Gestein.class)).getWassergehalt()+"%.");
|
||||||
|
Greenfoot.delay(1);
|
||||||
|
removeTouching(Gestein.class);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
nachricht("HieristkeinGestein");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
voidsetzeMarke()
|
||||||
|
{
|
||||||
|
getWorld().addObject(newMarke(),getX(),getY());
|
||||||
|
}
|
||||||
|
booleanmarkeVorhanden()
|
||||||
|
{
|
||||||
|
if(getOneIntersectingObject(Marke.class)!=null)
|
||||||
|
{
|
||||||
|
returntrue;
|
||||||
|
}
|
||||||
|
returnfalse;
|
||||||
|
}
|
||||||
|
voidentferneMarke()
|
||||||
|
{
|
||||||
|
if(markeVorhanden())
|
||||||
|
{
|
||||||
|
removeTouching(Marke.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
voidnachricht(StringpText)
|
||||||
|
{
|
||||||
|
if(anzeige!=null)
|
||||||
|
{
|
||||||
|
anzeige.anzeigen(pText);
|
||||||
|
Greenfoot.delay(1);
|
||||||
|
anzeige.loeschen();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
voiddisplayAusschalten()
|
||||||
|
{
|
||||||
|
getWorld().removeObject(anzeige);
|
||||||
|
}
|
||||||
|
protectedvoidaddedToWorld(Worldworld)
|
||||||
|
{
|
||||||
|
setImage("images/rover.png");
|
||||||
|
world=getWorld();
|
||||||
|
anzeige=newDisplay();
|
||||||
|
anzeige.setImage("images/nachricht.png");
|
||||||
|
world.addObject(anzeige,7,0);
|
||||||
|
if(getY()==0)
|
||||||
|
{
|
||||||
|
setLocation(getX(),1);
|
||||||
|
}
|
||||||
|
anzeige.anzeigen("Ichbinbereit");
|
||||||
|
}
|
||||||
|
classDisplayextendsActor
|
||||||
|
{
|
||||||
|
GreenfootImagebild;
|
||||||
|
Display()
|
||||||
|
{
|
||||||
|
bild=getImage();
|
||||||
|
}
|
||||||
|
voidact()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
voidanzeigen(StringpText)
|
||||||
|
{
|
||||||
|
loeschen();
|
||||||
|
getImage().drawImage(newGreenfootImage(pText,25,Color.BLACK,newColor(0,0,0,0)),10,10);
|
||||||
|
}
|
||||||
|
voidloeschen()
|
||||||
|
{
|
||||||
|
getImage().clear();
|
||||||
|
setImage("images/nachricht.png");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
classDirection{
|
||||||
|
Direction(intval){
|
||||||
|
this.value=val;
|
||||||
|
}
|
||||||
|
intvalue;
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user