worked on pico build without cmake

This commit is contained in:
Kyler Olsen 2025-12-14 23:00:22 -07:00
parent abc2d2e24a
commit dd81a60ba9
8 changed files with 313 additions and 172 deletions

5
SLS_C/.gitignore vendored
View File

@ -1,7 +1,6 @@
obj/ obj/
bin/ bin/
build_pico/ generated/
*.o *.o
*.pdb *.pdb
CMakeLists.txt pico-sdk.txt
pico_arm_gcc_toolchain.cmake

View File

@ -13,17 +13,18 @@ SRC_DIR = Path("src")
TEST_DIR = Path("tests") TEST_DIR = Path("tests")
OBJ_DIR = Path("obj") OBJ_DIR = Path("obj")
BIN_DIR = Path("bin") BIN_DIR = Path("bin")
PICO_GENERATED = Path("generated")
TARGET = BIN_DIR / "sls" TARGET = BIN_DIR / "sls"
TEST_TARGET = BIN_DIR / "sls_tests" TEST_TARGET = BIN_DIR / "sls_tests"
# Platform-specific settings # Platform-specific settings
PICO_SDK_PATH = os.environ.get("PICO_SDK_PATH", Path.home() / "pico/pico-sdk") 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 # Unix gcc/clang flags
COMMON_FLAGS = ["-std=c99", "-Wall", "-Wextra", "-Werror", "-Iinclude", "-g"] 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", TEST_FLAGS = ["-std=c99", "-Wall", "-Wextra", "-Wno-unused-function", "-Werror",
"-Iinclude", "-g", "-O0"] "-Iinclude", "-g", "-O0"]
@ -35,82 +36,244 @@ MACOS_FLAGS = ["-std=c99", "-Wall", "-Wextra", "-Werror", "-Iinclude", "-g",
MSVC_FLAGS = ["/std:c11", "/Zi", "/Iinclude"] MSVC_FLAGS = ["/std:c11", "/Zi", "/Iinclude"]
MSVC_TEST_FLAGS = MSVC_FLAGS + [] MSVC_TEST_FLAGS = MSVC_FLAGS + []
# RP2040 toolchain file template # ---------------------------------------------------------------------
PICO_TOOLCHAIN_TEMPLATE = """set(CMAKE_SYSTEM_NAME Generic) # RP2040 SETTINGS
set(CMAKE_SYSTEM_PROCESSOR cortex-m0plus) # ---------------------------------------------------------------------
PICO_CPU_FLAGS = [
"-mcpu=cortex-m0plus",
"-mthumb",
"-O2",
"-ffunction-sections",
"-fdata-sections"
]
# Specify the cross compiler PICO_DEFINES = [
set(CMAKE_C_COMPILER arm-none-eabi-gcc) "-DPICO_BUILD=1",
set(CMAKE_CXX_COMPILER arm-none-eabi-g++) "-DPICO_ON_DEVICE=1",
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc) ]
# Compiler flags for Cortex-M0+ PICO_ASM_INCLUDES = [
set(CMAKE_C_FLAGS_INIT "-mcpu=cortex-m0plus -mthumb") PICO_GENERATED / "include",
set(CMAKE_CXX_FLAGS_INIT "-mcpu=cortex-m0plus -mthumb") f"{PICO_SDK_PATH}/src/rp2040/boot_stage2/asminclude",
set(CMAKE_ASM_FLAGS_INIT "-mcpu=cortex-m0plus -mthumb") 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 PICO_INCLUDES = [
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) PICO_GENERATED / "include",
# Adjust the default behavior of the FIND_XXX() commands: "src",
# search programs in the host environment "include",
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# Search headers and libraries in the target environment # Pico SDK core
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) f"{PICO_SDK_PATH}/src/common/pico_stdlib_headers/include",
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) f"{PICO_SDK_PATH}/src/common/pico_base_headers/include",
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) 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 hardware + platform
RP2040_CMAKE_TEMPLATE = """cmake_minimum_required(VERSION 3.13) 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 # Runtime + stdio
set(PICO_SDK_PATH "{pico_sdk_path}") f"{PICO_SDK_PATH}/src/rp2_common/pico_platform_compiler/include",
include({pico_sdk_path}/external/pico_sdk_import.cmake) 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) # Hardware libs you use
set(CMAKE_C_STANDARD 11) f"{PICO_SDK_PATH}/src/rp2_common/hardware_gpio/include",
set(CMAKE_CXX_STANDARD 17) 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() PICO_LINKER_SCRIPT = (
Path(PICO_SDK_PATH)
# Main executable / "src"
add_executable({project_name} / "rp2_common"
{source_files} / "pico_crt0"
/ "rp2040"
/ "memmap_default.ld"
) )
# Set output name with .elf extension PICO_LINKER_SCRIPTS_DIR = PICO_GENERATED / "link/pico"
set_target_properties({project_name} PROPERTIES
OUTPUT_NAME "{project_name}.elf"
SUFFIX ""
)
# Add include directories def pico_sdk_asm_sources():
target_include_directories({project_name} PRIVATE sdk = Path(PICO_SDK_PATH)
${{CMAKE_CURRENT_LIST_DIR}}/include
)
# Add compile definitions return [
target_compile_definitions({project_name} PRIVATE sdk / "src/rp2040/boot_stage2/boot2_generic_03h.S",
PICO_BUILD=1 sdk / "src/rp2_common/pico_crt0/crt0.S",
GIT_COMMIT_HASH="{git_hash}" ]
)
# Link libraries def compile_pico_asm_source(src: Path):
target_link_libraries({project_name} mkdir(OBJ_DIR)
pico_stdlib obj = OBJ_DIR / (src.stem + ".pico.o")
hardware_uart
hardware_gpio
)
# Enable USB/UART output if obj.exists() and src.stat().st_mtime < obj.stat().st_mtime:
pico_enable_stdio_usb({project_name} 1) return obj
pico_enable_stdio_uart({project_name} 1)
# Create map/bin/hex/uf2 files include_flags = [f"-I{p}" for p in PICO_ASM_INCLUDES]
pico_add_extra_outputs({project_name})
""" 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 # PLATFORM DETECTION
@ -293,84 +456,57 @@ def check_pico_sdk():
return True 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(): def build_rp2040():
"""Build for RP2040 using CMake"""
if not check_pico_sdk(): if not check_pico_sdk():
return False return False
print("\n=== Building for RP2040 ===\n") print("\n=== Building for RP2040 ===\n")
# Generate toolchain file print(f"PICO_SDK_PATH: {PICO_SDK_PATH}")
generate_pico_toolchain()
generate_pico_headers()
# Generate CMakeLists.txt generate_pico_linker_scripts()
if not generate_pico_cmake():
return False mkdir(OBJ_DIR)
mkdir(BIN_DIR)
# Create build directory
mkdir(PICO_BUILD_DIR) # Your application sources
app_sources = [
# Run CMake with explicit toolchain s for s in SRC_DIR.glob("*.c")
cmake_cmd = [ if s.name not in ["main.c", "repl.c", "file.c"]
"cmake", and "test" not in s.stem.lower()
"-B", str(PICO_BUILD_DIR),
"-S", ".",
f"-DCMAKE_TOOLCHAIN_FILE={PICO_TOOLCHAIN_PATH}"
] ]
run(cmake_cmd)
pico_main = SRC_DIR / "pico_main.c"
# Build if not pico_main.exists():
build_cmd = ["cmake", "--build", str(PICO_BUILD_DIR), "-j4"] print("ERROR: src/pico_main.c is required for Pico builds")
run(build_cmd) return False
# Show output files app_sources.append(pico_main)
uf2_file = PICO_BUILD_DIR / "sls.uf2"
if uf2_file.exists(): # SDK sources
print(f"\nBuild successful! UF2 file: {uf2_file}") sdk_asm_sources = pico_sdk_asm_sources()
print("To flash: Copy this file to your Pico in BOOTSEL mode") 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 return True
@ -418,13 +554,7 @@ def build_tests():
def clean(): def clean():
shutil.rmtree(OBJ_DIR, ignore_errors=True) shutil.rmtree(OBJ_DIR, ignore_errors=True)
shutil.rmtree(BIN_DIR, ignore_errors=True) shutil.rmtree(BIN_DIR, ignore_errors=True)
shutil.rmtree(PICO_BUILD_DIR, ignore_errors=True) shutil.rmtree(PICO_GENERATED, 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()
print("Cleaned.") print("Cleaned.")

View File

@ -12,6 +12,8 @@
#include "bool.h" #include "bool.h"
#define VA_ENUM(args, type) ((type)va_arg(args, int))
typedef struct { typedef struct {
size_t len; // Number of useable characters (does not include trailing null character) size_t len; // Number of useable characters (does not include trailing null character)
char *str; char *str;

View File

@ -1130,7 +1130,7 @@ Boolean builtin_abs(InterpreterState *interpreter_state) {
.type = TOKEN_INTEGER, .type = TOKEN_INTEGER,
.integer_literal = (IntegerLiteral){ .integer_literal = (IntegerLiteral){
.type = INTEGER_I64, .type = INTEGER_I64,
.value = labs((int64_t)ai), .value = llabs((int64_t)ai),
}, },
}); });
} else if (type & NUMERIC_SIGNED && type & NUMERIC_32) { } else if (type & NUMERIC_SIGNED && type & NUMERIC_32) {
@ -1138,7 +1138,7 @@ Boolean builtin_abs(InterpreterState *interpreter_state) {
.type = TOKEN_INTEGER, .type = TOKEN_INTEGER,
.integer_literal = (IntegerLiteral){ .integer_literal = (IntegerLiteral){
.type = INTEGER_I32, .type = INTEGER_I32,
.value = labs((int64_t)ai), .value = llabs((int64_t)ai),
}, },
}); });
} else if (type & NUMERIC_SIGNED && type & NUMERIC_16) { } else if (type & NUMERIC_SIGNED && type & NUMERIC_16) {
@ -1146,7 +1146,7 @@ Boolean builtin_abs(InterpreterState *interpreter_state) {
.type = TOKEN_INTEGER, .type = TOKEN_INTEGER,
.integer_literal = (IntegerLiteral){ .integer_literal = (IntegerLiteral){
.type = INTEGER_I16, .type = INTEGER_I16,
.value = labs((int64_t)ai), .value = llabs((int64_t)ai),
}, },
}); });
} else { } else {
@ -1154,7 +1154,7 @@ Boolean builtin_abs(InterpreterState *interpreter_state) {
.type = TOKEN_INTEGER, .type = TOKEN_INTEGER,
.integer_literal = (IntegerLiteral){ .integer_literal = (IntegerLiteral){
.type = INTEGER_I8, .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; interpreter_state->stack = start_item->next;
start_item->next = NULL; start_item->next = NULL;
size_t start_value; size_t start_value = 0;
SCALAR(start_item, start_value); SCALAR(start_item, start_value);
size_t end_value; size_t end_value = 0;
SCALAR(end_item, end_value); SCALAR(end_item, end_value);
Boolean return_value = FALSE; Boolean return_value = FALSE;
@ -1905,10 +1905,10 @@ Boolean builtin_roll(InterpreterState *interpreter_state) {
StackItem *times_item = interpreter_state->stack; StackItem *times_item = interpreter_state->stack;
StackItem *count_item = times_item->next; StackItem *count_item = times_item->next;
size_t times; size_t times = 0;
SCALAR(times_item, times); SCALAR(times_item, times);
size_t count; size_t count = 0;
SCALAR(count_item, count); SCALAR(count_item, count);
StackItem *head = NULL, *tail = NULL, *prev = NULL; StackItem *head = NULL, *tail = NULL, *prev = NULL;

View File

@ -93,6 +93,8 @@ Boolean push_token(InterpreterState *interpreter_state, Token token) {
case INTEGER_U8: case INTEGER_U8:
type = STACK_U8; type = STACK_U8;
break; break;
default:
return FALSE;
} }
break; break;
case TOKEN_FLOAT: case TOKEN_FLOAT:
@ -116,6 +118,8 @@ Boolean push_token(InterpreterState *interpreter_state, Token token) {
break; break;
case TOKEN_TYPE_TUPLE: case TOKEN_TYPE_TUPLE:
return FALSE; return FALSE;
default:
return FALSE;
} }
StackItem *item = (StackItem *)malloc(sizeof(StackItem)); StackItem *item = (StackItem *)malloc(sizeof(StackItem));

View File

@ -220,7 +220,8 @@ static uint64_t create_binary_integer(LexerInfo *lexer_info, size_t start) {
i += 1; i += 1;
} }
for (; i < lexer_info->pos - start; i++) { 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; if (token[i] == '.' || token[i] == '_') continue;
value *= 2; value *= 2;
switch (token[i]) { switch (token[i]) {
@ -241,7 +242,8 @@ static uint64_t create_octal_integer(LexerInfo *lexer_info, size_t start) {
i += 1; i += 1;
} }
for (; i < lexer_info->pos - start; i++) { 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; if (token[i] == '.' || token[i] == '_') continue;
value *= 8; value *= 8;
switch (token[i]) { switch (token[i]) {
@ -268,7 +270,8 @@ static uint64_t create_decimal_integer(LexerInfo *lexer_info, size_t start) {
i += 1; i += 1;
} }
for (; i < lexer_info->pos - start; i++) { 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] == '_') continue;
value *= 10; value *= 10;
switch (token[i]) { switch (token[i]) {
@ -297,7 +300,8 @@ static uint64_t create_hexadecimal_integer(LexerInfo *lexer_info, size_t start)
i += 1; i += 1;
} }
for (; i < lexer_info->pos - start; i++) { 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; if (token[i] == '.' || token[i] == '_') continue;
value *= 16; value *= 16;
switch (token[i]) { switch (token[i]) {
@ -387,7 +391,8 @@ static double create_float(LexerInfo *lexer_info, size_t start) {
i += 1; i += 1;
} }
for (; i < lexer_info->pos - start; i++) { 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] == '_') continue;
if (token[i] == '.') { if (token[i] == '.') {
fractional = 1; 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) { 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 // Skip Comments
if ((peek(lexer_info) == '/' && far_peek(lexer_info, 1) == '/') || peek(lexer_info) == '#') if ((peek(lexer_info) == '/' && far_peek(lexer_info, 1) == '/') || peek(lexer_info) == '#')
while (!(peek(lexer_info) == '\n' || peek(lexer_info) == '\0')) while (!(peek(lexer_info) == '\n' || peek(lexer_info) == '\0'))
advance(lexer_info); advance(lexer_info);
// Skip whitespace // 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 // End of file tokens
if (c == '\0') return lexer_result(lexer_info, (Token){.type = TOKEN_EOF}, start, start_line); if (c == '\0') return lexer_result(lexer_info, (Token){.type = TOKEN_EOF}, start, start_line);
// Integers and Floats // 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); return parse_numeric_literal(lexer_info, c, start, start_line);
// Character Literals // Character Literals
if (c == '\'') { if (c == '\'') {

View File

@ -34,10 +34,10 @@ void print_top_of_stack(InterpreterState *interpreter_state) {
printf("#0: ::%s\n", interpreter_state->stack->identifier.name.str); printf("#0: ::%s\n", interpreter_state->stack->identifier.name.str);
break; break;
case STACK_I64: case STACK_I64:
printf("#0: %ld:i64\n", interpreter_state->stack->i64); printf("#0: %lld:i64\n", interpreter_state->stack->i64);
break; break;
case STACK_I32: case STACK_I32:
printf("#0: %d\n", interpreter_state->stack->i32); printf("#0: %ld\n", interpreter_state->stack->i32);
break; break;
case STACK_I16: case STACK_I16:
printf("#0: %d:i16\n", interpreter_state->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); printf("#0: %d:i8\n", interpreter_state->stack->i8);
break; break;
case STACK_U64: case STACK_U64:
printf("#0: %lu:u64\n", interpreter_state->stack->u64); printf("#0: %llu:u64\n", interpreter_state->stack->u64);
break; break;
case STACK_U32: case STACK_U32:
printf("#0: %u:u32\n", interpreter_state->stack->u32); printf("#0: %lu:u32\n", interpreter_state->stack->u32);
break; break;
case STACK_U16: case STACK_U16:
printf("#0: %u:u16\n", interpreter_state->stack->u16); printf("#0: %u:u16\n", interpreter_state->stack->u16);

View File

@ -203,7 +203,7 @@ SlsStr sls_format(const SlsStr s, ...) {
break; break;
case 't': case 't':
items[i].type = FORMAT_SLS_TOKEN_TYPE; 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) { if (items[i].token_type >= TOKEN_TYPE_COUNT) {
free(items); free(items);
return SLS_STR_NULL; return SLS_STR_NULL;
@ -213,7 +213,7 @@ SlsStr sls_format(const SlsStr s, ...) {
break; break;
case 'a': case 'a':
items[i].type = FORMAT_SLS_ARRAY_TYPE; 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) { if (items[i].array_type >= ARRAY_TYPE_COUNT) {
free(items); free(items);
return SLS_STR_NULL; return SLS_STR_NULL;
@ -223,7 +223,7 @@ SlsStr sls_format(const SlsStr s, ...) {
break; break;
case 'i': case 'i':
items[i].type = FORMAT_SLS_BUILTIN_INTEGER; 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) { if (items[i].builtin_integer >= INTEGER_TYPE_COUNT) {
free(items); free(items);
return SLS_STR_NULL; return SLS_STR_NULL;
@ -239,7 +239,7 @@ SlsStr sls_format(const SlsStr s, ...) {
break; break;
case 'b': case 'b':
items[i].type = FORMAT_SLS_BOOLEAN; 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 += items[i].self_length = (items[i].boolean ? 4 : 5);
length -= 2; length -= 2;
break; break;
@ -283,13 +283,13 @@ SlsStr sls_format(const SlsStr s, ...) {
snprintf(temp, items[item_i].self_length + 1, "%c", items[item_i].character); snprintf(temp, items[item_i].self_length + 1, "%c", items[item_i].character);
break; break;
case FORMAT_INTEGER_32: 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; break;
case FORMAT_INTEGER_64: 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; break;
case FORMAT_UNSIGNED_INTEGER_64: 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; break;
case FORMAT_SIZE_INTEGER: case FORMAT_SIZE_INTEGER:
snprintf(temp, items[item_i].self_length + 1, "%zu", items[item_i].size_integer); snprintf(temp, items[item_i].self_length + 1, "%zu", items[item_i].size_integer);