public override PinnedVariableResult PinFloatingPointExpressionToVariable( ExpressionBuilderParams p, TypeDescriptor typeDescriptor, string nameHint, string expression, EvaluationStatement template) { var variableName = p.Scope.NewHelperVariable(typeDescriptor, nameHint); expression = $"`awk \"BEGIN {{print ({expression})}}\"`"; BashVariableDefinitionStatementTranspiler.WriteVariableDefinition( p.Context, p.Scope, p.NonInlinePartWriter, variableName, expression ); template = new VariableAccessStatement(variableName, template.Info); return(new PinnedVariableResult( typeDescriptor, variableName, FormatVariableAccessExpression(p, typeDescriptor, variableName, template ), template)); }
public override PinnedVariableResult PinExpressionToVariable(ExpressionBuilderParams p, string nameHint, ExpressionResult result) { var variableName = p.Scope.NewHelperVariable(result.TypeDescriptor, nameHint); BashVariableDefinitionStatementTranspiler.WriteVariableDefinition( p.Context, p.Scope, p.NonInlinePartWriter, variableName, result.Expression ); var template = new VariableAccessStatement(variableName, result.Template.Info); return(new PinnedVariableResult( result.TypeDescriptor, variableName, FormatVariableAccessExpression(p, result.TypeDescriptor, variableName, template ), template)); }
public override bool ShouldBePinnedToFloatingPointVariable( ExpressionBuilderParams p, EvaluationStatement template, ExpressionResult left, ExpressionResult right) { if (left.TypeDescriptor.IsNumericOrFloat() || right.TypeDescriptor.IsNumericOrFloat()) { if (template is LogicalEvaluationStatement) { return(true); } var parent = template.ParentStatement; if (parent is VariableDefinitionStatement) { return(false); } if (parent is ArithmeticEvaluationStatement arithmeticEvaluationStatement && arithmeticEvaluationStatement.Operator is AdditionOperator) { return(arithmeticEvaluationStatement.Left.GetDataType(p.Context, p.Scope).IsString() || arithmeticEvaluationStatement.Right.GetDataType(p.Context, p.Scope).IsString()); } if (parent is FunctionCallStatement) { return(true); } return(!(parent is EvaluationStatement)); } return(false); }
public override string FormatVariableAccessExpression(ExpressionBuilderParams p, TypeDescriptor typeDescriptor, string expression, EvaluationStatement template) { //$10 should be surrounded by braces or it will considered $1 followed by a zero var exp = expression.All(char.IsDigit) && expression.Length > 1 ? $"${{{expression}}}" : '$' + expression; if (!typeDescriptor.IsString()) { return(exp); } if (template.ParentStatement is ArithmeticEvaluationStatement arithmeticEvaluationStatement && arithmeticEvaluationStatement.Operator is AdditionOperator) { return(exp); } // if (template.ParentStatement is AssignmentStatement || // template.ParentStatement is VariableDefinitionStatement) // { // return $"\"{base.FormatVariableAccessExpression(p, dataType, expression, template)}\""; // } return(StringHelpers.EnQuote(exp)); }
public static IApiMethodBuilderResult CompileResourceMethod <TFunc>( TFunc func, ExpressionBuilderParams p, string resourceName, FunctionInfo functionInfo, EvaluationStatement[] parameters, StatementInfo statementInfo) where TFunc : ApiBaseFunction { // if (p.Scope.TryGetFunctionInfo(functionInfo, out var funcInfo)) // { // return Inline(new FunctionCallStatement(funcInfo.ObjectName, funcInfo.Name, funcInfo.DataType, // parameters, statementInfo)); // } // // using (var file = Assembly.GetCallingAssembly() // .GetManifestResourceStream(typeof(TFunc), resourceName)) // using (var reader = new StreamReader(file)) // { // string line; // while ((line = reader.ReadLine()) != null) // { // p.MetaWriter.WriteLine(line); // } // // p.MetaWriter.WriteLine(); // } // // p.Context.GeneralScope.ReserveNewFunction(functionInfo); return(Inline(new FunctionCallStatement(func.ClassName, functionInfo.Name, functionInfo.TypeDescriptor, parameters, statementInfo))); }
public override IApiMethodBuilderResult Build(ExpressionBuilderParams p, FunctionCallStatement functionCallStatement) { AssertParameters(p, functionCallStatement.Parameters); var param = functionCallStatement.Parameters[0]; var dt = param.GetDataType(p.Context, p.Scope); EvaluationStatement exp; if (dt.IsBoolean() || dt.IsNumber()) { //create an string concatenation expression, ExpressionBuilder has this functionality inside. exp = new ArithmeticEvaluationStatement(param, new AdditionOperator(param.Info), new ConstantValueStatement(TypeDescriptor.String, "", param.Info), param.Info, param.ParentStatement); param.ParentStatement = exp; } else if (dt.IsString()) { exp = param; } else { throw new MethodParameterMismatchCompilerException(Name, functionCallStatement.Info); } var transpiler = p.Context.GetEvaluationTranspilerForStatement(exp); var result = transpiler.GetExpression(p, exp); return(new ApiMethodBuilderRawResult(result)); }
public override string FormatFunctionCallParameterSubExpression(ExpressionBuilderParams p, ExpressionResult result) { if (result.Template is ConstantValueStatement) { return(result.Expression); } if (result.Template is VariableAccessStatement) { return(result.Expression); } if (result.Template is FunctionCallStatement) { return(result.Expression); } if (result.TypeDescriptor.IsNumericOrFloat()) { return(result.Expression); } if (result.TypeDescriptor.IsString()) { return($"\"{result.Expression}\""); } return($"$(({result.Expression}))"); }
public static ExpressionResult CreateBashConditionalExpression(ExpressionBuilderParams p, EvaluationStatement evalStt) { evalStt = ProcessEvaluation(p.Context, p.Scope, evalStt); return(BashConditionalExpressionBuilder.Instance.CreateExpression(p, evalStt)); }
public static IApiMethodBuilderResult WriteNativeMethod <TFunc>( TFunc func, ExpressionBuilderParams p, string methodBody, FunctionInfo functionInfo, EvaluationStatement[] parameters, StatementInfo statementInfo) where TFunc : ApiBaseFunction { if (p.Scope.TryGetFunctionInfo(functionInfo, out var funcInfo)) { return(Inline(new FunctionCallStatement(funcInfo.ClassName, funcInfo.Name, funcInfo.TypeDescriptor, parameters, statementInfo))); } using (var funcWriter = new StringWriter()) { funcWriter.Write("function "); funcWriter.Write(functionInfo.Fqn); funcWriter.WriteLine("() {"); funcWriter.WriteLine(methodBody); funcWriter.WriteLine("}"); p.MetaWriter.Write(funcWriter); } p.Context.GeneralScope.ReserveNewFunction(functionInfo); return(Inline(new FunctionCallStatement(func.ClassName, functionInfo.Name, functionInfo.TypeDescriptor, parameters, statementInfo))); }
public static IApiMethodBuilderResult CompileMethod <TFunc>( TFunc func, ExpressionBuilderParams p, string methodBody, FunctionInfo functionInfo, EvaluationStatement[] parameters, StatementInfo statementInfo) where TFunc : ApiBaseFunction { return(Inline(new FunctionCallStatement(func.ClassName, functionInfo.Name, functionInfo.TypeDescriptor, parameters, statementInfo))); }
public void AssertParameters(ExpressionBuilderParams p, EvaluationStatement[] parameters) { if (!FunctionStatementTranspilerBase.ValidateParameters(p.Context, p.Scope, Parameters, parameters, out var exception)) { throw exception; } }
public override string FormatSubExpression(ExpressionBuilderParams p, ExpressionResult result) { if (result.TypeDescriptor.IsString()) { return(result.Expression); } return(base.FormatSubExpression(p, result)); }
public override string FormatSubExpression(ExpressionBuilderParams p, ExpressionResult result) { if (result.Template is LogicalEvaluationStatement) { return(result.Expression); } return(base.FormatSubExpression(p, result)); }
public override IApiMethodBuilderResult Build(ExpressionBuilderParams p, FunctionCallStatement functionCallStatement) { AssertParameters(p, functionCallStatement.Parameters); var result = CreateVariableAccess(TypeDescriptor, "LANG", functionCallStatement.Info); return(new ApiMethodBuilderRawResult(result)); }
public override string FormatLogicalExpression(ExpressionBuilderParams p, ExpressionResult left, IOperator op, ExpressionResult right, EvaluationStatement template) { return(FormatLogicalExpression(p, left.TypeDescriptor, left.Expression, op, right.TypeDescriptor, right.Expression, template)); }
public override IApiMethodBuilderResult Build(ExpressionBuilderParams p, FunctionCallStatement functionCallStatement) { return(BashTestCommand.CreateTestExpression(this, p, functionCallStatement, (p1, fcs) => new ExpressionResult( TypeDescriptor, $"[ $(whoami) == 'root' ]", functionCallStatement ))); }
private static void WriteAssignment(Context context, Scope scope, TextWriter writer, TextWriter metaWriter, TextWriter nonInlinePartWriter, AssignmentStatement assignmentStatement) { var target = assignmentStatement.LeftSide as VariableAccessStatement; if (target == null) { if (assignmentStatement.LeftSide == null) { throw new InvalidStatementStructureCompilerException(assignmentStatement, assignmentStatement.Info); } throw new InvalidStatementStructureCompilerException( "Only variables can be presented in the left side of an assignment", assignmentStatement.LeftSide.Info); } var evaluation = assignmentStatement.RightSide; if (evaluation == null) { throw new InvalidStatementStructureCompilerException( "Unknown right side of an assignment found.", assignmentStatement.RightSide.Info); } if (!scope.TryGetVariableInfo(target, out var varInfo)) { throw new IdentifierNotFoundCompilerException(target); } var p = new ExpressionBuilderParams(context, scope, metaWriter, nonInlinePartWriter, assignmentStatement); var transpiler = context.GetEvaluationTranspilerForStatement(evaluation); if (varInfo.TypeDescriptor.IsArray()) { var call = transpiler.CallApiFunction <ApiArray.Copy>(p, new[] { target, evaluation }, assignmentStatement, assignmentStatement.Info); if (!call.IsEmptyResult) { writer.Write($"{varInfo.AccessName}="); writer.WriteLine(call.Expression); } } else { var result = transpiler.GetExpression(context, scope, metaWriter, nonInlinePartWriter, null, evaluation); writer.Write($"{varInfo.AccessName}="); writer.WriteLine(result.Expression); } }
public override string FormatVariableAccessExpression(ExpressionBuilderParams p, ExpressionResult result) { if (result.TypeDescriptor.IsBoolean() && result.Template.ParentStatement is ConditionalBlockStatement) { return(_formatBoolVariable( base.FormatVariableAccessExpression(p, result) )); } return(base.FormatVariableAccessExpression(p, result)); }
public override string FormatExpression(ExpressionBuilderParams p, ExpressionResult result) { if (result.TypeDescriptor.IsBoolean() || result.Template is LogicalEvaluationStatement) { if (result.Expression[0] != '[' && result.Expression[result.Expression.Length - 1] != ']') { return($"[ {result.Expression} ]"); } } return(base.FormatExpression(p, result)); }
public override string FormatArrayAccessExpression(ExpressionBuilderParams p, ExpressionResult source, ExpressionResult indexer) { var sExp = source.Expression; if (sExp.StartsWith('$')) { sExp = sExp.Substring(1); } return($"${{{sExp}[{indexer.Expression}]}}"); }
public override string FormatVariableAccessExpression(ExpressionBuilderParams p, TypeDescriptor typeDescriptor, string expression, EvaluationStatement template) { if (typeDescriptor.IsBoolean() && template.ParentStatement is ConditionalBlockStatement) { return(_formatBoolVariable( base.FormatVariableAccessExpression(p, typeDescriptor, expression, template) )); } return(base.FormatVariableAccessExpression(p, typeDescriptor, expression, template)); }
public override IApiMethodBuilderResult Build(ExpressionBuilderParams p, FunctionCallStatement functionCallStatement) { AssertParameters(p, functionCallStatement.Parameters); var param = functionCallStatement.Parameters[0]; var transpiler = p.Context.GetEvaluationTranspilerForStatement(param); var result = transpiler.GetExpression(p, param); return(new ApiMethodBuilderRawResult(result)); }
public override string FormatExpression(ExpressionBuilderParams p, ExpressionResult result) { if (result.Template is ConstantValueStatement || result.Template is VariableAccessStatement || result.Template is IndexerAccessStatement || result.Template is ArrayStatement) { if (result.TypeDescriptor.IsString()) { if (p.FormatString) { return(StringHelpers.EnQuote(result.Expression)); } return(result.Expression); } return(result.Expression); } //if (result.Template is VariableAccessStatement) // return result.Expression; if (result.Template is FunctionCallStatement) { return(result.Expression); } if (result.TypeDescriptor.IsNumericOrFloat()) { string expression; if (result.Expression.Contains("\"")) { expression = result.Expression.Replace('"', '\''); } else { expression = result.Expression; } return($"`awk \"BEGIN {{print ({expression})}}\"`"); } if (result.TypeDescriptor.IsString()) { if (p.FormatString) { return(StringHelpers.EnQuote(result.Expression)); } return(result.Expression); } return(FormatEvaluationExpression(p, result)); }
public override ExpressionResult CallApiFunction <TApiFunc>(ExpressionBuilderParams p, EvaluationStatement[] parameters, IStatement parentStatement, StatementInfo statementInfo) { var nParams = new List <EvaluationStatement>(parameters.Length); foreach (var param in parameters) { nParams.Add(ProcessEvaluation(p.Context, p.Scope, param)); } return(BashDefaultExpressionBuilder.Instance.CallApiFunction <TApiFunc>(p, nParams.ToArray(), parentStatement, statementInfo)); }
public override void WriteInline(Context context, Scope scope, TextWriter writer, TextWriter metaWriter, TextWriter nonInlinePartWriter, IStatement statement) { if (!(statement is EvaluationStatement evalStt)) { throw new InvalidOperationException(); } var parameters = new ExpressionBuilderParams(context, scope, metaWriter, nonInlinePartWriter, null); var expression = CreateBashExpression(parameters, evalStt); writer.Write(expression); }
public override IApiMethodBuilderResult Build(ExpressionBuilderParams p, FunctionCallStatement functionCallStatement) { AssertParameters(p, functionCallStatement.Parameters); var number = functionCallStatement.Parameters[0]; switch (number) { case ConstantValueStatement constantValueStatement: { return(InlineConstant(constantValueStatement.TypeDescriptor, constantValueStatement.Value, constantValueStatement)); } case VariableAccessStatement variableAccessStatement: { if (p.Scope.TryGetVariableInfo(variableAccessStatement, out var varInfo)) { if (varInfo.TypeDescriptor.IsInteger()) { return(new ApiMethodBuilderRawResult(new ExpressionResult( varInfo.TypeDescriptor, $"${{{varInfo.AccessName}#-}}", variableAccessStatement ))); } return(CreateNativeMethodWithUtilityExpressionSelector(this, p, _functionInfo, _absUtilityExpressions, functionCallStatement.Parameters, functionCallStatement.Info)); } if (p.Scope.TryGetConstantInfo(variableAccessStatement, out var constInfo)) { return(InlineConstant(constInfo.TypeDescriptor, constInfo.Value, variableAccessStatement)); } throw new IdentifierNotFoundCompilerException(variableAccessStatement); } default: { return(CreateNativeMethodWithUtilityExpressionSelector(this, p, _functionInfo, _absUtilityExpressions, functionCallStatement.Parameters, functionCallStatement.Info)); } } }
public override IApiMethodBuilderResult Build(ExpressionBuilderParams p, FunctionCallStatement functionCallStatement) { AssertParameters(p, functionCallStatement.Parameters); var str = functionCallStatement.Parameters[0]; switch (str) { case ConstantValueStatement constantValueStatement: { return(InlineConstant(constantValueStatement.TypeDescriptor, constantValueStatement.Value, constantValueStatement)); } case VariableAccessStatement variableAccessStatement: { if (p.Scope.TryGetVariableInfo(variableAccessStatement, out var varInfo)) { if (varInfo.TypeDescriptor.IsString()) { return(new ApiMethodBuilderRawResult(new ExpressionResult( TypeDescriptor, $"${{#{varInfo.AccessName}}}", variableAccessStatement ))); } throw new TypeMismatchCompilerException(varInfo.TypeDescriptor, TypeDescriptor.String, variableAccessStatement.Info); } if (p.Scope.TryGetConstantInfo(variableAccessStatement, out var constInfo)) { return(InlineConstant(constInfo.TypeDescriptor, constInfo.Value, variableAccessStatement)); } throw new IdentifierNotFoundCompilerException(variableAccessStatement); } default: { return(WriteNativeMethod(this, p, "echo ${#1}", _functionInfo, functionCallStatement.Parameters, functionCallStatement.Info)); } } }
public override PinnedVariableResult PinEvaluationToVariable(Context context, Scope scope, TextWriter metaWriter, TextWriter pinCodeWriter, EvaluationStatement statement) { if (statement == null) { throw new ArgumentNullException(nameof(statement)); } var expressionBuilder = GetBashExpressionBuilder(context, scope, ref statement); var parameters = new ExpressionBuilderParams(context, scope, metaWriter, pinCodeWriter, null); var result = expressionBuilder.CreateExpression(parameters, statement); return(expressionBuilder.PinExpressionToVariable(parameters, null, result)); }
public override ExpressionResult CreateExpression(ExpressionBuilderParams p, EvaluationStatement statement) { var result = base.CreateExpression(p, statement); if (result.IsEmptyResult) { throw new InvalidStatementStructureCompilerException(statement, statement.Info); } if (!result.TypeDescriptor.IsBoolean()) { throw new TypeMismatchCompilerException(result.TypeDescriptor, TypeDescriptor.Boolean, statement.Info); } return(result); }
public override bool ShouldBePinnedToFloatingPointVariable(ExpressionBuilderParams p, TypeDescriptor typeDescriptor, EvaluationStatement template) { if (template is ConstantValueStatement) { return(false); } if (template is VariableAccessStatement) { return(false); } if (template is FunctionCallStatement) { return(false); } return(typeDescriptor.IsNumericOrFloat()); }