ls

simple syscall based programming language from scratch
git clone git://git.kocotian.pl/ls.git
Log | Files | Refs | README

commit bfef720c437bf49f0158f9db7350b03c0060e103
parent 6820ec8440f6beee5ed2fc224df922b9f94bf39e
Author: kocotian <kocotian@kocotian.pl>
Date:   Fri, 26 Feb 2021 18:37:08 +0100

improved grammar, fixed errwarn

Diffstat:
MMakefile | 2+-
Mgrammar.c | 10+++++++---
Mlsc.c | 9++++-----
Mlsc.h | 3++-
Mutil.c | 6+++---
Mutil.h | 4+++-
6 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/Makefile b/Makefile @@ -20,7 +20,7 @@ lsc: ${OBJ} lsc.h ${CC} -o $@ ${OBJ} clean: - rm -f lsc ${OBJ} + rm -f lsc ${OBJ} tokentype.c tokentype.h grammar.c: tokentype.c diff --git a/grammar.c b/grammar.c @@ -17,7 +17,7 @@ g_expecttype(Token token, TokenType type) { if (token.type != type) errwarn("expected \033[1m%s\033[0m, got \033[1m%s\033[0m", 1, - g_typetostr(type), g_typetostr(token.type)); + token, g_typetostr(type), g_typetostr(token.type)); return 0; } @@ -30,6 +30,9 @@ g_statement(Token *tokens, size_t toksize) ++i; while (tokens[i].type != TokenBrace && i < toksize); g_statement(tokens + i, toksize - i); ++i; + } else { + errwarn("unexpected token \033[1m%s\033[0m, in place of a statement", 1, + tokens[i], g_typetostr(tokens[i].type)); } } @@ -40,7 +43,7 @@ g_function(Token *tokens, size_t toksize) i = 0; fputs("identifier\n", stderr); g_expecttype(tokens[i++], TokenIdentifier); - fputs("parenthesis\n", stderr); + fputs("parenthesis (opening)\n", stderr); g_expecttype(tokens[i], TokenParenthesis); fputs("loop:\n", stderr); do { @@ -48,9 +51,10 @@ g_function(Token *tokens, size_t toksize) fputs("\tidentifier\n", stderr); g_expecttype(tokens[i++], TokenIdentifier); } while (tokens[i].type == TokenComma); - fputs("parenthesis\n", stderr); + fputs("parenthesis (closing)\n", stderr); g_expecttype(tokens[i++], TokenParenthesis); g_statement(tokens + i, toksize - i); + ++i; return i; } diff --git a/lsc.c b/lsc.c @@ -59,6 +59,9 @@ parseline(char *input, size_t ilen, size_t off, Token **tokens, size_t *toksiz, *tokens = realloc(*tokens, sizeof(Token) * (*toksiz += 128)); ch = input[i]; if (!type) { + (*tokens)[*tokiter].file = filename; + (*tokens)[*tokiter].line = fileline; + (*tokens)[*tokiter].col = valstart - off + 1; valstart = off + i; if (ISNUM(ch)) type = TokenNumber; @@ -72,9 +75,6 @@ parseline(char *input, size_t ilen, size_t off, Token **tokens, size_t *toksiz, --j; continue; } else if (ISPAR(ch) || ISBRK(ch) || ISBRC(ch) || ISCOMM(ch) || ISEQUSIGN(ch)) { - (*tokens)[*tokiter].file = filename; - (*tokens)[*tokiter].line = fileline; - (*tokens)[*tokiter].col = valstart - off + 1; (*tokens)[*tokiter].off = valstart; (*tokens)[*tokiter].len = j + 1; (*tokens)[(*tokiter)++].type = @@ -87,7 +87,7 @@ parseline(char *input, size_t ilen, size_t off, Token **tokens, size_t *toksiz, j = -1; } else errwarn("unexpected character: \033[1m%c \033[0m(\033[1m\\%o\033[0m)", - 1, ch, ch & 0xff); + 1, (*tokens)[*tokiter], ch, ch & 0xff); } else if ((type == TokenNumber && !ISNUMCHAR(ch)) || (type == TokenIdentifier && !ISIDENCHAR(ch)) || (type == TokenString && ISQUOT(ch))) { @@ -150,7 +150,6 @@ main(int argc, char *argv[]) write(1, "\n", 1); } - printf("tokiter: %d\n", tokiter); g_main(tokens, tokiter); free(tokens); diff --git a/lsc.h b/lsc.h @@ -2,7 +2,6 @@ #define _LSC_H #include "tokentype.h" -#include "util.h" typedef struct { short line, col; @@ -11,6 +10,8 @@ typedef struct { TokenType type; } Token; +#include "util.h" + static int getsyscallbyname(char *name); static ssize_t diff --git a/util.c b/util.c @@ -62,14 +62,14 @@ nextline(int fd, char *buf, size_t size) } void -errwarn(const char *fmt, int iserror, ...) +errwarn(const char *fmt, int iserror, Token token, ...) { extern const char *filename, *line; extern int fileline; va_list ap; - fprintf(stderr, "\033[0;1m%s:%d: \033[1;3%s: \033[0m", - filename, fileline, iserror ? "1merror" : "3mwarning"); + fprintf(stderr, "\033[0;1m%s:%d:%d: \033[1;3%s: \033[0m", + token.file, token.line, token.col, iserror ? "1merror" : "3mwarning"); va_start(ap, iserror); vfprintf(stderr, fmt, ap); diff --git a/util.h b/util.h @@ -1,6 +1,8 @@ /* See licenses/LIBSL file for copyright and license details. */ /* Extended by kocotian */ +#include "lsc.h" + #define MAX(A, B) ((A) > (B) ? (A) : (B)) #define MIN(A, B) ((A) < (B) ? (A) : (B)) #define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B)) @@ -8,4 +10,4 @@ void die(const char *fmt, ...); void *ecalloc(size_t nmemb, size_t size); ssize_t nextline(int fd, char *buf, size_t size); -void errwarn(const char *fmt, int iserror, ...); +void errwarn(const char *fmt, int iserror, Token token, ...);