static private Function ParseFunctionCall() { if (CurrentToken.tokenType == TokenType.Symbol) { Type functionType; if (functionMap.TryGetValue(CurrentToken.symbol.ToLowerInvariant(), out functionType)) { Function function = (Function)functionType.GetConstructor(new Type[0]).Invoke(null); Function[] inputValues = function.GetDefaultInputValues(); DiscardCurrentToken(); //Parse parameters (if they are available) if (CurrentToken.tokenType == TokenType.StartFunctionCall) { DiscardCurrentToken(); if (CurrentToken.tokenType == TokenType.EndFunctionCall) { //End of function call, no parameters DiscardCurrentToken(); } else { //Parameters! while (CurrentToken.tokenType == TokenType.Symbol) { String parameterName = CurrentToken.symbol; DiscardCurrentToken(); if (CurrentToken.tokenType == TokenType.Equal) { DiscardCurrentToken(); if (CurrentToken.tokenType == TokenType.Number) { //Parameter value is a number int parameterNumber = CurrentToken.number; DiscardCurrentToken(); int index = function.GetInputParameterIndex(parameterName); if (index >= 0) { //It's an input parameter! if (function.InputParameters[index].Type == ParameterType.Number) { inputValues[index] = new ConstantNumber(parameterNumber); } else { //Error! return(null); } } else { //Try to lookup a public propery with the same name PropertyInfo property = function.GetType().GetProperty(parameterName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); if (property != null) { if (property.PropertyType == typeof(int)) { property.SetValue(function, (int)parameterNumber, null); } else if (property.PropertyType == typeof(float)) { property.SetValue(function, (int)parameterNumber, null); } } } } else if (CurrentToken.tokenType == TokenType.Symbol) { //Parameter value is anothe function call! Function parameterFunction = ParseFunctionCall(); if (parameterFunction != null) { int index = function.GetInputParameterIndex(parameterName); if (index >= 0) { //It's an input parameter! if (function.InputParameters[index].Type == parameterFunction.OutputParameter.Type) { inputValues[index] = parameterFunction; } else { //Error! return(null); } } } } else { //Error return(null); } if (CurrentToken.tokenType == TokenType.Comma) { DiscardCurrentToken(); //More parameters to come! } else if (CurrentToken.tokenType == TokenType.EndFunctionCall) { DiscardCurrentToken(); //End of function call break; } } else { //Error return(null); } } } } function.SetInputValues(inputValues); return(function); } } return(null); }
private static Function ParseFunctionCall() { if (CurrentToken.tokenType == TokenType.Symbol) { Type functionType; if (functionMap.TryGetValue(CurrentToken.symbol.ToLowerInvariant(), out functionType)) { Function function = (Function)functionType.GetConstructor(new Type[0]).Invoke(null); Function[] inputValues = function.GetDefaultInputValues(); DiscardCurrentToken(); //Parse parameters (if they are available) if (CurrentToken.tokenType == TokenType.StartFunctionCall) { DiscardCurrentToken(); if (CurrentToken.tokenType == TokenType.EndFunctionCall) { //End of function call, no parameters DiscardCurrentToken(); } else { //Parameters! while (CurrentToken.tokenType == TokenType.Symbol) { String parameterName = CurrentToken.symbol; DiscardCurrentToken(); if (CurrentToken.tokenType == TokenType.Equal) { DiscardCurrentToken(); if (CurrentToken.tokenType == TokenType.Number) { //Parameter value is a number int parameterNumber = CurrentToken.number; DiscardCurrentToken(); int index = function.GetInputParameterIndex(parameterName); if (index >= 0) { //It's an input parameter! if (function.InputParameters[index].Type == ParameterType.Number) { inputValues[index] = new ConstantNumber(parameterNumber); } else { //Error! return null; } } else { //Try to lookup a public propery with the same name PropertyInfo property = function.GetType().GetProperty(parameterName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); if (property != null) { if (property.PropertyType == typeof(int)) { property.SetValue(function, (int)parameterNumber, null); } else if (property.PropertyType == typeof(float)) { property.SetValue(function, (int)parameterNumber, null); } } } } else if (CurrentToken.tokenType == TokenType.Symbol) { //Parameter value is anothe function call! Function parameterFunction = ParseFunctionCall(); if (parameterFunction != null) { int index = function.GetInputParameterIndex(parameterName); if (index >= 0) { //It's an input parameter! if (function.InputParameters[index].Type == parameterFunction.OutputParameter.Type) { inputValues[index] = parameterFunction; } else { //Error! return null; } } } } else { //Error return null; } if (CurrentToken.tokenType == TokenType.Comma) { DiscardCurrentToken(); //More parameters to come! } else if (CurrentToken.tokenType == TokenType.EndFunctionCall) { DiscardCurrentToken(); //End of function call break; } } else { //Error return null; } } } } function.SetInputValues(inputValues); return function; } } return null; }