summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpeter <peter@pmikkelsen.com>2026-08-21 20:38:39 +0000
committerpeter <peter@pmikkelsen.com>2026-08-21 20:38:39 +0000
commitefe230205f254fc5369b53d3de2841723beb6ff8 (patch)
tree39e096bf204f2fc33ba140f44566127985422b0c
parent2d34e3d861e14d58faad68ea5c7d37f1b201addf (diff)
Start work on testing the evaluator
FossilOrigin-Name: 530cc9567bc825fd8a7eb21f2ca12dbf7ecbecae3ed02638537dc4a8dca9ae1a
-rw-r--r--CMakeLists.txt10
-rw-r--r--src/include/aplwc.h4
-rw-r--r--src/lib/aplwc_internal.h13
-rw-r--r--src/lib/eval.c4
-rw-r--r--src/lib/main.c13
-rw-r--r--src/lib/memory.c16
-rw-r--r--tests/aplwc_eval.c65
-rw-r--r--tests/aplwc_eval/good/1.txt1
-rw-r--r--tests/aplwc_eval/good/2.txt2
-rw-r--r--tests/aplwc_eval/good/3.txt1
-rwxr-xr-xtools/check4
-rwxr-xr-xtools/checkcov2
-rwxr-xr-xtools/tests2
13 files changed, 125 insertions, 12 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index fdd5bd5..089ab7c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -71,13 +71,13 @@ foreach(source ${TEST_SOURCES})
add_executable("${test_bin}" "${source}")
target_link_libraries("${test_bin}" PRIVATE common aplwc)
- if("${name}" STREQUAL "aplwc_read")
- file(GLOB APLWC_READ_INPUTS_GOOD CONFIGURE_DEPENDS "tests/aplwc_read/good/*.txt")
- file(GLOB APLWC_READ_INPUTS_BAD CONFIGURE_DEPENDS "tests/aplwc_read/bad/*.txt")
- foreach(input ${APLWC_READ_INPUTS_GOOD})
+ if("${name}" STREQUAL "aplwc_read" OR "${name}" STREQUAL "aplwc_eval")
+ file(GLOB GOOD_INPUTS CONFIGURE_DEPENDS "tests/${name}/good/*.txt")
+ file(GLOB BAD_INPUTS CONFIGURE_DEPENDS "tests/${name}/bad/*.txt")
+ foreach(input ${GOOD_INPUTS})
add_test(NAME "${name} ${input}" COMMAND "${test_bin}" "${input}")
endforeach()
- foreach(input ${APLWC_READ_INPUTS_BAD})
+ foreach(input ${BAD_INPUTS})
add_test(NAME "${name} ${input}" COMMAND "${test_bin}" "${input}")
set_tests_properties("${name} ${input}" PROPERTIES
WILL_FAIL TRUE
diff --git a/src/include/aplwc.h b/src/include/aplwc.h
index 5ec3761..61c7e9f 100644
--- a/src/include/aplwc.h
+++ b/src/include/aplwc.h
@@ -16,6 +16,7 @@ enum aplwc_rc {
struct aplwc;
struct aplwc_line;
+struct aplwc_value;
enum aplwc_rc aplwc_new(struct aplwc **, uint8_t *, size_t);
void aplwc_destroy(struct aplwc *);
@@ -24,7 +25,8 @@ const char *aplwc_errstr(struct aplwc *);
enum aplwc_rc aplwc_last_error(struct aplwc *);
struct aplwc_line *aplwc_read(struct aplwc *, const char *);
void aplwc_free_line(struct aplwc_line *);
-enum aplwc_rc aplwc_eval(struct aplwc_line *);
+struct aplwc_value *aplwc_eval(struct aplwc_line *);
+void aplwc_free_value(struct aplwc_value *);
#define APLWC_MIN_MEM (64)
diff --git a/src/lib/aplwc_internal.h b/src/lib/aplwc_internal.h
index 833a351..4ad30f8 100644
--- a/src/lib/aplwc_internal.h
+++ b/src/lib/aplwc_internal.h
@@ -53,8 +53,17 @@ struct token_list {
struct token tokens[1];
};
+enum value_tag {
+ VALUE_NUMBER
+};
+
+struct aplwc_value {
+ struct aplwc *aplwc;
+ enum value_tag tag;
+};
+
/* eval.c */
-void eval_line(struct aplwc_line *);
+struct aplwc_value *eval_line(struct aplwc_line *);
/* memory.c */
struct memory memory_init(uint8_t *, size_t);
@@ -62,10 +71,12 @@ struct aplwc *memory_alloc_aplwc(struct memory *);
struct aplwc_line *memory_alloc_aplwc_line(struct memory *);
struct string *memory_alloc_string(struct memory *, size_t);
struct token_list *memory_alloc_token_list(struct memory *, size_t);
+struct aplwc_value *memory_alloc_aplwc_value(struct memory *);
void memory_free_aplwc(struct memory *, struct aplwc *);
void memory_free_aplwc_line(struct memory *, struct aplwc_line *);
void memory_free_string(struct memory *, struct string *);
void memory_free_token_list(struct memory *, struct token_list *);
+void memory_free_aplwc_value(struct memory *, struct aplwc_value *);
/* string.c */
struct string *chars_to_string(struct aplwc *, const char *);
diff --git a/src/lib/eval.c b/src/lib/eval.c
index 89bdf59..07bbedd 100644
--- a/src/lib/eval.c
+++ b/src/lib/eval.c
@@ -1,7 +1,9 @@
#include "aplwc_internal.h"
-void
+struct aplwc_value *
eval_line(struct aplwc_line *line)
{
+ assert(line->aplwc->last_error == APLWC_OK);
line->aplwc->last_error = APLWC_NOT_YET_IMPLEMENTED;
+ return NULL;
}
diff --git a/src/lib/main.c b/src/lib/main.c
index 9335bf0..7456958 100644
--- a/src/lib/main.c
+++ b/src/lib/main.c
@@ -99,10 +99,17 @@ aplwc_free_line(struct aplwc_line *line)
memory_free_aplwc_line(&line->aplwc->memory, line);
}
-enum aplwc_rc
+struct aplwc_value *
aplwc_eval(struct aplwc_line *line)
{
line->aplwc->last_error = APLWC_OK;
- eval_line(line);
- return line->aplwc->last_error;
+ return eval_line(line);
+}
+
+void
+aplwc_free_value(struct aplwc_value *value)
+{
+ if(value == NULL)
+ return;
+ memory_free_aplwc_value(&value->aplwc->memory, value);
}
diff --git a/src/lib/memory.c b/src/lib/memory.c
index 41ac90d..f34ff43 100644
--- a/src/lib/memory.c
+++ b/src/lib/memory.c
@@ -102,3 +102,19 @@ memory_free_token_list(struct memory *m, struct token_list *p)
UNUSED_PARAMETER(m);
UNUSED_PARAMETER(p);
}
+
+struct aplwc_value *
+memory_alloc_aplwc_value(struct memory *m)
+{
+ struct aplwc_value *p;
+
+ p = alloc(m, sizeof(struct aplwc_value), alignof(struct aplwc_value));
+ return p;
+}
+
+void
+memory_free_aplwc_value(struct memory *m, struct aplwc_value *p)
+{
+ UNUSED_PARAMETER(m);
+ UNUSED_PARAMETER(p);
+}
diff --git a/tests/aplwc_eval.c b/tests/aplwc_eval.c
new file mode 100644
index 0000000..8f6abe0
--- /dev/null
+++ b/tests/aplwc_eval.c
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <aplwc.h>
+
+int
+main(int argc, char *argv[])
+{
+ uint8_t data[APLWC_MIN_MEM+1000];
+ char *input;
+ struct aplwc *aplwc;
+ struct aplwc_line *line;
+ struct aplwc_value *value;
+ enum aplwc_rc ret;
+ FILE *f;
+ long input_size;
+ long i, j;
+
+ if(argc != 2)
+ return 1;
+
+ f = fopen(argv[1], "r");
+ if(f == NULL)
+ return 1;
+ if(fseek(f, 0, SEEK_END))
+ return 1;
+ input_size = ftell(f);
+ if(input_size < 0)
+ return 1;
+ rewind(f);
+
+ input = malloc(input_size+1);
+ if(input == NULL)
+ return 1;
+ if(fread(input, 1, input_size, f) < (size_t)input_size)
+ return 1;
+ input[input_size] = 0;
+
+ ret = aplwc_new(&aplwc, data, sizeof(data));
+ if(ret)
+ return 1;
+
+ i = j = 0;
+ do{
+ for(i = j; (input[i] != '\n') && (input[i] != 0); i++);
+
+ input[i] = 0;
+ line = aplwc_read(aplwc, &input[j]);
+ if(line == NULL)
+ return 1;
+
+ value = aplwc_eval(line);
+ if((value == NULL) && (aplwc_last_error(aplwc) != APLWC_OUT_OF_MEMORY))
+ return 1;
+
+ aplwc_free_value(value);
+ aplwc_free_line(line);
+
+ j = i+1;
+ }while(j < input_size);
+
+ aplwc_destroy(aplwc);
+ free(input);
+
+ return 0;
+}
diff --git a/tests/aplwc_eval/good/1.txt b/tests/aplwc_eval/good/1.txt
new file mode 100644
index 0000000..190a180
--- /dev/null
+++ b/tests/aplwc_eval/good/1.txt
@@ -0,0 +1 @@
+123
diff --git a/tests/aplwc_eval/good/2.txt b/tests/aplwc_eval/good/2.txt
new file mode 100644
index 0000000..93be7e0
--- /dev/null
+++ b/tests/aplwc_eval/good/2.txt
@@ -0,0 +1,2 @@
+abc←123
+abc
diff --git a/tests/aplwc_eval/good/3.txt b/tests/aplwc_eval/good/3.txt
new file mode 100644
index 0000000..cee2abc
--- /dev/null
+++ b/tests/aplwc_eval/good/3.txt
@@ -0,0 +1 @@
+abc←123 ⋄ abc
diff --git a/tools/check b/tools/check
index a6c5d20..3a8c822 100755
--- a/tools/check
+++ b/tools/check
@@ -1,8 +1,10 @@
#!/bin/sh
+set -e
+
types="Debug Release Coverage"
-mkdir build
+mkdir -p build
cd build
for type in $types
diff --git a/tools/checkcov b/tools/checkcov
index 3d9fedf..c228c2f 100755
--- a/tools/checkcov
+++ b/tools/checkcov
@@ -1,5 +1,7 @@
#!/bin/sh
+set -e
+
cd build/$1
case "$1" in
diff --git a/tools/tests b/tools/tests
index 66ac467..0455070 100755
--- a/tools/tests
+++ b/tools/tests
@@ -1,5 +1,7 @@
#!/bin/sh
+set -e
+
types=$*
for type in $types