From 832fdc34f62648eef7f0fa7bb57e510ba08460e9 Mon Sep 17 00:00:00 2001 From: Brandon Rozek Date: Thu, 20 Sep 2018 21:42:59 -0400 Subject: [PATCH] Allows parser to recognize true/false in code. No logic has been written yet to handle it. --- src/lexer.l | 2 ++ src/node.h | 1 + src/parser.y | 20 ++++++++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/lexer.l b/src/lexer.l index 6c6c927..cefd7d9 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -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;} diff --git a/src/node.h b/src/node.h index 3b410cb..4338986 100644 --- a/src/node.h +++ b/src/node.h @@ -8,6 +8,7 @@ #define DOUBLE 300 #define LONG 301 +#define BOOLEAN 302 // Share the line number between files extern int linenum; diff --git a/src/parser.y b/src/parser.y index c9e9db9..fdca7cf 100644 --- a/src/parser.y +++ b/src/parser.y @@ -51,6 +51,8 @@ struct Node* result; %token COMMENT %token WHITESPACE %token DONE +%token ETRUE +%token EFALSE /* declare non-terminals */ %type 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));