Allows parser to recognize true/false in code. No logic has been written yet to handle it.
This commit is contained in:
parent
16575da1dc
commit
832fdc34f6
3 changed files with 21 additions and 2 deletions
|
@ -39,6 +39,8 @@ DIGIT [0-9]
|
||||||
"while" {return WHILE;}
|
"while" {return WHILE;}
|
||||||
"print" {return PRINT;}
|
"print" {return PRINT;}
|
||||||
"input" {return INPUT;}
|
"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} {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;}
|
{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;}
|
[_a-zA-Z][_a-zA-Z0-9]* {yylval.value = make_node(IDENTIFIER, NULL, strdup(yytext)); return IDENTIFIER;}
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#define DOUBLE 300
|
#define DOUBLE 300
|
||||||
#define LONG 301
|
#define LONG 301
|
||||||
|
#define BOOLEAN 302
|
||||||
|
|
||||||
// Share the line number between files
|
// Share the line number between files
|
||||||
extern int linenum;
|
extern int linenum;
|
||||||
|
|
20
src/parser.y
20
src/parser.y
|
@ -51,6 +51,8 @@ struct Node* result;
|
||||||
%token COMMENT
|
%token COMMENT
|
||||||
%token WHITESPACE
|
%token WHITESPACE
|
||||||
%token DONE
|
%token DONE
|
||||||
|
%token <value> ETRUE
|
||||||
|
%token <value> EFALSE
|
||||||
|
|
||||||
/* declare non-terminals */
|
/* 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
|
%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; }
|
identvalue: IDENTIFIER { $$ = $1; }
|
||||||
| VALUE { $$ = $1; }
|
| VALUE { $$ = $1; }
|
||||||
| INPUT { $$ = make_node(INPUT, NULL , ""); }
|
| 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 INPUT: printf("INPUT:\n"); break;
|
||||||
case STATEMENT: printf("STATEMENT:\n"); break;
|
case STATEMENT: printf("STATEMENT:\n"); break;
|
||||||
case VALUE:
|
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));
|
printf("VALUE: %li\n", get_long(node->value));
|
||||||
} else { // Assume double
|
} else { // Assume double
|
||||||
printf("VALUE: %lf\n", get_double(node->value));
|
printf("VALUE: %lf\n", get_double(node->value));
|
||||||
|
@ -807,7 +817,13 @@ void eval_statement(struct Node* node, struct Environment* env) {
|
||||||
case PRINT:
|
case PRINT:
|
||||||
check_num_nodes(node, 1, "can only print out one expression at a time.");
|
check_num_nodes(node, 1, "can only print out one expression at a time.");
|
||||||
tempVal = eval_expression(node->children[0], env);
|
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));
|
printf("%li\n", get_long(tempVal));
|
||||||
} else {
|
} else {
|
||||||
printf("%lf\n", get_double(tempVal));
|
printf("%lf\n", get_double(tempVal));
|
||||||
|
|
Reference in a new issue