Skip to content

Commit

Permalink
compiler: boolean operations "and" and "or" (WIP, untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
wpwrak committed Jan 19, 2012
1 parent b66d924 commit 0e6cf55
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/compiler/parser.y
Expand Up @@ -85,6 +85,8 @@ static const enum ast_op tok2op[] = {
[TOK_MAX] = op_max,
[TOK_INT] = op_int,
[TOK_BNOT] = op_bnot,
[TOK_BAND] = op_band,
[TOK_BOR] = op_bor,
};

static struct ast_node *node_op(enum ast_op op,
Expand Down Expand Up @@ -192,6 +194,8 @@ static struct id *symbolify(struct id *id)

%type expr {struct ast_node *}
%type cond_expr {struct ast_node *}
%type bool_or_expr {struct ast_node *}
%type bool_and_expr {struct ast_node *}
%type equal_expr {struct ast_node *}
%type rel_expr {struct ast_node *}
%type add_expr {struct ast_node *}
Expand All @@ -201,6 +205,8 @@ static struct id *symbolify(struct id *id)

%destructor expr { free($$); }
%destructor cond_expr { free($$); }
%destructor bool_or_expr { free($$); }
%destructor bool_and_expr { free($$); }
%destructor equal_expr { free($$); }
%destructor rel_expr { free($$); }
%destructor add_expr { free($$); }
Expand Down Expand Up @@ -349,10 +355,26 @@ cond_expr(N) ::= equal_expr(A). {
N = A;
}

cond_expr(N) ::= equal_expr(A) TOK_QUESTION expr(B) TOK_COLON cond_expr(C). {
cond_expr(N) ::= bool_or_expr(A) TOK_QUESTION expr(B) TOK_COLON cond_expr(C). {
N = conditional(A, B, C);
}

bool_or_expr(N) ::= bool_and_expr(A). {
N = A;
}

bool_or_expr(N) ::= bool_or_expr(A) TOK_OROR bool_and_expr(B). {
FOLD_BINARY(N, op_bor, A, B, a || b);
}

bool_and_expr(N) ::= equal_expr(A). {
N = A;
}

bool_and_expr(N) ::= bool_and_expr(A) TOK_ANDAND equal_expr(B). {
FOLD_BINARY(N, op_band, A, B, a && b);
}

equal_expr(N) ::= rel_expr(A). {
N = A;
}
Expand Down Expand Up @@ -485,6 +507,14 @@ primary_expr(N) ::= TOK_MIN TOK_LPAREN expr(A) TOK_COMMA expr(B) TOK_RPAREN. {
FOLD_BINARY(N, op_min, A, B, a < b ? a : b);
}

primary_expr(N) ::= TOK_BAND TOK_LPAREN expr(A) TOK_COMMA expr(B) TOK_RPAREN. {
FOLD_BINARY(N, op_band, A, B, a && b);
}

primary_expr(N) ::= TOK_BOR TOK_LPAREN expr(A) TOK_COMMA expr(B) TOK_RPAREN. {
FOLD_BINARY(N, op_bor, A, B, a || b);
}


/* ----- Trinary functions ------------------------------------------------- */

Expand Down Expand Up @@ -567,7 +597,9 @@ unary(O) ::= TOK_SQRT(I). { O = I; }
unary(O) ::= TOK_BNOT(I). { O = I; }

binary(O) ::= TOK_ABOVE(I). { O = I; }
binary(O) ::= TOK_BAND(I). { O = I; }
binary(O) ::= TOK_BELOW(I). { O = I; }
binary(O) ::= TOK_BOR(I). { O = I; }
binary(O) ::= TOK_EQUAL(I). { O = I; }
binary(O) ::= TOK_MAX(I). { O = I; }
binary(O) ::= TOK_MIN(I). { O = I; }
Expand Down
28 changes: 28 additions & 0 deletions src/compiler/ptest/ptest.c
Expand Up @@ -15,6 +15,7 @@
#include <string.h>

#include "fpvm/pfpu.h"
#include "fpvm/schedulers.h"

#include "../parser_helper.h"
#include "../parser.h"
Expand Down Expand Up @@ -343,6 +344,33 @@ static void compile(const char *pgm)
}


static void compile_raw(const char *pgm)
{
struct patch patch;
struct compiler_sc sc = {
.p = &patch
};
struct patch *p;

memset(&patch, 0, sizeof(patch));
patch.rmc = report;

if (!parse_patch(&sc, pgm)) {
symtab_free();
exit(1);
}
patch.perframe_prog_length = fpvm_default_schedule(&sc.pfv_fragment,
(unsigned *) patch.perframe_prog,
(unsigned *) patch.perframe_regs);
patch.pervertex_prog_length = fpvm_default_schedule(&sc.pvv_fragment,
(unsigned *) patch.pervertex_prog,
(unsigned *) patch.pervertex_regs);

if (!quiet)
show_patch(p);
}


static void free_buffer(void)
{
free((void *) buffer);
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/scanner.re
Expand Up @@ -100,8 +100,10 @@ int scan(struct scanner *s)

<N>"above" { return TOK_ABOVE; }
<N>"abs" { return TOK_ABS; }
<N>"band" { return TOK_BAND; }
<N>"below" { return TOK_BELOW; }
<N>"bnot" { return TOK_BNOT; }
<N>"bor" { return TOK_BOR; }
<N>"cos" { return TOK_COS; }
<N>"equal" { return TOK_EQUAL; }
<N>"f2i" { return TOK_F2I; }
Expand Down Expand Up @@ -150,6 +152,8 @@ int scan(struct scanner *s)
<N>">" { return TOK_GT; }
<N>"<=" { return TOK_LE; }
<N>">=" { return TOK_GE; }
<N>"&&" { return TOK_ANDAND; }
<N>"||" { return TOK_OROR; }

<N,FNAME1>"=" { if (YYGETCONDITION() == yycFNAME1)
YYSETCONDITION(yycFNAME2);
Expand Down

0 comments on commit 0e6cf55

Please sign in to comment.