improved interpreter
This commit is contained in:
@@ -1,11 +1,16 @@
|
|||||||
import logging
|
import logging
|
||||||
|
from os import remove
|
||||||
import re
|
import re
|
||||||
from typing import List, Tuple
|
from typing import List, Text, Tuple
|
||||||
|
|
||||||
from draw.Iinstruction import *
|
from draw.Iinstruction import *
|
||||||
|
|
||||||
COMMENT_REGEX = r"""^//|^#|^COMMENT|^--"""
|
COMMENT_REGEX = r"""^//|^#|^COMMENT|^--"""
|
||||||
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)
|
||||||
|
remove_pattern = re.compile("|".join(REPLACE.keys()))
|
||||||
|
|
||||||
class JavaSyntaxError(Exception):
|
class JavaSyntaxError(Exception):
|
||||||
pass
|
pass
|
||||||
@@ -13,13 +18,16 @@ class JavaSyntaxError(Exception):
|
|||||||
class ScopeNotFoundException(Exception):
|
class ScopeNotFoundException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def replace_all_tags(org: str):
|
||||||
|
return remove_pattern.sub(lambda m: REPLACE[re.escape(m.group(0))], org)
|
||||||
|
|
||||||
def load_src(filepath: str) -> List[str]:
|
def load_src(filepath: str) -> List[str]:
|
||||||
lines: List[str] = []
|
lines: List[str] = []
|
||||||
brace_open_count, brace_closed_count = 0,0
|
brace_open_count, brace_closed_count = 0,0
|
||||||
try:
|
try:
|
||||||
with open(filepath, encoding="utf-8") as file:
|
with open(filepath, encoding="utf-8") as file:
|
||||||
for _line in file:
|
for _line in file:
|
||||||
line = _line.strip().replace(' ', '')
|
line = replace_all_tags(_line.strip())
|
||||||
if line and not re.match(COMMENT_REGEX, line):
|
if line and not re.match(COMMENT_REGEX, line):
|
||||||
lines.append(line)
|
lines.append(line)
|
||||||
if line.__contains__('{'):
|
if line.__contains__('{'):
|
||||||
@@ -48,6 +56,7 @@ def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[
|
|||||||
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
|
||||||
|
|||||||
@@ -27,6 +27,10 @@
|
|||||||
|
|
||||||
//the following code was heavily distorted in order to test the interpreter. Sorry to everyone who has to read this
|
//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");
|
drehe("links");
|
||||||
while(huegelVorhanden("rechts"))
|
while(huegelVorhanden("rechts"))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user