void CreateAPIMethods() { // Helper variable for setting API functions methodproperties MethodProperties methodProperties; // WriteLine methodProperties = new MethodProperties { Type = "void", ParameterTypes = new List <string> { "text" } }; CreateMethod("WriteLine", methodProperties); // ReadLine methodProperties = new MethodProperties { Type = "text", ParameterTypes = new List <string> { } }; CreateMethod("ReadLine", methodProperties); }
public override int VisitFunction([NotNull] CBluntParser.FunctionContext context) { #if DEBUG Console.WriteLine("VisitFunction"); #endif // Create a new scope to the linked list SymbolTable.MethodScopeLinkedList.AddLast(new Dictionary <string, VariableProperties>()); // Get the method's type var methodType = context.functiontype().GetText(); // Get the method's name var methodName = context.ID(0).GetText(); // Create the actual properties for the method MethodProperties methodProperties = new MethodProperties { // Set the type of the method Type = methodType, // Create the parameter types list for parsing of the method's parameters ParameterTypes = new List <string>() }; // Check whether the method contains parameters. If so, add the parameter types to the methodProperties and add the variables // to the main scope of the method for (int i = 0; i < context.variabletype().Count(); ++i) { // Get the parameter's type var parameterType = context.variabletype(i).GetText(); // Get the parameter's name. This is +1 due to that the first ID is the method's name var parameterName = context.ID(i + 1).GetText(); // Add the parameter type to the list of parameter types methodProperties.ParameterTypes.Add(parameterType); // All parameters are guranteed to be initialized, set the initialized flag var variableProperties = new VariableProperties(parameterType) { Initialized = true }; // Add the variable along with properties to the method scope SymbolTable.MethodScopeLinkedList.Last.Value.Add(parameterName, variableProperties); } SymbolTable.CurrentMethodType = methodType; // Visit the block of the method Visit(context.block()); // After the function has finished, remove the scope SymbolTable.MethodScopeLinkedList.RemoveLast(); return(0); }
void CreateMethod(string methodName, MethodProperties methodProperties) { // Determine if the method already exists if (SymbolTable.MethodDictionary.ContainsKey(methodName)) { Console.WriteLine("Symbol table generation error! Script contains declaration for API method with name: " + methodName); return; } // Add the method to the symbol table SymbolTable.MethodDictionary.Add(methodName, methodProperties); }
public override int VisitFunction([NotNull] CBluntParser.FunctionContext context) { #if DEBUG Console.WriteLine("VisitFunction"); #endif // The type of the method var methodType = context.functiontype().GetText(); // The name of the method var methodName = context.ID(0).GetText(); // The list of parameter types the method has var methodTypes = new List <string>(); for (int i = 0; i < context.variabletype().Count(); ++i) { // Get the parameter type var methodParameter = context.variabletype(i).GetText(); // Add it to the list of parameter types the method has methodTypes.Add(methodParameter); } // Create MethodProperties object var methodProperties = new MethodProperties { Type = methodType, ParameterTypes = methodTypes }; // Add the method along with its properties to the symbol table CreateMethod(methodName, methodProperties); return(base.VisitFunction(context)); }