all repos — hex @ 153649ed7756d94780deec2282e5ec74414cdc6b

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

Implemented vim syntax file, support for block comments, and hashbang.
h3rald h3rald@h3rald.com
Tue, 10 Dec 2024 09:25:05 +0100
commit

153649ed7756d94780deec2282e5ec74414cdc6b

parent

2105a2cb859f84ad5bc6637df571f9fb15d1b6ce

3 files changed, 112 insertions(+), 0 deletions(-)

jump to
A hex.vim

@@ -0,0 +1,64 @@

+" Vim syntax file +" Language: hex +" Maintainer: Fabio Cevasco +" Last Change: 2024-12-10 +" Version: 0.1.0 + +if exists("b:current_syntax") + finish +endif + +syntax keyword hexNativeSymbol if when while error try dup pop swap stack clear and or not xor int str hex dec type +syntax keyword hexNativeSymbol cat slice len get insert index join split replace each map filter puts warn print gets +syntax keyword hexNativeSymbol read write append args exit exec run +syntax match hexNativeSymbol /\v\!/ +syntax match hexNativeSymbol /\v\!\=/ +syntax match hexNativeSymbol /\v\%/ +syntax match hexNativeSymbol /\v\&\&/ +syntax match hexNativeSymbol /\v\./ +syntax match hexNativeSymbol /\v\'/ +syntax match hexNativeSymbol /\v\+/ +syntax match hexNativeSymbol /\v\-/ +syntax match hexNativeSymbol /\v\*/ +syntax match hexNativeSymbol /\v\// +syntax match hexNativeSymbol /\v\:/ +syntax match hexNativeSymbol /\v\</ +syntax match hexNativeSymbol /\v\>/ +syntax match hexNativeSymbol /\v\<\=/ +syntax match hexNativeSymbol /\v\=\=/ +syntax match hexNativeSymbol /\v\>\=/ +syntax match hexNativeSymbol /\v\>\>/ +syntax match hexNativeSymbol /\v\<\</ +syntax match hexNativeSymbol /\v\^/ +syntax match hexNativeSymbol /\v\~/ +syntax match hexNativeSymbol /\v\|\|/ +syntax match hexNativeSymbol /\v\|/ +syntax match hexNativeSymbol /\v\#/ +syntax match hexNativeSymbol /\v\&/ + +syntax match hexUserSymbol ;[a-zA-Z_][a-zA-Z0-9_-]*; + +syntax keyword hexCommentTodo TODO FIXME XXX TBD contained +syntax match hexComment /;.*$/ contains=hexCommentTodo +syntax region hexComment start=;#|; end=;|#; contains=hexCommentTodo + +syntax match hexNumber ;0x[0-9a-f]\{1,8\}; +syntax region hexString start=+"+ skip=+\\\\\|\\$"+ end=+"+ + + +syntax match hexParen ;(\|); + +syntax match hexSharpBang /\%^#!.*/ + + +" Highlighting +hi default link hexComment Comment +hi default link hexCommentTodo Todo +hi default link hexString String +hi default link hexNumber Number +hi default link hexNativeSymbol Statement +hi default link hexUserSymbol Identifier +hi default link hexParen Special +hi default link hexSharpBang Preproc + +let b:current_syntax = "hex"
M scripts/test.hexscripts/test.hex

@@ -1,3 +1,5 @@

+#!/usr/bin/env hex + 0x0 "test-count" : 0x0 "successes" : 0x0 "failures" :
M src/hex.csrc/hex.c

@@ -579,6 +579,41 @@ strncpy(token->value, start, len);

token->value[len] = '\0'; token->type = HEX_TOKEN_COMMENT; } + else if (strncmp(ptr, "#|", 2) == 0) + { + // Block comment token + const char *start = ptr; + ptr += 2; // Skip the "#|" prefix + position->column += 2; + while (*ptr != '\0' && strncmp(ptr, "|#", 2) != 0) + { + if (*ptr == '\n') + { + position->line++; + position->column = 1; + } + else + { + position->column++; + } + ptr++; + } + if (*ptr == '\0') + { + token->type = HEX_TOKEN_INVALID; + token->position.line = position->line; + token->position.column = position->column; + hex_error(ctx, "(%d,%d) Unterminated block comment", position->line, position->column); + return token; + } + ptr += 2; // Skip the "|#" suffix + position->column += 2; + int len = ptr - start; + token->value = (char *)malloc(len + 1); + strncpy(token->value, start, len); + token->value[len] = '\0'; + token->type = HEX_TOKEN_COMMENT; + } else if (*ptr == '"') { // String token

@@ -3954,6 +3989,17 @@ }

int bytesReadTotal = 0; int bytesRead = 0; + + // Handle hashbang if present + char hashbangLine[1024]; + if (fgets(hashbangLine, sizeof(hashbangLine), file) != NULL) + { + if (strncmp(hashbangLine, "#!", 2) != 0) + { + // Not a hashbang line, reset file pointer to the beginning + fseek(file, 0, SEEK_SET); + } + } while ((bytesRead = fread(content + bytesReadTotal, 1, bufferSize - bytesReadTotal, file)) > 0) {