/// <summary> /// Deze mag wel in een includefile op het hoogste niveau. /// </summary> /// <param name="l"></param> public void Visit(GlobalVarDeclaration l) { // We moeten een variabele hebben en een type, niet perse een initialisatie if (CheckGeneric(l)) { return; } // We moeten een variabele hebben en een type, niet perse een initialisatie if (l.VarType == String.Empty) { _errors.Add(new TypenameExpectedException(l.SourceCodeContext)); return; } if (l.VarName == String.Empty) { _errors.Add(new IdentifierExpectedException(l.SourceCodeContext)); return; } }
public void Visit(GlobalVarDeclaration l) { Format(l); }
/// <summary> /// Gets the translated code for the grammar structure. /// </summary> /// <returns>The translated code for the grammar structure.</returns> public IEnumerable <Block> Translate(TranslationContext context) { // Get variable IDeclaration variable = context.GetDeclaration(VariableName); // Check variable was found if (variable == null) { context.ErrorList.Add(new CompilerError($"Variable '{VariableName}' was not defined", ErrorType.NotDefined, ErrorToken, FileName)); return(Enumerable.Empty <Block>()); } // Try as stack variable StackValue stackValue = variable as StackValue; if (stackValue != null && stackValue.StackSpace == 1) { Operator.TestCompatible(stackValue.Type, context, FileName, ErrorToken); switch (Operator) { case AssignOperator.Equals: return(stackValue.CreateVariableAssignment(context, Value)); case AssignOperator.AddEquals: return(stackValue.CreateVariableIncrement(context, Value)); case AssignOperator.MinusEquals: return(stackValue.CreateVariableIncrement(context, new UnaryExpression(Value, UnaryOperator.Minus, FileName, ErrorToken))); case AssignOperator.DotEquals: return(stackValue.CreateVariableAssignment(context, new CompoundExpression(CompoundOperator.Concat, new LookupExpression(stackValue, FileName, ErrorToken), Value, FileName, ErrorToken))); case AssignOperator.PlusPlus: return(new[] { stackValue.CreateVariableIncrement(1) }); case AssignOperator.MinusMinus: return(new[] { stackValue.CreateVariableIncrement(-1) }); default: throw new ArgumentOutOfRangeException(); } } // Try as global variable GlobalVarDeclaration globalVarDeclaration = variable as GlobalVarDeclaration; if (globalVarDeclaration != null) { Operator.TestCompatible(globalVarDeclaration.Type, context, FileName, ErrorToken); switch (Operator) { case AssignOperator.Equals: return(new BlockBuilder(BlockSpecs.SetVariableTo, context) .AddParam(VariableName) .AddParam(Value, globalVarDeclaration.Type) .Create()); case AssignOperator.AddEquals: return(new BlockBuilder(BlockSpecs.ChangeVarBy, context) .AddParam(VariableName) .AddParam(Value) .Create()); case AssignOperator.MinusEquals: return(new BlockBuilder(BlockSpecs.ChangeVarBy, context) .AddParam(VariableName) .AddParam(new UnaryExpression(Value, UnaryOperator.Minus, FileName, ErrorToken)) .Create()); case AssignOperator.DotEquals: return(new BlockBuilder(BlockSpecs.SetVariableTo, context) .AddParam(VariableName).AddParam( new CompoundExpression(CompoundOperator.Concat, new LookupExpression(globalVarDeclaration, FileName, ErrorToken), Value, FileName, ErrorToken)) .Create()); case AssignOperator.PlusPlus: return(new BlockBuilder(BlockSpecs.ChangeVarBy, context) .AddParam(VariableName) .AddParam(1) .Create()); case AssignOperator.MinusMinus: return(new BlockBuilder(BlockSpecs.ChangeVarBy, context) .AddParam(VariableName) .AddParam(-1) .Create()); default: throw new ArgumentOutOfRangeException(); } } // Try as any readonly type if (variable is ParamDeclaration || variable is ConstDeclaration) { context.ErrorList.Add(new CompilerError($"Value '{VariableName}' is read-only", ErrorType.ValueIsReadonly, ErrorToken, FileName)); return(Enumerable.Empty <Block>()); } // Fail context.ErrorList.Add(new CompilerError($"'{VariableName}' is not a variable", ErrorType.ImproperUsage, ErrorToken, FileName)); return(Enumerable.Empty <Block>()); }
public void Visit(GlobalVarDeclaration l) { Visit(l as VarDeclaration); }