Initial commit

This commit is contained in:
soup 2024-12-14 16:50:56 -05:00
commit 18103df342
Signed by: soup
SSH key fingerprint: SHA256:GYxje8eQkJ6HZKzVWDdyOUF1TyDiprruGhE0Ym8qYDY
9 changed files with 203 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.cache/
build/

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "deps/tree-sitter-c"]
path = deps/tree-sitter-c
url = https://github.com/tree-sitter/tree-sitter-c

16
Makefile Normal file
View file

@ -0,0 +1,16 @@
CC := gcc -g -Og -std=c23 -Wall -Wextra -Wpedantic -Isrc -ltree-sitter
build/bin/cgen: src/main.c | build/.build-dirs
$(CC) deps/tree-sitter-c/src/parser.c $^ -o build/bin/cgen
.PHONY: run
run: build/bin/cgen
build/bin/cgen
build/.build-dirs: Makefile
mkdir -p build/bin
touch build/.build-dirs
.PHONY: clean
clean:
rm -rf build

21
compile_commands.json Normal file
View file

@ -0,0 +1,21 @@
[
{
"arguments": [
"/nix/store/1ciadcliylyyi9vx21id9vi1p1mayjn8-gcc-wrapper-14.2.0/bin/gcc",
"-c",
"-g",
"-Og",
"-std=c23",
"-Wall",
"-Wextra",
"-Wpedantic",
"-Isrc",
"-o",
"build/bin/cgen",
"src/main.c"
],
"directory": "/home/n/src/cgen",
"file": "/home/n/src/cgen/src/main.c",
"output": "/home/n/src/cgen/build/bin/cgen"
}
]

1
deps/tree-sitter-c vendored Submodule

@ -0,0 +1 @@
Subproject commit e8841a6a9431b7365ac9055688429e1deb8db90f

58
flake.lock Normal file
View file

@ -0,0 +1,58 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1733550349,
"narHash": "sha256-NcGumB4Lr6KSDq+nIqXtNA8QwAQKDSZT7N9OTGWbTrs=",
"path": "/nix/store/sqmn1ky3k66661h32djyjvsr8l99330z-source",
"rev": "e2605d0744c2417b09f8bf850dfca42fcf537d34",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

24
flake.nix Normal file
View file

@ -0,0 +1,24 @@
{
inputs.nixpkgs.url = "nixpkgs";
inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = inputs:
(inputs.flake-utils.lib.eachDefaultSystem (system:
let
pkgs = inputs.nixpkgs.legacyPackages.${system};
in {
devShells.default =
pkgs.mkShell {
packages = with pkgs; [
gdb
gcc15
valgrind
strace
llvmPackages_17.clang-tools
bear
tree-sitter
];
};
}
));
}

5
src/gen_enum.tsq Normal file
View file

@ -0,0 +1,5 @@
(enum_specifier
name: (type_identifier)? @enum_name
body: (enumerator_list
(enumerator
name: (identifier) @enum_variant)?))

73
src/main.c Normal file
View file

@ -0,0 +1,73 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tree_sitter/api.h>
TSLanguage const *tree_sitter_c(void);
char const *USAGE = "\
cgen <mode>\
";
size_t read_stdin(char *buf, size_t buf_cap) {
char *at = buf;
char *end = buf + buf_cap;
for (; at < end; ++at) {
if ((*at = fgetc(stdin)) == EOF) {
break;
}
}
if (at < end) { *at = '\0'; }
return at - buf;
}
char const gen_enum_query_str[] = {
#embed "./gen_enum.tsq", '\0'
};
void gen_enum_strs() {
size_t buf_cap = 2 << 16;
char *buf = calloc(buf_cap, sizeof(*buf));
size_t buf_len = read_stdin(buf, buf_cap);
TSParser *parser = ts_parser_new();
TSLanguage const *lang_c = tree_sitter_c();
ts_parser_set_language(parser, lang_c);
TSTree *tree = ts_parser_parse_string(parser, nullptr, buf, buf_len);
TSNode root_node = ts_tree_root_node(tree);
char *string = ts_node_string(root_node);
printf("%s\n", string);
uint32_t err_offset = 0;
TSQueryError err = {};
TSQuery *query = ts_query_new(
lang_c,
gen_enum_query_str,
strlen(gen_enum_query_str),
&err_offset,
&err
);
if (err != TSQueryErrorNone) {
printf("query error %d near: %s\n", err, gen_enum_query_str + err_offset);
assert(false);
}
}
int main(int argc, char **argv) {
if (argc != 2) {
printf("%s", USAGE);
exit(1);
}
char const *mode = argv[1];
if (!strcmp(mode, "enum-strs")) {
gen_enum_strs();
}
return 0;
}