commit 97f96edafa117130485f16fbafee61556c831dd4
parent bfef720c437bf49f0158f9db7350b03c0060e103
Author: kocotian <kocotian@kocotian.pl>
Date: Fri, 26 Feb 2021 19:24:49 +0100
semicolon as token, opening/closing tokens
Diffstat:
3 files changed, 43 insertions(+), 16 deletions(-)
diff --git a/grammar.c b/grammar.c
@@ -2,6 +2,8 @@
#include "grammar.h"
+extern char *contents;
+
static char *
g_typetostr(TokenType type)
{
@@ -26,10 +28,18 @@ g_statement(Token *tokens, size_t toksize)
{
size_t i;
i = 0;
- if (tokens[i].type == TokenBrace) {
+ if (tokens[i].type == TokenOpeningBrace) { /* compound */
++i;
- while (tokens[i].type != TokenBrace && i < toksize);
+ while (tokens[i].type != TokenClosingBrace && i < toksize) {
g_statement(tokens + i, toksize - i); ++i;
+ }
+ } else if (0) { /* conditional */
+ } else if (0) { /* loop */
+ } else if (0) { /* return */
+ } else if (0) { /* variable */
+ } else if (0) { /* constant */
+ } else if (0) { /* expression */
+ } else if (0) { /* noop */
} else {
errwarn("unexpected token \033[1m%s\033[0m, in place of a statement", 1,
tokens[i], g_typetostr(tokens[i].type));
@@ -44,7 +54,7 @@ g_function(Token *tokens, size_t toksize)
fputs("identifier\n", stderr);
g_expecttype(tokens[i++], TokenIdentifier);
fputs("parenthesis (opening)\n", stderr);
- g_expecttype(tokens[i], TokenParenthesis);
+ g_expecttype(tokens[i], TokenOpeningParenthesis);
fputs("loop:\n", stderr);
do {
++i;
@@ -52,7 +62,7 @@ g_function(Token *tokens, size_t toksize)
g_expecttype(tokens[i++], TokenIdentifier);
} while (tokens[i].type == TokenComma);
fputs("parenthesis (closing)\n", stderr);
- g_expecttype(tokens[i++], TokenParenthesis);
+ g_expecttype(tokens[i++], TokenClosingParenthesis);
g_statement(tokens + i, toksize - i);
++i;
return i;
diff --git a/lsc.c b/lsc.c
@@ -13,14 +13,19 @@
#define ISUPP(ch) ((ch) > 0x40 && (ch) < 0x5b)
#define ISNUM(ch) ((ch) > 0x2f && (ch) < 0x3a)
#define ISUND(ch) ((ch) == 0x5f)
-#define ISPAR(ch) ((ch) == 0x28 || (ch) == 0x29)
-#define ISBRK(ch) ((ch) == 0x5b || (ch) == 0x5d)
-#define ISBRC(ch) ((ch) == 0x7b || (ch) == 0x7d)
+#define ISOPPAR(ch) ((ch) == 0x28)
+#define ISOPBRK(ch) ((ch) == 0x5b)
+#define ISOPBRC(ch) ((ch) == 0x7b)
+#define ISCLPAR(ch) ((ch) == 0x29)
+#define ISCLBRK(ch) ((ch) == 0x5d)
+#define ISCLBRC(ch) ((ch) == 0x7d)
#define ISQUOT(ch) ((ch) == 0x22)
#define ISCOMM(ch) ((ch) == 0x2c)
#define ISEQUSIGN(ch) ((ch) == 0x3d)
+#define ISSEMICOLON(ch) ((ch) == 0x3b)
+
#define ISIGNORABLE(ch) ((ch) > 0x00 && (ch) < 0x21)
-#define ISLINECOMMSTARTCHAR(ch) ((ch) == 0x3b || (ch) == 0x23)
+#define ISLINECOMMSTARTCHAR(ch) ((ch) == 0x23)
#define ISIDENSTARTCHAR(ch) (ISUND(ch) || ISLOW(ch) || ISUPP(ch))
#define ISIDENCHAR(ch) (ISIDENSTARTCHAR(ch) || ISNUM(ch))
@@ -35,6 +40,8 @@ char *argv0;
char *filename, *line;
int fileline;
+char *contents;
+
static int
getsyscallbyname(char *name)
{
@@ -74,14 +81,20 @@ parseline(char *input, size_t ilen, size_t off, Token **tokens, size_t *toksiz,
} else if (ISIGNORABLE(ch)) {
--j;
continue;
- } else if (ISPAR(ch) || ISBRK(ch) || ISBRC(ch) || ISCOMM(ch) || ISEQUSIGN(ch)) {
+ } else if (ISOPPAR(ch) || ISOPBRK(ch) || ISOPBRC(ch)
+ || ISCLPAR(ch) || ISCLBRK(ch) || ISCLBRC(ch)
+ || ISCOMM(ch) || ISSEMICOLON(ch) || ISEQUSIGN(ch)) {
(*tokens)[*tokiter].off = valstart;
(*tokens)[*tokiter].len = j + 1;
(*tokens)[(*tokiter)++].type =
- ISPAR(ch) ? TokenParenthesis :
- ISBRK(ch) ? TokenBracket :
- ISBRC(ch) ? TokenBrace :
+ ISOPPAR(ch) ? TokenOpeningParenthesis :
+ ISOPBRK(ch) ? TokenOpeningBracket :
+ ISOPBRC(ch) ? TokenOpeningBrace :
+ ISCLPAR(ch) ? TokenClosingParenthesis :
+ ISCLBRK(ch) ? TokenClosingBracket :
+ ISCLBRC(ch) ? TokenClosingBrace :
ISCOMM(ch) ? TokenComma :
+ ISSEMICOLON(ch) ? TokenSemicolon :
TokenEqualSign;
type = TokenNull;
j = -1;
@@ -115,7 +128,7 @@ usage(void)
int
main(int argc, char *argv[])
{
- char buffer[BUFSIZ], *contents;
+ char buffer[BUFSIZ];
ssize_t rb;
size_t csiz, toksiz, tokiter;
int lindex;
diff --git a/tokentypes b/tokentypes
@@ -8,11 +8,15 @@ TokenString
# "punctuation"
TokenComma
+TokenSemicolon
# any brackets
-TokenParenthesis
-TokenBracket
-TokenBrace
+TokenOpeningParenthesis
+TokenOpeningBracket
+TokenOpeningBrace
+TokenClosingParenthesis
+TokenClosingBracket
+TokenClosingBrace
# operators
TokenEqualSign