/*
*****************************************************************************/
-void exec_file(FILE* file, const char* prompt)
-{
+void exec_file(FILE* file, const char* prompt) {
File = file;
while(true) {
printf("%s", prompt);
/* Parsing Rules
*****************************************************************************/
-static AST* expression(void)
-{
+static AST* expression(void) {
AST* expr = keyword_send();
optional(SEMICOLON);
return expr;
}
-static AST* keyword_send(void)
-{
+static AST* keyword_send(void) {
AST* expr = binary_send();
AST* sel = NULL;
while (accept(KEYWORD)) {
return expr;
}
-static AST* binary_send(void)
-{
+static AST* binary_send(void) {
AST* expr = unary_send();
if (accept(OPERATOR)) {
AST* msg = ast_new(BINARY_MSG);
return expr;
}
-static AST* unary_send(void)
-{
+static AST* unary_send(void) {
AST* expr = operand();
while (accept(IDENTIFIER)) {
AST* msg = ast_new(UNARY_MSG);
return expr;
}
-static AST* operand(void)
-{
+static AST* operand(void) {
AST* obj = NULL;
if (accept(LPAR)) {
expect(LPAR);
return obj;
}
-static AST* literal(void)
-{
+static AST* literal(void) {
AST* obj = NULL;
switch (CurrTok) {
case IDENTIFIER: obj = ast_tok(IDENTIFIER); break;
return obj;
}
-static AST* array(void)
-{
+static AST* array(void) {
AST* array_obj = ast_new(ARRAY);
expect(LBRACK);
while (!accept(RBRACK)) {
return array_obj;
}
-static AST* hashmap(void)
-{
+static AST* hashmap(void) {
AST* hashmap = ast_new(HASHMAP);
expect(AT_LBRACE);
while (!accept(RBRACE)) {
return hashmap;
}
-static AST* hashset(void)
-{
+static AST* hashset(void) {
AST* hashset = ast_new(HASHSET);
expect(AT_LBRACK);
while (!accept(RBRACK)) {
return hashset;
}
-static AST* object(void)
-{
+static AST* object(void) {
AST* object = ast_new(OBJECT);
expect(LBRACE);
if (accept(PIPE)) {
/* Parsing Helpers
*****************************************************************************/
-static void error(const char* msg)
-{
+static void error(const char* msg) {
fprintf(stderr, "Error: %s\n", msg);
exit(1);
}
-static bool accept(int expected)
-{
+static bool accept(int expected) {
if (CurrTok == UNKNOWN) {
CurrTok = token();
}
return (CurrTok == expected);
}
-static bool expect(int expected)
-{
+static bool expect(int expected) {
bool accepted = accept(expected);
if (!accepted)
error("unexpected symbol");
return accepted;
}
-static bool optional(int expected)
-{
+static bool optional(int expected) {
if (accept(expected))
return expect(expected);
else
/* Lexical Rules
*****************************************************************************/
-static int token(void)
-{
+static int token(void) {
// Re-init the token
strbuf_init(&Token);
}
}
-static void whitespace(void)
-{
+static void whitespace(void) {
while(isspace(current()) || ('#' == current())) {
if ('#' == current()) {
discard();
}
}
-static int symbol(void)
-{
+static int symbol(void) {
expect_ch('$');
while (isalnum(current()))
append();
return SYMBOL;
}
-static int identifier(void)
-{
+static int identifier(void) {
while (isalnum(current()))
append();
}
}
-static int number(void)
-{
+static int number(void) {
append();
while (isdigit(current()))
append();
return NUMBER;
}
-static int string(void)
-{
+static int string(void) {
expect_ch('"');
while('"' != current())
append();
return STRING;
}
-static int character(void)
-{
+static int character(void) {
expect_ch('\'');
append();
expect_ch('\'');
return CHARACTER;
}
-static int punctuation(void)
-{
+static int punctuation(void) {
if ('@' == current()) {
expect_ch('@');
if ('[' == current()) {
}
}
-static int operator(void)
-{
+static int operator(void) {
lex_error();
return UNKNOWN;
}
/* Lexical Analysis Helpers
*****************************************************************************/
-static int current(void)
-{
+static int current(void) {
return CurrChar;
}
-static void append(void)
-{
+static void append(void) {
strbuf_putc(&Token, CurrChar);
fetch();
}
-static void discard(void)
-{
+static void discard(void) {
fetch();
}
-static void expect_ch(int ch)
-{
+static void expect_ch(int ch) {
if (ch == current())
append();
else
lex_error();
}
-static void lex_error(void)
-{
+static void lex_error(void) {
fprintf(stderr, "Lexer error\n");
exit(1);
}
-static void fetch(void)
-{
+static void fetch(void) {
CurrChar = fgetc(File);
if (CurrChar == '\n') {
Line++;
/* Tree Routines
*****************************************************************************/
-static AST* ast_new(int type)
-{
+static AST* ast_new(int type) {
AST* tree = (AST*)malloc(sizeof(AST));
memset(tree, 0, sizeof(AST));
tree->type = type;
return tree;
}
-static AST* ast_tok(int type)
-{
+static AST* ast_tok(int type) {
AST* ast = ast_new(type);
expect(type);
ast->text = strbuf_string(&Token);
return ast;
}
-static void ast_add_child(AST* parent, AST* child)
-{
+static void ast_add_child(AST* parent, AST* child) {
slist_push_back(&(parent->children), &(child->link));
}
-static void ast_print(AST* tree, int depth)
-{
+static void ast_print(AST* tree, int depth) {
int indent = depth * 2;
printf("%*s(", indent, "");
printf("%d %s", tree->type, tree->text ? tree->text : "");
/* String Buffer
*****************************************************************************/
-static void strbuf_init(strbuf_t* buf)
-{
+static void strbuf_init(strbuf_t* buf) {
buf->index = 0;
buf->capacity = 8;
buf->string = (char*)malloc(buf->capacity);
buf->string[buf->index] = 0;
}
-static void strbuf_putc(strbuf_t* buf, int ch)
-{
+static void strbuf_putc(strbuf_t* buf, int ch) {
if (buf->string == NULL) {
strbuf_init(buf);
} else if ((buf->index + 2) >= buf->capacity) {
buf->string[buf->index] = '\0';
}
-static char* strbuf_string(strbuf_t* buf)
-{
+static char* strbuf_string(strbuf_t* buf) {
char* str = buf->string;
strbuf_init(buf);
return str;
#include "gir_internals.h"
-void slist_init(slist_t* list)
-{
+void slist_init(slist_t* list) {
list->head = NULL;
}
-bool slist_empty(slist_t* list)
-{
+bool slist_empty(slist_t* list) {
return (list->head == NULL);
}
-size_t slist_size(slist_t* list)
-{
+size_t slist_size(slist_t* list) {
size_t sz = 0;
slist_node_t* node = list->head;
while (node != NULL) {
return sz;
}
-slist_node_t* slist_front(slist_t* list)
-{
+slist_node_t* slist_front(slist_t* list) {
return list->head;
}
-void slist_push_front(slist_t* list, slist_node_t* node)
-{
+void slist_push_front(slist_t* list, slist_node_t* node) {
node->next = list->head;
list->head = node;
}
-slist_node_t* slist_pop_front(slist_t* list)
-{
+slist_node_t* slist_pop_front(slist_t* list) {
slist_node_t* node = list->head;
list->head = node->next;
node->next = NULL;
return node;
}
-slist_node_t* slist_back(slist_t* list)
-{
+slist_node_t* slist_back(slist_t* list) {
slist_node_t* node = list->head;
while (node && node->next != NULL)
node = node->next;
return (node) ? node : NULL;
}
-void slist_push_back(slist_t* list, slist_node_t* node)
-{
+void slist_push_back(slist_t* list, slist_node_t* node) {
slist_node_t* back = slist_back(list);
if (NULL != back)
back->next = node;
node->next = NULL;
}
-slist_node_t* slist_pop_back(slist_t* list)
-{
+slist_node_t* slist_pop_back(slist_t* list) {
slist_node_t* prev = NULL;
slist_node_t* curr = list->head;
while (curr && curr->next != NULL) {
return curr;
}
-bool slist_node_has_next(slist_node_t* node)
-{
+bool slist_node_has_next(slist_node_t* node) {
return (node->next != NULL);
}
-slist_node_t* slist_node_next(slist_node_t* node)
-{
+slist_node_t* slist_node_next(slist_node_t* node) {
return node->next;
}