Archived
1
0
Fork 0

Allows parser to recognize true/false in code. No logic has been written yet to handle it.

This commit is contained in:
Brandon Rozek 2018-09-20 21:42:59 -04:00
parent 16575da1dc
commit 832fdc34f6
3 changed files with 21 additions and 2 deletions

View file

@ -39,6 +39,8 @@ DIGIT [0-9]
"while" {return WHILE;}
"print" {return PRINT;}
"input" {return INPUT;}
"true" {yylval.value = make_node(BOOLEAN, make_value(LONG, 1, 0), ""); return ETRUE;}
"false" {yylval.value = make_node(BOOLEAN, make_value(LONG, 0, 0), ""); return EFALSE;}
{DIGIT} {yylval.value = make_node(VALUE, make_value(LONG, atoi(yytext), 0), ""); return VALUE;}
{DIGIT}*"."?{DIGIT}+ {yylval.value = make_node(VALUE, make_value(DOUBLE, 0, atof(yytext)), ""); return VALUE;}
[_a-zA-Z][_a-zA-Z0-9]* {yylval.value = make_node(IDENTIFIER, NULL, strdup(yytext)); return IDENTIFIER;}

View file

@ -8,6 +8,7 @@
#define DOUBLE 300
#define LONG 301
#define BOOLEAN 302
// Share the line number between files
extern int linenum;

View file

@ -51,6 +51,8 @@ struct Node* result;
%token COMMENT
%token WHITESPACE
%token DONE
%token <value> ETRUE
%token <value> EFALSE
/* declare non-terminals */
%type <value> program statement assignment if-statement if-else-statement while print statements substatements expression subexpression term subterm factor atom identvalue ident
@ -138,6 +140,8 @@ ident: IDENTIFIER { $$ = $1; }
identvalue: IDENTIFIER { $$ = $1; }
| VALUE { $$ = $1; }
| INPUT { $$ = make_node(INPUT, NULL , ""); }
| ETRUE { $$ = $1; }
| EFALSE { $$ = $1; }
@ -246,7 +250,13 @@ void print_tree(struct Node* node, int tabs) {
case INPUT: printf("INPUT:\n"); break;
case STATEMENT: printf("STATEMENT:\n"); break;
case VALUE:
if (node->value->type == LONG) {
if (node->value->type == BOOLEAN) {
if (get_long(node->value)) {
printf("VALUE: true\n");
} else {
printf("VALUE: false\n");
}
} else if (node->value->type == LONG) {
printf("VALUE: %li\n", get_long(node->value));
} else { // Assume double
printf("VALUE: %lf\n", get_double(node->value));
@ -807,7 +817,13 @@ void eval_statement(struct Node* node, struct Environment* env) {
case PRINT:
check_num_nodes(node, 1, "can only print out one expression at a time.");
tempVal = eval_expression(node->children[0], env);
if (tempVal->type == LONG) {
if (tempVal->type == BOOLEAN) {
if (get_long(tempVal)) {
printf("true\n");
} else {
printf("false\n");
}
} else if (tempVal->type == LONG) {
printf("%li\n", get_long(tempVal));
} else {
printf("%lf\n", get_double(tempVal));