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.
This commit is contained in:
parent
3b6198669f
commit
82f7e21d3a
3 changed files with 19 additions and 4 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;}
|
||||||
|
"lambda" {return LAMBDA;}
|
||||||
|
":" {return COLON;}
|
||||||
"true" {yylval.value = make_node(VALUE, make_value(BOOLEAN, 1, 0), ""); return VALUE;}
|
"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;}
|
"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;}
|
{DIGIT} {yylval.value = make_node(VALUE, make_value(LONG, atoi(yytext), 0), ""); return VALUE;}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#define ID_SIZE 100
|
#define ID_SIZE 100
|
||||||
#define MAX_CHILDREN 3
|
#define MAX_CHILDREN 3
|
||||||
#define STATEMENT 200
|
#define STATEMENT 200
|
||||||
|
#define CALLFUNC 201
|
||||||
#define MAX_VARIABLES 200
|
#define MAX_VARIABLES 200
|
||||||
|
|
||||||
enum TypeTag { DOUBLE, LONG, BOOLEAN };
|
enum TypeTag { DOUBLE, LONG, BOOLEAN };
|
||||||
|
|
20
src/parser.y
20
src/parser.y
|
@ -51,9 +51,11 @@ struct Node* result;
|
||||||
%token COMMENT
|
%token COMMENT
|
||||||
%token WHITESPACE
|
%token WHITESPACE
|
||||||
%token DONE
|
%token DONE
|
||||||
|
%token <value> LAMBDA
|
||||||
|
%token COLON
|
||||||
|
|
||||||
/* 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 callfunc expression subexpression term subterm factor atom identvalue ident
|
||||||
|
|
||||||
/* give us more detailed errors */
|
/* give us more detailed errors */
|
||||||
%error-verbose
|
%error-verbose
|
||||||
|
@ -135,9 +137,17 @@ atom: OPENPAREM expression ENDPAREM { $$ = $2; }
|
||||||
|
|
||||||
ident: IDENTIFIER { $$ = $1; }
|
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; }
|
| VALUE { $$ = $1; }
|
||||||
| INPUT { $$ = make_node(INPUT, NULL , ""); }
|
| 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 WHILE: printf("WHILE:\n"); break;
|
||||||
case PRINT: printf("PRINT:\n"); break;
|
case PRINT: printf("PRINT:\n"); break;
|
||||||
case INPUT: printf("INPUT:\n"); break;
|
case INPUT: printf("INPUT:\n"); break;
|
||||||
|
case LAMBDA: printf("LAMBDA:\n"); break;
|
||||||
case STATEMENT: printf("STATEMENT:\n"); break;
|
case STATEMENT: printf("STATEMENT:\n"); break;
|
||||||
case VALUE:
|
case VALUE:
|
||||||
if (node->value->type == BOOLEAN) {
|
if (node->value->type == BOOLEAN) {
|
||||||
|
@ -642,11 +653,11 @@ struct Value* eval_expression(struct Node* node, struct Environment* env) {
|
||||||
double temp;
|
double temp;
|
||||||
struct Variable* var = NULL;
|
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* val1 = NULL;
|
||||||
struct Value* val2 = NULL;
|
struct Value* val2 = NULL;
|
||||||
struct Value* val3 = 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);
|
val1 = eval_expression(node->children[0], env);
|
||||||
if (node->num_children > 1) {
|
if (node->num_children > 1) {
|
||||||
val2 = eval_expression(node->children[1], env);
|
val2 = eval_expression(node->children[1], env);
|
||||||
|
@ -658,6 +669,7 @@ struct Value* eval_expression(struct Node* node, struct Environment* env) {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(node->type) {
|
switch(node->type) {
|
||||||
|
case LAMBDA: printf("<LambdaExpression>\n"); return make_long(0); break;
|
||||||
case PLUS:
|
case PLUS:
|
||||||
check_num_nodes(node, 2, "cannot add more than two expressions.");
|
check_num_nodes(node, 2, "cannot add more than two expressions.");
|
||||||
return add(val1, val2);
|
return add(val1, val2);
|
||||||
|
|
Reference in a new issue