private void ExecuteCondition(NovelCondition condition) { var operandValue = OperandToValue(condition.ConditionOperand); if (operandValue.VariableType == typeof(bool)) { var boolValue = (bool)operandValue.Value; //If condition is true if (boolValue) { //Clean the stack if (condition.StackInstruction != null) { Stack.ExpandStack(condition.StackInstruction.ExpandSize); } } //If false else { //Move by the offset of instruction on false InstructionPointer += condition.InstructionOnFalse; //Clean the stack Stack.ExpandStack(condition.StackInstruction.ExpandSize); //Because IP is incremented each tick of loop it needs to be subtracted by 1 InstructionPointer--; } } else { //TODO error } }
public NovelCondition ParseCondition(string text, out int conditionLevel, ref List <NovelInstruction> instructions) { conditionLevel = -1; var conditionStart = new string [] { "if", "else if", "else" }; for (int i = 0; i < conditionStart.Length; i++) { if (text.IndexOf(conditionStart[i]) == 0) { conditionLevel = i; break; } } var conditionText = text.Substring( conditionStart[conditionLevel].Length).Trim(); var novelCondition = new NovelCondition(); if (conditionLevel == 0 || conditionLevel == 1) { var unfold = ParseExpression(conditionText); if (unfold.Last() is NovelExpandStack) { for (int i = 0; i < unfold.Count - 1; i++) { instructions.Add(unfold[i]); } novelCondition.StackInstruction = unfold.Last() as NovelExpandStack; var lastExpression = unfold[unfold.Count - 2] as NovelExpression; novelCondition.ConditionOperand = lastExpression.Term[0] as NovelExpressionOperand; } else { var lastExpression = unfold.Last() as NovelExpression; novelCondition.ConditionOperand = lastExpression.Term[0] as NovelExpressionOperand; } } else if (conditionLevel == 2) { var expression = new NovelExpression(); expression.Term.Add(new NovelExpressionLiteral(true)); novelCondition.ConditionOperand = new NovelExpressionLiteral(true); } return(novelCondition); }
public NovelCondition ParseWhileLoop(string text, ref List <NovelInstruction> instructions) { //Get the text inside parentheses var conditionText = text.Substring("while".Length).Trim(); //Use expression system for loops var novelCondition = new NovelCondition(); //Parse expression var expr = ParseExpression(conditionText); //If last is stack expansion if (expr.Last() is NovelExpandStack) { //Add all the instructions except the last one //which is stack expansion, coz SE is handled by //condition instruction itself for (int i = 0; i < expr.Count - 1; i++) { instructions.Add(expr[i]); } //Take stack expand instruction novelCondition.StackInstruction = expr.Last() as NovelExpandStack; //Take last expression var lastExpression = expr[expr.Count - 2] as NovelExpression; //Get the RESULT variable from expression novelCondition.ConditionOperand = lastExpression.Term[0] as NovelExpressionOperand; } else { //Take last expression var lastExpression = expr.Last() as NovelExpression; //Get the RESULT variable from expression novelCondition.ConditionOperand = lastExpression.Term[0] as NovelExpressionOperand; } return(novelCondition); }