summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-09-16 23:34:30 +0200
committerJo-Philipp Wich <jo@mein.io>2020-09-16 23:34:30 +0200
commit0e67bdaa847c6d67497016db52e06af357f2e85c (patch)
tree5af316649c506c17c645c782c0519e27392b9d03
parent54bb15b2be3656e91386b80074f45591b20fed3f (diff)
treewide: implement strict assignment mode
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--ast.h1
-rw-r--r--eval.c13
-rw-r--r--main.c11
3 files changed, 21 insertions, 4 deletions
diff --git a/ast.h b/ast.h
index cac75d2..6f2b12c 100644
--- a/ast.h
+++ b/ast.h
@@ -82,6 +82,7 @@ struct ut_state {
uint8_t srand_called:1;
uint8_t trim_blocks:1;
uint8_t lstrip_blocks:1;
+ uint8_t strict_declarations:1;
size_t off;
enum ut_block_type blocktype;
struct {
diff --git a/eval.c b/eval.c
index 954f596..4410d2d 100644
--- a/eval.c
+++ b/eval.c
@@ -303,8 +303,14 @@ ut_getref(struct ut_state *state, uint32_t off, struct json_object **key)
next = ut_getscope(state, ++i);
- if (!next)
+ if (!next) {
+ if (state->strict_declarations)
+ ut_throw(state, off,
+ "Reference error: access to undeclared variable %s",
+ json_object_get_string(op->val));
+
break;
+ }
scope = next;
}
@@ -1356,6 +1362,11 @@ ut_execute_op(struct ut_state *state, uint32_t off)
ut_putval(state->ctx);
state->ctx = NULL;
+ if (state->strict_declarations && scope == NULL) {
+ ut_throw(state, off, "Reference error: %s is not defined",
+ json_object_get_string(op->val));
+ }
+
val = ut_getval(scope, key);
ut_putval(scope);
diff --git a/main.c b/main.c
index 68b39c1..18db4a7 100644
--- a/main.c
+++ b/main.c
@@ -38,13 +38,14 @@ print_usage(char *app)
{
printf(
"== Usage ==\n\n"
- " # %s [-d] [-l] [-r] {-i <file> | -s \"utpl script...\"}\n"
+ " # %s [-d] [-l] [-r] [-S] {-i <file> | -s \"utpl script...\"}\n"
" -h, --help Print this help\n"
" -i file Specify an utpl script to parse\n"
" -s \"utpl script...\" Specify an utpl code fragment to parse\n"
" -d Instead of executing the script, dump the resulting AST as dot\n"
" -l Do not strip leading block whitespace\n"
- " -r Do not trim trailing block newlines\n",
+ " -r Do not trim trailing block newlines\n"
+ " -S Enable strict mode\n",
app);
}
@@ -198,7 +199,7 @@ main(int argc, char **argv)
state->lstrip_blocks = 1;
state->trim_blocks = 1;
- while ((opt = getopt(argc, argv, "dhlri:s:")) != -1)
+ while ((opt = getopt(argc, argv, "dhlrSi:s:")) != -1)
{
switch (opt) {
case 'h':
@@ -231,6 +232,10 @@ main(int argc, char **argv)
case 's':
source = optarg;
break;
+
+ case 'S':
+ state->strict_declarations = 1;
+ break;
}
}