diff --git a/devshell/default.nix b/devshell/default.nix index 46cb4f8..fd07a4f 100644 --- a/devshell/default.nix +++ b/devshell/default.nix @@ -1,24 +1,33 @@ -{ +{ system, pkgs, self -}: pkgs.mkShell { - buildInputs = - (with self.packages.${system}; [ - nvim-alias - #prettify-log - nvim-pager - ]) - ++ (with pkgs; [ - git - jq - yq-go - curl - (writeScriptBin "hemar-check" '' - ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null vm-postgres 'zsh -c check' - '') - ]); - - # environment - PAGER = "${self.packages.${system}.nvim-pager}/bin/pager"; +}: +(import ./dev { inherit self system pkgs; }) +// { + c = import ./c.nix { inherit self system pkgs; }; + postgres-c = import ./postgres-c.nix { inherit self system pkgs; }; + pure-c = import ./pure-c.nix { inherit self system pkgs; }; + rust = import ./rust.nix { inherit self system pkgs; }; + haskell = import ./haskell.nix { inherit self system pkgs; }; + default = pkgs.mkShell { + buildInputs = + (with self.packages.${system}; [ + nvim-alias + #prettify-log + nvim-pager + ]) + ++ (with pkgs; [ + git + jq + yq-go + curl + #(writeScriptBin "hemar-check" '' + # ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null vm-postgres 'zsh -c check' + #'') + ]); + + # environment + PAGER = "${self.packages.${system}.nvim-pager}/bin/pager"; + }; } diff --git a/devshell/dev/default.nix b/devshell/dev/default.nix new file mode 100644 index 0000000..bd9c4ba --- /dev/null +++ b/devshell/dev/default.nix @@ -0,0 +1,7 @@ +{ + system, + self, + pkgs +}: { + hemar = import ./hemar.nix { inherit self system pkgs; }; +} diff --git a/devshell/dev/hemar.nix b/devshell/dev/hemar.nix new file mode 100644 index 0000000..f4075bb --- /dev/null +++ b/devshell/dev/hemar.nix @@ -0,0 +1,5 @@ +{ pkgs, ... }: pkgs.mkShell { + buildInputs = (with pkgs; [ + dash + ]); +} diff --git a/flake.nix b/flake.nix index 856d997..26799c7 100644 --- a/flake.nix +++ b/flake.nix @@ -40,36 +40,27 @@ rust-overlay, ... }@inputs: let - flake = ./.; - nixpkgs = nixpkgs-25-05; + flake = ./.; + nixpkgs = nixpkgs-25-05; overlays = [ self.overlays.default ]; self-lib = import ./lib { inherit flake self inputs; }; in self-lib.forAllSystemsWithPkgs ([(import rust-overlay)] ++ overlays) ({ system, pkgs, }: { - packages.${system} = import ./package { inherit system pkgs self; }; - - legacyPackages.${system} = import nixpkgs { inherit system overlays; }; - - devShells.${system} = { - c = import ./devshell/c.nix { inherit self system pkgs; }; - postgres-c = import ./devshell/postgres-c.nix { inherit self system pkgs; }; - pure-c = import ./devshell/pure-c.nix { inherit self system pkgs; }; - default = import ./devshell/default.nix { inherit self system pkgs; }; - rust = import ./devshell/rust.nix { inherit self system pkgs; }; - haskell = import ./devshell/haskell.nix { inherit self system pkgs; }; - }; + packages.${system} = import ./package { inherit system pkgs self; }; + legacyPackages.${system} = import nixpkgs { inherit system overlays; }; + devShells.${system} = import ./devshell { inherit self system pkgs; }; nixosConfigurations = { "devvm-manual|${system}" = import ./nixos/system/devvm-manual/default.nix { inherit flake self inputs system; }; - "devvm-hemar|${system}" = import ./nixos/system/devvm-hemar/default.nix + "devvm-hemar|${system}" = import ./nixos/system/devvm-hemar/default.nix { inherit flake self inputs system; }; }; }) // { lib = self-lib; - overlays.default = import ./overlay { inherit flake self inputs nixpkgs; }; - nixosModules = import ./nixos/module { inherit flake self inputs nixpkgs; }; - templates = import ./template { inherit flake self inputs nixpkgs; }; + overlays.default = import ./overlay { inherit flake self inputs nixpkgs; }; + nixosModules = import ./nixos/module { inherit flake self inputs nixpkgs; }; + templates = import ./template { inherit flake self inputs nixpkgs; }; }; } diff --git a/package/c/hemar/.gitignore b/package/c/hemar-legacy/.gitignore similarity index 100% rename from package/c/hemar/.gitignore rename to package/c/hemar-legacy/.gitignore diff --git a/package/c/hemar/Makefile b/package/c/hemar-legacy/Makefile similarity index 100% rename from package/c/hemar/Makefile rename to package/c/hemar-legacy/Makefile diff --git a/package/c/hemar/default.nix b/package/c/hemar-legacy/default.nix similarity index 100% rename from package/c/hemar/default.nix rename to package/c/hemar-legacy/default.nix diff --git a/package/c/hemar/hemar--0.1.sql b/package/c/hemar-legacy/hemar--0.1.sql similarity index 100% rename from package/c/hemar/hemar--0.1.sql rename to package/c/hemar-legacy/hemar--0.1.sql diff --git a/package/c/hemar/hemar.c b/package/c/hemar-legacy/hemar.c similarity index 100% rename from package/c/hemar/hemar.c rename to package/c/hemar-legacy/hemar.c diff --git a/package/c/hemar/hemar.control b/package/c/hemar-legacy/hemar.control similarity index 100% rename from package/c/hemar/hemar.control rename to package/c/hemar-legacy/hemar.control diff --git a/package/c/hemar/hemar.h b/package/c/hemar-legacy/hemar.h similarity index 100% rename from package/c/hemar/hemar.h rename to package/c/hemar-legacy/hemar.h diff --git a/package/c/hemar/make.sh b/package/c/hemar-legacy/make.sh similarity index 100% rename from package/c/hemar/make.sh rename to package/c/hemar-legacy/make.sh diff --git a/package/c/hemar/postgreact--1.0.sql b/package/c/hemar-legacy/postgreact--1.0.sql similarity index 100% rename from package/c/hemar/postgreact--1.0.sql rename to package/c/hemar-legacy/postgreact--1.0.sql diff --git a/package/c/hemar/postgreact.control.in b/package/c/hemar-legacy/postgreact.control.in similarity index 100% rename from package/c/hemar/postgreact.control.in rename to package/c/hemar-legacy/postgreact.control.in diff --git a/package/c/hemar/postgreact.h b/package/c/hemar-legacy/postgreact.h similarity index 100% rename from package/c/hemar/postgreact.h rename to package/c/hemar-legacy/postgreact.h diff --git a/package/c/hemar/test/mod.sql b/package/c/hemar-legacy/test/mod.sql similarity index 100% rename from package/c/hemar/test/mod.sql rename to package/c/hemar-legacy/test/mod.sql diff --git a/package/c/hemar/test/test_jsonb_path.sql b/package/c/hemar-legacy/test/test_jsonb_path.sql similarity index 100% rename from package/c/hemar/test/test_jsonb_path.sql rename to package/c/hemar-legacy/test/test_jsonb_path.sql diff --git a/package/c/hemar/test/test_render_all.sql b/package/c/hemar-legacy/test/test_render_all.sql similarity index 100% rename from package/c/hemar/test/test_render_all.sql rename to package/c/hemar-legacy/test/test_render_all.sql diff --git a/package/c/hemar/test/test_render_exec.sql b/package/c/hemar-legacy/test/test_render_exec.sql similarity index 100% rename from package/c/hemar/test/test_render_exec.sql rename to package/c/hemar-legacy/test/test_render_exec.sql diff --git a/package/c/hemar/test/test_render_include.sql b/package/c/hemar-legacy/test/test_render_include.sql similarity index 100% rename from package/c/hemar/test/test_render_include.sql rename to package/c/hemar-legacy/test/test_render_include.sql diff --git a/package/c/hemar/test/test_render_interpolate.sql b/package/c/hemar-legacy/test/test_render_interpolate.sql similarity index 100% rename from package/c/hemar/test/test_render_interpolate.sql rename to package/c/hemar-legacy/test/test_render_interpolate.sql diff --git a/package/c/hemar/test/test_render_section.sql b/package/c/hemar-legacy/test/test_render_section.sql similarity index 100% rename from package/c/hemar/test/test_render_section.sql rename to package/c/hemar-legacy/test/test_render_section.sql diff --git a/package/c/hemar/test/test_template_parser.sql b/package/c/hemar-legacy/test/test_template_parser.sql similarity index 100% rename from package/c/hemar/test/test_template_parser.sql rename to package/c/hemar-legacy/test/test_template_parser.sql diff --git a/package/hemar/hemar.sh b/package/hemar/hemar.sh new file mode 100644 index 0000000..1d5b827 --- /dev/null +++ b/package/hemar/hemar.sh @@ -0,0 +1,130 @@ +#!/bin/dash + +# 0 - text +# 1 - deside tag type +# 2 - interpolation +# 3 - section +# 4 - include +# 5 - compute +STAGE=0 +STAGE_BUFFER="$(mktemp)" +open_tag_flag=0 + +# data structure :) + +# Type = 0..=5 +# +# Text = string # just a text body +# +# Interpolation = string # path to variable +# +# Include = string # path to include data +# +# Section = { +# v = string # item variable name for loop +# p = string # path to array for iteration +# b = [Element] # section body +# +# } +# +# End = null +# +# Compute = { +# l = string # programing language +# b = string # function body +# } +# +# Element = { +# t = Type # element type +# b = Text # element body +# | Interpolation +# | Section +# | End +# | Include +# | Compute +# } +# +# Hemar = { +# e = [Element] # elements array +#} +# finds close pattern and store the char to the STAGE_BUFFER +find_close_pattern() { + char="${1:?}" + if [ "${close_tag_flag:?}" -eq 0 ] && [ "$char" = ']' ]; then + close_tag_flag=1 + elif [ "${close_tag_flag:?}" -eq 1 ] && [ "$char" = '}' ]; then + close_tag_flag=0 + + # removes first and last white spaces from the buffer + sed -i 's/[[:space:]]$//g' "$STAGE_BUFFER" + sed -i 's/^[[:space:]]//g' "$STAGE_BUFFER" + + # removes last char from buffer (]) is part of close pattern + truncate -s -1 "$STAGE_BUFFER" + return 0 + else + printf '%s' "$char" >> "$STAGE_BUFFER" + fi + + return 1 +} + +# finds open pattern and stores the char to the STAGE_BUFFER +find_open_pattern() { + char="${1:?}" + if [ "${open_tag_flag:?}" -eq 0 ] && [ "$char" = '{' ]; then + open_tag_flag=1 + elif [ "${open_tag_flag:?}" -eq 1 ] && [ "$char" = '[' ]; then + open_tag_flag=0 + + # removes last char from buffer ({) is part of open pattern + truncate -s -1 "$STAGE_BUFFER" + return 0 + else + printf '%s' "$char" >> "$STAGE_BUFFER" + fi + + return 1 +} + +parse() { + char="$1" + + case "$STAGE" in + 0) + if find_open_pattern "$char"; then + plex_set "$data_pointer"'' + STAGE=1 + fi + ;; + 1) + if find_close_pattern "$char"; then + STAGE=0 + fi + ;; + 2) + + ;; + 3) + + ;; + 4) + + ;; + *) + + ;; + esac +} + +# Using dd to read one character at a time +input=$(cat) +i=1 +while :; do + char=$(printf '%s' "$input" | dd bs=1 skip=$((i-1)) count=1 2>/dev/null) + [ -z "$char" ] && break + + parse "$char" + + i=$((i+1)) +done diff --git a/package/hemar/plex.sh b/package/hemar/plex.sh new file mode 100644 index 0000000..0ec3b74 --- /dev/null +++ b/package/hemar/plex.sh @@ -0,0 +1,77 @@ +#!/bin/dash + +#data_size="$(eval printf '%s' "\$$structname" | wc -c)" +#if [ "$data_size" -ge "$(getconf ARG_MAX)" ]; then +# # TODO: handle large text +# echo "Data too large for an environment variable" +# exit 1 +#fi + +plex_set() { + local structname key val regex base esc_key regex esc temp + structname=$1 key=$2 val=$3 + + # construct regex for ancestors + regex="^$key=" + + base=$key + while expr "$base" : '.*\.' >/dev/null; do + base=$(printf '%s\n' "$base" | sed 's/\.[^.]*$//') + esc=$(printf '%s\n' "$base" | sed 's/\./\\./g') + regex="$regex|^$esc=" + done + + # add descendants + esc_key="$(printf '%s\n' "$key" | sed 's/\./\\./g')" + regex="$regex|^${esc_key}\." + + # remove old + # =$(printf '%s\n' "$" | grep -v -E "$regex") + temp="$(eval "printf '%s\\n' \"\$$structname\"" | grep -v -E "$regex")" + eval "$structname=\"\$temp\"" + + # add new + eval "$structname=\$(printf '%s\\n%s=%s\\n' \"\$$structname\" \"\$key\" \"\$val\")" +} + +plex_child() { + local structname prefix + structname=$1 prefix=$2 + + eval printf '%s\\n' \"\$"$structname"\" \ + | grep "^$prefix\." \ + | sed "s|^$prefix\.||" +} + +plex_val() { + local structname key + structname=$1 key=$2 + eval printf '%s\n' \"\$"$structname"\" | grep "^$key=" | cut -d= -f2- +} + +plex_fetch() { + local structname key + structname=$1 key=$2 + if eval printf '%s\\n' \"\$"$structname"\" | grep -q "^$key="; then + eval printf '%s\\n' \"\$"$structname"\" | grep "^$key=" | cut -d= -f2- + else + eval printf '%s\\n' \"\$"$structname"\" | grep "^$key\." | sed "s|^$key\.||" + fi +} + +plex_push() { + local structname prefix val max idx newidx kv + structname=${1:?} + prefix=${2:?} + val=${3:?} + + # find max index + max=0 + for kv in $(plex_fetch "$structname" "$prefix"); do + idx=${kv%%=*} + [ "$idx" -gt "$max" ] 2>/dev/null && max=$idx + done + + newidx=$((max + 1)) + plex_set "$structname" "$prefix.$newidx" "$val" +} diff --git a/package/hemar/test.sh b/package/hemar/test.sh new file mode 100644 index 0000000..de5a587 --- /dev/null +++ b/package/hemar/test.sh @@ -0,0 +1,6 @@ +#!/bin/dash + +WORKSPACE="$(cd "$(dirname "$0")" && pwd)" + +#. "${WORKSPACE:?}/test/plex.sh" +. "${WORKSPACE:?}/test/time.sh" diff --git a/package/hemar/test/plex/time.sh b/package/hemar/test/plex/time.sh new file mode 100644 index 0000000..a9383ff --- /dev/null +++ b/package/hemar/test/plex/time.sh @@ -0,0 +1,111 @@ +#!/bin/dash + +. "${WORKSPACE:?}/plex.sh" + +MY_STRUCT='' + +math() { + awk "BEGIN {print $1}" +} + +elapsed() { + local task time count decrease avg + task=$1 + time=$2 + count=$3 + decrease=${4:-0} + avg=$(math "$time/$count-$decrease") + + printf '\n[%s]\nelapsed %s seconds\n%s per second\n' \ + "$task" "$avg" "$(math "1/$avg")" >&2 + printf '%s' "$avg" +} + +set_word_length() { + local length + length=${1:?} + + # shellcheck disable=SC2183 + __WORD_OFFSET_PATERN="$(printf '%*s' "$length" | tr ' ' '?')" +} + +UNIQ_8_WORDS_COUNT=1000 +DEFAULT_WORD_LENGTH=8 +set_word_length "$DEFAULT_WORD_LENGTH" + +randomword() { + local length + length=${1:-$DEFAULT_WORD_LENGTH} + LC_ALL=C tr -dc 'A-Za-z0-9' /dev/null +} + +DEFAULT_TRIES=1000 +ACCURATE_TRIES=10000 +SUPPER_ACCURATE_TRIES=100000 + +WORD_CREATE_ACCURACY="$ACCURATE_TRIES" +BENCH_ACCURACY="$DEFAULT_TRIES" + +count="$WORD_CREATE_ACCURACY" +set_word_length 8 +i=0 +start=$(date +%s) +while [ "$i" -lt "$count" ]; do + new_word >/dev/null + i=$((i + 1)) +done +end=$(date +%s) +wordtime=$(elapsed 'Word creation' "$((end - start))" "$count") + +bench_set 'Set element with depth 1 length 8' 1 "$BENCH_ACCURACY" "$wordtime" +bench_set 'Set element with depth 2 length 8' 2 "$BENCH_ACCURACY" "$wordtime" +bench_set 'Set element with depth 3 length 8' 3 "$BENCH_ACCURACY" "$wordtime" + +count="$W" +set_word_length 2 +i=0 +start=$(date +%s) +while [ "$i" -lt "$count" ]; do + new_word >/dev/null + i=$((i + 1)) +done +end=$(date +%s) +wordtime=$(elapsed 'Word creation' "$((end - start))" "$count") + +bench_set 'Set element with depth 1 length 2' 1 "$BENCH_ACCURACY" "$wordtime" +bench_set 'Set element with depth 2 length 2' 2 "$BENCH_ACCURACY" "$wordtime" +bench_set 'Set element with depth 3 length 2' 3 "$BENCH_ACCURACY" "$wordtime" +