public UserDefinedFunctionInfo(UserDefinedFunction udf, string fileName, NodeValueInfo nvi) : base(fileName, nvi) { Function = udf; }
public void AddFunction(UserDefinedFunction function) { EnsureNotJoined(); if (GetFunction(function.Name, function.ParameterCount) != null) { throw new Exceptions.PsimulexCoreException(string.Format("There is already a user defined function with name \"{0}\" and having {1} parameters.", function.Name, function.ParameterCount)); } Functions.Add(function); }
public override void Visit(FunctionDeclarationNode node) { // Start Compile a new function lastCompiledUserDefinedFunction = new UserDefinedFunction(); // FunctionType node.FunctionType.Accept(this); TypeEnum functionType = lastCompiledDataType; string functionTypeName = lastCompiledDataTypeName; int functionDimensionCount = lastCompiledDimensionCount; List<int> functionDimensionList = lastCompiledDimensionList; // FunctionReference bool functionIsReferenceType = false; if (node.FunctionReference != null) functionIsReferenceType = true; // FunctionName string functionName = node.FunctionName.Value; // Message string function = "Function Found : " + functionType + " " + functionName + " () "; AddInformation(function, node.NodeValueInfo); lastCompiledUserDefinedFunction.Name = functionName; // FunctionParameterList node.FunctionParameterList.Accept(this); // Check function existance if (IsFunctionFounded(lastCompiledUserDefinedFunction.Name, lastCompiledUserDefinedFunction.ParameterCount)) { AddError(CompilerErrorCode.FunctionCollison, string.Format( "Function \"{0}\" with {1} parameter is already exist! The function will skipped.", lastCompiledUserDefinedFunction.Name, lastCompiledUserDefinedFunction.ParameterCount), node.NodeValueInfo); } else { // Set Up rest of the function property-s. lastCompiledUserDefinedFunction.ReturnValueType = new VariableDescriptor { Type = new TypeIdentifier { TypeEnum = functionType, TypeName = functionTypeName, Dimensions = new List<int>(functionDimensionCount) }, IsReference = functionIsReferenceType, Name = "" }; // Add to UserDefinedFunctionInfoList UserDefinedFunctionInfoList.Add( new UserDefinedFunctionInfo( lastCompiledUserDefinedFunction, SourceFileName, node.NodeValueInfo)); } }
private void InitHelpers() { lastCompiledConstantValue = null; lastCompiledDataType = TypeEnum.Undefined; lastCompiledDataTypeName = ""; lastCompiledDimensionCount = 0; lastCompiledDimensionList = new List<int>(); lastCompiledUserDefinedFunction = new UserDefinedFunction(); }
private string GetPsimulexFactorial(int n) { Program program = VapeTeam.Psimulex.Core.Factories.ProgramBuilder.Create().Add( new Push(n), new Call("factor"), new Call("print") ); UserDefinedFunction factor = new UserDefinedFunction { Name = "factor", Parameters = new List<VariableDescriptor> { new VariableDescriptor { Type = new TypeIdentifier { TypeEnum = TypeEnum.Integer }, Name = "n" } }, ReturnValueType = new VariableDescriptor { Type = new TypeIdentifier { TypeEnum = TypeEnum.Integer } }, Commands = new CommandList { new Push("n", ValueAccessModes.LocalVariable), new Push(1), new Compare(Compare.ComparisonModes.LessThanOrEqual), new RelativeJumpIfFalse(3), new Push(1), new Return(true), new Push("n", ValueAccessModes.LocalVariable), new Push("n", ValueAccessModes.LocalVariable), new Push(1), new BinaryOperation(BinaryOperation.Operations.Subtraction), new Call("factor"), new BinaryOperation(BinaryOperation.Operations.Multiplication), new Return(true), } }; program.AddFunction(factor); var process = Helpers.SystemHelper.CreateMachineAndRunProgram(program); return process.StandardOutput; }
public void UserDefinedFunctions3() { Program program = VapeTeam.Psimulex.Core.Factories.ProgramBuilder.Create().Add( new Push(2), new Push(3), new Call("multiply", 2), new Call("print"), new Push(2), new Push(3), new Push(4), new Call("multiply", 3), new Call("print") ); UserDefinedFunction mulFunction1 = new UserDefinedFunction { Name = "multiply", Parameters = new List<VariableDescriptor> { new VariableDescriptor { Type = new TypeIdentifier { TypeEnum = TypeEnum.Integer }, Name = "a" }, new VariableDescriptor { Type = new TypeIdentifier { TypeEnum = TypeEnum.Integer }, Name = "b" } }, ReturnValueType = new VariableDescriptor { Type = new TypeIdentifier { TypeEnum = TypeEnum.Integer } }, Commands = new CommandList { new Push("a", ValueAccessModes.LocalVariable), new Push("b", ValueAccessModes.LocalVariable), new BinaryOperation(BinaryOperation.Operations.Multiplication), new Return(true), } }; UserDefinedFunction mulFunction2 = new UserDefinedFunction { Name = "multiply", Parameters = new List<VariableDescriptor> { new VariableDescriptor { Type = new TypeIdentifier { TypeEnum = TypeEnum.Integer }, Name = "a" }, new VariableDescriptor { Type = new TypeIdentifier { TypeEnum = TypeEnum.Integer }, Name = "b" }, new VariableDescriptor { Type = new TypeIdentifier { TypeEnum = TypeEnum.Integer }, Name = "c" } }, ReturnValueType = new VariableDescriptor { Type = new TypeIdentifier { TypeEnum = TypeEnum.Integer } }, Commands = new CommandList { new Push("a", ValueAccessModes.LocalVariable), new Push("b", ValueAccessModes.LocalVariable), new BinaryOperation(BinaryOperation.Operations.Multiplication), new Push("c", ValueAccessModes.LocalVariable), new BinaryOperation(BinaryOperation.Operations.Multiplication), new Return(true), } }; program.AddFunction(mulFunction1); program.AddFunction(mulFunction2); var process = Helpers.SystemHelper.CreateMachineAndRunProgram(program); Assert.AreEqual("624", process.StandardOutput); }