codesparser.y.in 6.5 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

7 8 9 10 11 12
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <fcntl.h>
13 14
#include "codes/CodesIOKernelTypes.h"
#include "codes/CodesIOKernelContext.h"
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

/* prototypes */
nodeType *opr(int64_t oper, int64_t nops, ...);
nodeType *id(int64_t i);
nodeType *con(int64_t value);
void freeNode(nodeType *p);
int64_t ex(nodeType *p);

int64_t * sym = NULL; /* symbol table */
int64_t * var = NULL;
int * inst_ready = NULL;
int * group_rank = NULL;
int * group_size = NULL;
%}

/* start autogenerated code from CODES build system */
@CODES_PURE_PARSER_DEFINES@
@CODES_PUSH_PARSER_DEFINES@
/* end autogenerated code from CODES build system */

%name-prefix="CodesIOKernel_"
%locations
%defines
%error-verbose

%parse-param { CodesIOKernelContext * context }
%lex-param { void * scanner }

%union {
    int64_t iValue;                 /* integer value */
    int64_t sIndex;              /* symbol table index */
    nodeType *nPtr;             /* node pointer */
};

%token <iValue> INTEGER
%token <sIndex> VARIABLE
%token WHILE IF PRINT
%token WRITE WRITEAT WRITE_ALL WRITEAT_ALL READ READAT READ_ALL READAT_ALL SYNC SLEEP OPEN CLOSE DELETE FLUSH SEEK EXIT
%nonassoc IFX
%nonassoc ELSE

%left GE LE EQ NE '>' '<'
%left '+' '-'
%left '*' '/' '%'
%left GETGROUPRANK GETGROUPSIZE GETCURTIME GETGROUPID GETNUMGROUPS
%nonassoc UMINUS

%type <nPtr> stmt expr stmt_list

%{
65
	#include "codes/CodesIOKernelContext.h"
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

	int CodesIOKernel_lex(YYSTYPE * lvalp, YYLTYPE * llocp, void * scanner);

	void CodesIOKernel_error(YYLTYPE * locp, CodesIOKernelContext * context, const char * err)
	{
		fprintf(stderr, "%i error = %s\n", locp->first_line, err);
	}

	#define scanner context->scanner_
%}

%%

program:
        function                { /*exit(0)*/; }
        ;

function:
          function stmt         { ex($2); freeNode($2); }
        | /* NULL */
        ;

stmt:
          ';'                            { $$ = opr(';', 2, NULL, NULL); }
        | expr ';'                       { $$ = $1; }
        | EXIT expr ';'                	 { $$ = opr(EXIT, 1, $2); }
        | PRINT expr ';'                 { $$ = opr(PRINT, 1, $2); }
        | WRITE expr ',' expr ';'        { $$ = opr(WRITE, 2, $2, $4); }
        | WRITE_ALL expr ',' expr ';'        { $$ = opr(WRITE_ALL, 2, $2, $4); }
        | WRITEAT expr ',' expr ',' expr ';'      { $$ = opr(WRITEAT, 3, $2, $4, $6); }
        | WRITEAT_ALL expr ',' expr ',' expr ';'      { $$ = opr(WRITEAT_ALL, 3, $2, $4, $6); }
        | READ expr ',' expr ';'         { $$ = opr(READ, 2, $2, $4); }
        | READ_ALL expr ',' expr ';'         { $$ = opr(READ_ALL, 2, $2, $4); }
        | READAT expr ',' expr ',' expr ';'       { $$ = opr(READAT, 3, $2, $4, $6 ); }
        | READAT_ALL expr ',' expr ',' expr ';'       { $$ = opr(READAT_ALL, 3, $2, $4, $6 ); }
        | SYNC expr ';'                  { $$ = opr(SYNC, 1, $2); }
        | SLEEP expr ';'                 { $$ = opr(SLEEP, 1, $2); }
        | OPEN expr ';'                  { $$ = opr(OPEN, 1, $2); }
        | CLOSE expr ';'                 { $$ = opr(CLOSE, 1, $2); }
        | DELETE expr ';'                { $$ = opr(DELETE, 1, $2); }
        | FLUSH expr ';'                 { $$ = opr(FLUSH, 1, $2); }
        | SEEK expr ',' expr ';'         { $$ = opr(SEEK, 2, $2, $4); }
        | VARIABLE '=' expr ';'          { $$ = opr('=', 2, id($1), $3); }
        | WHILE '(' expr ')' stmt        { $$ = opr(WHILE, 2, $3, $5); }
        | IF '(' expr ')' stmt %prec IFX { $$ = opr(IF, 2, $3, $5); }
        | IF '(' expr ')' stmt ELSE stmt { $$ = opr(IF, 3, $3, $5, $7); }
        | '{' stmt_list '}'              { $$ = $2; }
        ;

stmt_list:
          stmt                  { $$ = $1; }
        | stmt_list stmt        { $$ = opr(';', 2, $1, $2); }
        ;

expr:
          INTEGER               { $$ = con($1); }
        | VARIABLE              { $$ = id($1); }
        | '-' expr %prec UMINUS { $$ = opr(UMINUS, 1, $2); }
        | expr '+' expr         { $$ = opr('+', 2, $1, $3); }
        | expr '-' expr         { $$ = opr('-', 2, $1, $3); }
        | expr '*' expr         { $$ = opr('*', 2, $1, $3); }
        | expr '/' expr         { $$ = opr('/', 2, $1, $3); }
        | expr '%' expr         { $$ = opr('%', 2, $1, $3); }
        | expr '<' expr         { $$ = opr('<', 2, $1, $3); }
        | expr '>' expr         { $$ = opr('>', 2, $1, $3); }
        | expr GE expr          { $$ = opr(GE, 2, $1, $3); }
        | expr LE expr          { $$ = opr(LE, 2, $1, $3); }
        | expr NE expr          { $$ = opr(NE, 2, $1, $3); }
        | expr EQ expr          { $$ = opr(EQ, 2, $1, $3); }
        | GETGROUPRANK expr     { $$ = opr(GETGROUPRANK, 1, $2); }
        | GETGROUPSIZE expr     { $$ = opr(GETGROUPSIZE, 1, $2); }
        | GETCURTIME            { $$ = opr(GETCURTIME, 0); }
        | GETGROUPID            { $$ = opr(GETGROUPID, 0); }
        | GETNUMGROUPS          { $$ = opr(GETNUMGROUPS, 0); }
        | '(' expr ')'          { $$ = $2; }
        ;

%%

#define SIZEOF_NODETYPE ((char *)&p->con - (char *)p)

nodeType * con(int64_t value)
{
    nodeType *p;
    size_t nodeSize;

    /* allocate node */
    nodeSize = SIZEOF_NODETYPE + sizeof(conNodeType);
    if((p = malloc(nodeSize)) == NULL)
    {
        fprintf(stderr, "out of memory\n");
    }

    /* copy information */
    p->type = typeCon;
    p->con.value = value;

    return p;
}

nodeType * id(int64_t i)
{
    nodeType *p;
    size_t nodeSize;

    /* allocate node */
    nodeSize = SIZEOF_NODETYPE + sizeof(idNodeType);
    if((p = malloc(nodeSize)) == NULL)
    {
        fprintf(stderr, "out of memory\n");
    }

    /* copy information */
    p->type = typeId;
    p->id.i = i;

    return p;
}

nodeType * opr(int64_t oper, int64_t nops, ...)
{
    va_list ap;
    nodeType *p;
    size_t nodeSize;
    int64_t i;

    /* allocate node */
    nodeSize = SIZEOF_NODETYPE + sizeof(oprNodeType) +
        (nops - 1) * sizeof(nodeType*);

    if ((p = malloc(nodeSize)) == NULL)
    {
        fprintf(stderr, "out of memory\n");
    }

    /* copy information */
    p->type = typeOpr;
    p->opr.oper = oper;
    p->opr.nops = nops;
    va_start(ap, nops);
    for (i = 0; i < nops; i++)
    {
        p->opr.op[i] = va_arg(ap, nodeType*);
    }
    va_end(ap);
    return p;
}

void freeNode(nodeType *p)
{
    int64_t i;

    if (!p)
    {
        return;
    }

    if(p->type == typeOpr)
    {
        for(i = 0; i < p->opr.nops; i++)
        {
            freeNode(p->opr.op[i]);
        }
    }
    free(p);
}