Skip to content

Commit

Permalink
fix: better compiler log handling to adapt to syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
chaaals committed May 22, 2024
1 parent c8d10bd commit 30dc339
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
5 changes: 4 additions & 1 deletion src/components/code_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from src.analyzer import MemberAnalyzer, TypeChecker
from src.analyzer.error_handler import ErrorSrc as AnalyzerErrorSrc
from src.compiler import Compiler
from .util import generate_log

from enum import Enum
from PIL import Image
Expand Down Expand Up @@ -247,6 +248,8 @@ def run_analyzer(self) -> bool:
return False

def compile_and_run(self, editor, compiler_status, update_logs_callback):
log = generate_log(lx_errors=editor.lx_errors, p_errors=editor.p_errors, a_errors=editor.a_errors)

filename = self.filename.split('.')[0]
c = Compiler(py_source=self.transpiled_program, filename=self.filename)
c.compile()
Expand All @@ -258,7 +261,7 @@ def compile_and_run(self, editor, compiler_status, update_logs_callback):
subprocess.run([cmd], shell=True)

compiler_status.is_compiling = False
update_logs_callback(editor=editor, is_compiling=compiler_status.is_compiling)
update_logs_callback(editor=editor, is_compiling=compiler_status.is_compiling, generated_log=log)

def start(self, editor, compiler_status, update_logs_callback):
compile_thread = threading.Thread(target=lambda: self.compile_and_run(editor=editor, compiler_status=compiler_status, update_logs_callback=update_logs_callback), name="UwU Compile", daemon=True)
Expand Down
36 changes: 8 additions & 28 deletions src/components/console_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from PIL import Image, ImageTk
from constants.path import *
from src.components.code_view import CodeEditor
from .util import generate_log

class GeneratedLogFrame(CTkFrame):
def __init__(self, master, generated_log: dict, **kwargs):
Expand Down Expand Up @@ -32,46 +33,25 @@ def __init__(self, master, **kwargs):
intro_label = CTkLabel(master=self, text=message, text_color='#FFFFFF', font=('JetBrains Mono', 11))
intro_label.grid(row=i, column=0, sticky="nw")
self.labels.append(intro_label)

def generate_log(self, lx_errors: list, p_errors: list, a_errors: list, is_compiling = False, is_formatting = False) -> dict:
is_passed = not lx_errors and not p_errors and not a_errors
status = None

if is_passed:
if is_formatting:
status = "Your source code has been formatted!"
else:
if is_compiling:
status = "[PASSED] Lexer, [PASSED] Parser, [PASSED] Analyzer. Compiling..."
else:
status = "UwU++ Compiler compiled successfully!"
else:
if is_formatting:
status = "Formatting unsuccessful. Resolve the following errors first:"
else:
status = "UwU++ Compiler compilation unsuccessful. Following errors were found:"

return {
"Status": status,
"Lexical Errors": len(lx_errors) if len(lx_errors) > 0 else None,
"Syntax Errors": len(p_errors) if len(p_errors) > 0 else None,
"Semantic Errors": len(a_errors) if len(a_errors) > 0 else None
}

def render_logs(self, lx_errors: list, p_errors: list, a_errors: list, is_compiling = False, is_formatting = False):
generated_log = self.generate_log(lx_errors=lx_errors, p_errors=p_errors, a_errors=a_errors, is_compiling=is_compiling, is_formatting = is_formatting)
generated_log = generate_log(lx_errors=lx_errors, p_errors=p_errors, a_errors=a_errors, is_compiling=is_compiling, is_formatting = is_formatting)

self.generated_log_frame = GeneratedLogFrame(master=self, fg_color='transparent', generated_log=generated_log)
self.generated_log_frame.grid(row=0, column=0, columnspan=2, sticky='nsew')

def update_logs(self, editor: CodeEditor, is_compiling, is_formatting = False):
def update_logs(self, editor: CodeEditor, is_compiling, generated_log = None, is_formatting = False):
if len(self.labels) > 0:
for label in self.labels:
label.destroy()

self.labels = []

self.render_logs(lx_errors=editor.lx_errors,p_errors=editor.p_errors, a_errors=editor.a_errors, is_compiling=is_compiling, is_formatting = is_formatting)
if(generated_log):
self.generated_log_frame = GeneratedLogFrame(master=self, fg_color='transparent', generated_log=generated_log)
self.generated_log_frame.grid(row=0, column=0, columnspan=2, sticky='nsew')
else:
self.render_logs(lx_errors=editor.lx_errors,p_errors=editor.p_errors, a_errors=editor.a_errors, is_compiling=is_compiling, is_formatting = is_formatting)

class CompilerLogs(CTkScrollableFrame):
def __init__(self, master, editor: CodeEditor, **kwargs):
Expand Down
24 changes: 24 additions & 0 deletions src/components/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def generate_log(lx_errors: list, p_errors: list, a_errors: list, is_compiling = False, is_formatting = False) -> dict:
is_passed = not lx_errors and not p_errors and not a_errors
status = None

if is_passed:
if is_formatting:
status = "Your source code has been formatted!"
else:
if is_compiling:
status = "[PASSED] Lexer, [PASSED] Parser, [PASSED] Analyzer. Compiling..."
else:
status = "UwU++ Compiler compiled successfully!"
else:
if is_formatting:
status = "Formatting unsuccessful. Resolve the following errors first:"
else:
status = "UwU++ Compiler compilation unsuccessful. Following errors were found:"

return {
"Status": status,
"Lexical Errors": len(lx_errors) if len(lx_errors) > 0 else None,
"Syntax Errors": len(p_errors) if len(p_errors) > 0 else None,
"Semantic Errors": len(a_errors) if len(a_errors) > 0 else None
}

0 comments on commit 30dc339

Please sign in to comment.