public void DomainEventDeclaration_WithNestedDto_DtoArray_ShouldCompile() { var dtoDeclaration = new DtoDeclaration( "EmployeeName", new[] { new PropertyDeclaration("First", new StringReferenceType()), new PropertyDeclaration("Last", new StringReferenceType()), }); var eventDeclaration = new DomainEventDeclaration( "DepartmentImported", new[] { new PropertyDeclaration( "DepartmentNumber", new SimpleType(typeof(int))), new PropertyDeclaration( "Employees", new ArrayType(new TypeName("EmployeeName"), new RankSpecifiers(new [] { new RankSpecifier(1) })) ) }); var model = CreateSemanticModelWith(dtoDeclaration, eventDeclaration); var actual = CodeDomCompiler.Compile(model); var source = CompileToSource(actual); Assert.That(source, Is.StringMatching(@"EmployeeName\[\]\s+Employees")); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeExpression = (CodeObjectReference)code; if (codeExpression.Modifiers.Count == 0) { var op = machine.CreateOperation <GetValueOperation>(); op.Id = codeExpression.Id; } else { foreach (var modifier in codeExpression.Modifiers) { var functionCall = modifier as CodeObjectFunctionCall; if (functionCall == null) { continue; } CodeDomCompiler.Compile(modifier, machine); var callOp = machine.CreateOperation <ObjectMemberOperation>(); callOp.MemberName = codeExpression.Id; } } if (codeExpression.Next != null) { machine.CreateOperation <PushOperation>(); CodeDomCompiler.Compile(codeExpression.Next, machine); } return(machine); }
public void Evaluator_CodeDomCompilerBuilderScript_StringResult() { using (var compiler = new CodeDomCompiler()) { var evaluator = new Evaluator <MyProducer>(compiler); var script = @" Builder.Append(Counter.ToString(""0000"")); Builder.Append("" ""); Builder.AppendLine(Message);" ; var producer = evaluator.CreateProducer(script); producer.Message = "Hello World!"; producer.Counter = 1; for (var i = 0; i <= 10; i++) { producer.Run(); producer.Counter *= 2; } Assert.AreEqual(@"0001 Hello World! 0002 Hello World! 0004 Hello World! 0008 Hello World! 0016 Hello World! 0032 Hello World! 0064 Hello World! 0128 Hello World! 0256 Hello World! 0512 Hello World! 1024 Hello World! ", producer.Builder.ToString()); } }
private CodeCompileUnit CompileEmployeeImportedEvent() { var declaration = CreateEmployeeImportedEventDeclaration(); var model = CreateSemanticModelWith(declaration); return(CodeDomCompiler.Compile(model)); }
/// <summary> /// Compile the installer. /// </summary> /// <param name="installOptions">The install options.</param> private void CompileInstaller(InstallOptions installOptions) { // Ask to close and restart to update files with installer var _references = new List <string> { "System.dll", "System.Windows.Forms.dll" }; ResourcesManager.CreateSettingsResource(installOptions.ResourceSettingsPath, installOptions); var _resources = new List <string> { installOptions.ResourceSettingsPath }; var _sources = new[] { Resources.MainEntryPoint, Resources.ConsoleManager, Resources.Installer, Resources.ResourceSettings }; string _updaterFileName = Application.StartupPath + @"\\Updater.exe"; CompilerResults _results = CodeDomCompiler.Build(_references, _sources, _updaterFileName, _resources); if (_results.Errors.Count > 0) { VisualCompileErrorDialog.Show(_results); } else { Process.Start(_updaterFileName); } File.Delete(installOptions.ResourceSettingsPath); }
public void AstDomCompiler_CompileBinaryOperator2() { IScriptContext context = new ScriptContext(); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("x=1+2*3;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.Equal(7, context.GetItem("x", true)); Assert.Equal(7, context.Result); domTree = AstDomCompiler.Compile(Script.Compile("x=1*2+3;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.Equal(5, context.GetItem("x", true)); Assert.Equal(5, context.Result); domTree = AstDomCompiler.Compile(Script.Compile("x=1+2/2+3*2;", null, false).Ast); CodeDomCompiler .Compile(domTree) .Execute(context); Assert.Equal(8, context.GetItem("x", true)); Assert.Equal(8, context.Result); }
private CodeCompileUnit CompileNameDto() { var declaration = CreateNameDtoDeclaration(); var model = CreateSemanticModelWith(declaration); return(CodeDomCompiler.Compile(model)); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { // CCCC(indexA)SSSSSS(indexB)EEEEEE(indexC) var ifStatement = (CodeIfStatement)code; CodeDomCompiler.Compile(ifStatement.Condition, machine); //Put AX (result of condition evaluation) to BBX var exch = machine.CreateOperation <RegisterOperation>(); exch.Source = MachineRegisters.AX; exch.Destination = MachineRegisters.BBX; //Jmp To Else var jmpToElse = machine.CreateOperation <JmpIfOperation>(); int eCount = machine.CommandCount; //Compile Statement CodeDomCompiler.Compile(ifStatement.Statement, machine); jmpToElse.Offset = machine.CommandCount - eCount + 1; //Compile Else if Any if (ifStatement.ElseStatement != null) { //Jmp To Next var jmpToNext = machine.CreateOperation <JmpOperation>(); //Modify jmpToElse jmpToElse.Offset++; int sCount = machine.CommandCount; CodeDomCompiler.Compile(ifStatement.ElseStatement, machine); jmpToNext.Offset = machine.CommandCount - sCount + 1; } return(machine); }
public void AstDomCompiler_IfElse() { IScriptContext context = new ScriptContext(); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("x = 1; if (x>1) return 2; else return 3;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.Equal(3, context.Result); domTree = AstDomCompiler.Compile(Script.Compile("x = 2; if (x>1) return 2; else return 3;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.Equal(2, context.Result); domTree = AstDomCompiler.Compile(Script.Compile("x = 2; if (x>1) return 5;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.Equal(5, context.Result); domTree = AstDomCompiler.Compile(Script.Compile("x = 1; if (x>1) return 5; return 10;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.Equal(10, context.Result); }
public void AstDomCompiler_While() { IScriptContext context = new ScriptContext(); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("x = 1; while (x<10) x=x+1;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.Equal(10, context.GetItem("x", true)); Assert.False((bool)context.Result); domTree = AstDomCompiler.Compile(Script.Compile("x = 1; while (x<10) x=x+1; return 3;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.Equal(3, context.Result); domTree = AstDomCompiler.Compile(Script.Compile("x = 2; while (x<2) return 2; return 5;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.Equal(5, context.Result); domTree = AstDomCompiler.Compile(Script.Compile("x = 1; while (x<2) return 2; return 5;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.Equal(2, context.Result); }
public void AstDomCompiler_CompileBinaryOperator4() { IScriptContext context = new ScriptContext(); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("1>2;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.False((bool)context.Result); domTree = AstDomCompiler.Compile(Script.Compile("1<2;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.True((bool)context.Result); domTree = AstDomCompiler.Compile(Script.Compile("2!=3;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.True((bool)context.Result); domTree = AstDomCompiler.Compile(Script.Compile("5 == 5;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.True((bool)context.Result); domTree = AstDomCompiler.Compile(Script.Compile("5>=5;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.True((bool)context.Result); domTree = AstDomCompiler.Compile(Script.Compile("5<=4;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.False((bool)context.Result); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { // CCCC(indexA)SSSSSS(indexC) var whileStatement = (CodeWhileStatement)code; int cCount = machine.CommandCount; CodeDomCompiler.Compile(whileStatement.Condition, machine); //Put AX (result of condition evaluation) to BBX var exch = machine.CreateOperation <RegisterOperation>(); exch.Source = MachineRegisters.AX; exch.Destination = MachineRegisters.BBX; //Jmp To Else var jmpToNext = machine.CreateOperation <JmpIfOperation>(); int eCount = machine.CommandCount; //Compile Statement CodeDomCompiler.Compile(whileStatement.Statement, machine); var jmpToCondition = machine.CreateOperation <JmpIfOperation>(); jmpToCondition.Offset = cCount - machine.CommandCount + 1; jmpToNext.Offset = machine.CommandCount - eCount + 1; return(machine); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeSwitch = (CodeSwitchStatement)code; //switch(a) { case c1 : s1; case c2 : s2; default : s3 } //~ //switch_expr = a; //if (a == c1) s1 //else if (a == c2) s2 //else s3 _sid++; var block = new CodeBlockStatement { SourceSpan = codeSwitch.SourceSpan }; var switchExpr = new CodeAssignExpression( "#switch_" + _sid, codeSwitch.Expression); var ifStatement = BuildNextIf(codeSwitch.Cases, 0); block.Statements.Add(new CodeExpressionStatement(switchExpr)); block.Statements.Add(ifStatement); CodeDomCompiler.Compile(block, machine); return(machine); }
private CodeCompileUnit CompileOnOffStateEnum() { var declaration = new EnumDeclaration("State", new[] { "On", "Off" }); var model = CreateSemanticModelWith(declaration); return(CodeDomCompiler.Compile(model)); }
public void Compiler_can_compile_single_file_into_memory() { var compiler = new CodeDomCompiler(); var assembly = compiler.CompileFromFiles("InputFiles/HelloWorld.cs"); Assert.IsNotNull(assembly); }
public static string Compile(string modelSourceCode) { var ast = Grammar.Everything.Parse(modelSourceCode); var semanticModel = ModelTransformations.Transform(ast); var codeDom = CodeDomCompiler.Compile(semanticModel); return(CompileToSource(codeDom, GetCSharpProvider())); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var ret = (CodeReturnStatement)code; CodeDomCompiler.Compile(ret.Expression, machine); machine.CreateOperation <RetOperation>(); return(machine); }
public void AstDomCompiler_Return() { IScriptContext context = new ScriptContext(); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("return 1+1; 2+2;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.Equal(2, context.Result); }
/// <summary>Build Tool Strip Menu Item Click.</summary> /// <param name="sender">The sender.</param> /// <param name="e">The event.</param> private void BuildToolStripMenuItem_Click(object sender, EventArgs e) { var _references = new List <string> { "System.dll", "Comet.dll" }; CompilerResults _results = CodeDomCompiler.Build(_references, tbSource.Text, Settings.InstallerPath); ProcessCompilerResults(_results); }
public void AstDomCompiler_For() { IScriptContext context = new ScriptContext(); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("sum=0;for(i=0; i<10; i=i+1) sum = sum + i;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.Equal(45, context.GetItem("sum", true)); }
public void ValueType_ValidDeclaration_ShouldNotHaveDataContractAttributes() { var model = CreateSemanticModelWith( new ValueTypeDeclaration("EmployeeNumber", new[] { new PropertyDeclaration("Value", new SimpleType(typeof(int))) })); var actual = CodeDomCompiler.Compile(model); var source = CompileToSource(actual); Assert.That(source, Is.Not.StringContaining("DataContractAttribute")); Assert.That(source, Is.Not.StringContaining("DataMemberAttribute")); }
public void CommandDeclaration_WithConventionInheritICommand_ShouldProduceClassInheritingICommand() { var declaration = CreateImportEmployeeCommandDeclaration(); var conventions = new ConventionsDeclaration( new DomainEventConventions(new BaseTypes(new TypeName[0])), new CommandConventions(new BaseTypes(new[] { new TypeName("Test.Diesel.ICommand") }))); var model = CreateSemanticModelWith(conventions, declaration); var source = CompileToSource(CodeDomCompiler.Compile(model)); Assert.That(source, Is.StringMatching(@"class ImportEmployeeCommand : Test.Diesel.ICommand")); }
public void AstDomCompiler_CompileBinaryOperator1() { IScriptContext context = new ScriptContext(); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("x=1+1+1;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.AreEqual(3, context.GetItem("x", true)); Assert.AreEqual(3, context.Result); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var blockStatement = (CodeBlockStatement)code; foreach (var statement in blockStatement.Statements) { CodeDomCompiler.Compile(statement, machine); } return(machine); }
public void AstDomCompiler_CompileSimple() { IScriptContext context = new ScriptContext(); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("x=1;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.Equal(1, context.GetItem("x", true)); Assert.Equal(1, context.Result); }
private CodeCompileUnit CompileImportEmployeeCommand() { var commandDeclaration = new CommandDeclaration("ImportEmployeeCommand", new[] { new PropertyDeclaration("EmployeeNumber", new SimpleType(typeof(int))), new PropertyDeclaration("FirstName", new StringReferenceType()), new PropertyDeclaration("LastName", new StringReferenceType()) }); var model = CreateSemanticModelWith(commandDeclaration); return(CodeDomCompiler.Compile(model)); }
public void ValueType_ValidDeclaration_ShouldCompile() { var model = CreateSemanticModelWith( new ValueTypeDeclaration("EmployeeNumber", new[] { new PropertyDeclaration("Value", new SimpleType(typeof(int))) })); var actual = CodeDomCompiler.Compile(model); Assert.That(actual, Is.Not.Null); var source = CompileToSource(actual); Assert.That(source, Is.StringContaining("public partial struct EmployeeNumber")); Console.WriteLine(source); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeExpression = (CodeAssignExpression)code; CodeDomCompiler.Compile(codeExpression.RightExpression, machine); var op = machine.CreateOperation <SetValueOperation>(); op.Id = codeExpression.Id; op.SourceObject = code; return(machine); }
public void AstDomCompiler_ForEach() { IScriptContext context = new ScriptContext(); context.SetItem("a", new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("sum=0; foreach(i in a) sum = sum + i;", null, false).Ast); ExecutableMachine vm = CodeDomCompiler.Compile(domTree); vm.Execute(context); Assert.Equal(45, context.GetItem("sum", true)); }
public ExecutableMachine Compile(CodeObject code, ExecutableMachine machine) { var codeExpression = (CodeBinaryOperator)code; CodeDomCompiler.Compile(codeExpression.Right, machine); var op = machine.CreateOperation <PushOperation>(); op.SourceObject = codeExpression.Right; CodeDomCompiler.Compile(codeExpression.Left, machine); op = machine.CreateOperation <PushOperation>(); op.SourceObject = codeExpression.Left; Operation sop; switch (codeExpression.Type) { case OperatorType.Plus: sop = machine.CreateOperation <AddOperation>(); break; case OperatorType.Minus: sop = machine.CreateOperation <SubOperation>(); break; case OperatorType.Mul: sop = machine.CreateOperation <MulOperation>(); break; case OperatorType.Mod: sop = machine.CreateOperation <ModOperation>(); break; case OperatorType.Div: sop = machine.CreateOperation <DivOperation>(); break; default: var gop = machine.CreateOperation <GenericOperation>(); gop.Symbol = Mapping[codeExpression.Type]; sop = gop; break; } sop.SourceObject = codeExpression; var pop = machine.CreateOperation <PopOperation>(); pop.SourceObject = codeExpression; return(machine); }
public void Compiler_can_compile_single_file_into_memory() { var compiler = new CodeDomCompiler(); var assembly = compiler.CompileFromFiles("InputFiles/HelloWorld.cs"); Assert.IsNotNull(assembly); }