10 array correct

 avatar
unknown
plain_text
a year ago
9.4 kB
5
Indexable
/* Definition section */
%{
    #include "compiler_common.h"
    #include "compiler_util.h"
    #include "main.h"

    int yydebug = 1;
%}

/* Union to define types for semantic values */
%union {
    ObjectType var_type;

    bool b_var;
    int i_var;
    float f_var;
    char *s_var;

    Object object_val;
}

/* Token definitions without return types */
%token COUT
%token SHR SHL BAN BOR BNT BXO ADD SUB MUL DIV REM NOT GTR LES GEQ LEQ EQL NEQ LAN LOR
%token VAL_ASSIGN ADD_ASSIGN SUB_ASSIGN MUL_ASSIGN DIV_ASSIGN REM_ASSIGN BAN_ASSIGN BOR_ASSIGN BXO_ASSIGN SHR_ASSIGN SHL_ASSIGN INC_ASSIGN DEC_ASSIGN
%token IF ELSE FOR WHILE RETURN BREAK CONTINUE
%token '{' '}' '(' ')' '[' ']'
%token ',' ':' ';'

/* Token definitions with return types */
%token <var_type> VARIABLE_T
%token <b_var> BOOL_LIT
%token <i_var> INT_LIT
%token <f_var> FLOAT_LIT
%token <s_var> STR_LIT
%token <s_var> IDENT

/* Nonterminal definitions with return types */
%type <object_val> Expression
%type <object_val> Assign
%type <object_val> ARG
%type <object_val> num
%type <object_val> CALL
%type <object_val> boolean
%type <object_val> id

/* Operator precedence and associativity */
%left LOR
%left LAN
%left GEQ
%left NEQ EQL
%left GTR LES
%left ADD SUB
%left MUL DIV REM
%left VAL_ASSIGN
%left '(' ')'

/* Start symbol */
%start Program

%%

/* Grammar section */

Program
    : { pushScope(); } GlobalStmtList { dumpScope(); }
    | /* Empty file */
    ;

GlobalStmtList
    : GlobalStmtList GlobalStmt
    | GlobalStmt
    ;

GlobalStmt
    : FunctionDefStmt
    ;


/* Function definitions */
FunctionDefStmt
    : VARIABLE_T IDENT { createFunction($<var_type>1, $<s_var>2);} { pushScope(); } '(' FunctionParameterStmtList ')' '{' StmtList '}' { dumpScope(); }
    | VARIABLE_T IDENT '[' ']' { createFunction($<var_type>1, $<s_var>2); } '(' FunctionParameterStmtList ')' '{' StmtList '}' { dumpScope(); }
    ;

FunctionParameterStmtList
    : FunctionParameterStmtList ',' FunctionParameterStmt
    | FunctionParameterStmt
    | /* Empty function parameter */
    ;

FunctionParameterStmt
    : VARIABLE_T IDENT { pushFunParm($<var_type>1, $<s_var>2, VAR_FLAG_DEFAULT); }
    | VARIABLE_T IDENT '[' ']' { pushFunParm($<var_type>1, $<s_var>2, VAR_FLAG_DEFAULT); }
    ;

/* Statement lists and individual statements */
StmtList
    : StmtList Stmt
    | Stmt
    ;

Stmt
    : ';'
    | COUT CoutParmListStmt ';' { stdoutPrint(); }
    | RETURN Expression ';' { printf("RETURN\n"); }
    | VARIABLE_T {setVarT($<var_type>1);} MultiVar';'
    | Assign ';'
    | IF '(' Expression ')' {printf("IF\n");} {pushScope();} '{' StmtList '}' {dumpScope();} Elsepart
    | WHILE {printf("WHILE\n");}'(' Expression ')'  {pushScope();} '{' StmtList '}' {dumpScope();}
    | FOR {printf("FOR\n");} {pushScope();} '(' StmtForList ')' '{' StmtList '}' {dumpScope();}
    | VARIABLE_T IDENT'[' num ']' VAL_ASSIGN '{' ARGS '}' {printf("create array: %llu\n", $<object_val>4.value); createVariable($1, $2, VAR_FLAG_DEFAULT);}
;

StmtForList
    : StmtForList StmtFor
    | StmtFor
;

StmtFor
    :';'
    | VARIABLE_T {setVarT($<var_type>1);} MultiVar';'
    | Expression ';'
    | Expression
    | Assign
;

Elsepart
    : ELSE {printf("ELSE\n");} {pushScope();} '{' StmtList '}' {dumpScope();}
    |
;

MultiVar
    : MultiVar ',' IDENT {createVNoT($<s_var>3, VAR_FLAG_DEFAULT); }
    | MultiVar ',' IDENT VAL_ASSIGN Expression {
        createVNoT($<s_var>3, VAR_FLAG_DEFAULT);
        Object *variable = findVariable($<s_var>3);
        if (variable != NULL) {
            objectValueAssign(variable, &$<object_val>5, NULL);
        }
    }
    | IDENT VAL_ASSIGN Expression {
        createVNoT($<s_var>1, VAR_FLAG_DEFAULT);
        Object *variable = findVariable($<s_var>1);
        if (variable != NULL) {
            objectValueAssign(variable, &$<object_val>3, NULL);
        }
    }
    | IDENT { createVNoT($<s_var>1, VAR_FLAG_DEFAULT);}
    | '(' IDENT ')'{ createVNoT($<s_var>2, VAR_FLAG_DEFAULT);}
;


/* Print statement parameter list */
CoutParmListStmt
    : CoutParmListStmt SHL Expression { pushFunInParm(&$<object_val>3); }
    | SHL Expression { pushFunInParm(&$<object_val>2); }
;

/* Expressions */
Expression
    : Assign ';'
    | '(' Expression ')' {Object obj; obj.type = $2.type; $$ = obj;}
    | Expression LOR Expression {printf("LOR\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression LAN Expression {printf("LAN\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression BOR Expression {printf("BOR\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression BXO Expression {printf("BXO\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression BAN Expression {printf("BAN\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression NEQ Expression {printf("NEQ\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression EQL Expression {printf("EQL\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression GTR Expression {printf("GTR\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression LES Expression {printf("LES\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression GEQ Expression {printf("GEQ\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression LEQ Expression {printf("LEQ\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression SHR Expression {printf("SHR\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression SUB Expression {printf("SUB\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression ADD Expression {printf("ADD\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression REM Expression {printf("REM\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression DIV Expression {printf("DIV\n");Object obj; obj.type = $3.type; $$ = obj;}
    | Expression MUL Expression {printf("MUL\n");Object obj; obj.type = $3.type; $$ = obj;}
    | ARG {Object obj; obj.type = $1.type; $$ = obj;}
    | Expression INC_ASSIGN { printf("INC_ASSIGN\n"); Object obj; obj.type = $1.type; $$ = obj; }
    | Expression DEC_ASSIGN { printf("DEC_ASSIGN\n"); Object obj; obj.type = $1.type; $$ = obj; }
    | CALL { Object obj; obj.type = $1.type; $$ = obj; }
    | '(' VARIABLE_T ')' '(' IDENT ')'{ idFunc($<s_var>5); Object obj; obj.type = findVarType($<s_var>5); $$ = obj; } {changeVType($<var_type>2);}  // this is the focus
    | '(' VARIABLE_T ')' IDENT { idFunc($<s_var>4); Object obj; obj.type = findVarType($<s_var>4); $$ = obj; } {changeVType($<var_type>2);}
    | '(' VARIABLE_T ')' num { Object obj; obj.type = $2; $$ = obj; }{changeVType($<var_type>2);}
    | '(' VARIABLE_T ')' '(' num ')'{ Object obj; obj.type = $2; $$ = obj; }{changeVType($<var_type>2);}
    | IDENT '(' ARGS ')' {idFunc($1); printcall($1);}
    ;


/* Assignment expressions */
Assign
    : Expression VAL_ASSIGN Expression { printf("EQL_ASSIGN\n"); }
    | Expression ADD_ASSIGN Expression { printf("ADD_ASSIGN\n"); }
    | Expression SUB_ASSIGN Expression { printf("SUB_ASSIGN\n"); }
    | Expression MUL_ASSIGN Expression { printf("MUL_ASSIGN\n"); }
    | Expression DIV_ASSIGN Expression { printf("DIV_ASSIGN\n"); }
    | Expression REM_ASSIGN Expression { printf("REM_ASSIGN\n"); }
    | Expression SHR_ASSIGN Expression { printf("SHR_ASSIGN\n"); }
    | Expression SHL_ASSIGN Expression { printf("SHL_ASSIGN\n"); }
    | Expression BAN_ASSIGN Expression { printf("BAN_ASSIGN\n"); }
    | Expression BOR_ASSIGN Expression { printf("BOR_ASSIGN\n"); }
    | Expression BXO_ASSIGN Expression { printf("BOX_ASSIGN\n"); }
    | Expression INC_ASSIGN Expression { printf("INC_ASSIGN\n"); }
    | Expression DEC_ASSIGN Expression { printf("DEC_ASSIGN\n"); }
    ;

/* Function call expressions */
CALL
    : IDENT '(' ARGS ')' { Object obj; obj.type = OBJECT_TYPE_FLOAT; $$ = obj; }
    ;

/* Numeric expressions */
num
    : SUB num { printf("NEG\n"); }
    | NOT num { printf("NOT\n"); }
    | BNT num { printf("BNT\n"); }
    | FLOAT_LIT {printf("FLOAT_LIT %f\n", $1);Object obj; obj.type = OBJECT_TYPE_FLOAT; $$ = obj;}
    | INT_LIT {printf("INT_LIT %d\n", $1);Object obj; obj.value = $1 ; obj.type = OBJECT_TYPE_INT; $$ = obj;}
    ;

/* Argument list for function calls */
ARGS
    : ARGS ',' ARG
    | ARG
    ;

/* Individual argument in a function call */
ARG
    : STR_LIT { printf("STR_LIT \"%s\"\n", $1); Object obj; obj.type = OBJECT_TYPE_STR; $$ = obj;}
    | boolean { $$ = $1; Object obj; obj.type = $1.type; $$ = obj; }
    | num {$$ = $1; Object obj; obj.type = $1.type; $$ = obj; }
    | id {$$ = $1; Object obj; obj.type = $1.type; $$ = obj; }
    ;

/* Boolean expressions */
boolean
    : NOT boolean { printf("NOT\n"); Object obj; obj.type = OBJECT_TYPE_BOOL; $$ = obj; }
    | BNT boolean { printf("BNT\n"); Object obj; obj.type = OBJECT_TYPE_BOOL; $$ = obj; }
    | BOOL_LIT { printf("BOOL_LIT %s\n", $1 ? "TRUE" : "FALSE"); Object obj; obj.type = OBJECT_TYPE_BOOL; $$ = obj; }
    ;

/* Identifier expressions */
id
    : SUB id { printf("NEG\n"); }
    | NOT id { printf("NOT\n"); }
    | BNT id { printf("BNT\n"); }
    | IDENT { idFunc($<s_var>1); Object obj; obj.type = findVarType($<s_var>1); $$ = obj; }
    | IDENT '[' ARG ']' '[' ARG ']' { idFunc($1); Object obj; obj.type = findVarType($<s_var>1); $$ = obj; }
    | IDENT '[' ARG ']' { idFunc($1); Object obj; obj.type = findVarType($<s_var>1); $$ = obj; }
    ;

%%

/* C code section for supporting functions and utilities */
Editor is loading...
Leave a Comment