public override void ExecuteDirective() { if (Assembler.CurrentLine.InstructionName.Equals(".next")) { foreach (Token child in _iterations.Children) { if (!child.HasChildren) { throw new ExpressionException(child.Position, "Iteration expression cannot be empty."); } if (!Assembler.SymbolManager.SymbolExists(child.Children[0].Name)) { throw new ExpressionException(child.Children[0].Position, $"Variable \"{child.Children[0].Name}\" must be defined before it is used."); } Assembler.SymbolManager.Define(child.Children, true); } if (_condition == null || Evaluator.EvaluateCondition(_condition)) { Assembler.LineIterator.Rewind(Index); } } }
void EvaluateCondition(SourceLine line) { if (line.InstructionName.EndsWith("ndef")) { _ifTrue = !Assembler.SymbolManager.SymbolExists(line.OperandExpression); } else if (line.InstructionName.EndsWith("def")) { _ifTrue = Assembler.SymbolManager.SymbolExists(line.OperandExpression); } else { _ifTrue = Evaluator.EvaluateCondition(line.Operand.Children); } if (line.InstructionName.EndsWith("def")) { // save the evaluation line.Instruction.Name = Regex.Replace(line.Instruction.Name, @"n?def", string.Empty); line.Operand = new Token { Type = TokenType.Operator, OperatorType = OperatorType.Separator, Children = new List <Token> { new Token { Type = TokenType.Operator, OperatorType = OperatorType.Separator, Children = new List <Token> { new Token { Name = _ifTrue.ToString().ToLower(), Type = TokenType.Operand } } } } }; } }
void DoAssert(SourceLine line) { if (line.Operand.Children == null || !line.Operand.HasChildren) { Assembler.Log.LogEntry(line, line.Operand, "One or more arguments expected for assertion directive."); } else if (line.Operand.Children.Count > 2) { Assembler.Log.LogEntry(line, line.Operand.Children[2], "Unexpected expression found."); } else if (!Evaluator.EvaluateCondition(line.Operand.Children[0])) { if (line.Operand.Children.Count > 1) { Output(line, line.Operand.Children[1]); } else { Output(line, "Assertion failed."); } } }
void ThrowConditional(SourceLine line) { if (line.Operand.Children == null || line.Operand.Children.Count < 2 || !line.Operand.Children[1].HasChildren) { Assembler.Log.LogEntry(line, line.Operand, $"Missing arguments for directive \"{line.InstructionName}\"."); } else if (line.Operand.Children.Count > 2 || !line.Operand.Children[1].Children[0].ToString().EnclosedInDoubleQuotes()) { Assembler.Log.LogEntry(line, line.Operand, $"Argument error for directive \"{line.InstructionName}\"."); } else if (Evaluator.EvaluateCondition(line.Operand.Children[0].Children)) { if (line.Operand.Children.Count > 1) { Output(line, line.Operand.Children[1]); } else { Output(line, $"Expression \"{line.UnparsedSource.Substring(line.Operand.Position)}\" evaluated to true."); } } }