npc

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

commit e3b21e0397697b76c65418847f3e2c552886904b
parent 3c88fb39f47b89eb18d562916cbac1fda7e30880
Author: kocotian <kocotian@kocotian.pl>
Date:   Fri,  9 Apr 2021 21:20:54 +0200

Splitted this shit to more files

Diffstat:
Acompile.c | 7+++++++
Acompile.h | 8++++++++
Mnpc.c | 243+------------------------------------------------------------------------------
Aparse.c | 202+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aparse.h | 34++++++++++++++++++++++++++++++++++
Astr.c | 1+
Astr.h | 11+++++++++++
7 files changed, 265 insertions(+), 241 deletions(-)

diff --git a/compile.c b/compile.c @@ -0,0 +1,7 @@ +#include <compile.h> + +void +compile(Token *tokens) +{ + (void)tokens; +} diff --git a/compile.h b/compile.h @@ -0,0 +1,8 @@ +#ifndef _COMPILE_H +#define _COMPILE_H + +#include <parse.h> + +void compile(Token *tokens); + +#endif diff --git a/npc.c b/npc.c @@ -1,248 +1,9 @@ -#include <ctype.h> #include <stdio.h> #include <stdlib.h> -#include <sys/types.h> #include <unistd.h> -typedef struct { - char *data; - size_t len; -} String; - -typedef enum { - /* NULL Token */ - TokenNULL, - /* Literals */ - TokenNumber, TokenIdentifier, TokenCharacter, TokenString, - /* Operators */ - TokenAssignment, TokenLogicalEqual, - TokenPlus, TokenPlusEqual, TokenIncrement, - TokenMinus, TokenMinusEqual, TokenDecrement, - TokenAnd, TokenLogicalAnd, - TokenOr, TokenLogicalOr, - TokenExclamation, TokenLogicalNotEqual, - TokenComment, - /* TokenBrackets */ - TokenOpeningParenthesis, TokenClosingParenthesis, - TokenOpeningBracket, TokenClosingBracket, - TokenOpeningBrace, TokenClosingBrace, - /* TokenOther tokens */ - TokenSemicolon, TokenColon, TokenComma, TokenAt, TokenDollar, -} TokenType; - -typedef struct { - TokenType type; - String value; -} Token; - -static Token *parse(String input); -static void compile(Token *tokens); - -static Token * -parse(String input) -{ - size_t i; - ssize_t osiz; - Token *output; - - output = malloc((unsigned)(osiz = -1) + 1); - for (i = 0; i < input.len; ++i) { - output = realloc(output, (unsigned)(++osiz + 1) * (sizeof *output)); - output[osiz].value.data = input.data + i; - output[osiz].type = TokenNULL; - /* begin definitions */ - if (0); - /* == Literals == */ - /* define token Number */ - else if ((input.data[i] >= '0' && input.data[i] <= '9')) { - output[osiz].type = TokenNumber; - while (((input.data[i] >= '0' && input.data[i] <= '9')) - && (i < input.len)) ++i; - } - /* define token Identifier */ - else if ((input.data[i] >= 'a' && input.data[i] <= 'z') - || (input.data[i] >= 'A' && input.data[i] <= 'Z')) { - output[osiz].type = TokenIdentifier; - while (((input.data[i] >= 'a' && input.data[i] <= 'z') - || (input.data[i] >= 'A' && input.data[i] <= 'Z') - || (input.data[i] >= '0' && input.data[i] <= '9')) - && (i < input.len)) ++i; - } - /* define token Character */ - else if (input.data[i] == '\'') { - output[osiz].type = TokenCharacter; - if (!input.data[++i] && (i < input.len)) return NULL; - if (!(input.data[++i] == '\'') && (i < input.len)) return NULL; - ++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 */ - else if (input.data[i] == '=') { - output[osiz].type = TokenAssignment; - /* begin definitions -> then */ - if (0); - /* define token LogicalEqual */ - else if (input.data[i + 1] == '=') { - output[osiz].type = TokenLogicalEqual; - ++i; - } - ++i; - } - /* define token Plus */ - else if (input.data[i] == '+') { - output[osiz].type = TokenPlus; - /* begin definitions -> then */ - if (0); - /* define token LogicalEqual */ - else if (input.data[i + 1] == '=') { - output[osiz].type = TokenPlusEqual; - ++i; - } - else if (input.data[i + 1] == '+') { - output[osiz].type = TokenIncrement; - ++i; - } - ++i; - } - /* define token Minus */ - else if (input.data[i] == '-') { - output[osiz].type = TokenMinus; - /* begin definitions -> then */ - if (0); - /* define token LogicalEqual */ - else if (input.data[i + 1] == '=') { - output[osiz].type = TokenMinusEqual; - ++i; - } - else if (input.data[i + 1] == '-') { - output[osiz].type = TokenDecrement; - ++i; - } - ++i; - } - /* define token Or */ - else if (input.data[i] == '|') { - output[osiz].type = TokenOr; - /* begin definitions -> then */ - if (0); - /* define token LogicalEqual */ - else if (input.data[i + 1] == '|') { - output[osiz].type = TokenLogicalOr; - ++i; - } - ++i; - } - /* define token And */ - else if (input.data[i] == '&') { - output[osiz].type = TokenAnd; - /* begin definitions -> then */ - if (0); - /* define token LogicalEqual */ - else if (input.data[i + 1] == '&') { - output[osiz].type = TokenLogicalAnd; - ++i; - } - ++i; - } - /* define token Exclamation */ - else if (input.data[i] == '!') { - output[osiz].type = TokenExclamation; - /* begin definitions -> then */ - if (0); - /* define token LogicalNotEqual */ - else if (input.data[i + 1] == '=') { - output[osiz].type = TokenLogicalNotEqual; - ++i; - } - ++i; - } - /* define token Semicolon */ - else if (input.data[i] == ';') { - output[osiz].type = TokenSemicolon; - /* begin definitions -> then */ - if (0); - /* define token Comment */ - else if (input.data[i + 1] == ';') { - output[osiz].type = TokenComment; - while (!(input.data[i] == '\n' - || input.data[i] == '\r') && (i < input.len)) ++i; - } - ++i; - } - /* define token OpeningParenthesis */ - else if (input.data[i] == '(') { - output[osiz].type = TokenOpeningParenthesis; - } - /* 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); - printf("%.*s ", - (int)output[osiz].value.len, - output[osiz].value.data); - } - return output; -} - -static void -compile(Token *tokens) -{ - (void)tokens; -} +#include <compile.h> +#include <parse.h> int main(int argc, char *argv[]) diff --git a/parse.c b/parse.c @@ -0,0 +1,202 @@ +#include <ctype.h> +#include <stdlib.h> +#include <sys/types.h> +#include <parse.h> +#include <unistd.h> + +Token * +parse(String input) +{ + size_t i; + ssize_t osiz; + Token *output; + + output = malloc((unsigned)(osiz = -1) + 1); + for (i = 0; i < input.len; ++i) { + output = realloc(output, (unsigned)(++osiz + 1) * (sizeof *output)); + output[osiz].value.data = input.data + i; + output[osiz].type = TokenNULL; + /* begin definitions */ + if (0); + /* == Literals == */ + /* define token Number */ + else if ((input.data[i] >= '0' && input.data[i] <= '9')) { + output[osiz].type = TokenNumber; + while (((input.data[i] >= '0' && input.data[i] <= '9')) + && (i < input.len)) ++i; + } + /* define token Identifier */ + else if ((input.data[i] >= 'a' && input.data[i] <= 'z') + || (input.data[i] >= 'A' && input.data[i] <= 'Z')) { + output[osiz].type = TokenIdentifier; + while (((input.data[i] >= 'a' && input.data[i] <= 'z') + || (input.data[i] >= 'A' && input.data[i] <= 'Z') + || (input.data[i] >= '0' && input.data[i] <= '9')) + && (i < input.len)) ++i; + } + /* define token Character */ + else if (input.data[i] == '\'') { + output[osiz].type = TokenCharacter; + if (!input.data[++i] && (i < input.len)) return NULL; + if (!(input.data[++i] == '\'') && (i < input.len)) return NULL; + ++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 */ + else if (input.data[i] == '=') { + output[osiz].type = TokenAssignment; + /* begin definitions -> then */ + if (0); + /* define token LogicalEqual */ + else if (input.data[i + 1] == '=') { + output[osiz].type = TokenLogicalEqual; + ++i; + } + ++i; + } + /* define token Plus */ + else if (input.data[i] == '+') { + output[osiz].type = TokenPlus; + /* begin definitions -> then */ + if (0); + /* define token LogicalEqual */ + else if (input.data[i + 1] == '=') { + output[osiz].type = TokenPlusEqual; + ++i; + } + else if (input.data[i + 1] == '+') { + output[osiz].type = TokenIncrement; + ++i; + } + ++i; + } + /* define token Minus */ + else if (input.data[i] == '-') { + output[osiz].type = TokenMinus; + /* begin definitions -> then */ + if (0); + /* define token LogicalEqual */ + else if (input.data[i + 1] == '=') { + output[osiz].type = TokenMinusEqual; + ++i; + } + else if (input.data[i + 1] == '-') { + output[osiz].type = TokenDecrement; + ++i; + } + ++i; + } + /* define token Or */ + else if (input.data[i] == '|') { + output[osiz].type = TokenOr; + /* begin definitions -> then */ + if (0); + /* define token LogicalEqual */ + else if (input.data[i + 1] == '|') { + output[osiz].type = TokenLogicalOr; + ++i; + } + ++i; + } + /* define token And */ + else if (input.data[i] == '&') { + output[osiz].type = TokenAnd; + /* begin definitions -> then */ + if (0); + /* define token LogicalEqual */ + else if (input.data[i + 1] == '&') { + output[osiz].type = TokenLogicalAnd; + ++i; + } + ++i; + } + /* define token Exclamation */ + else if (input.data[i] == '!') { + output[osiz].type = TokenExclamation; + /* begin definitions -> then */ + if (0); + /* define token LogicalNotEqual */ + else if (input.data[i + 1] == '=') { + output[osiz].type = TokenLogicalNotEqual; + ++i; + } + ++i; + } + /* define token Semicolon */ + else if (input.data[i] == ';') { + output[osiz].type = TokenSemicolon; + /* begin definitions -> then */ + if (0); + /* define token Comment */ + else if (input.data[i + 1] == ';') { + output[osiz].type = TokenComment; + while (!(input.data[i] == '\n' + || input.data[i] == '\r') && (i < input.len)) ++i; + } + ++i; + } + /* define token OpeningParenthesis */ + else if (input.data[i] == '(') { + output[osiz].type = TokenOpeningParenthesis; + } + /* 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); + } + return output; +} diff --git a/parse.h b/parse.h @@ -0,0 +1,34 @@ +#ifndef _PARSE_H +#define _PARSE_H + +#include <str.h> + +typedef enum { + /* NULL Token */ + TokenNULL, + /* Literals */ + TokenNumber, TokenIdentifier, TokenCharacter, TokenString, + /* Operators */ + TokenAssignment, TokenLogicalEqual, + TokenPlus, TokenPlusEqual, TokenIncrement, + TokenMinus, TokenMinusEqual, TokenDecrement, + TokenAnd, TokenLogicalAnd, + TokenOr, TokenLogicalOr, + TokenExclamation, TokenLogicalNotEqual, + TokenComment, + /* TokenBrackets */ + TokenOpeningParenthesis, TokenClosingParenthesis, + TokenOpeningBracket, TokenClosingBracket, + TokenOpeningBrace, TokenClosingBrace, + /* TokenOther tokens */ + TokenSemicolon, TokenColon, TokenComma, TokenAt, TokenDollar, +} TokenType; + +typedef struct { + TokenType type; + String value; +} Token; + +Token *parse(String input); + +#endif diff --git a/str.c b/str.c @@ -0,0 +1 @@ +#include <str.h> diff --git a/str.h b/str.h @@ -0,0 +1,11 @@ +#ifndef _STR_H +#define _STR_H + +#include <sys/types.h> + +typedef struct { + char *data; + size_t len; +} String; + +#endif