int main() { Init(); CustomKernel mandelbrot; MODULE_INT_START(mandelbrot) MOUDLE_INPUT_FLOAT(x) MOUDLE_INPUT_FLOAT(y) MODULE_INPUT_INT(maxItt) MODULE_MAIN FLOAT(a = 0.0f) FLOAT(b = 0.0f) INT(i = 0) INT(j = 0) WHILE(i < maxItt) BEGIN IF( a * a + b * b > 4) BEGIN STATEMENT(j = 1) BREAK END FLOAT(aTemp = a) STATEMENT(a = a * a - b * b + x) STATEMENT(b = 2 * aTemp * b + y) STATEMENT(i++) END MODULE_RETURN(i) MODULE_END CustomKernel mandelbrotRun; KERNEL_START(mandelbrotRun) KERNEL_INPUT_INT(itt) INCLUDE(mandelbrot) KERNEL_MAIN INT(val) FLOAT(x = (MYCOORD.x - 0.5) * 4) FLOAT(y = (MYCOORD.y - 0.5) * 4) CALL(val, mandelbrot, x, y, itt) KERNEL_OUTPUT(1.5 / float(val)) KERNEL_END DeviceVector dVOut(1024, 1024); HostVector hVOut; mandelbrotRun.Input(500); mandelbrotRun.Output(dVOut); mandelbrotRun.Execute(); hVOut = dVOut; ImageProcessing::SaveImageFile("mandelbrot.bmp", hVOut); getchar(); return 0; }
void BLOCK(void){ /*block = defstartsymbol statementlist defendsymbol | statement*/ switch(l){ case '{': case KW_BEGIN: DEFSTARTSYMBOL(); STATEMENTLIST(); DEFENDSYMBOL(); return; case KW_IF: case KW_RETURN: case KW_PRINTF: case ID: STATEMENT(); return; default: StdError(__func__); } }
/******************************************************************************* *** FUNCTION STAT_LIST() ******************************************************************************* *** DESCRIPTION : Processes STAT_LIST grammar rule. *** *** STAT_LIST -> STATEMENT ; STAT_LIST | *** e *** ******************************************************************************/ void RecursiveParser::STAT_LIST() { if (global->Token == Global::idt || global->Token == Global::cint || global->Token == Global::coutt) { STATEMENT(); match(Global::semicolont); STAT_LIST(); } }
void BLOCK() { if(TOKEN == constsym) { do { GETTOKEN(); if(TOKEN != identsym) { ERROR("Error number 4, const must be followed by identifier."); } GETTOKEN(); if(TOKEN == becomessym) { ERROR("Error number 1, use = instead of :="); } if(TOKEN != eqlsym) { ERROR("Error number 2, identifier must be followed by ="); } GETTOKEN(); if(TOKEN != numbersym) { ERROR("Error number 3, = must be followed by a number."); } ENTER(1); GETTOKEN(); } while(TOKEN == commasym); if(TOKEN != semicolonsym) { ERROR("Error number 5, semicolon or comma missing."); } GETTOKEN(); } if(TOKEN == varsym) { do { GETTOKEN(); if(TOKEN != identsym) { ERROR("Error number 4, var must be followed by identifier."); } ENTER(2); GETTOKEN(); } while(TOKEN == commasym); if(TOKEN != semicolonsym) { ERROR("Error number 5, semicolon or comma missing."); } GETTOKEN(); } //procedure section (will not be used in module 3) while(TOKEN == procsym) { GETTOKEN(); if(TOKEN != identsym) { ERROR("Error number 4, procedure must be followed by identifier."); } ENTER(3); GETTOKEN(); if(TOKEN != semicolonsym) { ERROR("Error number 6, incorrect symbol after procedure declaration."); } GETTOKEN(); BLOCK(); if(TOKEN != semicolonsym) { //may need to be a different error message ERROR("Error number 5, semicolon or comma missing."); } GETTOKEN(); } //INC O (4+numOfVariables) printToFile(6,0,4+numOfVariables); lines++; STATEMENT(); }
void STATEMENT() { //initialization if(TOKEN == identsym) { //stores the name of the identifier that will be initialized char name[12]; strcpy(name, IDENTIFIER); //if identifier is not a variable, produce error if(getSymbol(IDENTIFIER).kind != 2) { ERROR("Error number 12, assignment to constant or procedure not allowed."); } GETTOKEN(); if(TOKEN != becomessym) { ERROR("Error number 13, assignment operator expected."); } GETTOKEN(); EXPRESSION(); symbol current = getSymbol(name); //STO 0 M printToFile(4,current.level,current.addr); lines++; } //procedure call (not in tiny PL/0) else if(TOKEN == callsym) { GETTOKEN(); if(TOKEN != identsym) { ERROR("Error number 14, call must be followed by an identifier."); } //if the identifier is not a procedure, produce an error if(getSymbol(IDENTIFIER).kind != 3) { ERROR("Error number 15, call of a constant or variable is meaningless."); } GETTOKEN(); } //a group of statements else if(TOKEN == beginsym) { GETTOKEN(); STATEMENT(); while(TOKEN == semicolonsym) { GETTOKEN(); STATEMENT(); } if(TOKEN != endsym) { ERROR("Error number 26, end is expected."); } GETTOKEN(); } else if(TOKEN == ifsym) { GETTOKEN(); CONDITION(); //top of the stack has whether it is true or false if(TOKEN != thensym) { ERROR("Error number 16, then expected."); } //after the condition, count how many instructions are written int currentLines = lines; inConditional++; fpos_t filePos; fgetpos(ifp, &filePos); //loop ensures this is done twice int i; for(i = 0; i < 2; i++) { if(i == 1) { inConditional--; //make branch here (lines contains the line that you jump to if the condition is not met) //printToFile() //JPC 0 M = lines printToFile(8,0,lines); //returns the file to the previous position fsetpos(ifp, &filePos); lines = currentLines; //Lines increment for prinToFile used in for loop //lines++; } lines++; GETTOKEN(); STATEMENT(); } } else if(TOKEN == whilesym) { int jumpBackLine = lines; GETTOKEN(); CONDITION(); //top of the stack has whether it is true or false if(TOKEN != dosym) { ERROR("Error number 18, do expected."); } //after the condition, count how many instructions are written int currentLines = lines; inConditional++; fpos_t filePos; fgetpos(ifp, &filePos); //loop ensures this is done twice int i; for(i = 0; i < 2; i++) { if(i == 1) { inConditional--; //make branch here (lines + 1 contains the line that you jump to if the condition is not met) //printToFile() //JPC 0 M = l printToFile(8,0,lines + 1); //returns the file to the previous position fsetpos(ifp, &filePos); lines = currentLines; //Lines increment for the printToFile used in for loop //lines++; } //the line for the branch is added lines++; GETTOKEN(); STATEMENT(); } //JMP 0 M = jumpBackLines printToFile(7,0,jumpBackLine); lines++; } }
void BLOCK(int enterIntoTable) { lexiLevel++; if(TOKEN == constsym) { do { GETTOKEN(); if(TOKEN != identsym) { ERROR("Error number 4, const must be followed by identifier."); } GETTOKEN(); if(TOKEN == becomessym) { ERROR("Error number 1, use = instead of :="); } if(TOKEN != eqlsym) { ERROR("Error number 2, identifier must be followed by ="); } GETTOKEN(); if(TOKEN != numbersym) { ERROR("Error number 3, = must be followed by a number."); } if(enterIntoTable == 0) { ENTER(1); } GETTOKEN(); } while(TOKEN == commasym); if(TOKEN != semicolonsym) { ERROR("Error number 5, semicolon or comma missing."); } GETTOKEN(); } if(TOKEN == varsym) { do { GETTOKEN(); if(TOKEN != identsym) { ERROR("Error number 4, var must be followed by identifier."); } if(enterIntoTable == 0) { ENTER(2); } GETTOKEN(); } while(TOKEN == commasym); if(TOKEN != semicolonsym) { ERROR("Error number 5, semicolon or comma missing."); } GETTOKEN(); } while(TOKEN == procsym) { GETTOKEN(); if(TOKEN != identsym) { ERROR("Error number 4, procedure must be followed by identifier."); } if(enterIntoTable == 0) { ENTER(3); } GETTOKEN(); if(TOKEN != semicolonsym) { ERROR("Error number 6, incorrect symbol after procedure declaration."); } //saves the current position, for the jmp statement int currentLines = lines; inConditional++; fpos_t filePos; fgetpos(ifp, &filePos); int i; for(i = 0; i < 2; i++) { if(i == 1) { inConditional--; //JMP 0 M = lines printToFile(7,0,lines); //returns the file to the previous position fsetpos(ifp, &filePos); lines = currentLines; } lines++; GETTOKEN(); BLOCK(i+enterIntoTable); //return from the procedure call printToFile(2, 0, 0); lines++; } //semicolon is needed after a procedure if(TOKEN != semicolonsym) { //may need to be a different error message ERROR("Error number 5, semicolon or comma missing."); } GETTOKEN(); } //INC O (4+numOfVariables) printToFile(6,0,4+numOfVariablesInLexiLevel()); lines++; STATEMENT(); lexiLevel--; }
void STATEMENT() { //initialization if(TOKEN == identsym) { //stores the name of the identifier that will be initialized char name[12]; strcpy(name, IDENTIFIER); //if identifier is not a variable, produce error if(getSymbol(IDENTIFIER).kind != 2) { ERROR("Error number 12, assignment to constant or procedure not allowed."); } GETTOKEN(); if(TOKEN != becomessym) { ERROR("Error number 13, assignment operator expected."); } GETTOKEN(); EXPRESSION(); symbol current = getSymbol(name); //STO L M printToFile(4,lexiLevel-current.level,current.addr); lines++; } else if(TOKEN == callsym) { GETTOKEN(); if(TOKEN != identsym) { ERROR("Error number 14, call must be followed by an identifier."); } //if the identifier is not a procedure, produce an error if(getSymbol(IDENTIFIER).kind != 3) { ERROR("Error number 15, call of a constant or variable is meaningless."); } symbol current = getSymbol(IDENTIFIER); //CAL L M printToFile(5, lexiLevel-current.level, current.addr); lines++; GETTOKEN(); } //a group of statements else if(TOKEN == beginsym) { GETTOKEN(); STATEMENT(); while(TOKEN == semicolonsym) { GETTOKEN(); STATEMENT(); } if(TOKEN != endsym) { printf("Line: %d %s\n", lines, IDENTIFIER); ERROR("Error number 26, end is expected."); } GETTOKEN(); } else if(TOKEN == ifsym) { GETTOKEN(); CONDITION(); //top of the stack has whether it is true or false if(TOKEN != thensym) { ERROR("Error number 16, then expected."); } //after the condition, count how many instructions are written int currentLines = lines; inConditional++; fpos_t filePos; fgetpos(ifp, &filePos); //loop ensures this is done twice int i; for(i = 0; i < 2; i++) { if(i == 1) { inConditional--; //JPC 0 M = lines printToFile(8,0,lines); //returns the file to the previous position fsetpos(ifp, &filePos); lines = currentLines; } lines++; GETTOKEN(); STATEMENT(); fpos_t filePos2; fgetpos(ifp, &filePos2); if(TOKEN == semicolonsym) { GETTOKEN(); } //we need another line for the jump if(i == 0 && TOKEN == elsesym) { lines++; } else if(TOKEN != elsesym) { TOKEN = semicolonsym; fsetpos(ifp, &filePos2); } } if(TOKEN == elsesym) { //gets the position of the else currentLines = lines; inConditional++; fgetpos(ifp, &filePos); for(i = 0; i < 2; i++) { if(i == 1) { inConditional--; //jmp end of loop printToFile(7,0,lines); //returns the file to the previous position fsetpos(ifp, &filePos); lines = currentLines; } lines++; GETTOKEN(); STATEMENT(); } } } else if(TOKEN == whilesym) { int jumpBackLine = lines; GETTOKEN(); CONDITION(); //top of the stack has whether it is true or false if(TOKEN != dosym) { ERROR("Error number 18, do expected."); } //after the condition, count how many instructions are written int currentLines = lines; inConditional++; fpos_t filePos; fgetpos(ifp, &filePos); //loop ensures this is done twice int i; for(i = 0; i < 2; i++) { if(i == 1) { inConditional--; //make branch here (lines + 1 contains the line that you jump to if the condition is not met) //printToFile() //JPC 0 M = l printToFile(8,0,lines + 1); //returns the file to the previous position fsetpos(ifp, &filePos); lines = currentLines; //Lines increment for the printToFile used in for loop //lines++; } //the line for the branch is added lines++; GETTOKEN(); STATEMENT(); } //JMP 0 M = jumpBackLines printToFile(7,0,jumpBackLine); lines++; } else if(TOKEN == readsym) { GETTOKEN(); if(TOKEN != identsym) { ERROR("Error number 28, identifier expected after read."); } symbol current = getSymbol(IDENTIFIER); if(current.kind != 2) { ERROR("Error number 29, writing to a constant or procedure is not allowed."); } //SIO 0 1 //STO 0 M printToFile(9, 0, 1); printToFile(4,lexiLevel-current.level,current.addr); lines += 2; GETTOKEN(); } else if(TOKEN == writesym) { GETTOKEN(); if(TOKEN != identsym) { ERROR("Error number 30, identifier expected after write."); } symbol current = getSymbol(IDENTIFIER); if(current.kind == 3) { ERROR("Error number 31, cannot write a procedure."); } if(current.kind == 2) { //LOD L M printToFile(3, lexiLevel-current.level, current.addr); } if(current.kind == 1) { //LIT 0 val printToFile(1, 0, current.val); } //SIO 0 0 printToFile(9, 0, 0); lines +=2; GETTOKEN(); } }