npc

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

commit 3c88fb39f47b89eb18d562916cbac1fda7e30880
parent f35994746aca26b413f463de36253eebe95085ee
Author: kocotian <kocotian@kocotian.pl>
Date:   Fri,  9 Apr 2021 21:08:01 +0200

Initial code

Diffstat:
Anpc.c | 270+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 270 insertions(+), 0 deletions(-)

diff --git a/npc.c b/npc.c @@ -0,0 +1,270 @@ +#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; +} + +int +main(int argc, char *argv[]) +{ + (void)argc; (void)argv; + + char input_data[640 * 1024]; + ssize_t rb; + Token *tokens; + String input; + + input.data = input_data; + if ((rb = read(0, input_data, 640 * 1024)) < 0) { + perror("read"); + exit(1); + } + input.len = (unsigned)rb; + + tokens = parse(input); + compile(tokens); + + free(tokens); + + return 0; +}