import seems working

This commit is contained in:
dekuNukem
2026-01-07 18:10:29 +00:00
parent 4d7d34d576
commit 9546037f19
5 changed files with 58 additions and 23 deletions
+6
View File
@@ -941,5 +941,11 @@ def is_ds_keyword(name):
return True
return False
def make_list_of_ds_line_obj_from_str_listing(pgm_listing, source_fn=None):
obj_list = []
for index, item in enumerate(pgm_listing):
obj_list.append(ds_line(item, index+1, source_fn=source_fn))
return obj_list
if __name__ == "__main__":
generate_c_code()
+15 -6
View File
@@ -102,7 +102,7 @@ AST_ARITH_NODES = (
ast.unaryop,
)
def get_orig_ds_line_from_py_lnum(rdict, this_pylnum_sf1):
def get_orig_ds_line_from_py_lnum_has_exception(rdict, this_pylnum_sf1):
if this_pylnum_sf1 is None:
return ""
og_index_sf0 = None
@@ -114,6 +114,13 @@ def get_orig_ds_line_from_py_lnum(rdict, this_pylnum_sf1):
return ""
return rdict['orig_listing'][og_index_sf0].content
def get_orig_ds_line_from_py_lnum(rdict, this_pylnum_sf1):
try:
return get_orig_ds_line_from_py_lnum_has_exception(rdict, this_pylnum_sf1)
except Exception as e:
print(f"get_orig_ds_line_from_py_lnum: {e}")
return ""
def search_in_symtable(name: str, table: symtable.SymbolTable):
try:
return table.lookup(name)
@@ -614,13 +621,13 @@ def replace_dummy_with_drop_from_context_dict(ctx_dict):
for key in ctx_dict['func_assembly_dict']:
replace_dummy_with_drop(ctx_dict['func_assembly_dict'][key])
def make_dsb_with_exception(program_listing, should_print=False, remove_unused_func=True, header_dict=None):
def make_dsb_with_exception(program_listing, should_print=False, remove_unused_func=True, import_name_to_strlist_dict=None):
global global_context_dict
global print_asm
print_asm = should_print
orig_listing = copy.deepcopy(program_listing)
rdict = dsvm_preprocessor.run_all(program_listing)
rdict = dsvm_preprocessor.run_all(program_listing, import_name_to_strlist_dict=import_name_to_strlist_dict)
if rdict['is_success'] is False:
comp_result = compile_result(
@@ -680,11 +687,11 @@ def make_dsb_with_exception(program_listing, should_print=False, remove_unused_f
)
return comp_result
def make_dsb_no_exception(program_listing, should_print=False, remove_unused_func=True, header_dict=None):
def make_dsb_no_exception(program_listing, should_print=False, remove_unused_func=True, import_name_to_strlist_dict=None):
global print_asm
print_asm = should_print
try:
return make_dsb_with_exception(program_listing, should_print, remove_unused_func, header_dict)
return make_dsb_with_exception(program_listing, should_print, remove_unused_func, import_name_to_strlist_dict)
except Exception as e:
print("MDNE:", traceback.format_exc())
comp_result = compile_result(
@@ -720,7 +727,9 @@ if __name__ == "__main__":
line = line.rstrip("\r\n")
program_listing.append(ds_line(line, index + 1))
comp_result = make_dsb_no_exception(program_listing, should_print=True)
import_str_dict = {'IMPORT_GH': ['FUN test(a, b)', ' VAR test = 10', ' RETURN a+b*test', 'END_FUN']}
comp_result = make_dsb_no_exception(program_listing, should_print=True, import_name_to_strlist_dict=import_str_dict)
if comp_result.is_success is False:
error_msg = (f"Error on Line {comp_result.error_line_number_starting_from_1}: {comp_result.error_comment}\n\t{comp_result.error_line_str}")
print(error_msg)
+32 -5
View File
@@ -384,7 +384,7 @@ def check_var_declare(pgm_line, var_dict, fss):
return PARSE_OK, ''
# this makes sure the code is suitable for converting into python
def single_pass(program_listing, define_dict):
def single_pass(program_listing, define_dict, import_name_to_line_obj_dict=None):
loop_numbers = set()
func_table = {}
if_take_table = {}
@@ -612,10 +612,30 @@ def get_default_def_dict():
}
return default_dict
def run_all(program_listing):
def make_import_name_to_lineobj_dict(import_name_to_strlist_dict):
lineobj_dict = {}
if import_name_to_strlist_dict is None:
return lineobj_dict
for key in import_name_to_strlist_dict:
lineobj_dict[key] = make_list_of_ds_line_obj_from_str_listing(import_name_to_strlist_dict[key], source_fn=key)
return lineobj_dict
already_imported_header_set = set()
def get_import_lineobjs(first_word, import_name_to_line_obj_dict):
new_lineobj_list = []
if first_word in import_name_to_line_obj_dict:
if first_word in already_imported_header_set:
return True, []
new_lineobj_list += import_name_to_line_obj_dict[first_word]
already_imported_header_set.add(first_word)
return False, new_lineobj_list
def run_all(program_listing, import_name_to_strlist_dict=None):
all_def_dict = get_default_def_dict()
already_imported_header_set.clear()
import_name_to_line_obj_dict = make_import_name_to_lineobj_dict(import_name_to_strlist_dict)
# ----------- expand STRING_BLOCK and STRINGLN_BLOCK, split STRING and STRINGLN ----------
rdict = single_pass(program_listing, all_def_dict)
rdict = single_pass(program_listing, all_def_dict, import_name_to_line_obj_dict)
if rdict['is_success'] is False:
return rdict
@@ -634,6 +654,13 @@ def run_all(program_listing):
continue
first_word = line_obj.content.split(" ")[0]
is_already_imported, imported_lineobj_list = get_import_lineobjs(first_word, import_name_to_line_obj_dict)
if is_already_imported:
continue
if len(imported_lineobj_list):
new_program_listing += imported_lineobj_list
continue
if first_word in [kw_STRINGLN_BLOCK, kw_END_STRINGLN, kw_STRING_BLOCK, kw_END_STRING]:
continue
@@ -678,7 +705,7 @@ def run_all(program_listing):
program_listing = new_program_listing
rdict = single_pass(program_listing, all_def_dict)
rdict = single_pass(program_listing, all_def_dict, import_name_to_line_obj_dict)
if rdict['is_success'] is False:
return rdict
@@ -766,7 +793,7 @@ def run_all(program_listing):
second_pass_program_listing = new_program_listing
# -----------------
return single_pass(second_pass_program_listing, all_def_dict)
return single_pass(second_pass_program_listing, all_def_dict, import_name_to_line_obj_dict)
if __name__ == "__main__":
# Require at least input and output arguments
+4 -5
View File
@@ -1101,7 +1101,7 @@ def edit_header_button_click(global_setting_obj):
# 1. Setup UI elements first so the function below can reference them
top_label = Label(
header_edit_window,
text="Add \"IMPORT GLOBAL_HEADER\" to your script to include this header AS-IS.",
text=f"To include this global header, add \"{global_header_source_tag_NO_SPACE}\" to your script.",
justify="center",
)
top_label.pack(side="top", pady=(10, 5))
@@ -1135,7 +1135,7 @@ def edit_header_button_click(global_setting_obj):
program_listing = header_script_textbox.get("1.0", "end-1c").replace("\r", "").split("\n")
global_setting_obj.global_header_line_list = program_listing
ds_line_obj_list = make_list_of_ds_line_obj_from_str_listing(program_listing, source_fn=global_header_source_tag)
ds_line_obj_list = make_list_of_ds_line_obj_from_str_listing(program_listing, source_fn=global_header_source_tag_NO_SPACE)
comp_result = dsvm_make_bytecode.make_dsb_no_exception(ds_line_obj_list, remove_unused_func=False)
if comp_result.is_success:
@@ -1718,11 +1718,10 @@ def check_syntax():
if program_listing == last_check_syntax_listing:
# print("check_syntax: same")
return
header_dict = {global_header_source_tag:this_global_setting.global_header_line_list}
print(header_dict)
import_name_to_strlist_dict = {global_header_source_tag_NO_SPACE:this_global_setting.global_header_line_list}
last_check_syntax_listing = program_listing.copy()
ds_line_obj_list = make_list_of_ds_line_obj_from_str_listing(program_listing)
comp_result = dsvm_make_bytecode.make_dsb_no_exception(ds_line_obj_list, header_dict=header_dict)
comp_result = dsvm_make_bytecode.make_dsb_no_exception(ds_line_obj_list, import_name_to_strlist_dict=import_name_to_strlist_dict)
script_textbox.tag_remove("error", '1.0', 'end')
if comp_result.is_success:
check_syntax_label.config(text="Code seems OK...", fg="green")
+1 -7
View File
@@ -55,12 +55,6 @@ def open_duckypad_troubleshooting_url():
def rgb_to_hex(rgb_tuple):
return '#%02x%02x%02x' % rgb_tuple
def make_list_of_ds_line_obj_from_str_listing(pgm_listing, source_fn=None):
obj_list = []
for index, item in enumerate(pgm_listing):
obj_list.append(ds_line(item, index+1, source_fn=source_fn))
return obj_list
def make_final_script(ds_key, pgm_listing):
final_listing = []
if ds_key.allow_abort:
@@ -107,7 +101,7 @@ MAX_KEY_COUNT = (MECH_OBSW_COUNT + ROTARY_ENCODER_SW_COUNT + ONBOARD_SPARE_GPIO_
profile_info_dot_txt = "profile_info.txt"
global_header_dot_txt = "global_header.txt"
global_header_source_tag = "IMPORT GLOBAL_HEADER"
global_header_source_tag_NO_SPACE = "IMPORT_GH"
HID_COMMAND_READ_FILE = 11