npc

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

commit cd9f72d1f6617148f007d0521a7436cb689a5f06
parent f3aa4973ba06aca983e959d3154d430c02e1f194
Author: kocotian <kocotian@kocotian.pl>
Date:   Fri,  9 Apr 2021 23:05:33 +0200

parse() now returns amount of tokens, using Array(Token) in npc,
compile beginning

Diffstat:
Mcompile.c | 9+++++++--
Mcompile.h | 31++++++++++++++++++++++++++++++-
Mnpc.c | 8++++----
Mparse.c | 81+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mparse.h | 2+-
5 files changed, 82 insertions(+), 49 deletions(-)

diff --git a/compile.c b/compile.c @@ -1,7 +1,12 @@ +#include <stdio.h> + #include <compile.h> void -compile(Token *tokens) +compile(Token *tokens, size_t n) { - (void)tokens; + size_t i; + for (i = 0; i < n; ++i) + printf("%.*s\n", + (int)tokens[i].value.len, tokens[i].value.data); } diff --git a/compile.h b/compile.h @@ -3,6 +3,35 @@ #include <parse.h> -void compile(Token *tokens); +typedef enum { + ExprNULL, ExprFunction, ExprOperator, ExprKeyword +} ExprType; + +typedef struct { + String name; + String value; +} VariableDef; + +typedef struct { + ExprType type; + Array(void) expr; +} ExprDef; + +typedef struct { + String name; + Array(ExprDef) params; +} FunctionDef; + +typedef struct { + Array(ExprDef) expressions; +} MatchDef; + +typedef struct { + String name; + String value; + Array(MatchDef) match; +} TokenDef; + +void compile(Token *tokens, size_t n); #endif diff --git a/npc.c b/npc.c @@ -13,7 +13,7 @@ main(int argc, char *argv[]) char input_data[640 * 1024]; ssize_t rb; - Token *tokens; + Array(Token) tokens; String input; input.data = input_data; @@ -21,10 +21,10 @@ main(int argc, char *argv[]) die("read:"); input.len = (unsigned)rb; - tokens = parse(input); - compile(tokens); + tokens.len = (unsigned)parse(input, &(tokens.data)); + compile(tokens.data, tokens.len); - free(tokens); + free(tokens.data); return 0; } diff --git a/parse.c b/parse.c @@ -4,31 +4,30 @@ #include <parse.h> #include <unistd.h> -Token * -parse(String input) +ssize_t +parse(String input, Token **output) { size_t i; ssize_t osiz; - Token *output; - output = malloc((unsigned)(osiz = -1) + 1); + (*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; + (*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; + (*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; + (*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')) @@ -36,14 +35,14 @@ parse(String input) } /* 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; + (*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; + (*output)[osiz].type = TokenString; ++i; while (!(input.data[i] == '"') && (i < input.len)) ++i; ++i; @@ -51,92 +50,92 @@ parse(String input) /* == Operators == */ /* define token Assignment */ else if (input.data[i] == '=') { - output[osiz].type = TokenAssignment; + (*output)[osiz].type = TokenAssignment; /* begin definitions -> then */ if (0); /* define token LogicalEqual */ else if (input.data[i + 1] == '=') { - output[osiz].type = TokenLogicalEqual; + (*output)[osiz].type = TokenLogicalEqual; ++i; } ++i; } /* define token Plus */ else if (input.data[i] == '+') { - output[osiz].type = TokenPlus; + (*output)[osiz].type = TokenPlus; /* begin definitions -> then */ if (0); /* define token LogicalEqual */ else if (input.data[i + 1] == '=') { - output[osiz].type = TokenPlusEqual; + (*output)[osiz].type = TokenPlusEqual; ++i; } else if (input.data[i + 1] == '+') { - output[osiz].type = TokenIncrement; + (*output)[osiz].type = TokenIncrement; ++i; } ++i; } /* define token Minus */ else if (input.data[i] == '-') { - output[osiz].type = TokenMinus; + (*output)[osiz].type = TokenMinus; /* begin definitions -> then */ if (0); /* define token LogicalEqual */ else if (input.data[i + 1] == '=') { - output[osiz].type = TokenMinusEqual; + (*output)[osiz].type = TokenMinusEqual; ++i; } else if (input.data[i + 1] == '-') { - output[osiz].type = TokenDecrement; + (*output)[osiz].type = TokenDecrement; ++i; } ++i; } /* define token Or */ else if (input.data[i] == '|') { - output[osiz].type = TokenOr; + (*output)[osiz].type = TokenOr; /* begin definitions -> then */ if (0); /* define token LogicalEqual */ else if (input.data[i + 1] == '|') { - output[osiz].type = TokenLogicalOr; + (*output)[osiz].type = TokenLogicalOr; ++i; } ++i; } /* define token And */ else if (input.data[i] == '&') { - output[osiz].type = TokenAnd; + (*output)[osiz].type = TokenAnd; /* begin definitions -> then */ if (0); /* define token LogicalEqual */ else if (input.data[i + 1] == '&') { - output[osiz].type = TokenLogicalAnd; + (*output)[osiz].type = TokenLogicalAnd; ++i; } ++i; } /* define token Exclamation */ else if (input.data[i] == '!') { - output[osiz].type = TokenExclamation; + (*output)[osiz].type = TokenExclamation; /* begin definitions -> then */ if (0); /* define token LogicalNotEqual */ else if (input.data[i + 1] == '=') { - output[osiz].type = TokenLogicalNotEqual; + (*output)[osiz].type = TokenLogicalNotEqual; ++i; } ++i; } /* define token Semicolon */ else if (input.data[i] == ';') { - output[osiz].type = TokenSemicolon; + (*output)[osiz].type = TokenSemicolon; /* begin definitions -> then */ if (0); /* define token Comment */ else if (input.data[i + 1] == ';') { - output[osiz].type = TokenComment; + (*output)[osiz].type = TokenComment; while (!(input.data[i] == '\n' || input.data[i] == '\r') && (i < input.len)) ++i; } @@ -144,51 +143,51 @@ parse(String input) } /* define token OpeningParenthesis */ else if (input.data[i] == '(') { - output[osiz].type = TokenOpeningParenthesis; + (*output)[osiz].type = TokenOpeningParenthesis; } /* define token ClosingParenthesis */ else if (input.data[i] == ')') { - output[osiz].type = TokenClosingParenthesis; + (*output)[osiz].type = TokenClosingParenthesis; ++i; } /* define token OpeningBracket */ else if (input.data[i] == '[') { - output[osiz].type = TokenOpeningBracket; + (*output)[osiz].type = TokenOpeningBracket; ++i; } /* define token ClosingBracket */ else if (input.data[i] == ']') { - output[osiz].type = TokenClosingBracket; + (*output)[osiz].type = TokenClosingBracket; ++i; } /* define token OpeningBrace */ else if (input.data[i] == '{') { - output[osiz].type = TokenOpeningBrace; + (*output)[osiz].type = TokenOpeningBrace; ++i; } /* define token ClosingBrace */ else if (input.data[i] == '}') { - output[osiz].type = TokenClosingBrace; + (*output)[osiz].type = TokenClosingBrace; ++i; } /* define token Colon */ else if (input.data[i] == ':') { - output[osiz].type = TokenColon; + (*output)[osiz].type = TokenColon; ++i; } /* define token Comma */ else if (input.data[i] == ',') { - output[osiz].type = TokenComma; + (*output)[osiz].type = TokenComma; ++i; } /* define token At */ else if (input.data[i] == '@') { - output[osiz].type = TokenAt; + (*output)[osiz].type = TokenAt; ++i; } /* define token Dollar */ else if (input.data[i] == '$') { - output[osiz].type = TokenDollar; + (*output)[osiz].type = TokenDollar; ++i; } /* define space */ @@ -196,7 +195,7 @@ parse(String input) --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); } - return output; + return (signed)osiz; } diff --git a/parse.h b/parse.h @@ -29,6 +29,6 @@ typedef struct { String value; } Token; -Token *parse(String input); +ssize_t parse(String input, Token **output); #endif