-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
543 additions
and
8 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
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
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
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
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,122 @@ | ||
# This code is hereby placed in the public domain. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS | ||
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | ||
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | ||
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | ||
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
%prefix "calc" | ||
|
||
%value "pcc_ast_node_t *" # <-- must be set | ||
|
||
%auxil "pcc_ast_manager_t *" # <-- must be set | ||
|
||
%header { | ||
#define PCC_AST_NODE_CUSTOM_DATA_DEFINED /* <-- enables node custom data */ | ||
|
||
typedef struct text_data_tag { /* <-- node custom data type */ | ||
char *text; | ||
} pcc_ast_node_custom_data_t; | ||
} | ||
|
||
%source { | ||
#include <stdio.h> | ||
#include <string.h> | ||
} | ||
|
||
statement <- _ e:expression _ EOL { $$ = e; } | ||
/ ( !EOL . )* EOL { $$ = NULL; } | ||
|
||
expression <- e:term { $$ = e; } | ||
|
||
term <- l:term _ '+' _ r:factor { $$ = pcc_ast_node__create_2(l, r); $$->custom.text = strdup("+"); } | ||
/ l:term _ '-' _ r:factor { $$ = pcc_ast_node__create_2(l, r); $$->custom.text = strdup("-"); } | ||
/ e:factor { $$ = e; } | ||
|
||
factor <- l:factor _ '*' _ r:unary { $$ = pcc_ast_node__create_2(l, r); $$->custom.text = strdup("*"); } | ||
/ l:factor _ '/' _ r:unary { $$ = pcc_ast_node__create_2(l, r); $$->custom.text = strdup("/"); } | ||
/ e:unary { $$ = e; } | ||
|
||
unary <- '+' _ e:unary { $$ = pcc_ast_node__create_1(e); $$->custom.text = strdup("+"); } | ||
/ '-' _ e:unary { $$ = pcc_ast_node__create_1(e); $$->custom.text = strdup("-"); } | ||
/ e:primary { $$ = e; } | ||
|
||
primary <- < [0-9]+ > { $$ = pcc_ast_node__create_0(); $$->custom.text = strdup($1); } | ||
/ '(' _ e:expression _ ')' { $$ = e; } | ||
|
||
_ <- [ \t]* | ||
EOL <- '\n' / '\r\n' / '\r' / ';' | ||
|
||
%import "code/pcc_ast.peg" # <-- provides AST build functions | ||
|
||
%% | ||
void pcc_ast_node_custom_data__initialize(pcc_ast_node_custom_data_t *obj) { /* <-- must be implemented when enabling node custom data */ | ||
obj->text = NULL; | ||
} | ||
|
||
void pcc_ast_node_custom_data__finalize(pcc_ast_node_custom_data_t *obj) { /* <-- must be implemented when enabling node custom data */ | ||
free(obj->text); | ||
} | ||
|
||
static void dump_ast(const pcc_ast_node_t *obj, int depth) { | ||
if (obj) { | ||
switch (obj->type) { | ||
case PCC_AST_NODE_TYPE_NULLARY: | ||
printf("%*s%s: \"%s\"\n", 2 * depth, "", "nullary", obj->custom.text); | ||
break; | ||
case PCC_AST_NODE_TYPE_UNARY: | ||
printf("%*s%s: \"%s\"\n", 2 * depth, "", "unary", obj->custom.text); | ||
dump_ast(obj->data.unary.node, depth + 1); | ||
break; | ||
case PCC_AST_NODE_TYPE_BINARY: | ||
printf("%*s%s: \"%s\"\n", 2 * depth, "", "binary", obj->custom.text); | ||
dump_ast(obj->data.binary.node[0], depth + 1); | ||
dump_ast(obj->data.binary.node[1], depth + 1); | ||
break; | ||
case PCC_AST_NODE_TYPE_TERNARY: | ||
printf("%*s%s: \"%s\"\n", 2 * depth, "", "ternary", obj->custom.text); | ||
dump_ast(obj->data.ternary.node[0], depth + 1); | ||
dump_ast(obj->data.ternary.node[1], depth + 1); | ||
dump_ast(obj->data.ternary.node[2], depth + 1); | ||
break; | ||
case PCC_AST_NODE_TYPE_VARIADIC: | ||
printf("%*s%s: \"%s\"\n", 2 * depth, "", "variadic", obj->custom.text); | ||
{ | ||
size_t i; | ||
for (i = 0; i < obj->data.variadic.len; i++) { | ||
dump_ast(obj->data.variadic.node[i], depth + 1); | ||
} | ||
} | ||
break; | ||
default: | ||
printf("%*s%s: \"%s\"\n", 2 * depth, "", "(unknown)", obj->custom.text); | ||
break; | ||
} | ||
} | ||
else { | ||
printf("%*s(null)\n", 2 * depth, ""); | ||
} | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
pcc_ast_manager_t mgr; | ||
pcc_ast_manager__initialize(&mgr); | ||
{ | ||
calc_context_t *ctx = calc_create(&mgr); | ||
pcc_ast_node_t *ast = NULL; | ||
while (calc_parse(ctx, &ast)) { | ||
dump_ast(ast, 0); | ||
pcc_ast_node__destroy(ast); | ||
} | ||
calc_destroy(ctx); | ||
} | ||
pcc_ast_manager__finalize(&mgr); | ||
return 0; | ||
} |
Oops, something went wrong.