-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parsing Type, Initial Book/Tree Function Work
- Loading branch information
1 parent
76c4ae2
commit 2290a0a
Showing
4 changed files
with
164 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "syntac_private.h" | ||
#include "../../helpers/string/handy.h" | ||
#include "../../helpers/log/warn.h" | ||
|
||
//Allocate an Empty Rule Book | ||
SyntacBook * SyntacBookAllocate() { | ||
SyntacBook *book = malloc(sizeof(struct stc_book)); | ||
|
||
book->rules = NULL; | ||
book->rule_count = 0; | ||
|
||
return book; | ||
} | ||
|
||
//Free a rule book (assumes everything within the rulebook and rules is dynamically allocated) | ||
void SyntacBookFree(SyntacBook *book) { | ||
if (book == NULL) { | ||
HLTWarn("SyntacBookFree. Supplied book pointer was null?", 0, 0, HLT_STDWRN); | ||
return; | ||
} | ||
|
||
for (int i = 0; i< book->rule_count; i++){ | ||
struct stc_rule *rule = &book->rules[i]; | ||
if (rule == NULL) { | ||
HLTWarn("SyntacBookFree. Rule pointer was null?", 0, 0, HLT_MJRWRN); | ||
continue; | ||
} | ||
|
||
free(rule->name); rule->name = NULL; | ||
for (int j = 0; rule->elements[j] != NULL; j++) { | ||
free(rule->elements[j]); //free all elements inside | ||
} | ||
free(rule->elements); rule->elements = NULL; //free container | ||
} | ||
|
||
if (book->rules != NULL) free(book->rules); | ||
book->rules = NULL; book->rule_count = 0; | ||
free(book); | ||
} | ||
|
||
void SyntacBookRuleAdd(SyntacBook *book, char *left, char *right) { | ||
if (book == NULL) { | ||
HLTWarn("SyntacBookRuleAdd. Supplied book pointer was null?", 0, 0 HLT_STDWRN); | ||
return; | ||
} | ||
|
||
if (left == NULL || left[0] == '\0') { | ||
HLTWarn("SyntacBookRuleAdd. Supplied left of rule was null/empty?", 0, 0, HLT_STDWRN); | ||
return; | ||
} | ||
|
||
if (right == NULL || left[0] == '\0') { | ||
HLTWarn("SyntacBookRuleAdd. Supplied right of rule was null/empty?", 0, 0, HLT_STDWRN); | ||
return; | ||
} | ||
|
||
struct stc_rule *rule = malloc(sizeof(struct stc_rule)); | ||
rule->name = calloc(strlen(left)+1, sizeof(char)); strcpy(left_copy, left); | ||
|
||
int elm_cnt = 0; bool lstwasSpace = true; | ||
for (int i = 0; i < strlen(right); i++) { | ||
if (isspace(right[i])) { | ||
if (!lstspace) elm_cnt++; | ||
lstwasSpace = true; | ||
} else { | ||
lstwasSpace = false; | ||
} | ||
} | ||
|
||
rule->elements = malloc(sizeof(char *) * (elm_cnt+1)); | ||
rule->elements[elm_cnt] = NULL; | ||
|
||
//TODO: Actually parse out the elements (dynamically allocate them too) | ||
} | ||
|
||
SyntacBook * SyntacBookFromFile(char *file_name, SyntacTreeType type){ | ||
if (file_name == NULL || file_name[0] == '\0') { | ||
HLTWarn("SyntacBookFromFile. Supplied file_name was null/empty?"); | ||
return NULL; | ||
} | ||
|
||
char *cntnts = ftostr(file_name); | ||
|
||
SyntacBook *book = SyntacBookFromString(cntnts); | ||
|
||
free(cntnts); | ||
return book; | ||
} | ||
|
||
SyntacBook * SyntacBookFromString(char *stream, SyntacTreeType type) { | ||
//TODO | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "../syntac.h" | ||
|
||
#ifndef __HEPH_INTERNAL_PRIVATE_SYNTAC__ | ||
#define __HEPH_INTERNAL_PRIVATE_SYNTAC__ | ||
|
||
//Rule Definition | ||
// left -> right | ||
// a -> b c | ||
struct stc_rule { | ||
char *name; //also the left part of the rule | ||
char **elements; //also the right part of the rule (null-terminated) | ||
}; | ||
|
||
//Rule Book | ||
struct stc_book { | ||
enum stc_parsing_style type; | ||
struct stc_rule *rules; | ||
int rule_count; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "syntac_private.h" | ||
#include "../../helpers/log/warn.h" | ||
#include "../../helpers/string/handy.h" | ||
|
||
SyntacTreeNode * SyntacTreeFromTokens(LexicToken *stream, Syntac *book) { | ||
//TODO | ||
} | ||
|
||
SyntacTreeNode * SyntacTreeFromStream(char **stream, SyntacBook *book) { | ||
//TODO | ||
} | ||
|
||
void SyntacTreeFree(SyntacTreeNode *tree) { | ||
if (tree == NULL) { | ||
HLTWarn("SyntacTreeFree. Supplied tree pointer was null?", 0, 0, HLT_STDWRN); | ||
return; | ||
} | ||
|
||
if (tree->children[0] != NULL) { | ||
for (int i = 0; tree->children[i] != NULL; i++) { | ||
SyntacTreeFree(tree->children[i]); | ||
} | ||
} | ||
|
||
free(tree->rule_name); tree->rule_name = NULL; | ||
free(tree->contains); tree->contains = NULL; | ||
|
||
free(tree); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters