public override void Execute(RunScope scope) { base.Execute(scope); if (_initExp != null) { var initScope = scope.Clone(); _initExp.ReadValue(initScope); scope.Merge(initScope); } if (_condExp != null) { var condScope = scope.Clone(); _condExp.ReadValue(condScope); scope.Merge(condScope); } if (_incExp != null) { var incScope = scope.Clone(); _incExp.ReadValue(incScope); scope.Merge(incScope); } var bodyScope = scope.Clone(canBreak: true, canContinue: true); foreach (var stmt in _body) { stmt.Execute(bodyScope); } scope.Merge(bodyScope, false, false); }
public override Value ReadValue(RunScope scope) { if (_arrayAccessExps != null) { foreach (var exp in _arrayAccessExps) { var accessScope = scope.Clone(); exp.ReadValue(accessScope); scope.Merge(accessScope); } } if (_subscriptAccessExps != null) { foreach (var exp in _subscriptAccessExps) { var accessScope = scope.Clone(); exp.ReadValue(accessScope); scope.Merge(accessScope); } } if (_def is VariableDefinition) { var v = scope.GetVariable(Text); if (v != null) { v.IsUsed = true; if (v.IsInitialized != TriState.True && !scope.SuppressInitializedCheck) { ReportError(Span, CAError.CA0110, v.Name); // Use of uninitialized variable '{0}'. } return(v.Value); } return(base.ReadValue(scope)); } else if (_def is EnumOptionDefinition) { return(new EnumValue(_def.DataType, _def.Name)); } else if (_def is TableDefinition || _def is ExtractTableDefinition) { return(new TableValue(_def.DataType, _def.Name)); } else if (_def is RelIndDefinition) { return(new IndRelValue(_def.DataType, _def.Name)); } else if (_def.CanRead && _def.DataType != null) { return(Value.CreateUnknownFromDataType(_def.DataType)); } return(base.ReadValue(scope)); }
protected override void Execute(RunScope scope) { var castScope = scope.Clone(); base.Execute(castScope); scope.Merge(castScope); }
public override Value ReadValue(RunScope scope) { var castScope = scope.Clone(); var value = base.ReadValue(castScope); var dataTypeValue = Value.CreateUnknownFromDataType(DataType); value = dataTypeValue.Convert(scope, Span, value); scope.Merge(castScope); return(value); }
public override void Execute(RunScope scope) { var headerScope = scope.Clone(); foreach (var stmt in _body) { stmt.Execute(headerScope); } // Don't merge the headerScope back into the parent scope because there's no guarantees // the header is called asynchronously, or might not be called at all. }
public override void WriteValue(RunScope scope, Value value) { if (_arrayAccessExps != null) { foreach (var exp in _arrayAccessExps) { var accessScope = scope.Clone(); exp.ReadValue(accessScope); scope.Merge(accessScope); } } if (_subscriptAccessExps != null) { foreach (var exp in _subscriptAccessExps) { var accessScope = scope.Clone(); exp.ReadValue(accessScope); scope.Merge(accessScope); } } if (_def is VariableDefinition) { var v = scope.GetVariable(Text); if (v != null) { v.Value = v.Value.Convert(scope, Span, value); v.IsInitialized = TriState.True; return; } base.WriteValue(scope, value); } else if (_def.CanWrite) { } else { base.WriteValue(scope, value); } }
public override void Execute(RunScope scope) { base.Execute(scope); if (_whereExp != null) { _whereExp.ReadValue(scope); } var foundScope = scope.Clone(canBreak: true, canContinue: true); foreach (var group in _groups) { if (group.isDefault) { continue; } foreach (var stmt in group.stmts) { stmt.Execute(foundScope); } } var defaultScope = scope.Clone(canBreak: true, canContinue: true); foreach (var group in _groups) { if (!group.isDefault) { continue; } foreach (var stmt in group.stmts) { stmt.Execute(defaultScope); } } scope.Merge(new RunScope[] { foundScope, defaultScope }, false, false); }
public override void Execute(RunScope scope) { base.Execute(scope); if (_root.NumChildren > 0) { var readScope = scope.Clone(); readScope.RemoveHeaderString = true; _root.ReadValue(readScope); scope.Merge(readScope); } }
public override void Execute(RunScope scope) { base.Execute(scope); var scopes = new List <RunScope>(); for (int i = 0, ii = _conditions.Count > _trueBodies.Count ? _conditions.Count : _trueBodies.Count; i < ii; i++) { if (i < _conditions.Count) { var condScope = scope.Clone(); _conditions[i].ReadValue(condScope); scope.Merge(condScope, true, true); } if (i < _trueBodies.Count) { var trueScope = scope.Clone(); foreach (var stmt in _trueBodies[i]) { stmt.Execute(trueScope); } scopes.Add(trueScope); } } var falseScope = scope.Clone(); if (_falseBody != null) { foreach (var stmt in _falseBody) { stmt.Execute(falseScope); } } scopes.Add(falseScope); scope.Merge(scopes, true, true); }
public override void Execute(RunScope scope) { base.Execute(scope); if (_exp != null) { var returnScope = scope.Clone(); _exp.ReadValue(returnScope); scope.Merge(returnScope); } scope.Returned = TriState.True; }
public override void Simplify(RunScope scope) { if (Parent == null) { throw new InvalidOperationException("Conditional operator must have a parent."); } var leftScope = scope.Clone(); var leftNode = Parent.GetLeftSibling(leftScope, this); scope.Merge(leftScope); if (leftNode == null) { ReportError(_opSpan, CAError.CA0007, "?"); // Operator '{0}' expects value on left. Parent.ReplaceWithResult(Value.Void, ResultSource.Conditional1, this); } else { var leftValue = leftNode.ReadValue(leftScope); if (leftValue.IsVoid) { leftNode.ReportError(_opSpan, CAError.CA0007, "?"); // Operator '{0}' expects value on left. } Value result = null; if (leftValue.IsTrue) { if (_trueExp != null) { result = _trueExp.ReadValue(scope); } else { result = Value.Void; } } else if (leftValue.IsFalse) { if (_falseExp != null) { result = _falseExp.ReadValue(scope); } else { result = Value.Void; } } else { if (_trueExp != null) { result = _trueExp.ReadValue(scope); } if (_falseExp != null) { var value = _falseExp.ReadValue(scope); if (result == null) { result = value; } } if (result == null) { result = Value.Void; } } Parent.ReplaceWithResult(result, leftNode, this); } }
private void ExecuteAssignment(RunScope scope) // = *= /= %= += -= { var leftNode = Parent.GetLeftSibling(scope, this); var rightNode = Parent.GetRightSibling(scope, this); if (leftNode == null) { ReportError(Span, CAError.CA0100, Text); // Operator '{0}' expects assignable value on left. } else if (rightNode == null) { ReportError(Span, CAError.CA0008, Text); // Operator '{0}' expects value on right. } if (leftNode != null && rightNode != null) { Value leftValue = null; if (Text != "=") { var leftScope = scope.Clone(); leftValue = leftNode.ReadValue(leftScope); scope.Merge(leftScope); } var rightScope = scope.Clone(); var rightValue = rightNode.ReadValue(rightScope); scope.Merge(rightScope); if (!leftNode.CanAssignValue(scope)) { leftNode.ReportError(leftNode.Span, CAError.CA0100, Text); // Operator '{0}' expects assignable value on left. } else if (rightValue.IsVoid) { rightNode.ReportError(rightNode.Span, CAError.CA0008, Text); // Operator '{0}' expects value on right. } Value result = null; switch (Text) { case "=": result = rightValue; break; case "*=": result = leftValue.Multiply(scope, Span, rightValue); break; case "/=": result = leftValue.Divide(scope, Span, rightValue); break; case "%=": result = leftValue.ModulusDivide(scope, Span, rightValue); break; case "+=": result = leftValue.Add(scope, Span, rightValue); break; case "-=": result = leftValue.Subtract(scope, Span, rightValue); break; default: throw new InvalidOperationException(); } leftNode.WriteValue(scope, rightValue); Parent.ReplaceNodes(null, this, rightNode); //Parent.ReplaceWithResult(rightValue, leftNode, this, rightNode); } else { Value resultValue = Value.Void; if (leftNode != null && rightNode == null) { resultValue = leftNode.ReadValue(scope); } else if (leftNode == null && rightNode != null) { resultValue = rightNode.ReadValue(scope); } Parent.ReplaceWithResult(resultValue, leftNode, this, rightNode); } }
private void ExecuteComparison(RunScope scope) // < > <= >= == != { var leftNode = Parent.GetLeftSibling(scope, this); var rightNode = Parent.GetRightSibling(scope, this); if (leftNode == null) { ReportError(Span, CAError.CA0007, Text); // Operator '{0}' expects value on left. } else if (rightNode == null) { ReportError(Span, CAError.CA0008, Text); // Operator '{0}' expects value on right. } if (leftNode != null && rightNode != null) { var leftValue = leftNode.ReadValue(scope); var rightScope = scope.Clone(); var rightValue = rightNode.ReadValue(rightScope); scope.Merge(rightScope); if (leftValue.IsVoid) { leftNode.ReportError(leftNode.Span, CAError.CA0007, Text); // Operator '{0}' expects value on left. } else if (rightValue.IsVoid) { rightNode.ReportError(rightNode.Span, CAError.CA0008, Text); // Operator '{0}' expects value on right. } Value result = null; switch (Text) { case "==": result = leftValue.CompareEqual(scope, Span, rightValue); break; case "!=": result = leftValue.CompareEqual(scope, Span, rightValue); break; case "<": result = leftValue.CompareEqual(scope, Span, rightValue); break; case ">": result = leftValue.CompareEqual(scope, Span, rightValue); break; case "<=": result = leftValue.CompareEqual(scope, Span, rightValue); break; case ">=": result = leftValue.CompareEqual(scope, Span, rightValue); break; case "and": case "&&": { var left = leftValue.ToNumber(scope, Span); var right = rightValue.ToNumber(scope, Span); if (left.HasValue && right.HasValue) { result = new NumberValue(DataType.Int, left.Value != 0 && right.Value != 0 ? 1 : 0); } else { result = new NumberValue(DataType.Int, null); } } break; case "or": case "||": { var left = leftValue.ToNumber(scope, Span); var right = rightValue.ToNumber(scope, Span); if (left.HasValue && right.HasValue) { result = new NumberValue(DataType.Int, left.Value != 0 || right.Value != 0 ? 1 : 0); } else { result = new NumberValue(DataType.Int, null); } } break; default: throw new InvalidOperationException(); } Parent.ReplaceWithResult(result, leftNode, this, rightNode); } else { Value resultValue = Value.Void; if (leftNode != null && rightNode == null) { resultValue = leftNode.ReadValue(scope); } else if (leftNode == null && rightNode != null) { resultValue = rightNode.ReadValue(scope); } Parent.ReplaceWithResult(resultValue, leftNode, this, rightNode); } }
private void ExecuteMath(RunScope scope) // * / % + - { var leftNode = Parent.GetLeftSibling(scope, this); var rightNode = Parent.GetRightSibling(scope, this); if (leftNode == null) { ReportError(Span, CAError.CA0007, Text); // Operator '{0}' expects value on left. } else if (rightNode == null) { ReportError(Span, CAError.CA0008, Text); // Operator '{0}' expects value on right. } if (leftNode != null && rightNode != null) { var leftValue = leftNode.ReadValue(scope); var rightScope = scope.Clone(); var rightValue = rightNode.ReadValue(rightScope); scope.Merge(rightScope); if (leftValue.IsVoid) { leftNode.ReportError(leftNode.Span, CAError.CA0007, Text); // Operator '{0}' expects value on left. } else if (rightValue.IsVoid) { rightNode.ReportError(rightNode.Span, CAError.CA0008, Text); // Operator '{0}' expects value on right. } Value result = null; switch (Text) { case "*": result = leftValue.Multiply(scope, Span, rightValue); break; case "/": result = leftValue.Divide(scope, Span, rightValue); break; case "%": result = leftValue.ModulusDivide(scope, Span, rightValue); break; case "+": result = leftValue.Add(scope, Span, rightValue); break; case "-": result = leftValue.Subtract(scope, Span, rightValue); break; default: throw new InvalidOperationException(); } Parent.ReplaceWithResult(result, leftNode, this, rightNode); } else { Value resultValue = Value.Void; if (leftNode != null && rightNode == null) { resultValue = leftNode.ReadValue(scope); } else if (leftNode == null && rightNode != null) { resultValue = rightNode.ReadValue(scope); } Parent.ReplaceWithResult(resultValue, leftNode, this, rightNode); } }
public override void Execute(RunScope scope) { base.Execute(scope); DataType dataType = null; List <Definition> enumOptions = null; if (_condExp != null) { dataType = _condExp.ReadValue(scope).DataType; if (dataType != null && dataType.HasCompletionOptions) { enumOptions = dataType.CompletionOptions.ToList(); } } var bodyScopes = new List <RunScope>(); foreach (var cas in _cases) { if (cas.exp != null) { var valueScope = scope.Clone(); var itemValue = cas.exp.ReadValue(valueScope); if (enumOptions != null) { // Remove this option from the list var itemStr = itemValue.ToStringValue(valueScope, cas.exp.Span); if (!string.IsNullOrEmpty(itemStr)) { itemStr = DataType.NormalizeEnumOption(itemStr); foreach (var enumItem in enumOptions) { if (enumItem.Name == itemStr) { enumOptions.Remove(enumItem); break; } } } } scope.Merge(valueScope, true, true); } if (!cas.safeFallThrough) { var bodyScope = scope.Clone(canBreak: true); foreach (var stmt in cas.body) { stmt.Execute(bodyScope); } bodyScopes.Add(bodyScope); if (bodyScope.Breaked == TriState.False && bodyScope.Returned == TriState.False && cas.body.Any()) { ReportError(cas.caseSpan, CAError.CA0031); // Switch fall-throughs are inadvisable. } } } if (_default != null) { var bodyScope = scope.Clone(canBreak: true); foreach (var stmt in _default) { stmt.Execute(bodyScope); } bodyScopes.Add(bodyScope); } else if (enumOptions != null && enumOptions.Count == 0) { // There is no default, but all the enum options have been covered off. // This is ok. } else { // Because there's no default, this switch doesn't cover all code branches. // Add a dummy scope to indicate that. bodyScopes.Add(scope.Clone()); } scope.Merge(bodyScopes, false, true); }
public override Value ReadValue(RunScope scope) { switch (_name) { case "abs": return(Read_abs(scope)); case "count": return(Read_count(scope)); case "max": return(Read_max(scope)); case "min": return(Read_min(scope)); case "oldvalue": return(Read_oldvalue(scope)); case "sum": return(Read_sum(scope)); } var defArgs = _def != null?_def.Arguments.ToArray() : new ArgumentDescriptor[0]; var argIndex = 0; foreach (var arg in _args) { var defArg = argIndex < defArgs.Length ? defArgs[argIndex] : null; if (defArg != null) { if (defArg.PassByMethod == PassByMethod.Reference || defArg.PassByMethod == PassByMethod.ReferencePlus) { var readScope = scope.Clone(); readScope.SuppressInitializedCheck = true; arg.ReadValue(readScope); scope.Merge(readScope); var writeScope = scope.Clone(); arg.WriteValue(writeScope, Value.CreateUnknownFromDataType(defArg.DataType)); scope.Merge(writeScope); } else { var readScope = scope.Clone(); arg.ReadValue(readScope); scope.Merge(readScope); } } else { arg.ReadValue(scope); } argIndex++; } if (_def == null) { return(Value.Void); } return(Value.CreateUnknownFromDataType(_def.DataType)); }