/// <summary> /// Creates all sub-instances for a module /// </summary> /// <param name="state">The state to use</param> /// <param name="module">The parent module</param> /// <returns></returns> private Instance.Module CreateAndRegisterInstance(ValidationState state, AST.Module module) { var modinstance = new Instance.Module(module); var parentCollection = modinstance.Instances; using (var scope = state.StartScope(module, modinstance)) { // TODO: Create module instances here foreach (var imp in module.Imports) { } foreach (var decl in module.Declarations) { if (decl is AST.ConstantDeclaration cdecl) { var cref = new Instance.ConstantReference(cdecl); scope.TryAddSymbol(cref.Name, cref, cdecl.Name); parentCollection.Add(cref); } else if (decl is AST.EnumDeclaration edecl) { var e = new Instance.EnumTypeReference(edecl); scope.TryAddSymbol(e.Name, e, edecl.Name); using (state.StartScope(e)) CreateAndRegisterInstance(state, e); parentCollection.Add(e); } else if (decl is AST.FunctionDefinition fdecl) { scope.TryAddSymbol(fdecl.Name.Name, fdecl, fdecl.Name); } } // The entry-level network is not user-instantiated if (module == state.TopLevel.Module) { modinstance.Instances.Add( state.TopLevel.NetworkInstance = CreateAndRegisterInstance(state, state.TopLevel.NetworkDeclaration, state.TopLevel.SourceNetwork) ); } } return(modinstance); }
/// <summary> /// Creates all sub-instances from declarations /// </summary> /// <param name="state">The state to use</param> /// <param name="declarations">The declarations to process</param> /// <param name="parentInstances">The collection of instances to append to</param> private void CreateAndRegisterInstancesForDeclarations(Validation.ValidationState state, IEnumerable <AST.Declaration> declarations, List <Instance.IInstance> parentInstances) { var scope = state.CurrentScope; // Add all variables and locally defined busses foreach (var decl in declarations) { if (decl is EnumDeclaration en) { var e = new Instance.EnumTypeReference(en); scope.TryAddSymbol(e.Name, e, en.Name); using (state.StartScope(e)) CreateAndRegisterInstance(state, e); parentInstances.Add(e); } else if (decl is FunctionDefinition fdef) { scope.TryAddSymbol(fdef.Name.Name, decl, fdef.Name); } else if (decl is ConstantDeclaration cdecl) { var c = new Instance.ConstantReference(cdecl); scope.TryAddSymbol(c.Name, c, cdecl.Name); parentInstances.Add(c); } else if (decl is VariableDeclaration variable) { var v = new Instance.Variable(variable); scope.TryAddSymbol(v.Name, v, variable.Name); parentInstances.Add(v); } else if (decl is BusDeclaration bus) { var b = new Instance.Bus(bus); scope.TryAddSymbol(b.Name, b, bus.Name); using (state.StartScope(b)) CreateAndRegisterInstance(state, b); parentInstances.Add(b); } else { throw new ParserException($"Unable to process {decl.GetType()} inside a process", decl); } } }
/// <summary> /// Creates all enum fields for an enum /// </summary> /// <param name="state">The state to use</param> /// <param name="parent">The enum instance</param> private void CreateAndRegisterInstance(ValidationState state, Instance.EnumTypeReference parent) { var scope = state.CurrentScope; var cur = 0; foreach (var field in parent.Source.Fields) { var ix = field.Value; if (ix < 0) { ix = cur; } var s = new Instance.EnumFieldReference(parent, field, ix); parent.Fields.Add(field.Name.Name, s); scope.TryAddSymbol(field.Name.Name, s, field.Name); parent.Instances.Add(s); cur = ix + 1; } }
/// <summary> /// Creates a new variable instnace /// </summary> /// <param name="source">The parent enum reference</param> /// <param name="field">The field to reference</param> /// <param name="value">The value the enum represents</param> public EnumFieldReference(Instance.EnumTypeReference source, AST.EnumField field, int value) { Source = field ?? throw new ArgumentNullException(nameof(field)); ParentType = source ?? throw new ArgumentNullException(nameof(ParentType)); Value = value; }