forked from NeoFlock/noom
lexer + parser: add formatting for enums + build script: spaces -> tabs
This commit is contained in:
22
src/lexer.c
22
src/lexer.c
@@ -106,6 +106,28 @@ int noomL_iskeyword(const char* s, noom_uint_t len) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *noomL_formatTokenType(noomL_TokenType token_type) {
|
||||||
|
switch (token_type) {
|
||||||
|
case NOOML_TOKEN_EOF:
|
||||||
|
return "EOF";
|
||||||
|
case NOOML_TOKEN_KEYWORD:
|
||||||
|
return "keyword";
|
||||||
|
case NOOML_TOKEN_WHITESPACE:
|
||||||
|
return "whitespace";
|
||||||
|
case NOOML_TOKEN_IDENTIFIER:
|
||||||
|
return "identifier";
|
||||||
|
case NOOML_TOKEN_STRING:
|
||||||
|
return "string";
|
||||||
|
case NOOML_TOKEN_SYMBOL:
|
||||||
|
return "symbol";
|
||||||
|
case NOOML_TOKEN_NUMBER:
|
||||||
|
return "number";
|
||||||
|
default:
|
||||||
|
return "unknown";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token) {
|
noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token) {
|
||||||
const char* str = s + start;
|
const char* str = s + start;
|
||||||
|
|
||||||
|
|||||||
@@ -32,5 +32,7 @@ int noomL_iswhitespace(char c);
|
|||||||
noom_uint_t noomL_getsymbol(const char* s);
|
noom_uint_t noomL_getsymbol(const char* s);
|
||||||
noom_uint_t noomL_getnumber(const char* s);
|
noom_uint_t noomL_getnumber(const char* s);
|
||||||
|
|
||||||
|
const char *noomL_formatTokenType(noomL_TokenType token_type);
|
||||||
|
|
||||||
noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token); // TODO: add more error data
|
noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token); // TODO: add more error data
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ void print_node(noomP_Node* node, noom_uint_t depth) {
|
|||||||
printf("{\n");
|
printf("{\n");
|
||||||
|
|
||||||
tab(depth+1);
|
tab(depth+1);
|
||||||
printf("type: %d\n", node->type);
|
printf("type: %s\n", noomP_formatNodeType(node->type));
|
||||||
|
|
||||||
tab(depth+1);
|
tab(depth+1);
|
||||||
printf("location: %lld\n", node->source_offset);
|
printf("location: %lld\n", node->source_offset);
|
||||||
@@ -42,7 +42,7 @@ int main(int argc, char** argv) {
|
|||||||
while (1) {
|
while (1) {
|
||||||
noomL_lex(code, pos, &token);
|
noomL_lex(code, pos, &token);
|
||||||
|
|
||||||
printf("%d ", token.type);
|
printf("%s ", noomL_formatTokenType(token.type));
|
||||||
for (noom_uint_t i = 0; i < token.length; i++) putchar((code + token.offset)[i]);
|
for (noom_uint_t i = 0; i < token.length; i++) putchar((code + token.offset)[i]);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
|
||||||
|
|||||||
33
src/parser.c
33
src/parser.c
@@ -1,6 +1,39 @@
|
|||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
|
|
||||||
|
const char *noomP_formatNodeType(noomP_NodeType node_type) {
|
||||||
|
switch (node_type) {
|
||||||
|
case NOOMP_NODE_PROGRAM:
|
||||||
|
return "program";
|
||||||
|
case NOOMP_NODE_VARNAME:
|
||||||
|
return "varname";
|
||||||
|
case NOOMP_NODE_LOCALDECLARATION:
|
||||||
|
return "local declaration";
|
||||||
|
case NOOMP_NODE_IFSTATEMENT:
|
||||||
|
return "if statement";
|
||||||
|
case NOOMP_NODE_WHILELOOP:
|
||||||
|
return "while loop";
|
||||||
|
case NOOMP_NODE_BLOCK:
|
||||||
|
return "block";
|
||||||
|
case NOOMP_NODE_BREAK:
|
||||||
|
return "break";
|
||||||
|
case NOOMP_NODE_VARIABLE:
|
||||||
|
return "variable";
|
||||||
|
case NOOMP_NODE_NUMBERLITERAL:
|
||||||
|
return "number literal";
|
||||||
|
case NOOMP_NODE_BOOLEANLITERAL:
|
||||||
|
return "boolean literal";
|
||||||
|
case NOOMP_NODE_NILLITERAL:
|
||||||
|
return "nil literal";
|
||||||
|
case NOOMP_NODE_UNARYOPERATOR:
|
||||||
|
return "unary operator";
|
||||||
|
case NOOMP_NODE_BINARYOPERATOR:
|
||||||
|
return "binary operator";
|
||||||
|
default:
|
||||||
|
return "unknown";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int noomP_peek(noomP_Parser* parser, noomL_Token* token) {
|
int noomP_peek(noomP_Parser* parser, noomL_Token* token) {
|
||||||
while (1) {
|
while (1) {
|
||||||
int success = noomL_lex(parser->code, parser->lex_offset, token);
|
int success = noomL_lex(parser->code, parser->lex_offset, token);
|
||||||
|
|||||||
@@ -19,7 +19,9 @@ typedef enum noomP_NodeType {
|
|||||||
NOOMP_NODE_NILLITERAL,
|
NOOMP_NODE_NILLITERAL,
|
||||||
|
|
||||||
NOOMP_NODE_UNARYOPERATOR,
|
NOOMP_NODE_UNARYOPERATOR,
|
||||||
NOOMP_NODE_BINARYOPERATOR
|
NOOMP_NODE_BINARYOPERATOR,
|
||||||
|
|
||||||
|
NOOMP_NODE_NCOUNT,
|
||||||
} noomP_NodeType;
|
} noomP_NodeType;
|
||||||
|
|
||||||
typedef struct noomP_Node {
|
typedef struct noomP_Node {
|
||||||
@@ -42,6 +44,8 @@ typedef struct noomP_Parser { // todo: track location in code with line/column?
|
|||||||
noomP_Node* last_node;
|
noomP_Node* last_node;
|
||||||
} noomP_Parser;
|
} noomP_Parser;
|
||||||
|
|
||||||
|
const char *noomP_formatNodeType(noomP_NodeType node_type);
|
||||||
|
|
||||||
int noomP_peek(noomP_Parser* parser, noomL_Token* token);
|
int noomP_peek(noomP_Parser* parser, noomL_Token* token);
|
||||||
void noomP_skip(noomP_Parser* parser, noomL_Token* token);
|
void noomP_skip(noomP_Parser* parser, noomL_Token* token);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user