From dd81a60ba912543afc1cadd6b1f4692e31bfeb99 Mon Sep 17 00:00:00 2001 From: Kyler Date: Sun, 14 Dec 2025 23:00:22 -0700 Subject: [PATCH] worked on pico build without cmake --- SLS_C/.gitignore | 5 +- SLS_C/build.py | 414 ++++++++++++++++++++++++------------- SLS_C/include/sls/string.h | 2 + SLS_C/src/builtin.c | 16 +- SLS_C/src/interpreter.c | 4 + SLS_C/src/lexer.c | 22 +- SLS_C/src/pico_main.c | 8 +- SLS_C/src/string.c | 14 +- 8 files changed, 313 insertions(+), 172 deletions(-) diff --git a/SLS_C/.gitignore b/SLS_C/.gitignore index a167c26..9048a50 100644 --- a/SLS_C/.gitignore +++ b/SLS_C/.gitignore @@ -1,7 +1,6 @@ obj/ bin/ -build_pico/ +generated/ *.o *.pdb -CMakeLists.txt -pico_arm_gcc_toolchain.cmake +pico-sdk.txt diff --git a/SLS_C/build.py b/SLS_C/build.py index a111233..7224f17 100644 --- a/SLS_C/build.py +++ b/SLS_C/build.py @@ -13,17 +13,18 @@ SRC_DIR = Path("src") TEST_DIR = Path("tests") OBJ_DIR = Path("obj") BIN_DIR = Path("bin") +PICO_GENERATED = Path("generated") TARGET = BIN_DIR / "sls" TEST_TARGET = BIN_DIR / "sls_tests" + # Platform-specific settings PICO_SDK_PATH = os.environ.get("PICO_SDK_PATH", Path.home() / "pico/pico-sdk") -PICO_BUILD_DIR = Path("build_pico") -PICO_TOOLCHAIN_PATH = Path("pico_arm_gcc_toolchain.cmake") # Unix gcc/clang flags COMMON_FLAGS = ["-std=c99", "-Wall", "-Wextra", "-Werror", "-Iinclude", "-g"] +PICO_FLAGS = ["-std=c11", "-Wall", "-Wextra", "-Werror", "-Iinclude", "-g"] TEST_FLAGS = ["-std=c99", "-Wall", "-Wextra", "-Wno-unused-function", "-Werror", "-Iinclude", "-g", "-O0"] @@ -35,82 +36,244 @@ MACOS_FLAGS = ["-std=c99", "-Wall", "-Wextra", "-Werror", "-Iinclude", "-g", MSVC_FLAGS = ["/std:c11", "/Zi", "/Iinclude"] MSVC_TEST_FLAGS = MSVC_FLAGS + [] -# RP2040 toolchain file template -PICO_TOOLCHAIN_TEMPLATE = """set(CMAKE_SYSTEM_NAME Generic) -set(CMAKE_SYSTEM_PROCESSOR cortex-m0plus) +# --------------------------------------------------------------------- +# RP2040 SETTINGS +# --------------------------------------------------------------------- +PICO_CPU_FLAGS = [ + "-mcpu=cortex-m0plus", + "-mthumb", + "-O2", + "-ffunction-sections", + "-fdata-sections" +] -# Specify the cross compiler -set(CMAKE_C_COMPILER arm-none-eabi-gcc) -set(CMAKE_CXX_COMPILER arm-none-eabi-g++) -set(CMAKE_ASM_COMPILER arm-none-eabi-gcc) +PICO_DEFINES = [ + "-DPICO_BUILD=1", + "-DPICO_ON_DEVICE=1", +] -# Compiler flags for Cortex-M0+ -set(CMAKE_C_FLAGS_INIT "-mcpu=cortex-m0plus -mthumb") -set(CMAKE_CXX_FLAGS_INIT "-mcpu=cortex-m0plus -mthumb") -set(CMAKE_ASM_FLAGS_INIT "-mcpu=cortex-m0plus -mthumb") +PICO_ASM_INCLUDES = [ + PICO_GENERATED / "include", + f"{PICO_SDK_PATH}/src/rp2040/boot_stage2/asminclude", + f"{PICO_SDK_PATH}/src/rp2040/pico_platform/include", + f"{PICO_SDK_PATH}/src/rp2040/hardware_regs/include", + f"{PICO_SDK_PATH}/src/common/pico_base_headers/include", + f"{PICO_SDK_PATH}/src/rp2_common/pico_platform_compiler/include", + f"{PICO_SDK_PATH}/src/rp2_common/pico_platform_sections/include", + f"{PICO_SDK_PATH}/src/rp2_common/pico_platform_panic/include", + f"{PICO_SDK_PATH}/src/rp2_common/pico_platform_common/include", + f"{PICO_SDK_PATH}/src/common/pico_binary_info/include", + f"{PICO_SDK_PATH}/src/common/boot_picobin_headers/include", + f"{PICO_SDK_PATH}/src/rp2_common/pico_bootrom/include", + f"{PICO_SDK_PATH}/src/rp2_common/boot_bootrom_headers/include", +] -# Don't run the linker on compiler check -set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) +PICO_INCLUDES = [ + PICO_GENERATED / "include", -# Adjust the default behavior of the FIND_XXX() commands: -# search programs in the host environment -set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) + "src", + "include", -# Search headers and libraries in the target environment -set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) -set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) -set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) -""" + # Pico SDK core + f"{PICO_SDK_PATH}/src/common/pico_stdlib_headers/include", + f"{PICO_SDK_PATH}/src/common/pico_base_headers/include", + f"{PICO_SDK_PATH}/src/common/pico_base_headers/include", + f"{PICO_SDK_PATH}/src/common/pico_time/include", + f"{PICO_SDK_PATH}/src/common/pico_sync/include", + f"{PICO_SDK_PATH}/src/common/pico_binary_info/include", -# RP2040 settings -RP2040_CMAKE_TEMPLATE = """cmake_minimum_required(VERSION 3.13) + # RP2040 hardware + platform + f"{PICO_SDK_PATH}/src/rp2040/hardware_regs/include", + f"{PICO_SDK_PATH}/src/rp2040/hardware_structs/include", + f"{PICO_SDK_PATH}/src/rp2040/pico_platform/include", -# Pico SDK initialization -set(PICO_SDK_PATH "{pico_sdk_path}") -include({pico_sdk_path}/external/pico_sdk_import.cmake) + # Runtime + stdio + f"{PICO_SDK_PATH}/src/rp2_common/pico_platform_compiler/include", + f"{PICO_SDK_PATH}/src/rp2_common/pico_platform_sections/include", + f"{PICO_SDK_PATH}/src/rp2_common/pico_platform_panic/include", + f"{PICO_SDK_PATH}/src/rp2_common/pico_platform_common/include", + # f"{PICO_SDK_PATH}/src/rp2_common/pico_platform/include", + f"{PICO_SDK_PATH}/src/rp2_common/pico_runtime/include", + f"{PICO_SDK_PATH}/src/rp2_common/pico_stdio/include", + f"{PICO_SDK_PATH}/src/rp2_common/pico_stdio_usb/include", + f"{PICO_SDK_PATH}/src/rp2_common/pico_runtime_init/include", + f"{PICO_SDK_PATH}/src/rp2_common/pico_stdio_uart/include", -project({project_name} C CXX ASM) -set(CMAKE_C_STANDARD 11) -set(CMAKE_CXX_STANDARD 17) + # Hardware libs you use + f"{PICO_SDK_PATH}/src/rp2_common/hardware_gpio/include", + f"{PICO_SDK_PATH}/src/rp2_common/hardware_timer/include", + f"{PICO_SDK_PATH}/src/rp2_common/hardware_base/include", + f"{PICO_SDK_PATH}/src/rp2_common/hardware_gpio/include", + f"{PICO_SDK_PATH}/src/rp2_common/hardware_irq/include", + f"{PICO_SDK_PATH}/src/rp2_common/hardware_uart/include", + f"{PICO_SDK_PATH}/src/rp2_common/hardware_resets/include", + f"{PICO_SDK_PATH}/src/rp2_common/hardware_sync/include", + f"{PICO_SDK_PATH}/src/rp2_common/hardware_sync_spin_lock/include", + f"{PICO_SDK_PATH}/src/rp2_common/hardware_clocks/include", + f"{PICO_SDK_PATH}/src/common/hardware_claim/include", + f"{PICO_SDK_PATH}/src/rp2_common/hardware_pll/include", +] -pico_sdk_init() - -# Main executable -add_executable({project_name} -{source_files} +PICO_LINKER_SCRIPT = ( + Path(PICO_SDK_PATH) + / "src" + / "rp2_common" + / "pico_crt0" + / "rp2040" + / "memmap_default.ld" ) -# Set output name with .elf extension -set_target_properties({project_name} PROPERTIES - OUTPUT_NAME "{project_name}.elf" - SUFFIX "" -) +PICO_LINKER_SCRIPTS_DIR = PICO_GENERATED / "link/pico" -# Add include directories -target_include_directories({project_name} PRIVATE - ${{CMAKE_CURRENT_LIST_DIR}}/include -) +def pico_sdk_asm_sources(): + sdk = Path(PICO_SDK_PATH) -# Add compile definitions -target_compile_definitions({project_name} PRIVATE - PICO_BUILD=1 - GIT_COMMIT_HASH="{git_hash}" -) + return [ + sdk / "src/rp2040/boot_stage2/boot2_generic_03h.S", + sdk / "src/rp2_common/pico_crt0/crt0.S", + ] -# Link libraries -target_link_libraries({project_name} - pico_stdlib - hardware_uart - hardware_gpio -) +def compile_pico_asm_source(src: Path): + mkdir(OBJ_DIR) + obj = OBJ_DIR / (src.stem + ".pico.o") -# Enable USB/UART output -pico_enable_stdio_usb({project_name} 1) -pico_enable_stdio_uart({project_name} 1) + if obj.exists() and src.stat().st_mtime < obj.stat().st_mtime: + return obj -# Create map/bin/hex/uf2 files -pico_add_extra_outputs({project_name}) -""" + include_flags = [f"-I{p}" for p in PICO_ASM_INCLUDES] + + cmd = ( + ["arm-none-eabi-gcc"] + + PICO_CPU_FLAGS + + ["-DPICO_FLASH_SPI_CLKDIV=2"] + + include_flags + + ["-c", str(src), "-o", str(obj)] + ) + + run(cmd) + return obj + +def pico_sdk_sources(): + sdk = Path(PICO_SDK_PATH) + + return [ + sdk / "src/rp2_common/pico_runtime/runtime.c", + sdk / "src/rp2_common/pico_runtime_init/runtime_init.c", + sdk / "src/rp2040/pico_platform/platform.c", + sdk / "src/rp2_common/pico_stdio/stdio.c", + sdk / "src/rp2_common/pico_stdio_uart/stdio_uart.c", + sdk / "src/rp2_common/hardware_uart/uart.c", + sdk / "src/rp2_common/hardware_gpio/gpio.c", + sdk / "src/rp2_common/pico_stdlib/stdlib.c", + sdk / "src/rp2_common/hardware_clocks/clocks.c", + sdk / "src/rp2_common/hardware_sync/sync.c", + sdk / "src/rp2_common/hardware_irq/irq.c", + ] + +def compile_pico_source(src: Path): + mkdir(OBJ_DIR) + obj = OBJ_DIR / (src.stem + ".pico.o") + + if obj.exists() and src.stat().st_mtime < obj.stat().st_mtime: + return obj + + include_flags = [f"-I{p}" for p in PICO_INCLUDES] + + cmd = ( + ["arm-none-eabi-gcc"] + + PICO_CPU_FLAGS + + PICO_FLAGS + + PICO_DEFINES + + include_flags + + [f"-DGIT_COMMIT_HASH=\"{GIT_HASH}\""] + + ["-c", str(src), "-o", str(obj)] + ) + + run(cmd) + return obj + +def link_pico_elf(objects, elf_path: Path): + cmd = [ + "arm-none-eabi-gcc", + "-nostdlib", + "-Wl,--gc-sections", + f"-Wl,-L{PICO_LINKER_SCRIPTS_DIR}", + f"-T{PICO_LINKER_SCRIPT}", + "-Wl,-Map=" + str(elf_path.with_suffix(".map")), + ] + list(map(str, objects)) + [ + "-o", + str(elf_path), + "-lc", + "-lgcc", + ] + + run(cmd) + +def elf_to_uf2(elf: Path): + elf2uf2 = Path(PICO_SDK_PATH) / "tools/elf2uf2/elf2uf2" + + if not elf2uf2.exists(): + raise RuntimeError("elf2uf2 not found") + + uf2 = elf.with_suffix(".uf2") + run([str(elf2uf2), str(elf), str(uf2)]) + return uf2 + +def generate_pico_headers(): + out = PICO_GENERATED / "include/pico" + mkdir(out) + + version_template = Path(PICO_SDK_PATH) / "src/common/pico_base_headers/include/pico/version.h.in" + version_out = out / "version.h" + + if not version_out.exists(): + + version_out.write_text(version_template.read_text() + .replace("${PICO_SDK_VERSION_MAJOR}", "1") + .replace("${PICO_SDK_VERSION_MINOR}", "5") + .replace("${PICO_SDK_VERSION_REVISION}", "1") + .replace("${PICO_SDK_VERSION_STRING}", "1.5.1") + ) + + config_out = out / "config_autogen.h" + + if not config_out.exists(): + config_out.write_text("""// generated/pico/config_autogen.h +#pragma once + +/* Platform */ +#define PICO_RP2040 1 + +/* Board / runtime */ +#define PICO_ON_DEVICE 1 +#define PICO_NO_HARDWARE 0 + +/* stdio */ +#define PICO_STDIO_UART 1 +#define PICO_STDIO_USB 0 + +/* Assertions / debug */ +#define PICO_ENABLE_ASSERTIONS 0 +#define PICO_USE_STACK_GUARDS 0 + +/* Time */ +#define PICO_TIME_DEFAULT_ALARM_POOL_DISABLED 1 + +/* Multicore (disable if unused) */ +#define PICO_MULTICORE 0 +""") + +def generate_pico_linker_scripts(): + mkdir(PICO_LINKER_SCRIPTS_DIR) + + pico_flash_region_template = Path(PICO_SDK_PATH) / "src/rp2_common/pico_standard_link/pico_flash_region.template.ld" + pico_flash_region_out = PICO_LINKER_SCRIPTS_DIR / "pico_flash_region.ld" + + if not pico_flash_region_out.exists(): + + pico_flash_region_out.write_text(pico_flash_region_template.read_text() + .replace("${PICO_FLASH_SIZE_BYTES_STRING}", hex(2 * 1024 * 1024)) + ) # --------------------------------------------------------------------- # PLATFORM DETECTION @@ -293,84 +456,57 @@ def check_pico_sdk(): return True -def generate_pico_toolchain(): - """Generate ARM GCC toolchain file for CMake""" - with open(PICO_TOOLCHAIN_PATH, "w") as f: - f.write(PICO_TOOLCHAIN_TEMPLATE) - print(f"Generated {PICO_TOOLCHAIN_PATH}") - - -def generate_pico_cmake(project_name="sls"): - """Generate CMakeLists.txt for RP2040 build""" - sources = list(SRC_DIR.glob("*.c")) - # Exclude main.c, repl.c, file.c and test files for Pico build - # pico_main.c will provide its own REPL implementation - sources = [s for s in sources - if s.name not in ["main.c", "repl.c", "file.c"] - and "test" not in s.stem.lower()] - - # Check for pico_main.c - pico_main = SRC_DIR / "pico_main.c" - if not pico_main.exists(): - print(f"WARNING: {pico_main} not found! Please create it.") - print("The Pico build requires a pico_main.c file.") - return False - - sources.append(pico_main) - - source_files = "\n".join(f" {s}" for s in sources) - - cmake_content = RP2040_CMAKE_TEMPLATE.format( - project_name=project_name, - source_files=source_files, - git_hash=GIT_HASH, - pico_sdk_path=PICO_SDK_PATH - ) - - cmake_file = Path("CMakeLists.txt") - with open(cmake_file, "w") as f: - f.write(cmake_content) - - print(f"Generated {cmake_file}") - return True - - def build_rp2040(): - """Build for RP2040 using CMake""" if not check_pico_sdk(): return False - + print("\n=== Building for RP2040 ===\n") - - # Generate toolchain file - generate_pico_toolchain() - - # Generate CMakeLists.txt - if not generate_pico_cmake(): - return False - - # Create build directory - mkdir(PICO_BUILD_DIR) - - # Run CMake with explicit toolchain - cmake_cmd = [ - "cmake", - "-B", str(PICO_BUILD_DIR), - "-S", ".", - f"-DCMAKE_TOOLCHAIN_FILE={PICO_TOOLCHAIN_PATH}" + + print(f"PICO_SDK_PATH: {PICO_SDK_PATH}") + + generate_pico_headers() + generate_pico_linker_scripts() + + mkdir(OBJ_DIR) + mkdir(BIN_DIR) + + # Your application sources + app_sources = [ + s for s in SRC_DIR.glob("*.c") + if s.name not in ["main.c", "repl.c", "file.c"] + and "test" not in s.stem.lower() ] - run(cmake_cmd) - - # Build - build_cmd = ["cmake", "--build", str(PICO_BUILD_DIR), "-j4"] - run(build_cmd) - - # Show output files - uf2_file = PICO_BUILD_DIR / "sls.uf2" - if uf2_file.exists(): - print(f"\nBuild successful! UF2 file: {uf2_file}") - print("To flash: Copy this file to your Pico in BOOTSEL mode") - + + pico_main = SRC_DIR / "pico_main.c" + if not pico_main.exists(): + print("ERROR: src/pico_main.c is required for Pico builds") + return False + + app_sources.append(pico_main) + + # SDK sources + sdk_asm_sources = pico_sdk_asm_sources() + sdk_sources = pico_sdk_sources() + + # Compile everything + objects = [] + for src in sdk_asm_sources: + objects.append(compile_pico_asm_source(src)) + for src in set(app_sources + sdk_sources): + objects.append(compile_pico_source(src)) + + # Link ELF + elf = BIN_DIR / "sls_pico.elf" + link_pico_elf(objects, elf) + + # Generate UF2 + uf2 = elf_to_uf2(elf) + + print("\nBuild successful!") + print(f"ELF: {elf}") + print(f"UF2: {uf2}") + print("Flash by copying UF2 to Pico BOOTSEL drive") + return True @@ -418,13 +554,7 @@ def build_tests(): def clean(): shutil.rmtree(OBJ_DIR, ignore_errors=True) shutil.rmtree(BIN_DIR, ignore_errors=True) - shutil.rmtree(PICO_BUILD_DIR, ignore_errors=True) - cmake_file = Path("CMakeLists.txt") - if cmake_file.exists(): - cmake_file.unlink() - toolchain_file = PICO_TOOLCHAIN_PATH - if toolchain_file.exists(): - toolchain_file.unlink() + shutil.rmtree(PICO_GENERATED, ignore_errors=True) print("Cleaned.") diff --git a/SLS_C/include/sls/string.h b/SLS_C/include/sls/string.h index 862480c..e8da024 100644 --- a/SLS_C/include/sls/string.h +++ b/SLS_C/include/sls/string.h @@ -12,6 +12,8 @@ #include "bool.h" +#define VA_ENUM(args, type) ((type)va_arg(args, int)) + typedef struct { size_t len; // Number of useable characters (does not include trailing null character) char *str; diff --git a/SLS_C/src/builtin.c b/SLS_C/src/builtin.c index 8948430..813f7d6 100644 --- a/SLS_C/src/builtin.c +++ b/SLS_C/src/builtin.c @@ -1130,7 +1130,7 @@ Boolean builtin_abs(InterpreterState *interpreter_state) { .type = TOKEN_INTEGER, .integer_literal = (IntegerLiteral){ .type = INTEGER_I64, - .value = labs((int64_t)ai), + .value = llabs((int64_t)ai), }, }); } else if (type & NUMERIC_SIGNED && type & NUMERIC_32) { @@ -1138,7 +1138,7 @@ Boolean builtin_abs(InterpreterState *interpreter_state) { .type = TOKEN_INTEGER, .integer_literal = (IntegerLiteral){ .type = INTEGER_I32, - .value = labs((int64_t)ai), + .value = llabs((int64_t)ai), }, }); } else if (type & NUMERIC_SIGNED && type & NUMERIC_16) { @@ -1146,7 +1146,7 @@ Boolean builtin_abs(InterpreterState *interpreter_state) { .type = TOKEN_INTEGER, .integer_literal = (IntegerLiteral){ .type = INTEGER_I16, - .value = labs((int64_t)ai), + .value = llabs((int64_t)ai), }, }); } else { @@ -1154,7 +1154,7 @@ Boolean builtin_abs(InterpreterState *interpreter_state) { .type = TOKEN_INTEGER, .integer_literal = (IntegerLiteral){ .type = INTEGER_I8, - .value = labs((int64_t)ai), + .value = llabs((int64_t)ai), }, }); } @@ -1685,9 +1685,9 @@ Boolean builtin_for(InterpreterState *interpreter_state) { interpreter_state->stack = start_item->next; start_item->next = NULL; - size_t start_value; + size_t start_value = 0; SCALAR(start_item, start_value); - size_t end_value; + size_t end_value = 0; SCALAR(end_item, end_value); Boolean return_value = FALSE; @@ -1905,10 +1905,10 @@ Boolean builtin_roll(InterpreterState *interpreter_state) { StackItem *times_item = interpreter_state->stack; StackItem *count_item = times_item->next; - size_t times; + size_t times = 0; SCALAR(times_item, times); - size_t count; + size_t count = 0; SCALAR(count_item, count); StackItem *head = NULL, *tail = NULL, *prev = NULL; diff --git a/SLS_C/src/interpreter.c b/SLS_C/src/interpreter.c index a7d905a..c0f6300 100644 --- a/SLS_C/src/interpreter.c +++ b/SLS_C/src/interpreter.c @@ -93,6 +93,8 @@ Boolean push_token(InterpreterState *interpreter_state, Token token) { case INTEGER_U8: type = STACK_U8; break; + default: + return FALSE; } break; case TOKEN_FLOAT: @@ -116,6 +118,8 @@ Boolean push_token(InterpreterState *interpreter_state, Token token) { break; case TOKEN_TYPE_TUPLE: return FALSE; + default: + return FALSE; } StackItem *item = (StackItem *)malloc(sizeof(StackItem)); diff --git a/SLS_C/src/lexer.c b/SLS_C/src/lexer.c index 4f9fe40..fd7a7f7 100644 --- a/SLS_C/src/lexer.c +++ b/SLS_C/src/lexer.c @@ -220,7 +220,8 @@ static uint64_t create_binary_integer(LexerInfo *lexer_info, size_t start) { i += 1; } for (; i < lexer_info->pos - start; i++) { - if (isspace(token[i]) || token[i] == '/' || token[i] == '\0' || token[i] == ':') break; + if (isspace((unsigned char)token[i]) || token[i] == '/' || token[i] == '\0' || token[i] == ':') + break; if (token[i] == '.' || token[i] == '_') continue; value *= 2; switch (token[i]) { @@ -241,7 +242,8 @@ static uint64_t create_octal_integer(LexerInfo *lexer_info, size_t start) { i += 1; } for (; i < lexer_info->pos - start; i++) { - if (isspace(token[i]) || token[i] == '/' || token[i] == '\0' || token[i] == ':') break; + if (isspace((unsigned char)token[i]) || token[i] == '/' || token[i] == '\0' || token[i] == ':') + break; if (token[i] == '.' || token[i] == '_') continue; value *= 8; switch (token[i]) { @@ -268,7 +270,8 @@ static uint64_t create_decimal_integer(LexerInfo *lexer_info, size_t start) { i += 1; } for (; i < lexer_info->pos - start; i++) { - if (isspace(token[i]) || token[i] == '/' || token[i] == '\0' || token[i] == ':') break; + if (isspace((unsigned char)token[i]) || token[i] == '/' || token[i] == '\0' || token[i] == ':') + break; if (token[i] == '_') continue; value *= 10; switch (token[i]) { @@ -297,7 +300,8 @@ static uint64_t create_hexadecimal_integer(LexerInfo *lexer_info, size_t start) i += 1; } for (; i < lexer_info->pos - start; i++) { - if (isspace(token[i]) || token[i] == '/' || token[i] == '\0' || token[i] == ':') break; + if (isspace((unsigned char)token[i]) || token[i] == '/' || token[i] == '\0' || token[i] == ':') + break; if (token[i] == '.' || token[i] == '_') continue; value *= 16; switch (token[i]) { @@ -387,7 +391,8 @@ static double create_float(LexerInfo *lexer_info, size_t start) { i += 1; } for (; i < lexer_info->pos - start; i++) { - if (isspace(token[i]) || token[i] == '/' || token[i] == '\0' || token[i] == ':') break; + if (isspace((unsigned char)token[i]) || token[i] == '/' || token[i] == '\0' || token[i] == ':') + break; if (token[i] == '_') continue; if (token[i] == '.') { fractional = 1; @@ -653,13 +658,14 @@ static LexerResult parse_string_literal(LexerInfo *lexer_info, char c, size_t st } static void skip_comments_and_whitespace(LexerInfo *lexer_info) { - while (isspace(peek(lexer_info)) || (peek(lexer_info) == '/' && far_peek(lexer_info, 1) == '/') || peek(lexer_info) == '#') { + while (isspace((unsigned char)peek(lexer_info)) || (peek(lexer_info) == '/' && far_peek(lexer_info, 1) == '/') || peek(lexer_info) == '#') + { // Skip Comments if ((peek(lexer_info) == '/' && far_peek(lexer_info, 1) == '/') || peek(lexer_info) == '#') while (!(peek(lexer_info) == '\n' || peek(lexer_info) == '\0')) advance(lexer_info); // Skip whitespace - while (isspace(peek(lexer_info))) advance(lexer_info); + while (isspace((unsigned char)peek(lexer_info))) advance(lexer_info); } } @@ -863,7 +869,7 @@ static LexerResult lexer_next(LexerInfo *lexer_info) { // End of file tokens if (c == '\0') return lexer_result(lexer_info, (Token){.type = TOKEN_EOF}, start, start_line); // Integers and Floats - if (isdigit(c) || (c == '.' && isdigit(far_peek(lexer_info, 1))) || (c == '-' && isdigit(far_peek(lexer_info, 1)))) + if (isdigit(c) || (c == '.' && isdigit((unsigned char)far_peek(lexer_info, 1))) || (c == '-' && isdigit((unsigned char)far_peek(lexer_info, 1)))) return parse_numeric_literal(lexer_info, c, start, start_line); // Character Literals if (c == '\'') { diff --git a/SLS_C/src/pico_main.c b/SLS_C/src/pico_main.c index dcc85f5..aa4d268 100644 --- a/SLS_C/src/pico_main.c +++ b/SLS_C/src/pico_main.c @@ -34,10 +34,10 @@ void print_top_of_stack(InterpreterState *interpreter_state) { printf("#0: ::%s\n", interpreter_state->stack->identifier.name.str); break; case STACK_I64: - printf("#0: %ld:i64\n", interpreter_state->stack->i64); + printf("#0: %lld:i64\n", interpreter_state->stack->i64); break; case STACK_I32: - printf("#0: %d\n", interpreter_state->stack->i32); + printf("#0: %ld\n", interpreter_state->stack->i32); break; case STACK_I16: printf("#0: %d:i16\n", interpreter_state->stack->i16); @@ -46,10 +46,10 @@ void print_top_of_stack(InterpreterState *interpreter_state) { printf("#0: %d:i8\n", interpreter_state->stack->i8); break; case STACK_U64: - printf("#0: %lu:u64\n", interpreter_state->stack->u64); + printf("#0: %llu:u64\n", interpreter_state->stack->u64); break; case STACK_U32: - printf("#0: %u:u32\n", interpreter_state->stack->u32); + printf("#0: %lu:u32\n", interpreter_state->stack->u32); break; case STACK_U16: printf("#0: %u:u16\n", interpreter_state->stack->u16); diff --git a/SLS_C/src/string.c b/SLS_C/src/string.c index f60c16f..668677d 100644 --- a/SLS_C/src/string.c +++ b/SLS_C/src/string.c @@ -203,7 +203,7 @@ SlsStr sls_format(const SlsStr s, ...) { break; case 't': items[i].type = FORMAT_SLS_TOKEN_TYPE; - items[i].token_type = va_arg(args, TokenType); + items[i].token_type = VA_ENUM(args, TokenType); if (items[i].token_type >= TOKEN_TYPE_COUNT) { free(items); return SLS_STR_NULL; @@ -213,7 +213,7 @@ SlsStr sls_format(const SlsStr s, ...) { break; case 'a': items[i].type = FORMAT_SLS_ARRAY_TYPE; - items[i].array_type = va_arg(args, ArrayType); + items[i].array_type = VA_ENUM(args, ArrayType); if (items[i].array_type >= ARRAY_TYPE_COUNT) { free(items); return SLS_STR_NULL; @@ -223,7 +223,7 @@ SlsStr sls_format(const SlsStr s, ...) { break; case 'i': items[i].type = FORMAT_SLS_BUILTIN_INTEGER; - items[i].builtin_integer = va_arg(args, IntegerBuiltInType); + items[i].builtin_integer = VA_ENUM(args, IntegerBuiltInType); if (items[i].builtin_integer >= INTEGER_TYPE_COUNT) { free(items); return SLS_STR_NULL; @@ -239,7 +239,7 @@ SlsStr sls_format(const SlsStr s, ...) { break; case 'b': items[i].type = FORMAT_SLS_BOOLEAN; - items[i].boolean = va_arg(args, Boolean); + items[i].boolean = VA_ENUM(args, Boolean); length += items[i].self_length = (items[i].boolean ? 4 : 5); length -= 2; break; @@ -283,13 +283,13 @@ SlsStr sls_format(const SlsStr s, ...) { snprintf(temp, items[item_i].self_length + 1, "%c", items[item_i].character); break; case FORMAT_INTEGER_32: - snprintf(temp, items[item_i].self_length + 1, "%d", items[item_i].integer_32); + snprintf(temp, items[item_i].self_length + 1, "%ld", items[item_i].integer_32); break; case FORMAT_INTEGER_64: - snprintf(temp, items[item_i].self_length + 1, "%ld", items[item_i].integer_64); + snprintf(temp, items[item_i].self_length + 1, "%lld", items[item_i].integer_64); break; case FORMAT_UNSIGNED_INTEGER_64: - snprintf(temp, items[item_i].self_length + 1, "%lu", items[item_i].unsigned_integer_64); + snprintf(temp, items[item_i].self_length + 1, "%llu", items[item_i].unsigned_integer_64); break; case FORMAT_SIZE_INTEGER: snprintf(temp, items[item_i].self_length + 1, "%zu", items[item_i].size_integer);