npc

nanohard parser compiler
git clone git://git.kocotian.pl/npc.git
Log | Files | Refs

commit a3aabd2237b6c5c5fecd1bdc92dd6b0e8bab8ddd
parent cd9f72d1f6617148f007d0521a7436cb689a5f06
Author: kocotian <kocotian@kocotian.pl>
Date:   Sat, 10 Apr 2021 16:16:39 +0200

Keyword and FunctionArrow tokens, Strcmp*, toString, fixed parsing

Diffstat:
Mcompile.c | 9++++++++-
Mnpc.npc | 10+++++++++-
Mparse.c | 32+++++++++++++-------------------
Mparse.h | 3++-
Mstr.c | 22++++++++++++++++++++++
Mstr.h | 5+++++
6 files changed, 59 insertions(+), 22 deletions(-)

diff --git a/compile.c b/compile.c @@ -7,6 +7,13 @@ compile(Token *tokens, size_t n) { size_t i; for (i = 0; i < n; ++i) - printf("%.*s\n", + printf("\033[%sm%.*s ", + tokens[i].type == TokenNumber ? "0;92" : + tokens[i].type == TokenIdentifier ? "0;96" : + tokens[i].type == TokenCharacter ? "0;93" : + tokens[i].type == TokenString ? "0;33" : + tokens[i].type == TokenKeyword ? "0;95" : + tokens[i].type == TokenComment ? "0;32" : + "0;97", (int)tokens[i].value.len, tokens[i].value.data); } diff --git a/npc.npc b/npc.npc @@ -7,6 +7,7 @@ define function charFrom(c) => ($ichr >= c); define function charTo(c) => ($ichr <= c); define function charAny() => ($ichr); define function charBetween(c1, c2) => (@charFrom(c1) && @charTo(c2)); +define function compare(s) => (!Strcmpc($itok, s)); ;; >>> Tokens <<< @@ -17,7 +18,13 @@ define token Number define token Identifier match (charBetween('a', 'z') or charBetween('A', 'Z')) then until true - (charBetween('a', 'z') or charBetween('A', 'Z') or charBetween('0', '9')); + (charBetween('a', 'z') or charBetween('A', 'Z') or charBetween('0', '9')) + next { + define token Keyword + match ( + compare("define") + ); + }; define token Character match char(''') then charAny() then char('''); @@ -30,6 +37,7 @@ define token String define token Assignment match char('=') then { define token LogicalEqual match char('='); + define token FunctionArrow match char('>'); }; define token Plus diff --git a/parse.c b/parse.c @@ -23,6 +23,7 @@ parse(String input, Token **output) (*output)[osiz].type = TokenNumber; while (((input.data[i] >= '0' && input.data[i] <= '9')) && (i < input.len)) ++i; + --i; } /* define token Identifier */ else if ((input.data[i] >= 'a' && input.data[i] <= 'z') @@ -32,20 +33,24 @@ parse(String input, Token **output) || (input.data[i] >= 'A' && input.data[i] <= 'Z') || (input.data[i] >= '0' && input.data[i] <= '9')) && (i < input.len)) ++i; + --i; + /* define token Keyword */ + (*output)[osiz].value.len = (size_t)((input.data + i) - (*output)[osiz].value.data) + 1; + if (!Strcmpc((*output)[osiz].value, "define")) { + (*output)[osiz].type = TokenKeyword; + } } /* define token Character */ else if (input.data[i] == '\'') { (*output)[osiz].type = TokenCharacter; if (!input.data[++i] && (i < input.len)) return -1; if (!(input.data[++i] == '\'') && (i < input.len)) return -1; - ++i; } /* define token String */ else if (input.data[i] == '"') { (*output)[osiz].type = TokenString; ++i; while (!(input.data[i] == '"') && (i < input.len)) ++i; - ++i; } /* == Operators == */ /* define token Assignment */ @@ -58,7 +63,11 @@ parse(String input, Token **output) (*output)[osiz].type = TokenLogicalEqual; ++i; } - ++i; + /* define token FunctionArrow */ + else if (input.data[i + 1] == '>') { + (*output)[osiz].type = TokenFunctionArrow; + ++i; + } } /* define token Plus */ else if (input.data[i] == '+') { @@ -74,7 +83,6 @@ parse(String input, Token **output) (*output)[osiz].type = TokenIncrement; ++i; } - ++i; } /* define token Minus */ else if (input.data[i] == '-') { @@ -90,7 +98,6 @@ parse(String input, Token **output) (*output)[osiz].type = TokenDecrement; ++i; } - ++i; } /* define token Or */ else if (input.data[i] == '|') { @@ -102,7 +109,6 @@ parse(String input, Token **output) (*output)[osiz].type = TokenLogicalOr; ++i; } - ++i; } /* define token And */ else if (input.data[i] == '&') { @@ -114,7 +120,6 @@ parse(String input, Token **output) (*output)[osiz].type = TokenLogicalAnd; ++i; } - ++i; } /* define token Exclamation */ else if (input.data[i] == '!') { @@ -126,7 +131,6 @@ parse(String input, Token **output) (*output)[osiz].type = TokenLogicalNotEqual; ++i; } - ++i; } /* define token Semicolon */ else if (input.data[i] == ';') { @@ -139,7 +143,6 @@ parse(String input, Token **output) while (!(input.data[i] == '\n' || input.data[i] == '\r') && (i < input.len)) ++i; } - ++i; } /* define token OpeningParenthesis */ else if (input.data[i] == '(') { @@ -148,54 +151,45 @@ parse(String input, Token **output) /* define token ClosingParenthesis */ else if (input.data[i] == ')') { (*output)[osiz].type = TokenClosingParenthesis; - ++i; } /* define token OpeningBracket */ else if (input.data[i] == '[') { (*output)[osiz].type = TokenOpeningBracket; - ++i; } /* define token ClosingBracket */ else if (input.data[i] == ']') { (*output)[osiz].type = TokenClosingBracket; - ++i; } /* define token OpeningBrace */ else if (input.data[i] == '{') { (*output)[osiz].type = TokenOpeningBrace; - ++i; } /* define token ClosingBrace */ else if (input.data[i] == '}') { (*output)[osiz].type = TokenClosingBrace; - ++i; } /* define token Colon */ else if (input.data[i] == ':') { (*output)[osiz].type = TokenColon; - ++i; } /* define token Comma */ else if (input.data[i] == ',') { (*output)[osiz].type = TokenComma; - ++i; } /* define token At */ else if (input.data[i] == '@') { (*output)[osiz].type = TokenAt; - ++i; } /* define token Dollar */ else if (input.data[i] == '$') { (*output)[osiz].type = TokenDollar; - ++i; } /* define space */ else if (isspace(input.data[i])) { --osiz; continue; } - (*output)[osiz].value.len = (size_t)((input.data + i) - (*output)[osiz].value.data); + (*output)[osiz].value.len = (size_t)((input.data + i) - (*output)[osiz].value.data) + 1; } return (signed)osiz; } diff --git a/parse.h b/parse.h @@ -8,8 +8,9 @@ typedef enum { TokenNULL, /* Literals */ TokenNumber, TokenIdentifier, TokenCharacter, TokenString, + TokenKeyword, /* Operators */ - TokenAssignment, TokenLogicalEqual, + TokenAssignment, TokenLogicalEqual, TokenFunctionArrow, TokenPlus, TokenPlusEqual, TokenIncrement, TokenMinus, TokenMinusEqual, TokenDecrement, TokenAnd, TokenLogicalAnd, diff --git a/str.c b/str.c @@ -1 +1,23 @@ #include <str.h> + +String +toString(char *s) +{ + return (String){ + .data = s, .len = strlen(s), + }; +} + +int +Strcmp(String s1, String s2) +{ + if (s1.len != s2.len) return -1; + return strncmp(s1.data, s2.data, s1.len); +} + +int +Strcmpc(String s1, char *s2) +{ + if (s1.len != strlen(s2)) return -1; + return strncmp(s1.data, s2, s1.len); +} diff --git a/str.h b/str.h @@ -2,6 +2,7 @@ #define _STR_H #include <sys/types.h> +#include <string.h> #define Array(TYPE) struct { TYPE *data; size_t len; } @@ -10,4 +11,8 @@ typedef struct { size_t len; } String; +String toString(char *s); +int Strcmp(String s1, String s2); +int Strcmpc(String s1, char *s2); + #endif