From 82f7e21d3aa02c9a46be311e512ef7a6a1840eb3 Mon Sep 17 00:00:00 2001 From: Brandon Rozek Date: Mon, 24 Sep 2018 14:21:32 -0400 Subject: [PATCH] Added the abilty for lambda expressions to be added into the AST. Have not added logic to properly evaulate lambda expressions yet. Currently defaults to a long with the value 0. --- src/lexer.l | 2 ++ src/node.h | 1 + src/parser.y | 20 ++++++++++++++++---- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/lexer.l b/src/lexer.l index 9a75420..f369d48 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;} +"lambda" {return LAMBDA;} +":" {return COLON;} "true" {yylval.value = make_node(VALUE, make_value(BOOLEAN, 1, 0), ""); return VALUE;} "false" {yylval.value = make_node(VALUE, make_value(BOOLEAN, 0, 0), ""); return VALUE;} {DIGIT} {yylval.value = make_node(VALUE, make_value(LONG, atoi(yytext), 0), ""); return VALUE;} diff --git a/src/node.h b/src/node.h index 2b5546f..f1bdb6e 100644 --- a/src/node.h +++ b/src/node.h @@ -4,6 +4,7 @@ #define ID_SIZE 100 #define MAX_CHILDREN 3 #define STATEMENT 200 +#define CALLFUNC 201 #define MAX_VARIABLES 200 enum TypeTag { DOUBLE, LONG, BOOLEAN }; diff --git a/src/parser.y b/src/parser.y index 6b84814..2baf386 100644 --- a/src/parser.y +++ b/src/parser.y @@ -51,9 +51,11 @@ struct Node* result; %token COMMENT %token WHITESPACE %token DONE +%token LAMBDA +%token COLON /* declare non-terminals */ -%type program statement assignment if-statement if-else-statement while print statements substatements expression subexpression term subterm factor atom identvalue ident +%type program statement assignment if-statement if-else-statement while print statements substatements callfunc expression subexpression term subterm factor atom identvalue ident /* give us more detailed errors */ %error-verbose @@ -135,9 +137,17 @@ atom: OPENPAREM expression ENDPAREM { $$ = $2; } ident: IDENTIFIER { $$ = $1; } -identvalue: IDENTIFIER { $$ = $1; } +callfunc: ident OPENPAREM ident ENDPAREM { $$ = make_node(CALLFUNC, NULL, 0); attach_node($$, $1); attach_node($$, $3); } + +identvalue: callfunc { $$ = $1; } + | ident { $$ = $1; } | VALUE { $$ = $1; } | INPUT { $$ = make_node(INPUT, NULL , ""); } + | LAMBDA ident COLON expression { + // Only supports one argument functions for now + $$ = make_node(LAMBDA, NULL, ""); + attach_node($$, $2); + attach_node($$, $4); } @@ -244,6 +254,7 @@ void print_tree(struct Node* node, int tabs) { case WHILE: printf("WHILE:\n"); break; case PRINT: printf("PRINT:\n"); break; case INPUT: printf("INPUT:\n"); break; + case LAMBDA: printf("LAMBDA:\n"); break; case STATEMENT: printf("STATEMENT:\n"); break; case VALUE: if (node->value->type == BOOLEAN) { @@ -642,11 +653,11 @@ struct Value* eval_expression(struct Node* node, struct Environment* env) { double temp; struct Variable* var = NULL; - // Evaluate subexpressions if existent + // Evaluate subexpressions if existent and node is not a lambda expression struct Value* val1 = NULL; struct Value* val2 = NULL; struct Value* val3 = NULL; - if (node->num_children > 0) { + if (node->num_children > 0 && node->type != LAMBDA) { val1 = eval_expression(node->children[0], env); if (node->num_children > 1) { val2 = eval_expression(node->children[1], env); @@ -658,6 +669,7 @@ struct Value* eval_expression(struct Node* node, struct Environment* env) { } switch(node->type) { + case LAMBDA: printf("\n"); return make_long(0); break; case PLUS: check_num_nodes(node, 2, "cannot add more than two expressions."); return add(val1, val2);