Cosmetics
This commit is contained in:
parent
61301aaa97
commit
b2995e2475
1 changed files with 9 additions and 3 deletions
12
src/parser.y
12
src/parser.y
|
@ -212,8 +212,6 @@ void attach_node(struct Node* parent, struct Node* child) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_tree(struct Node* node, int tabs) {
|
void print_tree(struct Node* node, int tabs) {
|
||||||
int i;
|
|
||||||
|
|
||||||
/* base case */
|
/* base case */
|
||||||
if(!node) {
|
if(!node) {
|
||||||
fprintf(stderr, "NO TREE STRUCTURE\n");
|
fprintf(stderr, "NO TREE STRUCTURE\n");
|
||||||
|
@ -221,7 +219,7 @@ void print_tree(struct Node* node, int tabs) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* print leading tabs */
|
/* print leading tabs */
|
||||||
for(i = 0; i < tabs; i++) {
|
for(int i = 0; i < tabs; i++) {
|
||||||
printf(" ");
|
printf(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,6 +263,7 @@ void delete_tree(struct Node* node) {
|
||||||
}
|
}
|
||||||
free(node);
|
free(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* creates a new variable and returns it */
|
/* creates a new variable and returns it */
|
||||||
struct Variable* make_variable(char* id, double value) {
|
struct Variable* make_variable(char* id, double value) {
|
||||||
/* allocate space */
|
/* allocate space */
|
||||||
|
@ -282,6 +281,7 @@ void set_value(struct Variable* var, double value) {
|
||||||
if (!var) { fprintf(stderr, "Error: Invalid Variable\n"); return; }
|
if (!var) { fprintf(stderr, "Error: Invalid Variable\n"); return; }
|
||||||
var->value = value;
|
var->value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
double get_value(struct Variable* var) {
|
double get_value(struct Variable* var) {
|
||||||
if (!var) { fprintf(stderr, "Error: Invalid Variable\n"); return 0; }
|
if (!var) { fprintf(stderr, "Error: Invalid Variable\n"); return 0; }
|
||||||
return var->value;
|
return var->value;
|
||||||
|
@ -295,6 +295,7 @@ struct Environment* create_environment(void) {
|
||||||
}
|
}
|
||||||
return env;
|
return env;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Variable* find_variable(struct Environment* env, char* id) {
|
struct Variable* find_variable(struct Environment* env, char* id) {
|
||||||
for (int i = 0; i < env->num_vars; i++) {
|
for (int i = 0; i < env->num_vars; i++) {
|
||||||
if (strcmp(env->vars[i]->id, id) == 0) {
|
if (strcmp(env->vars[i]->id, id) == 0) {
|
||||||
|
@ -466,6 +467,7 @@ void eval_statement(struct Node* node, struct Environment* env) {
|
||||||
make_variable(node->children[0]->id,
|
make_variable(node->children[0]->id,
|
||||||
eval_expression(node->children[1], env)));
|
eval_expression(node->children[1], env)));
|
||||||
break;
|
break;
|
||||||
|
//------------
|
||||||
case IF:
|
case IF:
|
||||||
if (node->num_children != 2 && node->num_children != 3) {
|
if (node->num_children != 2 && node->num_children != 3) {
|
||||||
fprintf(stderr, "Error: The format of an if-statement is if expression statement with an optional else.\n");
|
fprintf(stderr, "Error: The format of an if-statement is if expression statement with an optional else.\n");
|
||||||
|
@ -476,16 +478,19 @@ void eval_statement(struct Node* node, struct Environment* env) {
|
||||||
eval_statement(node->children[2], env);
|
eval_statement(node->children[2], env);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
//------------
|
||||||
case WHILE:
|
case WHILE:
|
||||||
check_num_nodes(node, 2, "the format of a while statement is: while expression statement(s)");
|
check_num_nodes(node, 2, "the format of a while statement is: while expression statement(s)");
|
||||||
while (eval_expression(node->children[0], env)) {
|
while (eval_expression(node->children[0], env)) {
|
||||||
eval_statement(node->children[1], env);
|
eval_statement(node->children[1], env);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
//------------
|
||||||
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.");
|
||||||
printf("%lf\n", eval_expression(node->children[0], env));
|
printf("%lf\n", eval_expression(node->children[0], env));
|
||||||
break;
|
break;
|
||||||
|
//------------
|
||||||
case STATEMENT: // Can have a maximum of two children statement nodes
|
case STATEMENT: // Can have a maximum of two children statement nodes
|
||||||
if (node->num_children > 0) {
|
if (node->num_children > 0) {
|
||||||
eval_statement(node->children[0], env);
|
eval_statement(node->children[0], env);
|
||||||
|
@ -494,6 +499,7 @@ void eval_statement(struct Node* node, struct Environment* env) {
|
||||||
eval_statement(node->children[1], env);
|
eval_statement(node->children[1], env);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
//------------
|
||||||
default:
|
default:
|
||||||
printf("Error, %d not a valid statement type.\n", node->type);
|
printf("Error, %d not a valid statement type.\n", node->type);
|
||||||
return;
|
return;
|
||||||
|
|
Reference in a new issue