diff options
| -rw-r--r-- | .exrc | 44 | ||||
| -rwxr-xr-x | crbcDbg | bin | 0 -> 42808 bytes | |||
| -rw-r--r-- | kat.h | 175 | ||||
| -rw-r--r-- | rbc.c | 59 |
4 files changed, 278 insertions, 0 deletions
@@ -0,0 +1,44 @@ +if exists('g:crbc_compile_loaded') + finish +endif +let g:crbc_compile_loaded = 1 + +let s:cc = 'gcc' +let s:cflags = '-std=c23 -Wall -Wextra -pedantic' +let s:dbgflags = '-g -O0 -fsanitize=address,undefined' +let s:optflags = '-O2' + +function! s:Compile(debug) + if &filetype !=# 'c' && &filetype !=# 'cpp' + echoerr ">>> not a C/++ file!" + return + endif + + let l:src = expand('%') + "let l:out = expand('%:r') + let l:out = 'crbc' + + if a:debug + let l:cmd = printf( + \ '%s %s %s "%s" -o "%sDbg"', + \ s:cc, s:cflags, s:dbgflags, l:src, l:out + \ ) + echo ">>> compiling debug build .." + else + let l:cmd = printf( + \ '%s %s %s "%s" -o "%s"', + \ s:cc, s:cflags, s:optflags, l:src, l:out + \ ) + echo ">>> compiling release build .." + endif + + execute '!' . l:cmd +endfunction + +" user commands +command! Compn call s:Compile(0) +command! Cn call s:Compile(0) + +command! Compd call s:Compile(1) +command! Cd call s:Compile(1) + Binary files differ@@ -0,0 +1,175 @@ +// kat.cfg (kat.h) +// with love by czjstmax <jstmaxlol@disroot.org> +// header version 1 + +#pragma once + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#ifndef KAT +#define KAT + +struct kat_api { + int (*create)(const char*); + int (*add)(const char*, const char*, const char*); + int (*read)(const char*, const char*, char*, size_t); + int (*del)(const char*, const char*); + int (*edit)(const char*, const char*, const char*); + int (*addComment)(const char*, const char*); +}; + +// Creates a file +int katcreate(const char *path) { + FILE *f = fopen(path, "w"); + if (!f) return 1; + fclose(f); + return 0; // success +} + +// Appends a key=val to file +int katadd(const char *key, const char *val, const char *path) { + FILE *f = fopen(path, "a"); + if (f) { + fprintf(f, "%s=%s\n", key, val); + fclose(f); + return 0; + } + return 1; +} + +// Appends a comment to file +int kataddcomment(const char *str, const char *path) { + FILE *f = fopen(path, "a"); + if (f) { + fprintf(f, "#%s\n", str); + fclose(f); + return 0; + } + return 1; +} + +// Reads a key from file +int katreadkey(const char *key, const char *path, char *buffer, size_t bufsize) { + FILE *f = fopen(path, "r"); + if (!f) return 1; + + char line[1024]; + size_t key_len = strlen(key); + + while (fgets(line, sizeof(line), f)) { + // TODO: skip leading whitespace maybe? + // check if line starts with key and '=' right after + if (strncmp(line, key, key_len) == 0 && line[key_len] == '=') { + if (line[0] == '#' || line[0] == ';' || line[0] == '\n') continue; + // copy everything after '=' until newline or buffer full (aka bad) + char *val_start = line + key_len + 1; + size_t len = strcspn(val_start, "\r\n"); // length until newline + if (len >= bufsize) len = bufsize - 1; + strncpy(buffer, val_start, len); + buffer[len] = '\0'; + fclose(f); + return 0; // success + } + } + + fclose(f); + return 1; +} + +// Deletes a key from file +int katdelkey(const char *key, const char *path) { + FILE *f = fopen(path, "r"); + if (!f) return 1; + + char temp_path[1024]; + snprintf(temp_path, sizeof(temp_path), "%s.tmp", path); + FILE *temp = fopen(temp_path, "w"); + if (!temp) { + fclose(f); + return 1; + } + + char line[1024]; + size_t key_len = strlen(key); + int deleted = 0; + + while (fgets(line, sizeof(line), f)) { + // skip line if it matches 'key=' + if (strncmp(line, key, key_len) == 0 && line[key_len] == '=') { + deleted = 1; + continue; + } + fputs(line, temp); + } + + fclose(f); + fclose(temp); + + if (deleted) { + // replace original file with temp + if (remove(path) != 0) return 1; + if (rename(temp_path, path) != 0) return 1; + } else { + remove(temp_path); + } + + return deleted ? 0 : 1; // 0 if deleted, 1 if not found +} + +// Edits a key's value from file +int kateditkey(const char *key, const char *new_val, const char *path) { + FILE *f = fopen(path, "r"); + if (!f) return 1; + + char temp_path[1024]; + snprintf(temp_path, sizeof(temp_path), "%s.tmp", path); + FILE *temp = fopen(temp_path, "w"); + if (!temp) { + fclose(f); + return 1; + } + + char line[1024]; + size_t key_len = strlen(key); + int found = 0; + + while (fgets(line, sizeof(line), f)) { + // skip leading whitespace for accurate matching + char *start = line; + while (*start == ' ' || *start == '\t') start++; + + if (strncmp(start, key, key_len) == 0 && start[key_len] == '=') { + // replace line with new key=val + fprintf(temp, "%s=%s\n", key, new_val); + found = 1; + } else { + fputs(line, temp); + } + } + + if (!found) { + // key not found, add it at the end + fprintf(temp, "%s=%s\n", key, new_val); + } + + fclose(f); + fclose(temp); + + if (remove(path) != 0) return 1; + if (rename(temp_path, path) != 0) return 1; + + return 0; +} + +struct kat_api kat = { + .create = katcreate, + .add = katadd, + .read = katreadkey, + .del = katdelkey, + .edit = kateditkey, + .addComment = kataddcomment, +}; + +#endif @@ -0,0 +1,59 @@ +#ifndef CRBC +#define CRBC +/* CReborn - The first "official" C implementation of the Reborn programming language + * based on the Reborn Standard (reborn-lang.github.io/documents/standard) + * The name CReborn (C/Reborn, CREBORN or whatever) has been chosen because + * this implementation works by using C as its IR, or in much simpler terms, + * this compiler works by compiling Reborn code to C, and then using the + * GNU C Compiler (GNU Compiler Collection, gcc) to compile the output C program + * to an executable. This decision was made to simplify + *================================================================================================* + *#* Development currently lead by: [check README.md] + *================================================================================================* + *#* STYLE GUIDELINES FOR CODE CONTRIBUTORS (AND MAINTAINERS): + * - Use camelCase for variables. + * - Use PascalCase for functions. + * - 4 spaces indentation (not tabs but spaces.) + * - Don't exceed 100 characters on a single line unless you *really* must, it's mostly + * for readability since we think that it is better for code to extend vertically, rather + * than horizontally. (Also given some developers might use 3:2 or other less-wide formats.) + *================================================================================================* + *#* NOTES FOR VIM USERS + * - For Vim users that don't know how to set a limit to the coloumn size, you just: + * :set textwidth=100 + * :set formatoptions+=t + * You can also obviously also set this in your .vimrc. + * + * - For Vim users that want to (a)llow the local .exrc for commodity, you can just do: + * :set exrc secure + * and reopen any file in the repo, then you will be asked to (a)llow, meaning to trust this + * repo's .exrc, after (a)llowing it you will already have the 4 spaces indentation, the 100 + * characters limit (textwidth limit) and the format options set automatically. This only applies + * when editing files from this repo, but if you want them in your global .vimrc or neovim config + * you can also copy them, again, from the local .exrc file. + * + * - Note: If you use vim/nvim and you loaded (allowed) the .exrc from this repo you can also use + * these commands that will automatically compile for either release / normal mode testing or for + * debugging mode. + * + * :Cn / :Compn - Compiles with 'Normal compile' + * :Cd / :Compd - Compiles with 'Debug compile' + * + *#* COMPILE COMMANDS + * Normal compile = 'cc rbc.c -o crbc -O2 -Wall -Wextra -pedantic' + * Debug compile = 'cc rbc.c -o crbc -g -O0 -Wall -Wextra -pedantic -fsanitize=address,undefined' + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +// git@github.com:jstmaxlol/kat +#include "kat.h" + +int main(/*int argc, char **argv*/) { + printf(":> [work-in-progress]\n"); + return 0; +} + +#endif |