public static Script GetScriptFromContext(HS_Gen1Parser.ScriptDeclarationContext context, DatumIndex rootExpressionIndex, OpcodeLookup opcodes) { // Create a new Script. Script script = new Script { Name = context.scriptID().GetTextSanitized(), ExecutionType = (short)opcodes.GetScriptTypeOpcode(context.SCRIPTTYPE().GetTextSanitized()), ReturnType = (short)opcodes.GetTypeInfo(context.VALUETYPE().GetTextSanitized()).Opcode, RootExpressionIndex = rootExpressionIndex }; // Handle scripts with parameters. var parameterContext = context.scriptParameters(); if (parameterContext != null) { var parameters = parameterContext.parameter(); for (ushort i = 0; i < parameters.Length; i++) { string name = parameters[i].ID().GetTextSanitized(); var valueTypeNode = parameters[i].VALUETYPE(); string valueType = valueTypeNode is null ? "script" : valueTypeNode.GetTextSanitized(); // Add the parameter to the script object. ScriptParameter parameter = new ScriptParameter { Name = name, Type = opcodes.GetTypeInfo(valueType).Opcode }; script.Parameters.Add(parameter); } } return(script); }
/// <summary> /// Processes script declarations. Opens a datum. /// Creates the script node and the initial "begin" expression. /// Generates the variable lookup. Pushes return types. /// </summary> /// <param name="context"></param> public override void EnterScriptDeclaration(HS_Gen1Parser.ScriptDeclarationContext context) { if(_debug) { _logger.Script(context, CompilerContextAction.Enter); } // Create new script object and add it to the table. Script script = ScriptObjectCreation.GetScriptFromContext(context, _currentIndex, _opcodes); _scripts.Add(script); // Generate the parameter lookup. for(ushort i = 0; i < script.Parameters.Count; i++) { ScriptParameter parameter = script.Parameters[i]; var info = new ParameterInfo(parameter.Name, _opcodes.GetTypeInfo(parameter.Type).Name, i); _parameterLookup.Add(info.Name, info); } string returnType = context.VALUETYPE().GetTextSanitized(); int expressionCount = context.expression().Count(); // The final expression must match the return type of this script. _expectedTypes.PushType(returnType); // The other expressions can be of any type. if (expressionCount > 1) { _expectedTypes.PushTypes("void", expressionCount - 1); } CreateInitialBegin(returnType, context.GetCorrectTextPosition(_missingCarriageReturnPositions)); }
/// <summary> /// Closes a datum. /// </summary> /// <param name="context"></param> public override void ExitScriptDeclaration(HS_Gen1Parser.ScriptDeclarationContext context) { if (_debug) { _logger.Script(context, CompilerContextAction.Exit); } _parameterLookup.Clear(); CloseDatum(); ReportProgress(); }
public ScriptInfo(HS_Gen1Parser.ScriptDeclarationContext context, ushort index) { Name = context.scriptID().GetTextSanitized(); ScriptType = context.SCRIPTTYPE().GetTextSanitized(); ReturnType = context.VALUETYPE().GetTextSanitized(); Parameters = new List <ParameterInfo>(); Opcode = index; var parameterContext = context.scriptParameters(); if (parameterContext != null) { var parameters = parameterContext.parameter(); // Create parameters from the extracted strings for (ushort i = 0; i < parameters.Length; i++) { string name = parameters[i].ID().GetTextSanitized(); var valueTypeNode = parameters[i].VALUETYPE(); string valueType = valueTypeNode is null ? "script" : valueTypeNode.GetTextSanitized(); var param = new ParameterInfo(name, valueType, i); Parameters.Add(param); } } }
public void Script(HS_Gen1Parser.ScriptDeclarationContext context, CompilerContextAction action) { string name = context.scriptID().GetTextSanitized(); WriteContextIndent("SCRIPT", context, action, name); }
/// <summary> /// Exit a parse tree produced by <see cref="HS_Gen1Parser.scriptDeclaration"/>. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public virtual void ExitScriptDeclaration([NotNull] HS_Gen1Parser.ScriptDeclarationContext context) { }