ls

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

commit d054ac5cfe0f80656ec9f19ffc01f1d696dffd22
parent 9967b339eeeb35683e08e637c14d78c8370a752a
Author: kocotian <kocotian@kocotian.pl>
Date:   Sun, 28 Feb 2021 15:23:00 +0100

expressions theoretically done + question mark in parsing

Diffstat:
Mgrammar.c | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Mlsc.c | 7++++++-
Mtokenmacros.h | 1+
3 files changed, 69 insertions(+), 3 deletions(-)

diff --git a/grammar.c b/grammar.c @@ -35,7 +35,6 @@ g_expression(Token *tokens, size_t toksize) size_t i; i = 0; - /* temporarily, expression can be a number only; TODO */ if (tokens[i].type == TokenNumber) { /* number literal */ ++i; } else if (tokens[i].type == TokenString) { /* string literal */ @@ -43,10 +42,71 @@ g_expression(Token *tokens, size_t toksize) } else if (tokens[i].type == TokenIdentifier) { /* identifier literal */ ++i; } else if (tokens[i].type == TokenOpeningParenthesis) { /* (expression) */ + ++i; i += g_expression(tokens + i, toksize - i); g_expecttype(tokens[i++], TokenClosingParenthesis); - } else { /* expressions that starts with another expressions; TODO */ + } else if (tokens[i].type == TokenMinusSign) { /* -sign-change */ + ++i; i += g_expression(tokens + i, toksize - i); + } else if (tokens[i].type == TokenExclamationMark) { /* !negation */ + ++i; + i += g_expression(tokens + i, toksize - i); + } else if (tokens[i].type == TokenIncrement) { /* ++pre-incrementation */ + ++i; + i += g_expression(tokens + i, toksize - i); + } else if (tokens[i].type == TokenDecrement) { /* --pre-decrementation */ + ++i; + i += g_expression(tokens + i, toksize - i); + } + + /* expressions that starts with another expressions; TODO */ + { + /* i += g_expression(tokens + i, toksize - i); */ + if (tokens[i].type == TokenQuestionMark) { /* ternary expression */ + ++i; + i += g_expression(tokens + i, toksize - i); + g_expecttype(tokens[i++], TokenColon); + i += g_expression(tokens + i, toksize - i); + } else if (tokens[i].type == TokenIncrement) { /* post-incrementation++ */ + ++i; + } else if (tokens[i].type == TokenDecrement) { /* post-decrementation-- */ + ++i; + } else if (tokens[i].type == TokenOpeningBracket) { /* indexing[expr] */ + ++i; + i += g_expression(tokens + i, toksize - i); + g_expecttype(tokens[i++], TokenClosingBracket); + } else if (tokens[i].type == TokenOpeningParenthesis) { /* function(expr) */ + do { + ++i; + i += g_expression(tokens + i, toksize - i); + } while (tokens[i].type == TokenComma); + g_expecttype(tokens[i++], TokenClosingParenthesis); + } else { + if (tokens[i].type == TokenAssignmentSign) { /* expr = expr */ + ++i; + } else if (tokens[i].type == TokenPlusSign) { /* expr + expr */ + ++i; + } else if (tokens[i].type == TokenMinusSign) { /* expr - expr */ + ++i; + } else if (tokens[i].type == TokenPlusEqualSign) { /* expr += expr */ + ++i; + } else if (tokens[i].type == TokenMinusEqualSign) { /* expr -= expr */ + ++i; + } else if (tokens[i].type == TokenLogicalOrSign) { /* expr || expr */ + ++i; + } else if (tokens[i].type == TokenLogicalAndSign) { /* expr && expr */ + ++i; + } else if (tokens[i].type == TokenLogicalEquSign) { /* expr == expr */ + ++i; + } else if (tokens[i].type == TokenLogicalNotEquSign) { /* expr != expr */ + ++i; + } else if (tokens[i].type == TokenComma) { /* expr, expr */ + ++i; + } else { + return i; + } + i += g_expression(tokens + i, toksize - i); + } } return i; diff --git a/lsc.c b/lsc.c @@ -87,6 +87,7 @@ parseline(char *input, size_t ilen, size_t off, Token **tokens, size_t *toksiz, (*tokens)[*tokiter].len = ++j + 1; (*tokens)[(*tokiter)++].type = TokenPlusEqualSign; } else if (ISPLUSSIGN(input[i + 1])) { /* ++ */ + ++i; (*tokens)[*tokiter].len = ++j + 1; (*tokens)[(*tokiter)++].type = TokenIncrement; } else { @@ -103,6 +104,7 @@ parseline(char *input, size_t ilen, size_t off, Token **tokens, size_t *toksiz, (*tokens)[*tokiter].len = ++j + 1; (*tokens)[(*tokiter)++].type = TokenMinusEqualSign; } else if (ISMINUSSIGN(input[i + 1])) { /* -- */ + ++i; (*tokens)[*tokiter].len = ++j + 1; (*tokens)[(*tokiter)++].type = TokenDecrement; } else { @@ -157,16 +159,19 @@ parseline(char *input, size_t ilen, size_t off, Token **tokens, size_t *toksiz, continue; } else if (ISOPPAR(ch) || ISOPBRK(ch) || ISOPBRC(ch) || ISCLPAR(ch) || ISCLBRK(ch) || ISCLBRC(ch) - || ISCOMM(ch) || ISSEMICOLON(ch)) { + || ISCOLON(ch) || ISSEMICOLON(ch) + || ISCOMM(ch) || ISQUESTIONSIGN(ch)) { (*tokens)[*tokiter].off = valstart; (*tokens)[*tokiter].len = j + 1; (*tokens)[(*tokiter)++].type = + ISQUESTIONSIGN(ch) ? TokenQuestionMark : ISOPPAR(ch) ? TokenOpeningParenthesis : ISOPBRK(ch) ? TokenOpeningBracket : ISOPBRC(ch) ? TokenOpeningBrace : ISCLPAR(ch) ? TokenClosingParenthesis : ISCLBRK(ch) ? TokenClosingBracket : ISCLBRC(ch) ? TokenClosingBrace : + ISCOLON(ch) ? TokenColon : ISCOMM(ch) ? TokenComma : TokenSemicolon; type = TokenNull; diff --git a/tokenmacros.h b/tokenmacros.h @@ -12,6 +12,7 @@ #define ISCOMM(ch) ((ch) == 0x2c) #define ISCOLON(ch) ((ch) == 0x3a) #define ISSEMICOLON(ch) ((ch) == 0x3b) +#define ISQUESTIONSIGN(ch) ((ch) == 0x3f) #define ISEQUSIGN(ch) ((ch) == 0x3d) #define ISPLUSSIGN(ch) ((ch) == 0x2b)