Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions CompileError.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
from traceback import print_exception
from pathlib import Path
from contextvars import ContextVar

cur_file_var = ContextVar("cur_file_var",default=None)

class Pos:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use @dataclasses.dataclass from the standard library here (https://docs.python.org/3/library/dataclasses.html)

file: Path
line: int

def __init__(self, line: int, file: Path | None = None):
self.line = line
self.file = file or cur_file_var.get()

class CompileError(Exception):
def __init__(self, text):
super(Exception, self).__init__(text)
where: None | Pos
def __init__(self, text, where = None):
super(Exception, self).__init__(text)
self.where = where

line_cache = {}

def register_lines(filename: str, lines: list[str]):
line_cache[filename] = lines

def pretty_print_error(ex: Exception, file = None):
stack = []
while (ex.__cause__ is not None):
stack.append(ex)
ex = ex.__cause__
stack.append(ex)
for e in stack[::-1]:
if isinstance(e, CompileError):
print(e,file=file)
if e.where is not None and e.where.file in line_cache:
print(f"{e.where.line:>4} |",line_cache[e.where.file][e.where.line-1],file=file)
continue
print_exception(e,chain=False, file = file)
4 changes: 2 additions & 2 deletions Scripts/perf.cbscript
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dir "C:\Users\Seth\AppData\Roaming\.minecraft 1.20\saves\Perf"
dir "Perf"
desc "Test performance concerns"

import common
Expand Down Expand Up @@ -64,4 +64,4 @@ function macro_call_2()
end
k = val.val + 1
val.val = k
end
end
2 changes: 1 addition & 1 deletion Scripts/physics_sandbox.cbscript
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dir "C:\Users\Seth\AppData\Roaming\.minecraft 1.20\saves\Physics - One Wall"
dir "Physics - One Wall"
desc "Rigid Body Physics Sandbox"
scale 1000

Expand Down
4 changes: 2 additions & 2 deletions Scripts/plinko_demo.cbscript
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dir "C:\Users\Seth\AppData\Roaming\.minecraft 1.20\saves\Plinko Demo"
dir "Plinko Demo"
desc "Demonstrates plinko"
scale 1000

Expand Down Expand Up @@ -37,4 +37,4 @@ clock tick
end
end
end
end
end
4 changes: 2 additions & 2 deletions Scripts/stupid_mobile_game.cbscript
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dir "C:\Users\Seth\AppData\Roaming\.minecraft 1.20\saves\Stupid Mobile Game Test"
dir "Stupid Mobile Game Test"
desc "Physics engine for stupid mobile game."
scale 1000

Expand Down Expand Up @@ -492,4 +492,4 @@ function load_state()
end
end
end
end
end
2 changes: 1 addition & 1 deletion Scripts/tetherblock.cbscript
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dir "C:\Users\Seth\AppData\Roaming\.minecraft 1.20\saves\TetherBlock"
dir "TetherBlock"
desc "TetherBlock game"
scale 1000

Expand Down
4 changes: 2 additions & 2 deletions Scripts/transform_demo.cbscript
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dir "C:\Users\Seth\AppData\Roaming\.minecraft 1.20\saves\Transform Demo"
dir "Transform Demo"
desc "Demonstrates transform parameters"
scale 1000

Expand Down Expand Up @@ -123,4 +123,4 @@ clock tick
end

end
end
end
4 changes: 2 additions & 2 deletions block_types/array_definition_block.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .block_base import block_base
from .command_block import command_block
from variable_types.scoreboard_var import scoreboard_var
from CompileError import CompileError
from CompileError import CompileError, Pos

class array_definition_block(block_base):
def __init__(self, line, name, from_val, to_val, selector_based):
Expand All @@ -16,7 +16,7 @@ def compile(self, func):
from_val = int(self.from_val.get_value(func))
to_val = int(self.to_val.get_value(func))
except Exception:
raise CompileError(f'Unable to get array range for "{self.name}" at line {self.line}')
raise CompileError(f'Unable to get array range for "{self.name}" at line {self.line}', Pos(self.line)) from None

name = self.name

Expand Down
12 changes: 6 additions & 6 deletions block_types/block_base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from CompileError import CompileError
from CompileError import CompileError, Pos
import traceback

class block_base(object):
def __init__(self, line):
self.line = line
def compile(self, func):
raise NotImplementedError('Section does not implement compile()')

Expand All @@ -12,11 +14,9 @@ def compile_lines(self, func, lines):
try:
func.compile_blocks(lines)
except CompileError as e:
print(e)
raise CompileError(f'Unable to compile {self.block_name} at line {self.line}')
except e:
print(traceback.format_exc())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we getting rid of printing tracebacks? Having tracebacks are invaluable for debugging.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, sorry , I was thinking about the end user experience however , which suffers from them.

raise CompileError(f'Error compiling {self.block_name} at line {self.line}')
raise CompileError(f'Unable to compile {self.block_name} at line {self.line}', Pos(self.line)) from e
except Exception as e:
raise CompileError(f'Error compiling {self.block_name} at line {self.line}', Pos(self.line)) from e

@property
def block_name(self):
Expand Down
14 changes: 10 additions & 4 deletions block_types/block_definition_block.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .block_base import block_base
from data_types.relcoords import relcoords
from CompileError import CompileError
from CompileError import CompileError, Pos

class block_definition_block(block_base):
def __init__(self, line, block_id, items, coords):
Expand All @@ -21,7 +21,10 @@ def copy_to_objective(self, func, path, coords, macro_args, objective):
coords = self.coords

if path not in self.paths:
raise CompileError(f'No path "{path}" defined for [{self.block_id}].')
raise CompileError(
f'No path "{path}" defined for [{self.block_id}].',
Pos(self.line)
)

self.paths[id].copy_to_objective(func, coords, macro_args, objective)

Expand All @@ -30,7 +33,10 @@ def copy_from(self, func, path, coords, macro_args, var):
coords = self.coords

if path not in self.paths:
raise CompileError(f'No path "{path}" defined for [{self.block_id}].')
raise CompileError(
f'No path "{path}" defined for [{self.block_id}].',
Pos(self.line)
)

self.paths[path].copy_from(func, coords, macro_args, var)

Expand All @@ -39,6 +45,6 @@ def get_command(self, func, path, coords, macro_args):
coords = self.coords

if path not in self.paths:
raise CompileError(f'No path "{path}" defined for [{self.block_id}].')
raise CompileError(f'No path "{path}" defined for [{self.block_id}].',Pos(self.line))

return self.paths[path].get_command(func, coords, macro_args)
8 changes: 3 additions & 5 deletions block_types/block_id_switch_block.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .block_switch_base import block_switch_base
from CompileError import CompileError
from CompileError import CompileError, Pos

class block_id_switch_block(block_switch_base):
def __init__(self, line, expr, cases, include_block_states):
Expand All @@ -15,8 +15,7 @@ def compile_initialization(self, func):
var = self.expr.compile(func)
self.condition_var = var.get_scoreboard_var(func)
except CompileError as e:
print(e)
raise CompileError(f'Unable to compile switch expression at line {self.line}.')
raise CompileError(f'Unable to compile switch expression at line {self.line}.', Pos(self.line)) from e

def case_condition(self, block_id):
return f'score {self.condition_var.selector} {self.condition_var.objective} matches {block_id}'
Expand All @@ -36,8 +35,7 @@ def compile_block_case(self, func, id):
try:
case.compile(block, block_state, id, case_func, falling_block_nbt)
except CompileError as e:
print(e)
raise CompileError(f'Unable to compile block switch at line {self.line}')
raise CompileError(f'Unable to compile block switch at line {self.line}', Pos(self.line)) from e

func.call_function(
case_func,
Expand Down
7 changes: 4 additions & 3 deletions block_types/block_switch_base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from .block_base import block_base
from CompileError import CompileError
from CompileError import CompileError, Pos

class block_switch_base(block_base):
def __init__(self):
self.default_case = None
for case in self.cases:
if case.is_default:
if self.default_case:
raise CompileError(f'Block switch at line {self.line} has multiple default cases.')
raise CompileError(
f'Block switch at line {self.line} has multiple default cases.', Pos(self.line))
else:
self.default_case = case

Expand Down Expand Up @@ -123,4 +124,4 @@ def get_matching_case(self, func, block, state):
if not case.is_default and case.block_name in func.block_tags and block.replace('minecraft:','') in func.block_tags[case.block_name]:
return case

return self.default_case
return self.default_case
6 changes: 3 additions & 3 deletions block_types/block_switch_block.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .block_switch_base import block_switch_base
from CompileError import CompileError
from CompileError import CompileError, Pos

class block_switch_block(block_switch_base):
def __init__(self, line, coords, cases, include_block_states):
Expand Down Expand Up @@ -98,7 +98,7 @@ def compile_single_case(self, func, block, block_test, block_state, case_func_na
case.compile(block.replace('minecraft:', ''), block_state, id, case_func, falling_block_nbt)
except CompileError as e:
print(e)
raise CompileError(f'Unable to compile block switch case "{block_state}" at line {self.line}')
raise CompileError(f'Unable to compile block switch case "{block_state}" at line {self.line}', Pos(self.line)) from e

func.call_function(
case_func,
Expand All @@ -115,4 +115,4 @@ def get_range_condition(self, func, blocks):
return f'block {self.coords.get_value(func)} #{func.namespace}:{block_tag_name}'

def get_case_ids(self):
return sorted(self.block_list.keys())
return sorted(self.block_list.keys())
12 changes: 5 additions & 7 deletions block_types/execute_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .block_base import block_base
import traceback
from CompileError import CompileError
from CompileError import CompileError, Pos

class execute_base(block_base):
# Override to force the creation of a sub function, even for a single command
Expand All @@ -20,16 +20,14 @@ def perform_execute(self, func):

cmd = 'execute ' + func.get_execute_items(self.exec_items, exec_func)
if cmd == None:
raise CompileError(f'Unable to compile {self.display_name()} block at line {self.line}')
raise CompileError(f'Unable to compile {self.display_name()} block at line {self.line}', Pos(self.line))

try:
exec_func.compile_blocks(self.sub)
except CompileError as e:
print(e)
raise CompileError(f'Unable to compile {self.display_name()} block contents at line {self.line}')
raise CompileError(f'Unable to compile {self.display_name()} block contents at line {self.line}', Pos(self.line)) from e
except Exception as e:
print(traceback.format_exc())
raise CompileError(f'Unable to compile {self.display_name()} block contents at line {self.line}')
raise CompileError(f'Unable to compile {self.display_name()} block contents at line {self.line}') from e

single = exec_func.single_command()
if single == None or self.force_sub_function():
Expand Down Expand Up @@ -62,4 +60,4 @@ def perform_execute(self, func):
self.compile_else(func)

def compile_else(self, func):
None
None
10 changes: 4 additions & 6 deletions block_types/execute_block.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .execute_base import execute_base
from CompileError import CompileError
from CompileError import CompileError, Pos
import traceback

class execute_block(execute_base):
Expand Down Expand Up @@ -38,11 +38,9 @@ def compile_else(self, func):
try:
exec_func.compile_blocks(else_sub)
except CompileError as e:
print(e)
raise CompileError(f'Unable to compile else block contents at line {self.display_name()}')
raise CompileError(f'Unable to compile else block contents at line {self.display_name()}', Pos(self.line)) from e
except Exception as e:
print(traceback.format_exc())
raise CompileError(f'Unable to compile else block contents at line {self.display_name()}')
raise CompileError(f'Unable to compile else block contents at line {self.display_name()}',Pos(self.line)) from e

if execute_items == None:
exec_text = ''
Expand Down Expand Up @@ -77,4 +75,4 @@ def compile_else(self, func):

func.add_command(cmd)

func.free_scratch(self.scratch)
func.free_scratch(self.scratch)
8 changes: 3 additions & 5 deletions block_types/for_index_block.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .block_base import block_base
import traceback
from CompileError import CompileError
from CompileError import CompileError, Pos

class for_index_block(block_base):
def __init__(self, line, var, fr, to, by, sub):
Expand Down Expand Up @@ -46,11 +46,9 @@ def compile(self, func):
try:
loop_func.compile_blocks(sub)
except CompileError as e:
print(e)
raise CompileError(f'Unable to compile for block contents at line {self.line}')
raise CompileError(f'Unable to compile for block contents at line {self.line}', Pos(self.line)) from e
except Exception as e:
print(traceback.print_exc())
raise CompileError(f'Unable to compile for block contents at line {self.line}')
raise CompileError(f'Unable to compile for block contents at line {self.line}', Pos(self.line)) from e

if by == None:
# Use a temporary version of the counting var to work with the scoreboard
Expand Down
10 changes: 4 additions & 6 deletions block_types/for_selector_block.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .block_base import block_base
from CompileError import CompileError
from CompileError import CompileError, Pos
import traceback

class for_selector_block(block_base):
Expand All @@ -26,11 +26,9 @@ def compile(self, func):
try:
exec_func.compile_blocks(self.sub)
except CompileError as e:
print(e)
raise CompileError(f'Unable to compile for block contents at line {self.line}')
except:
print(traceback.format_exc())
raise CompileError(f'Unable to compile for block contents at line {self.line}')
raise CompileError(f'Unable to compile for block contents at line {self.line}',Pos(self.line)) from e
except Exception as e:
raise CompileError(f'Unable to compile for block contents at line {self.line}',Pos(self.line)) from e

exec_func.add_command(f'scoreboard players set @s {scratch_id} 0')

Expand Down
12 changes: 6 additions & 6 deletions block_types/import_block.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .block_base import block_base
import traceback
from CompileError import CompileError
from CompileError import CompileError, Pos

class import_block(block_base):
def __init__(self, line, filename):
Expand All @@ -11,11 +11,11 @@ def compile(self, func):
try:
func.import_file(self.filename + '.cblib')
except SyntaxError as e:
print(e)
raise CompileError(f'Importing file "{self.filename}" failed at line {self.line}:\n{e}')
raise CompileError(f'Importing file "{self.filename}" failed at line {self.line}:\n{e}',Pos(self.line)) from e
except CompileError as e:
print(e)
raise CompileError(f'Importing file "{self.filename}" failed at line {self.line}:\n{e}')
raise CompileError(f'Importing file "{self.filename}" failed at line {self.line}:\n{e}',Pos(self.line)) from e
except FileNotFoundError:
raise CompileError(f'Could not find file "{self.filename}", at line {self.line}',Pos(self.line)) from None
except Exception as e:
print(traceback.format_exc())
raise CompileError(f'Importing file "{self.filename}" failed at line {self.line}:\n{e}')
raise CompileError(f'Importing file "{self.filename}" failed at line {self.line}:\n{e}',Pos(self.line)) from e
Loading