aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkartofen <kartofen.mail.0@protonmail.com>2025-09-27 11:21:21 +0300
committerkartofen <kartofen.mail.0@protonmail.com>2025-09-27 11:21:21 +0300
commitadcadb01becb36c79d881080d399328de051e301 (patch)
treec759aac6773a8722d5d61291a9a1318aff967161
parentfec8e3a95becfb3dc2a3eb0f512a120a7a4551c5 (diff)
use ninja for building
-rw-r--r--build.ninja6
-rwxr-xr-xbuild.sh103
-rw-r--r--demos/sample-files/lbp-skeleton.c5
-rw-r--r--ninja.m479
4 files changed, 87 insertions, 106 deletions
diff --git a/build.ninja b/build.ninja
new file mode 100644
index 0000000..e09caa8
--- /dev/null
+++ b/build.ninja
@@ -0,0 +1,6 @@
+rule regen
+ command = m4 ninja.m4 > build.ninja
+ generator=1
+
+build force: phony
+build build.ninja: regen | force
diff --git a/build.sh b/build.sh
deleted file mode 100755
index cf56f9d..0000000
--- a/build.sh
+++ /dev/null
@@ -1,103 +0,0 @@
-#!/bin/sh
-
-set -e
-
-function log
-{
- >&2 echo "-> $@"
- "$@"
-}
-
-function cc
-{
- mkdir -p bin
- [ -n "$3" ] && NAME="$3" || NAME=$(basename "$1")
- log gcc -Wall -Wextra -Wpedantic -I. -g $2 "$1.c" -o "bin/$NAME"
-}
-
-function shared
-{
- mkdir -p bin
- [ -n "$3" ] && NAME="$3" || NAME=$(basename "$1")
- log gcc -Wall -Wextra -Wpedantic -I. -g -shared -fPIC $2 "$1.c" -o "bin/$NAME.so"
-}
-
-function leak
-{
- log valgrind --leak-check=full --show-leak-kinds=all -s bin/"$1" $2
-}
-
-# cc util/dict -D_DICT_STANDALONE
-# leak dict
-
-# cc demos/lexer util/dict.c
-# leak lexer
-
-# cc recursive/recursive-ascent
-# leak recursive-ascent
-
-# cc recursive/recursive-ascent-descent
-# leak recursive-ascent-descent
-
-# cc util-tables -D_UTIL_TABLES_STANDALONE
-# leak util-tables
-
-# cc slr-table -D_SLR_TABLE_STANDALONE
-# leak slr-table
-
-# cc clr-table -D_CLR_TABLE_STANDALONE
-# leak clr-table
-
-# cc clr-table "-D_CLR_TABLE_STANDALONE -D_LAZY_LALR" lalr-table
-# leak lalr-table
-
-# cc lr-parser -D_LR_PARSER_STANDALONE
-# leak lr-parser
-
-# cc demos/instant-parser # not working
-# leak instant-parser # not working
-
-#--------------------------------------------------------------------------------------------------#
-
-cc demos/generate-parser "-rdynamic"
-
-shared slr-table
-shared clr-table
-shared clr-table -D_LAZY_LALR lalr-table
-shared demos/sample-files/lalr-defs
-
-# --- Calc example ---
-# shared demos/sample-files/calc-defs
-# leak generate-parser "-o bin/calc -t lalr-table bin/calc-defs.so"
-# cc demos/sample-files/calc-skeleton "" parser
-# leak parser "13*10+9"
-# leak parser "-13+20"
-# leak parser "1>52?2+3:53"
-# exit 0
-
-# --- Grammar Definition example ---
-shared demos/sample-files/gram-defs
-leak generate-parser "-o bin/gram -t lalr-table bin/gram-defs.so"
-cc demos/sample-files/gram-skeleton "" gram-parser
-
-# !!! gram.g is outdated !!!
-# leak gram-parser < demos/sample-files/gram.g > bin/gram-gram.c
-# shared bin/gram-gram
-# leak generate-parser "-o bin/gram -t lalr-table bin/gram-gram.so"
-# cc demos/sample-files/gram-skeleton "" gram2-parser
-
-# leak gram-parser < demos/sample-files/calc.g > bin/calc-gram.c
-# shared bin/calc-gram
-# leak generate-parser "-o bin/calc -t lalr-table bin/calc-gram.so"
-# cc demos/sample-files/calc-skeleton "" calc-parser
-
-# leak calc-parser "13*10+9"
-# leak calc-parser "-13+20"
-# leak calc-parser "1>52?2+3:53"
-
-leak gram-parser < demos/sample-files/lbp.g > bin/lbp-gram.c
-shared bin/lbp-gram
-leak generate-parser "-o bin/lbp -t lalr-table bin/lbp-gram.so"
-
-cc demos/sample-files/lbp-skeleton "util/dict.c" lbp-parser
-leak lbp-parser < demos/sample-files/lbp-code.lbp
diff --git a/demos/sample-files/lbp-skeleton.c b/demos/sample-files/lbp-skeleton.c
index 1ee54d8..90d0448 100644
--- a/demos/sample-files/lbp-skeleton.c
+++ b/demos/sample-files/lbp-skeleton.c
@@ -7,10 +7,9 @@
// TODO: - lr parser is bad for debugging (now its better)
// - deal with errors (the token queue for example)!!!!
-// - debuginfo in the token that gets propagaded through the
-// stack_items and lr_parse returns a generic errorinfo
-// with user-implemented compilation error messages
// - ast should show the specific operation (match, assignment, etc)
+// - merge the debuginfo for each reduction !
+
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
diff --git a/ninja.m4 b/ninja.m4
new file mode 100644
index 0000000..62f3aa2
--- /dev/null
+++ b/ninja.m4
@@ -0,0 +1,79 @@
+divert(-1)
+changequote([,])
+divert(0)
+
+# rules
+
+cflags = -Wall -Wextra -g -I.
+bin = bin
+leak = valgrind -s --leak-check=full --show-leak-kinds=all
+
+rule cc
+ depfile = $out.d
+ command = gcc -MD -MF $out.d $cflags $in -o $out
+rule shared
+ depfile = $out.d
+ command = gcc -MD -MF $out.d $cflags -shared -fPIC $in -o $out
+rule m4
+ command = m4 $m4flags $in > $out
+
+build $bin/generate-parser: cc demos/generate-parser.c
+ cflags = $cflags -rdynamic
+rule parser_gen
+ command = $leak $bin/generate-parser -o $$(echo $out | cut -f1 -d'.') -t $tabletype $in
+
+define([gbuild], [ifelse(
+[$1], [from-gram], [
+build $bin/$2-gram.c: gram_parse $3/$2.g | $bin/gram-parser
+build $bin/$2-gram.so: shared $bin/$2-gram.c],
+
+[$1], [from-defs], [
+build $bin/$2-gram.so: shared $3/$2-defs.c])
+
+build $bin/$2.c $bin/$2.h: parser_gen $bin/$2-gram.so | $bin/generate-parser tables
+ tabletype = lalr-table
+build $bin/$2-parser: cc $3/$2-skeleton.c $5 | $bin/$2.c $bin/$2.h
+build $2: phony $bin/$2-parser
+])
+
+# building tools
+
+build $bin/slr-table.so: shared slr-table.c
+build $bin/clr-table.so: shared clr-table.c
+build $bin/lalr-table.so: shared clr-table.c
+ cflags = $cflags -D_LAZY_LALR
+build tables: phony $bin/slr-table.so $bin/clr-table.so $bin/lalr-table.so
+
+gbuild(from-defs, gram, demos/sample-files, lalr-table)
+rule gram_parse
+ command = $leak $bin/gram-parser < $in > $out
+
+# build actual
+
+gbuild(from-gram, lbp, demos/sample-files, lalr-table, util/dict.c)
+rule lbp_parse
+ command = $leak $bin/lbp-parser < $in
+build lbp_test: lbp_parse demos/sample-files/lbp-code.lbp | lbp
+
+gbuild(from-gram, calc, demos/sample-files, lalr-table) # skeleton doesn't work
+rule calc_parse
+ command = $leak $bin/calc-parser 5+3
+build calc_test: calc_parse | calc
+
+# targets
+
+rule ninja_clean
+ command = ninja -t clean
+build clean: ninja_clean
+
+default lbp_test
+
+# regeneration
+
+rule regen
+ command = m4 ninja.m4 > build.ninja
+ generator = 1
+ description = Regen build.ninja
+
+build build.ninja: regen ninja.m4
+build configure: regen