commit 8dc3257f1cd68efd60176c8d7a51dec9cbae1e40
parent c07f58b36f225695b8f38a44af83192d80d460f5
Author: sin <sin@2f30.org>
Date: Fri, 9 May 2014 08:12:05 +0100
lexerctx should be internal to lexer.c
Diffstat:
3 files changed, 38 insertions(+), 11 deletions(-)
diff --git a/lexer.c b/lexer.c
@@ -1,5 +1,6 @@
/* See LICENSE file for copyright and license details. */
#include <ctype.h>
+#include <stdlib.h>
#include <string.h>
#include "lexer.h"
@@ -22,6 +23,13 @@ enum state {
State_Illegal_Input,
};
+struct lexerctx {
+ enum state state;
+ int ready;
+ const char *s;
+ const char *e;
+};
+
int
delim(int c)
{
@@ -40,6 +48,12 @@ extractstring(struct tok *t)
return strndup(t->s, t->e - t->s);
}
+void
+freelexer(struct lexerctx *ctx)
+{
+ free(ctx);
+}
+
struct tok
gettok(struct lexerctx *ctx, const char *buf, size_t len)
{
@@ -222,6 +236,17 @@ gettok(struct lexerctx *ctx, const char *buf, size_t len)
return tok;
}
+struct lexerctx *
+initlexer(void)
+{
+ struct lexerctx *ctx;
+
+ ctx = calloc(1, sizeof(*ctx));
+ if (!ctx)
+ return NULL;
+ return ctx;
+}
+
void
resetlexer(struct lexerctx *ctx)
{
diff --git a/lexer.h b/lexer.h
@@ -19,13 +19,11 @@ struct tok {
const char *e;
};
-struct lexerctx {
- int ready;
- const char *s;
- const char *e;
-};
+struct lexerctx;
int delim(int);
char *extractstring(struct tok *);
+void freelexer(struct lexerctx *ctx);
struct tok gettok(struct lexerctx *, const char *, size_t);
+struct lexerctx *initlexer(void);
void resetlexer(struct lexerctx *);
diff --git a/repl.c b/repl.c
@@ -11,14 +11,18 @@ static void
lexertest(char *buf)
{
struct tok tok;
- struct lexerctx ctx;
+ struct lexerctx *ctx;
- resetlexer(&ctx);
- do {
- tok = gettok(&ctx, buf, strlen(buf));
- printtok(&tok);
+ ctx = initlexer();
+ if (ctx) {
+ resetlexer(ctx);
+ do {
+ tok = gettok(ctx, buf, strlen(buf));
+ printtok(&tok);
putchar('\n');
- } while (tok.type != Eof && tok.type != Error);
+ } while (tok.type != Eof && tok.type != Error);
+ freelexer(ctx);
+ }
}
int