WingedSeal / jmc

A compiler for JMC (JavaScript-like Minecraft Function), a mcfunction extension language for making Minecraft Datapack
https://jmc.wingedseal.com
MIT License
64 stars 8 forks source link

Switch-case statements can now start at any number #18

Closed Nico314159 closed 1 year ago

Nico314159 commented 1 year ago

Simple change that doesn't affect the optimization. Useful because often ones needs it to start at a value of 0 or -1 instead of at 1.

NOTE: this still keeps the "numbers have to be in sequence without skipping" restriction.

WingedSeal commented 1 year ago
switch($var) {
    case 2:
        say "2";
    case 3:
        say "3";
    case 4:
        say "4";
    case 5:
        say "5";
}
Unexpected error causes program to crash                                                                                                                                         
IndexError
list index out of range                                                                                                                                                          
Traceback (most recent call last):                                                                                                                                               
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\terminal_commands.py", line 62, in compile_                                                                            
    compile_jmc(global_data.config, debug=True)                                                                                                                                  
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\compiling.py", line 36, in compile_jmc                                                                         
    lexer = Lexer(config)                                                                                                                                                        
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\lexer.py", line 86, in __init__                                                                                
    self.parse_file(Path(self.config.target), _test_file, is_load=True)                                                                                                          
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\lexer.py", line 217, in parse_file                                                                             
    self.parse_current_load()                                                                                                                                                    
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\lexer.py", line 101, in parse_current_load                                                                     
    self.parse_load_func_content(programs=self.datapack.load_function))                                                                                                          
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\lexer.py", line 437, in parse_load_func_content                                                                
    return self._parse_func_content(tokenizer, programs, is_load=True)                                                                                                           
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\lexer.py", line 479, in _parse_func_content                                                                    
    return FuncContent(tokenizer, programs, is_load, self).parse()                                                                                                               
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\lexer_func_content.py", line 154, in parse                                                                     
    self.__parse_commands()                                                                                                                                                      
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\lexer_func_content.py", line 185, in __parse_commands                                                          
    if self.__expect_command(key_pos, token):                                                                                                                                    
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\lexer_func_content.py", line 363, in __expect_command                                                          
    if self.__is_flow_control_command(key_pos, token):                                                                                                                           
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\lexer_func_content.py", line 604, in __is_flow_control_command                                                 
    return_value = flow_control_command(                                                                                                                                         
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\command\_flow_control.py", line 303, in switch                                                                 
    return parse_switch(scoreboard_player, func_contents, case_start, datapack)                                                                                                  
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\command\_flow_control.py", line 205, in parse_switch                                                           
    __parse_switch_binary(start, len(func_contents) + start - 1, count,                                                                                                          
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\command\_flow_control.py", line 189, in __parse_switch_binary                                                  
    __parse_switch_binary(half2, max_, count_more,                                                                                                                               
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\command\_flow_control.py", line 189, in __parse_switch_binary                                                  
    __parse_switch_binary(half2, max_, count_more,                                                                                                                               
  File "C:\Users\User\Desktop\jmc-upgraded-switch\src\jmc\compile\command\_flow_control.py", line 167, in __parse_switch_binary                                                  
    name, func_contents[min_ - 1], count)                                                                                                                                        
IndexError: list index out of range
Nico314159 commented 1 year ago

Changed line 167 to say datapack.add_raw_private_function(name, func_contents[min_ - start_at], count) instead of datapack.add_raw_private_function(name, func_contents[min_ - 1], count), which should fix your issue.

WingedSeal commented 1 year ago

LGTM