further improvements to the interpreter WIP

This commit is contained in:
weckyy702
2020-12-26 21:12:34 +01:00
parent 1df4f18a5a
commit de7a71b15b
3 changed files with 105 additions and 80 deletions

2
.vscode/launch.json vendored
View File

@@ -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"
} }
] ]

View File

@@ -6,8 +6,9 @@ from typing import List, Text, Tuple
from draw.Iinstruction import * from draw.Iinstruction import *
COMMENT_REGEX = r"""^//|^#|^COMMENT|^--""" COMMENT_REGEX = r"""^//|^#|^COMMENT|^--"""
REMOVE_KEYWORDS = [' ', "public", "private", "void"]
WHILE_TAG = "solange " #german for 'while'. Change this depending on your language WHILE_TAG = "solange " #german for 'while'. Change this depending on your language
REMOVE_KEYWORDS = (' ', "public", "private", "void")
REPLACE = dict((re.escape(k), '') for k in REMOVE_KEYWORDS) REPLACE = dict((re.escape(k), '') for k in REMOVE_KEYWORDS)
remove_pattern = re.compile("|".join(REPLACE.keys())) remove_pattern = re.compile("|".join(REPLACE.keys()))
@@ -42,21 +43,25 @@ def load_src(filepath: str) -> List[str]:
return lines return lines
def get_scope_start_offset(src: List[str], start_idx: int) -> int: def get_next_occurence_of(src: List[str], start_idx:int, tag:str) -> int:
i = start_idx i = start_idx
while i < len(src): while i < len(src):
line = src[i] if src[i].__contains__(tag):
if line.__contains__("{"): return i
return i - start_idx + 1
i += 1 i += 1
def get_scope_start_offset(src: List[str], start_idx: int) -> int:
i = get_next_occurence_of(src, start_idx, '{')
if i != len(src):
return i + 1
raise ScopeNotFoundException("Unable to find scope start. Is the program ill-formed?") raise ScopeNotFoundException("Unable to find scope start. Is the program ill-formed?")
def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[Iinstruction], int]: def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[Iinstruction], int]:
outer_scope: List[Iinstruction] = [] outer_scope: List[Iinstruction] = []
i = start_idx i = start_idx
while i < len(src): while i < len(src):
line = src[i] line = src[i]
logging.debug(line)
try: try:
if line.__contains__('}'): #We exited this scope, return it if line.__contains__('}'): #We exited this scope, return it
return outer_scope, i return outer_scope, i
@@ -82,10 +87,16 @@ def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[
true_instructions, i = get_instructions_in_scope(src, i+brace_offset) true_instructions, i = get_instructions_in_scope(src, i+brace_offset)
false_instructions = None false_instructions = None
if src[i].__contains__("else"): #if there is an else statement, check it #if there is an else statement, check it
if src[i].__contains__("else"):
logging.debug("found else construct in line: %i", i+1) logging.debug("found else construct in line: %i", i+1)
brace_offset = get_scope_start_offset(src, i) brace_offset = get_scope_start_offset(src, i)
false_instructions, i = get_instructions_in_scope(src, i+2) false_instructions, i = get_instructions_in_scope(src, i+brace_offset)
elif src[i+1].__contains__("else"):
logging.debug("found else construct in line: %i", i+2)
brace_offset = get_scope_start_offset(src, i+1)
false_instructions, i = get_instructions_in_scope(src, i+1+brace_offset)
outer_scope.append(if_instruction(instruction_txt, true_instructions, false_instructions)) outer_scope.append(if_instruction(instruction_txt, true_instructions, false_instructions))
@@ -105,7 +116,7 @@ def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[
logging.debug("Found generic instruction in line: %i", i+1) logging.debug("Found generic instruction in line: %i", i+1)
outer_scope.append(generic_instruction(line)) outer_scope.append(generic_instruction(line))
except: except:
logging.error("Encountered error in line: %i", i) logging.error("Encountered error in line: %i", i+1)
raise # rethrow raise # rethrow
i += 1 i += 1
return outer_scope, 0 return outer_scope, 0

View File

@@ -1,124 +1,138 @@
// fahre1(); // // fahre1();
// fahre2(); // // fahre2();
// while(shouldNiet()) // // while(shouldNiet())
// // {
// // niet4();
// // niet5();
// // if(if6)
// // {
// // niet7();
// // niet8();
// // } else
// // {
// // niet10();
// // niet11();
// // }
// // niet13();
// // }
// // niet15();
// // niet16();
// // do{
// // niet21();
// // niet22();
// // }while(bool23);
// //the following code was heavily distorted in order to test the interpreter. Sorry to everyone who has to read this
// void
// private void
// drehe("links");
// while(huegelVorhanden("rechts"))
// { // {
// niet4(); // gesteinSammeln();
// niet5(); // fahre();
// if(if6)
// {
// niet7();
// niet8();
// } else
// {
// niet10();
// niet11();
// } // }
// niet13(); // drehe("rechts");
// gesteinSammeln();
// fahre();
// while(huegelVorhanden("rechts"))
// {
// gesteinSammeln();
// fahre();
// } // }
// niet15(); // drehe("rechts");
// niet16();
// do{
// niet21();
// niet22();
// }while(bool23);
//the following code was heavily distorted in order to test the interpreter. Sorry to everyone who has to read this
void
private void
drehe("links");
while(huegelVorhanden("rechts"))
{
gesteinSammeln();
fahre();
// gesteinSammeln();
// fahre();
// while(huegelVorhanden("rechts"))
// {
// gesteinSammeln();
// if(!huegelVorhanden("vorne"))
// {
// fahre();
// }
// else
// {
// fahre();
// }
// }
// drehe("rechts");
// do
// {
// insideDoWhile();
// insideDoWhile();
// insideDoWhile();
// insideDoWhile();
// }
// while( !huegelVorhanden( "vorne" ) ) ;
if(bool) {
true_case;
true_case;
} }
drehe("rechts"); else
gesteinSammeln();
fahre();
while(huegelVorhanden("rechts"))
{ {
gesteinSammeln(); false_case;
fahre(); false_case;
} }
drehe("rechts");
gesteinSammeln();
fahre();
while(huegelVorhanden("rechts"))
{
gesteinSammeln();
if(!huegelVorhanden("vorne"))
{
fahre();
} else
{
fahre();
}
}
drehe("rechts");
do
{
insideDoWhile();
insideDoWhile();
insideDoWhile();
insideDoWhile();
}while( !huegelVorhanden( "vorne" ) ) ;