]> mj.ucw.cz Git - eval.git/commitdiff
Added a simple judge on sequences of tokens.
authorMartin Mares <mj@ucw.cz>
Sat, 17 Nov 2007 21:58:49 +0000 (22:58 +0100)
committerMartin Mares <mj@ucw.cz>
Sat, 17 Nov 2007 21:58:49 +0000 (22:58 +0100)
judge/Makefile
judge/judge-tok.c [new file with mode: 0644]
judge/judge.h
judge/test-tok.c
judge/token.c

index e5fcce74a13a5769af18df0d68605469c06551f2..57a688e36ba47190418b43b853ab83c8fdc672a2 100644 (file)
@@ -2,12 +2,13 @@ CC=gcc-4.1.1
 CFLAGS=-O2 -Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Wundef -Wredundant-decls -Winline $(DEBUG) -std=gnu99
 CFLAGS+=-Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers
 
-all: test-io test-tok
+all: test-io test-tok judge-tok
 
 JLIB=utils.o io.o token.o
 
 test-io: test-io.o $(JLIB)
 test-tok: test-tok.o $(JLIB)
+judge-tok: judge-tok.o $(JLIB)
 
 clean::
        rm -f `find . -name "*~" -or -name "*.[oa]" -or -name "\#*\#" -or -name TAGS -or -name core`
diff --git a/judge/judge-tok.c b/judge/judge-tok.c
new file mode 100644 (file)
index 0000000..bb4e95f
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ *     A judge comparing two sequences of tokens
+ *
+ *     (c) 2007 Martin Krulis <bobrik@matfyz.cz>
+ *     (c) 2007 Martin Mares <mj@ucw.cz>
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "judge.h"
+
+static int trailing_nl(struct tokenizer *t)
+{
+  // Ignore empty lines at the end of file
+  if (t->token[0])
+    return 0;
+  t->flags &= ~TF_REPORT_LINES;
+  return !get_token(t);
+}
+
+int main(int argc, char **argv)
+{
+  struct tokenizer t1, t2;
+  int report_lines = 1;
+
+  if (argc != 3 && argc != 4)
+    die("Usage: judge-tok [-n] <file1> <file2>");
+
+  // Check for -n parameter
+  report_lines = !(argc == 4 && !strcmp(argv[1], "-n"));
+
+  tok_init(&t1, sopen_read(argv[argc-2]));
+  tok_init(&t2, sopen_read(argv[argc-1]));
+  if (report_lines)
+    t1.flags = t2.flags = TF_REPORT_LINES;
+
+  for (;;)
+    {
+      char *a = get_token(&t1), *b = get_token(&t2);
+      if (!a)
+       {
+         if (b && !trailing_nl(&t2))
+           tok_err(&t1, "Ends too early");
+         break;
+       }
+      else if (!b)
+       {
+         if (a && !trailing_nl(&t1))
+           tok_err(&t2, "Garbage at the end");
+         break;
+       }
+      else if (strcmp(a, b))
+       tok_err(&t1, "Found <%s>, expected <%s>", a, b);
+    }
+
+  return 0;
+}
index 4290b4492001d555301dc93b6d3374fdaaf12e1f..3ac8e4cf24b42eccb88ca700207d05f46b2f237d 100644 (file)
@@ -78,7 +78,7 @@ enum tokenizer_flags {
 
 void tok_init(struct tokenizer *t, struct stream *s);
 void tok_cleanup(struct tokenizer *t);
-void tok_err(struct tokenizer *t, char *msg) NONRET;
+void tok_err(struct tokenizer *t, char *msg, ...) NONRET;
 char *get_token(struct tokenizer *t);
 
 // Parsing functions
index 10e7b3b04c2a8a43ecc98274cd899f6d6a5c746b..92303bb06b8c202b8cbac100f9db2093198fc5f5 100644 (file)
@@ -8,6 +8,7 @@ int main(void)
 
   struct tokenizer t;
   tok_init(&t, i);
+  // t.maxtoken = 1000;
   t.flags = TF_REPORT_LINES;
   char *tok;
   while (tok = get_token(&t))
index 2f43ddc99a45adcefeb4de462ceccb0512a1c30d..69b9398395b89fb0e3c1a6ae1fd37dfd068fec6c 100644 (file)
@@ -4,7 +4,9 @@
  *     (c) 2007 Martin Mares <mj@ucw.cz>
  */
 
+#include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 #include <limits.h>
 #include <errno.h>
@@ -28,9 +30,15 @@ void tok_cleanup(struct tokenizer *t)
   free(t->token);
 }
 
-void tok_err(struct tokenizer *t, char *msg)
+void tok_err(struct tokenizer *t, char *msg, ...)
 {
-  die("%s:%d: %s", t->stream->name, t->line, msg);
+  va_list args;
+  va_start(args, msg);
+  printf("%s:%d: ", t->stream->name, t->line);
+  vprintf(msg, args);
+  putchar('\n');
+  va_end(args);
+  exit(1);
 }
 
 static inline int is_white(int c)