Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7744a28a12 | ||
|
|
1d7cc88c22 |
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
Language: Cpp
|
||||||
|
BasedOnStyle: LLVM
|
||||||
|
BreakBeforeBraces: Allman
|
||||||
|
IndentWidth: 2
|
||||||
|
TabWidth: 2
|
||||||
|
UseTab: Never
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
!/src
|
!/src
|
||||||
|
|
||||||
# Files
|
# Files
|
||||||
|
!/.clang-format
|
||||||
!/.gitignore
|
!/.gitignore
|
||||||
!/CMakeLists.txt
|
!/CMakeLists.txt
|
||||||
!/Makefile
|
!/Makefile
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 4.3)
|
|||||||
project(fz-lang
|
project(fz-lang
|
||||||
LANGUAGES C)
|
LANGUAGES C)
|
||||||
|
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
include_directories(include)
|
include_directories(include)
|
||||||
|
|
||||||
# Compiler
|
# Compiler
|
||||||
|
|||||||
@@ -7,4 +7,3 @@
|
|||||||
void fzl_debug_print_ast(fzl_node_t *root);
|
void fzl_debug_print_ast(fzl_node_t *root);
|
||||||
|
|
||||||
#endif // FZL_DEBUG_H
|
#endif // FZL_DEBUG_H
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +1,30 @@
|
|||||||
#ifndef FZL_PARSER_H
|
#ifndef FZL_PARSER_H
|
||||||
#define FZL_PARSER_H
|
#define FZL_PARSER_H
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <compiler/fzl_token.h>
|
|
||||||
#include <compiler/fzl_reader.h>
|
#include <compiler/fzl_reader.h>
|
||||||
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
// AST node type
|
// AST node type
|
||||||
typedef enum {
|
typedef enum
|
||||||
|
{
|
||||||
FZL_NODETYPE_BLOCK,
|
FZL_NODETYPE_BLOCK,
|
||||||
FZL_NODETYPE_CALL,
|
FZL_NODETYPE_CALL,
|
||||||
FZL_NODETYPE_LITERAL,
|
FZL_NODETYPE_LITERAL,
|
||||||
} fzl_nodetype_t;
|
} fzl_nodetype_t;
|
||||||
|
|
||||||
// AST node descriptor
|
// AST node descriptor
|
||||||
typedef struct fzl_node {
|
typedef struct fzl_node
|
||||||
|
{
|
||||||
fzl_nodetype_t type;
|
fzl_nodetype_t type;
|
||||||
fzl_token_t *token;
|
fzl_token_t *token;
|
||||||
struct fzl_node *parent;
|
struct fzl_node *parent;
|
||||||
struct fzl_node *next;
|
struct fzl_node *next;
|
||||||
|
|
||||||
union {
|
union
|
||||||
struct {
|
{
|
||||||
|
struct
|
||||||
|
{
|
||||||
struct fzl_node **entries;
|
struct fzl_node **entries;
|
||||||
size_t entry_count;
|
size_t entry_count;
|
||||||
} list;
|
} list;
|
||||||
@@ -28,7 +32,8 @@ typedef struct fzl_node {
|
|||||||
} fzl_node_t;
|
} fzl_node_t;
|
||||||
|
|
||||||
// Token sequence parser descriptor
|
// Token sequence parser descriptor
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
fzl_reader_ll_t *first;
|
fzl_reader_ll_t *first;
|
||||||
size_t token_count;
|
size_t token_count;
|
||||||
|
|
||||||
@@ -55,4 +60,3 @@ void fzl_freenode(fzl_node_t* node);
|
|||||||
void fzl_freeparser(fzl_parser_t *parser);
|
void fzl_freeparser(fzl_parser_t *parser);
|
||||||
|
|
||||||
#endif // FZL_PARSER_H
|
#endif // FZL_PARSER_H
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,21 @@
|
|||||||
#ifndef FZL_READER_H
|
#ifndef FZL_READER_H
|
||||||
#define FZL_READER_H
|
#define FZL_READER_H
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <compiler/fzl_token.h>
|
#include <compiler/fzl_token.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
// Tokens linked list descriptor
|
// Tokens linked list descriptor
|
||||||
typedef struct fzl_reader_ll {
|
typedef struct fzl_reader_ll
|
||||||
|
{
|
||||||
fzl_token_t *token;
|
fzl_token_t *token;
|
||||||
struct fzl_reader_ll *next;
|
struct fzl_reader_ll *next;
|
||||||
} fzl_reader_ll_t;
|
} fzl_reader_ll_t;
|
||||||
|
|
||||||
// Source reader description
|
// Source reader description
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
fzl_reader_ll_t *first;
|
fzl_reader_ll_t *first;
|
||||||
fzl_reader_ll_t *last;
|
fzl_reader_ll_t *last;
|
||||||
|
|
||||||
@@ -35,4 +37,3 @@ char *fzl_reader_geterror();
|
|||||||
void fzl_freereader(fzl_reader_t *reader);
|
void fzl_freereader(fzl_reader_t *reader);
|
||||||
|
|
||||||
#endif // FZL_READER_H
|
#endif // FZL_READER_H
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
#ifndef FZL_TOKEN_H
|
#ifndef FZL_TOKEN_H
|
||||||
#define FZL_TOKEN_H
|
#define FZL_TOKEN_H
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
// Token type
|
// Token type
|
||||||
typedef enum {
|
typedef enum
|
||||||
|
{
|
||||||
FZL_TOKTYPE_NAME,
|
FZL_TOKTYPE_NAME,
|
||||||
FZL_TOKTYPE_LPAIR,
|
FZL_TOKTYPE_LPAIR,
|
||||||
FZL_TOKTYPE_RPAIR,
|
FZL_TOKTYPE_RPAIR,
|
||||||
@@ -15,7 +16,8 @@ typedef enum {
|
|||||||
} fzl_toktype_t;
|
} fzl_toktype_t;
|
||||||
|
|
||||||
// Token descriptor
|
// Token descriptor
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
fzl_toktype_t type;
|
fzl_toktype_t type;
|
||||||
uint16_t i0;
|
uint16_t i0;
|
||||||
uint16_t i1;
|
uint16_t i1;
|
||||||
@@ -25,12 +27,10 @@ typedef struct {
|
|||||||
} fzl_token_t;
|
} fzl_token_t;
|
||||||
|
|
||||||
// Allocate new token
|
// Allocate new token
|
||||||
fzl_token_t *fzl_mktoken(
|
fzl_token_t *fzl_mktoken(const fzl_toktype_t type, uint16_t from, uint16_t to,
|
||||||
const fzl_toktype_t type, uint16_t from, uint16_t to,
|
|
||||||
void *value, size_t value_sz);
|
void *value, size_t value_sz);
|
||||||
|
|
||||||
// Free token and its contents
|
// Free token and its contents
|
||||||
void fzl_freetoken(fzl_token_t *token);
|
void fzl_freetoken(fzl_token_t *token);
|
||||||
|
|
||||||
#endif // FZL_TOKEN_H
|
#endif // FZL_TOKEN_H
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include <compiler/fzl_debug.h>
|
#include <compiler/fzl_debug.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
size_t print_level(char *buf, int level)
|
size_t print_level(char *buf, int level)
|
||||||
@@ -23,7 +23,8 @@ void print_node(fzl_node_t *node, int level)
|
|||||||
memset(levelbuf, 0, sizeof(levelbuf));
|
memset(levelbuf, 0, sizeof(levelbuf));
|
||||||
memset(printbuf, 0, sizeof(printbuf));
|
memset(printbuf, 0, sizeof(printbuf));
|
||||||
|
|
||||||
if (!node) return;
|
if (!node)
|
||||||
|
return;
|
||||||
|
|
||||||
size_t lvllen = print_level(levelbuf, level);
|
size_t lvllen = print_level(levelbuf, level);
|
||||||
strcpy(printbuf, levelbuf);
|
strcpy(printbuf, levelbuf);
|
||||||
@@ -33,17 +34,18 @@ void print_node(fzl_node_t *node, int level)
|
|||||||
case FZL_NODETYPE_CALL:
|
case FZL_NODETYPE_CALL:
|
||||||
if (node->list.entry_count > 0)
|
if (node->list.entry_count > 0)
|
||||||
{
|
{
|
||||||
printf("%s Call fn '%s' with args:\n", levelbuf, (char*)node->token->value);
|
printf("%s Call fn '%s' with args:\n", levelbuf,
|
||||||
|
(char *)node->token->value);
|
||||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||||
print_node(node->list.entries[i], level + 1);
|
print_node(node->list.entries[i], level + 1);
|
||||||
}
|
}
|
||||||
else printf("%s Call fn '%s' w/o args\n", levelbuf, (char*)node->token->value);
|
else
|
||||||
|
printf("%s Call fn '%s' w/o args\n", levelbuf,
|
||||||
|
(char *)node->token->value);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FZL_NODETYPE_LITERAL:
|
case FZL_NODETYPE_LITERAL:
|
||||||
printf("%s Literal '%s'\n",
|
printf("%s Literal '%s'\n", levelbuf, (char *)node->token->value);
|
||||||
levelbuf,
|
|
||||||
(char*)node->token->value);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FZL_NODETYPE_BLOCK:
|
case FZL_NODETYPE_BLOCK:
|
||||||
@@ -79,4 +81,3 @@ void fzl_debug_print_ast(fzl_node_t *root)
|
|||||||
|
|
||||||
printf("-> FINISH\n");
|
printf("-> FINISH\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+35
-26
@@ -1,6 +1,6 @@
|
|||||||
#include <compiler/fzl_parser.h>
|
#include <compiler/fzl_parser.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
char last_err[256];
|
char last_err[256];
|
||||||
|
|
||||||
@@ -13,11 +13,13 @@ fzl_parser_t *fzl_mkparser(fzl_reader_ll_t *first)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fzl_parser_t *parser = malloc(sizeof(fzl_parser_t));
|
fzl_parser_t *parser = malloc(sizeof(fzl_parser_t));
|
||||||
if (!parser) return NULL;
|
if (!parser)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
parser->root = NULL;
|
parser->root = NULL;
|
||||||
parser->cursor = first->next;
|
parser->cursor = first->next;
|
||||||
if (!parser->cursor) return NULL;
|
if (!parser->cursor)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return parser;
|
return parser;
|
||||||
}
|
}
|
||||||
@@ -29,7 +31,8 @@ fzl_node_t *fzl_mknode(fzl_nodetype_t type, fzl_token_t *token)
|
|||||||
{
|
{
|
||||||
fzl_node_t *node = malloc(sizeof(fzl_node_t));
|
fzl_node_t *node = malloc(sizeof(fzl_node_t));
|
||||||
|
|
||||||
if (!node) return NULL;
|
if (!node)
|
||||||
|
return NULL;
|
||||||
node->type = type;
|
node->type = type;
|
||||||
node->token = token;
|
node->token = token;
|
||||||
node->parent = NULL;
|
node->parent = NULL;
|
||||||
@@ -41,17 +44,20 @@ fzl_node_t *fzl_mknode(fzl_nodetype_t type, fzl_token_t *token)
|
|||||||
// Get token at cursor
|
// Get token at cursor
|
||||||
static fzl_token_t *peek(fzl_parser_t *parser)
|
static fzl_token_t *peek(fzl_parser_t *parser)
|
||||||
{
|
{
|
||||||
if (!parser->cursor) return NULL;
|
if (!parser->cursor)
|
||||||
|
return NULL;
|
||||||
return parser->cursor->token;
|
return parser->cursor->token;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get token at (cursor + 1)
|
// Get token at (cursor + 1)
|
||||||
static fzl_token_t *next(fzl_parser_t *parser)
|
static fzl_token_t *next(fzl_parser_t *parser)
|
||||||
{
|
{
|
||||||
if (!parser->cursor) return NULL;
|
if (!parser->cursor)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
fzl_reader_ll_t *next = parser->cursor->next;
|
fzl_reader_ll_t *next = parser->cursor->next;
|
||||||
if (!next) return NULL;
|
if (!next)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return next->token;
|
return next->token;
|
||||||
}
|
}
|
||||||
@@ -59,7 +65,8 @@ static fzl_token_t *next(fzl_parser_t *parser)
|
|||||||
// Consume token at cursor and move to the next
|
// Consume token at cursor and move to the next
|
||||||
static fzl_token_t *consume(fzl_parser_t *parser)
|
static fzl_token_t *consume(fzl_parser_t *parser)
|
||||||
{
|
{
|
||||||
if (!parser->cursor) return NULL;
|
if (!parser->cursor)
|
||||||
|
return NULL;
|
||||||
fzl_token_t *tok = parser->cursor->token;
|
fzl_token_t *tok = parser->cursor->token;
|
||||||
parser->cursor = parser->cursor->next;
|
parser->cursor = parser->cursor->next;
|
||||||
return tok;
|
return tok;
|
||||||
@@ -74,8 +81,8 @@ fzl_node_t *parse_primary(fzl_parser_t *parser)
|
|||||||
return fzl_mknode(FZL_NODETYPE_LITERAL, tok);
|
return fzl_mknode(FZL_NODETYPE_LITERAL, tok);
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(last_err, sizeof(last_err),
|
snprintf(last_err, sizeof(last_err), "failed to parse primary at %d",
|
||||||
"failed to parse primary at %d", peek(parser)->i0);
|
peek(parser)->i0);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,12 +110,14 @@ fzl_node_t *parse_call(fzl_parser_t *parser)
|
|||||||
while (peek(parser) && peek(parser)->type != FZL_TOKTYPE_RPAIR)
|
while (peek(parser) && peek(parser)->type != FZL_TOKTYPE_RPAIR)
|
||||||
{
|
{
|
||||||
fzl_node_t *arg = parse_expression(parser, 0);
|
fzl_node_t *arg = parse_expression(parser, 0);
|
||||||
if (!arg) return NULL;
|
if (!arg)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (node->list.entry_count == capacity)
|
if (node->list.entry_count == capacity)
|
||||||
{
|
{
|
||||||
capacity *= 2;
|
capacity *= 2;
|
||||||
node->list.entries = realloc(node->list.entries, capacity * sizeof(fzl_node_t*));
|
node->list.entries =
|
||||||
|
realloc(node->list.entries, capacity * sizeof(fzl_node_t *));
|
||||||
}
|
}
|
||||||
|
|
||||||
node->list.entries[node->list.entry_count++] = arg;
|
node->list.entries[node->list.entry_count++] = arg;
|
||||||
@@ -117,7 +126,8 @@ fzl_node_t *parse_call(fzl_parser_t *parser)
|
|||||||
consume(parser);
|
consume(parser);
|
||||||
else if (peek(parser) && peek(parser)->type != FZL_TOKTYPE_RPAIR)
|
else if (peek(parser) && peek(parser)->type != FZL_TOKTYPE_RPAIR)
|
||||||
{
|
{
|
||||||
snprintf(last_err, sizeof(last_err), "expected ',' or ')' after arg list");
|
snprintf(last_err, sizeof(last_err),
|
||||||
|
"expected ',' or ')' after arg list");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -137,8 +147,7 @@ fzl_node_t *parse_expression(fzl_parser_t *parser, int precedence)
|
|||||||
else if (peek(parser) && peek(parser)->type == FZL_TOKTYPE_STR_LITERAL)
|
else if (peek(parser) && peek(parser)->type == FZL_TOKTYPE_STR_LITERAL)
|
||||||
return parse_primary(parser);
|
return parse_primary(parser);
|
||||||
|
|
||||||
snprintf(last_err, sizeof(last_err),
|
snprintf(last_err, sizeof(last_err), "failed to parse expression at %d",
|
||||||
"failed to parse expression at %d",
|
|
||||||
peek(parser)->i0);
|
peek(parser)->i0);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -158,7 +167,8 @@ fzl_node_t *parse_program(fzl_parser_t *parser)
|
|||||||
while (peek(parser) != NULL)
|
while (peek(parser) != NULL)
|
||||||
{
|
{
|
||||||
node = parse_statement(parser);
|
node = parse_statement(parser);
|
||||||
if (!node) return NULL;
|
if (!node)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!last)
|
if (!last)
|
||||||
{
|
{
|
||||||
@@ -181,12 +191,12 @@ fzl_node_t *parse_program(fzl_parser_t *parser)
|
|||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Parse tokens and build AST
|
// Parse tokens and build AST
|
||||||
int fzl_parse(fzl_parser_t *parser)
|
int fzl_parse(fzl_parser_t *parser)
|
||||||
{
|
{
|
||||||
fzl_node_t *ast = parse_program(parser);
|
fzl_node_t *ast = parse_program(parser);
|
||||||
if (!ast) return -1;
|
if (!ast)
|
||||||
|
return -1;
|
||||||
|
|
||||||
parser->root = ast;
|
parser->root = ast;
|
||||||
return 0;
|
return 0;
|
||||||
@@ -194,21 +204,20 @@ int fzl_parse(fzl_parser_t *parser)
|
|||||||
|
|
||||||
// --- Parse zone end ---
|
// --- Parse zone end ---
|
||||||
|
|
||||||
char *fzl_parser_geterror()
|
char *fzl_parser_geterror() { return last_err; }
|
||||||
{
|
|
||||||
return last_err;
|
|
||||||
}
|
|
||||||
|
|
||||||
void fzl_freenode(fzl_node_t *node)
|
void fzl_freenode(fzl_node_t *node)
|
||||||
{
|
{
|
||||||
if (!node) return;
|
if (!node)
|
||||||
if (node->token) fzl_freetoken(node->token);
|
return;
|
||||||
|
if (node->token)
|
||||||
|
fzl_freetoken(node->token);
|
||||||
free(node);
|
free(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void fzl_freeparser(fzl_parser_t *parser)
|
void fzl_freeparser(fzl_parser_t *parser)
|
||||||
{
|
{
|
||||||
if (!parser) return;
|
if (!parser)
|
||||||
|
return;
|
||||||
free(parser);
|
free(parser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+26
-45
@@ -34,9 +34,8 @@ fzl_token_t *parse_name(fzl_reader_t *reader)
|
|||||||
{
|
{
|
||||||
lpos++;
|
lpos++;
|
||||||
size++;
|
size++;
|
||||||
}
|
} while ((reader->source[lpos] >= 'A' && reader->source[lpos] <= 'Z') ||
|
||||||
while ((reader->source[lpos] >= 'A' && reader->source[lpos] <= 'Z')
|
(reader->source[lpos] >= 'a' && reader->source[lpos] <= 'z'));
|
||||||
|| (reader->source[lpos] >= 'a' && reader->source[lpos] <= 'z'));
|
|
||||||
|
|
||||||
char *value = malloc(size);
|
char *value = malloc(size);
|
||||||
memcpy(value, reader->source + reader->position, size);
|
memcpy(value, reader->source + reader->position, size);
|
||||||
@@ -44,13 +43,8 @@ fzl_token_t *parse_name(fzl_reader_t *reader)
|
|||||||
size_t start = reader->position;
|
size_t start = reader->position;
|
||||||
reader->position += size;
|
reader->position += size;
|
||||||
|
|
||||||
fzl_token_t *tok = fzl_mktoken(
|
fzl_token_t *tok =
|
||||||
FZL_TOKTYPE_NAME,
|
fzl_mktoken(FZL_TOKTYPE_NAME, start, reader->position - 1, value, size);
|
||||||
start,
|
|
||||||
reader->position - 1,
|
|
||||||
value,
|
|
||||||
size
|
|
||||||
);
|
|
||||||
free(value);
|
free(value);
|
||||||
|
|
||||||
return tok;
|
return tok;
|
||||||
@@ -71,31 +65,20 @@ fzl_token_t *parse_string_literal(fzl_reader_t *reader)
|
|||||||
{
|
{
|
||||||
lpos++;
|
lpos++;
|
||||||
size++;
|
size++;
|
||||||
}
|
} while (reader->source[lpos] != open_symbol);
|
||||||
while (reader->source[lpos] != open_symbol);
|
|
||||||
size++;
|
size++;
|
||||||
|
|
||||||
if (size == 2)
|
if (size == 2)
|
||||||
{
|
{
|
||||||
fzl_token_t *tok = fzl_mktoken(
|
fzl_token_t *tok = fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, reader->position,
|
||||||
FZL_TOKTYPE_STR_LITERAL,
|
reader->position + 1, "", 0);
|
||||||
reader->position,
|
|
||||||
reader->position + 1,
|
|
||||||
"",
|
|
||||||
0
|
|
||||||
);
|
|
||||||
return tok;
|
return tok;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *value = malloc(size - 2);
|
char *value = malloc(size - 2);
|
||||||
memcpy(value, reader->source + reader->position + 1, size - 2);
|
memcpy(value, reader->source + reader->position + 1, size - 2);
|
||||||
fzl_token_t *tok = fzl_mktoken(
|
fzl_token_t *tok = fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, reader->position,
|
||||||
FZL_TOKTYPE_STR_LITERAL,
|
lpos, value, size - 2);
|
||||||
reader->position,
|
|
||||||
lpos,
|
|
||||||
value,
|
|
||||||
size - 2
|
|
||||||
);
|
|
||||||
free(value);
|
free(value);
|
||||||
|
|
||||||
reader->position += size;
|
reader->position += size;
|
||||||
@@ -106,9 +89,11 @@ fzl_token_t *parse_string_literal(fzl_reader_t *reader)
|
|||||||
int fzl_read(fzl_reader_t *reader)
|
int fzl_read(fzl_reader_t *reader)
|
||||||
{
|
{
|
||||||
const size_t len = strlen(reader->source);
|
const size_t len = strlen(reader->source);
|
||||||
if (reader->position >= len) return 0;
|
if (reader->position >= len)
|
||||||
|
return 0;
|
||||||
|
|
||||||
do {
|
do
|
||||||
|
{
|
||||||
size_t p = reader->position;
|
size_t p = reader->position;
|
||||||
fzl_token_t *token;
|
fzl_token_t *token;
|
||||||
char curr = reader->source[p];
|
char curr = reader->source[p];
|
||||||
@@ -123,20 +108,15 @@ int fzl_read(fzl_reader_t *reader)
|
|||||||
token = parse_string_literal(reader);
|
token = parse_string_literal(reader);
|
||||||
else if (curr == '(')
|
else if (curr == '(')
|
||||||
{
|
{
|
||||||
token = fzl_mktoken(
|
token = fzl_mktoken(FZL_TOKTYPE_LPAIR, p, p, NULL, 0);
|
||||||
FZL_TOKTYPE_LPAIR,
|
|
||||||
p, p, NULL, 0);
|
|
||||||
reader->position++;
|
reader->position++;
|
||||||
}
|
}
|
||||||
else if (curr == ')')
|
else if (curr == ')')
|
||||||
{
|
{
|
||||||
token = fzl_mktoken(
|
token = fzl_mktoken(FZL_TOKTYPE_RPAIR, p, p, NULL, 0);
|
||||||
FZL_TOKTYPE_RPAIR,
|
|
||||||
p, p, NULL, 0);
|
|
||||||
reader->position++;
|
reader->position++;
|
||||||
}
|
}
|
||||||
else if (curr >= 'A' && curr <= 'Z' ||
|
else if (curr >= 'A' && curr <= 'Z' || curr >= 'a' && curr <= 'z')
|
||||||
curr >= 'a' && curr <= 'z')
|
|
||||||
token = parse_name(reader);
|
token = parse_name(reader);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -154,10 +134,13 @@ int fzl_read(fzl_reader_t *reader)
|
|||||||
{
|
{
|
||||||
linepos = 1;
|
linepos = 1;
|
||||||
line++;
|
line++;
|
||||||
} else linepos++;
|
}
|
||||||
|
else
|
||||||
|
linepos++;
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf(last_err, "error parsing token (position %ld of line %ld)", line, linepos);
|
sprintf(last_err, "error parsing token (position %ld of line %ld)", line,
|
||||||
|
linepos);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,14 +157,12 @@ int fzl_read(fzl_reader_t *reader)
|
|||||||
|
|
||||||
// --- Read zone end ---
|
// --- Read zone end ---
|
||||||
|
|
||||||
char *fzl_reader_geterror()
|
char *fzl_reader_geterror() { return last_err; }
|
||||||
{
|
|
||||||
return last_err;
|
|
||||||
}
|
|
||||||
|
|
||||||
void free_node_and_next(fzl_reader_ll_t *node)
|
void free_node_and_next(fzl_reader_ll_t *node)
|
||||||
{
|
{
|
||||||
if (!node) return;
|
if (!node)
|
||||||
|
return;
|
||||||
if (node->next)
|
if (node->next)
|
||||||
free_node_and_next(node->next);
|
free_node_and_next(node->next);
|
||||||
|
|
||||||
@@ -193,10 +174,10 @@ void free_node_and_next(fzl_reader_ll_t *node)
|
|||||||
|
|
||||||
void fzl_freereader(fzl_reader_t *reader)
|
void fzl_freereader(fzl_reader_t *reader)
|
||||||
{
|
{
|
||||||
if (!reader) return;
|
if (!reader)
|
||||||
|
return;
|
||||||
if (reader->first)
|
if (reader->first)
|
||||||
free_node_and_next(reader->first);
|
free_node_and_next(reader->first);
|
||||||
|
|
||||||
free(reader);
|
free(reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
fzl_token_t *fzl_mktoken(
|
fzl_token_t *fzl_mktoken(const fzl_toktype_t type, uint16_t from, uint16_t to,
|
||||||
const fzl_toktype_t type, uint16_t from, uint16_t to,
|
|
||||||
void *value, size_t value_len)
|
void *value, size_t value_len)
|
||||||
{
|
{
|
||||||
void *valholder = malloc(value_len);
|
void *valholder = malloc(value_len);
|
||||||
@@ -23,7 +22,7 @@ fzl_token_t *fzl_mktoken(
|
|||||||
// Free allocated token
|
// Free allocated token
|
||||||
void fzl_freetoken(fzl_token_t *tok)
|
void fzl_freetoken(fzl_token_t *tok)
|
||||||
{
|
{
|
||||||
if (tok->store_value) free(tok->value);
|
if (tok->store_value)
|
||||||
|
free(tok->value);
|
||||||
free(tok);
|
free(tok);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+108
-22
@@ -1,27 +1,99 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <compiler/fzl_reader.h>
|
|
||||||
#include <compiler/fzl_parser.h>
|
|
||||||
#include <compiler/fzl_debug.h>
|
#include <compiler/fzl_debug.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <compiler/fzl_reader.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
// Build arguments setup descriptor
|
||||||
if (argc < 2)
|
typedef struct
|
||||||
{
|
{
|
||||||
printf("No files provided >:(\n");
|
char *filepath;
|
||||||
|
bool verbose;
|
||||||
|
bool help;
|
||||||
|
} build_args_t;
|
||||||
|
|
||||||
|
// Print command usage hint
|
||||||
|
void print_usage()
|
||||||
|
{
|
||||||
|
printf("Usage: fzc [flags] <file path>\n");
|
||||||
|
printf("Flags:\n");
|
||||||
|
printf("\t-v\t--verbose\tdisplay debug information\n");
|
||||||
|
printf("\t-h\t--help\t\tprint usage hint (which you're looking at)\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the arguments and build `build_args_t` struct
|
||||||
|
build_args_t *getargs(int argc, char **argv)
|
||||||
|
{
|
||||||
|
build_args_t *args = malloc(sizeof(build_args_t));
|
||||||
|
if (!args)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
args->filepath = NULL;
|
||||||
|
args->verbose = false;
|
||||||
|
args->help = false;
|
||||||
|
|
||||||
|
size_t args_total = 0;
|
||||||
|
if (argc > 2)
|
||||||
|
{
|
||||||
|
for (int i = 1; i < argc; i++)
|
||||||
|
{
|
||||||
|
if (argv[i][0] != '-')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
args_total++;
|
||||||
|
|
||||||
|
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
|
||||||
|
{
|
||||||
|
args->help = true;
|
||||||
|
}
|
||||||
|
else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0)
|
||||||
|
{
|
||||||
|
args->verbose = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Error: unknown argument '%s'\n", argv[i]);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argc - 1 - args_total < 1)
|
||||||
|
{
|
||||||
|
printf("Error: source code file is not specified.\n");
|
||||||
|
print_usage();
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argv[argc - 1][0] == '-')
|
||||||
|
{
|
||||||
|
printf("Error: last argument MUST be a path to file.\n");
|
||||||
|
printf("Write 'fzc --help' to read help\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
args->filepath = argv[argc - 1];
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
build_args_t *args = getargs(argc, argv);
|
||||||
|
if (args == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
else if (args->help)
|
||||||
else if (argc > 2)
|
return 0;
|
||||||
{
|
|
||||||
printf("warn: flags are not supported yet\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (args->verbose)
|
||||||
printf("> analyze\n-> reading file...");
|
printf("> analyze\n-> reading file...");
|
||||||
|
|
||||||
char source[8192];
|
char source[8192];
|
||||||
FILE *fd = fopen(argv[argc - 1], "r");
|
FILE *fd = fopen(args->filepath, "r");
|
||||||
if (!fd)
|
if (!fd)
|
||||||
{
|
{
|
||||||
printf("failed to open file '%s'\n", argv[argc-1]);
|
printf("Error: failed to open file '%s'\n", args->filepath);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,9 +103,12 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
fread(source, fsz, 1, fd);
|
fread(source, fsz, 1, fd);
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
|
if (args->verbose)
|
||||||
|
{
|
||||||
printf("ok!\n");
|
printf("ok!\n");
|
||||||
|
|
||||||
printf("-> tokenizing...");
|
printf("-> tokenizing...");
|
||||||
|
}
|
||||||
|
|
||||||
fzl_reader_t *rd = fzl_mkreader(source);
|
fzl_reader_t *rd = fzl_mkreader(source);
|
||||||
int result = fzl_read(rd);
|
int result = fzl_read(rd);
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
@@ -42,16 +117,21 @@ int main(int argc, char** argv) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (args->verbose)
|
||||||
printf("ok!\n");
|
printf("ok!\n");
|
||||||
|
|
||||||
fzl_reader_ll_t *entry = rd->first;
|
fzl_reader_ll_t *entry = rd->first;
|
||||||
|
|
||||||
|
if (args->verbose)
|
||||||
|
{
|
||||||
printf("--> structure: ");
|
printf("--> structure: ");
|
||||||
while (entry)
|
while (entry)
|
||||||
{
|
{
|
||||||
if (entry->token)
|
if (entry->token)
|
||||||
{
|
{
|
||||||
if (entry->token->store_value)
|
if (entry->token->store_value)
|
||||||
printf("<TOK:%d:%s>", entry->token->type, (char*)entry->token->value);
|
printf("<TOK:%d:%s>", entry->token->type,
|
||||||
|
(char *)entry->token->value);
|
||||||
else
|
else
|
||||||
printf("<TOK:%d>", entry->token->type);
|
printf("<TOK:%d>", entry->token->type);
|
||||||
}
|
}
|
||||||
@@ -60,21 +140,27 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
printf("-> parsing...");
|
printf("-> parsing...");
|
||||||
|
}
|
||||||
|
|
||||||
fzl_parser_t *parser = fzl_mkparser(rd->first);
|
fzl_parser_t *parser = fzl_mkparser(rd->first);
|
||||||
result = fzl_parse(parser);
|
result = fzl_parse(parser);
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
{
|
{
|
||||||
printf("error: %s\n", fzl_parser_geterror());
|
printf("Error: %s\n", fzl_parser_geterror());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (args->verbose)
|
||||||
|
{
|
||||||
printf("ok!\n");
|
printf("ok!\n");
|
||||||
fzl_debug_print_ast(parser->root);
|
fzl_debug_print_ast(parser->root);
|
||||||
|
|
||||||
fzl_freereader(rd);
|
|
||||||
fzl_freeparser(parser);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(args);
|
||||||
|
fzl_freereader(rd);
|
||||||
|
fzl_freeparser(parser);
|
||||||
|
|
||||||
|
printf("done.\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user