public void evalInstruction(InstructionExpression instr) { if (instr is ValueExpression) { valueExp = (ValueExpression)instr; if (valueExp.type == ObjectType.intType) { variables.Add(valueExp.name, new ObjectExpression(valueExp.type, 0)); } else if (valueExp.type == ObjectType.booleanType) { variables.Add(valueExp.name, new ObjectExpression(valueExp.type, false)); } else if (valueExp.type == ObjectType.stringType) { variables.Add(valueExp.name, new ObjectExpression(valueExp.type, "")); } if (valueExp.value != null) { variables[valueExp.name] = new ObjectExpression(valueExp.type, evalSimpleExpression(valueExp.value).value); } return; } else if (instr is AssignExpression) { assignExp = (AssignExpression)instr; variables[assignExp.name] = evalSimpleExpression(assignExp.value); return; } else if (instr is OpenMsgDialogInstruction) { gScreen.msgBox.openBox(); return; } else if (instr is MsgDisplayInstruction) { msgExp = (MsgDisplayInstruction)instr; o1 = evalSimpleExpression(msgExp.msg); if (o1.type != ObjectType.stringType) { return; } gScreen.msgBox.setText((string)o1.value); return; } else if (instr is NextMsgDialogInstruction) { shouldEvalSeq = false; gScreen.msgBox.showNextButton(); return; } else if (instr is CloseMsgDialogInstruction) { shouldEvalSeq = false; gScreen.msgBox.showCloseButton(); return; } else if (instr is SwitchCharacterInstruction) { try { gScreen.msgBox.switchCharacter(IrisData.characters[((SwitchCharacterInstruction)instr).character.name]); } catch (Exception e) { return; } return; } else if (instr is RemoveCharacter) { gScreen.msgBox.removeCharacter(); return; } else if (instr is SetVariableInstruction) { setExp = (SetVariableInstruction)instr; /*if (!playerVariables.ContainsKey(setExp.variable.name)) { playerVariables.Add(setExp.variable.name, -1); } playerVariables[setExp.variable.name] = (int)(evalSimpleExpression(setExp.value).value);*/ IrisData.setPlayerVariable(setExp.variable.name, (int)(evalSimpleExpression(setExp.value).value)); return; } else if (instr is InputInstruction) { inputExp = (InputInstruction)instr; o1 = variables[inputExp.name]; shouldEvalSeq = false; if (o1.type == ObjectType.intType) { gScreen.iBox.openBox(InputBox.InputType.INT_INPUT); lastInstr = inputExp; } else if (o1.type == ObjectType.stringType) { gScreen.iBox.openBox(InputBox.InputType.STRING_INPUT); lastInstr = inputExp; } return; } else if (instr is MenuInstruction) { menuExp = (MenuInstruction)instr; shouldEvalSeq = false; gScreen.menBox.setChoices(((MenuInstruction)instr).choices); gScreen.menBox.openMenu(); lastInstr = menuExp; return; } else if (instr is PlayMediaInstruction) { playExp = (PlayMediaInstruction)instr; /*if (!IrisData.sounds.ContainsKey(playExp.name)) { return; }*/ //else if(IrisData. try { gScreen.playMedia(IrisData.sounds[playExp.name]); } catch (Exception e) { return; } return; } else if (instr is StopMediaInstruction) { gScreen.stopMedia(); return; } else if (instr is ShowImageInstruction) { showExp = (ShowImageInstruction)instr; if (showExp.image is VariableExpression) { img = IrisData.images[((VariableExpression)showExp.image).name]; } else { memberExp = (MemberAccessExpression)showExp.image; charac = IrisData.characters[memberExp.name]; img = charac.getImage(memberExp.field); } gScreen.showImage(img, showExp.position); return; } else if (instr is SetBackgroundInstruction) { bgdInstr = (SetBackgroundInstruction)instr; try { gScreen.setBackground(IrisData.backgrounds[bgdInstr.image.name]); } catch (Exception e) { return; } return; } else if (instr is CleanBackgroundInstruction) { gScreen.clearBackground(); return; } else if (instr is CleanForegroundInstruction) { gScreen.clearForeGround(); return; } else if (instr is GotoInstruction) { gotoExp = (GotoInstruction)instr; npcExp = gotoExp.npc; evalExpression(IrisData.npcs[npcExp.npc].labels[npcExp.label]); return; } }
public Expression parseExpression() { List<Expression> exps = new List<Expression>(); bool parseEnd = false; while (!parseEnd) { switch (currentToken.type) { case Token.TokenValue.IF: exps.Add(parseIfExpression()); break; case Token.TokenValue.SWITCH: exps.Add(parseSwitchExpression()); break; case Token.TokenValue.FOR: exps.Add(parseForExpression()); break; case Token.TokenValue.WHILE: exps.Add(parseWhileExpression()); break; case Token.TokenValue.DO: exps.Add(parseDoWhileExpression()); break; case Token.TokenValue.TYPE_IDENTIFIER: exps.Add(parseValueExpression()); break; case Token.TokenValue.VALUE_IDENTIFIER: nextToken(); if (currentTokenEquals(Token.TokenValue.TWOPOINTS)) { previousToken(); parseEnd = true; } else if (isAnAssignOperator(currentToken.type)) { previousToken(); exps.Add(parseAssignExpression()); nextTokenIfEquals(Token.TokenValue.SEMICOLON); } else { throw new SyntaxError(lineNumber, colNumber, Token.TokenValue.EQ); } break; //**TODO** case Token.TokenValue.SET: SetVariableInstruction setExp = new SetVariableInstruction(); nextToken(); if (currentTokenEquals(Token.TokenValue.PLAYER_VALUE_IDENTIFIER)) { setExp.variable = new CharacterVariableExpression(currentToken.stringValue); nextToken(); nextTokenIfEquals(Token.TokenValue.COMMA); setExp.value = parseSimpleExpression(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); } else { throw new SyntaxError(lineNumber, colNumber, Token.TokenValue.PLAYER_VALUE_IDENTIFIER); } exps.Add(setExp); break; case Token.TokenValue.OPENDLG: nextToken(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); exps.Add(new OpenMsgDialogInstruction()); break; case Token.TokenValue.MSG: MsgDisplayInstruction mes = new MsgDisplayInstruction(); nextToken(); mes.msg = parseSimpleExpression(); if (mes.msg.getExpressionType() != ObjectType.stringType) { throw new IncompatibleTypesException(lineNumber, colNumber, ObjectType.stringType, mes.msg.getExpressionType()); } nextTokenIfEquals(Token.TokenValue.SEMICOLON); exps.Add(mes); break; case Token.TokenValue.NEXTDLG: nextToken(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); exps.Add(new NextMsgDialogInstruction()); break; case Token.TokenValue.CLOSEDLG: nextToken(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); exps.Add(new CloseMsgDialogInstruction()); break; case Token.TokenValue.PLAY: PlayMediaInstruction play = new PlayMediaInstruction(); nextToken(); if (currentTokenEquals(Token.TokenValue.VALUE_IDENTIFIER)) { play.name = currentToken.stringValue; nextToken(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); } else { throw new SyntaxError(lineNumber, colNumber, Token.TokenValue.VALUE_IDENTIFIER); } exps.Add(play); break; case Token.TokenValue.STOP: exps.Add(new StopMediaInstruction()); nextToken(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); break; case Token.TokenValue.SHOW: ShowImageInstruction show = new ShowImageInstruction(); string position; nextToken(); if (currentTokenEquals(Token.TokenValue.VALUE_IDENTIFIER)) { show.image = parsePathExpression(); nextTokenIfEquals(Token.TokenValue.COMMA); //TODO if (currentTokenEquals(Token.TokenValue.VALUE_IDENTIFIER)) { position = currentToken.stringValue; switch (position) { case "LEFT": case "left": show.position = GameScreen.LEFT; break; case "CENTER": case "center": show.position = GameScreen.CENTER; break; case "RIGHT": case "right": show.position = GameScreen.RIGHT; break; default: //TODO throw new SyntaxError(lineNumber, colNumber, Token.TokenValue.VALUE_IDENTIFIER); } } nextTokenIfEquals(Token.TokenValue.VALUE_IDENTIFIER); nextTokenIfEquals(Token.TokenValue.SEMICOLON); } exps.Add(show); break; case Token.TokenValue.SWITCHCHAR: SwitchCharacterInstruction switchChar = new SwitchCharacterInstruction(); nextToken(); if (currentTokenEquals(Token.TokenValue.VALUE_IDENTIFIER)) { switchChar.character = new VariableExpression(currentToken.stringValue); nextToken(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); } else { throw new SyntaxError(lineNumber, colNumber, Token.TokenValue.VALUE_IDENTIFIER); } exps.Add(switchChar); break; case Token.TokenValue.SETBGD: SetBackgroundInstruction bgdInstr = new SetBackgroundInstruction(); nextToken(); if (currentTokenEquals(Token.TokenValue.VALUE_IDENTIFIER)) { bgdInstr.image = new VariableExpression(currentToken.stringValue); nextToken(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); } else { throw new SyntaxError(lineNumber, colNumber, Token.TokenValue.VALUE_IDENTIFIER); } exps.Add(bgdInstr); break; case Token.TokenValue.CLEANBGD: exps.Add(new CleanBackgroundInstruction()); nextToken(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); break; case Token.TokenValue.CLEANFGD: exps.Add(new CleanForegroundInstruction()); nextToken(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); break; case Token.TokenValue.REMOVECHAR: nextToken(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); exps.Add(new RemoveCharacter()); break; case Token.TokenValue.GOTO: GotoInstruction gotoInstr = new GotoInstruction(); nextToken(); gotoInstr.npc = parseNpcMemberExpression(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); exps.Add(gotoInstr); break; case Token.TokenValue.INPUT: InputInstruction inputInstr = new InputInstruction(); nextToken(); if (currentTokenEquals(Token.TokenValue.VALUE_IDENTIFIER)) { inputInstr.name = currentToken.stringValue; nextToken(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); } else { throw new SyntaxError(lineNumber, colNumber, Token.TokenValue.VALUE_IDENTIFIER); } exps.Add(inputInstr); break; case Token.TokenValue.MENU: MenuInstruction menuInstr = new MenuInstruction(); nextToken(); nextTokenIfEquals(Token.TokenValue.LPAR); if (currentTokenEquals(Token.TokenValue.RPAR)) { throw new EmptyExpression(lineNumber, colNumber); } if (currentTokenEquals(Token.TokenValue.STRING)) { menuInstr.addChoice(currentToken.stringValue); nextToken(); } else { throw new SyntaxError(lineNumber, colNumber, Token.TokenValue.STRING); } while (!currentTokenEquals(Token.TokenValue.RPAR)) { nextTokenIfEquals(Token.TokenValue.COMMA); if (currentTokenEquals(Token.TokenValue.STRING)) { menuInstr.addChoice(currentToken.stringValue); nextToken(); } else { throw new SyntaxError(lineNumber, colNumber, Token.TokenValue.STRING); } } nextTokenIfEquals(Token.TokenValue.RPAR); nextTokenIfEquals(Token.TokenValue.LBRACE); while (currentTokenEquals(Token.TokenValue.CASE)) { if (menuInstr.hasDefaultCase) { throw new CaseException(lineNumber, colNumber); } menuInstr.addCase(parseCase()); } if (currentTokenEquals(Token.TokenValue.DEFAULT)) { if (menuInstr.hasDefaultCase) { throw new DefaultCaseException(lineNumber, colNumber); } menuInstr.addCase(parseCase()); } nextTokenIfEquals(Token.TokenValue.RBRACE); exps.Add(menuInstr); break; case Token.TokenValue.END: nextToken(); nextTokenIfEquals(Token.TokenValue.SEMICOLON); exps.Add(new EndInstruction()); break; default: parseEnd = true; break; } } switch (exps.Count) { case 0: return null; case 1: return exps.ElementAt(0); default: return new SequenceExpression(exps); } }