public void TestNegRealVar() { string source = @"MODULE Array; VAR a: REAL; BEGIN a := 1.5; a := -a; WriteReal(a); WriteLn; WriteReal(-a); WriteLn END Array."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal($"{-1.5}\n{1.5}\n", output.ToString().NlFix()); }
public void RepeatTest() { const string source = @"MODULE Test; VAR i: INTEGER; BEGIN i := 1; REPEAT WriteInt(i); WriteLn; i := i+1; UNTIL i > 5 END Test."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("1\n2\n3\n4\n5\n", output.ToString().NlFix()); }
public void Test1() { const string source = @"MODULE Test; TYPE rType = RECORD a: INTEGER; b: STRING END; VAR demo: rType; BEGIN demo.a := 1; WriteInt(demo.a); WriteLn END Test."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("1\n", output.ToString().NlFix()); }
public void SimpleArraySetAndGetValueGlobal() { const string source = @"MODULE Array3; VAR a: ARRAY 32 OF INTEGER; n: INTEGER; BEGIN a[1] := 1; n := a[1]; WriteInt(n); WriteLn END Array3."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("1\n", output.ToString().NlFix()); }
public void TestArrayCallByValue() { string source = @"MODULE Test; TYPE aType = ARRAY 5 OF INTEGER; VAR arr: aType; PROCEDURE TestArray(a: aType); BEGIN IF (a[1] # 1) THEN WriteString('a is 0') END; a[1] := 2 END TestArray; BEGIN arr[1] := 1; TestArray(arr); WriteBool(arr[1] = 1); WriteLn END Test."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal($"{true}\n", output.ToString().NlFix()); }
public void TestReadBoolVar() { string source = @"MODULE Array; VAR a,b: BOOLEAN; BEGIN ReadBool(a); ReadBool(b); WriteBool(a); WriteLn; WriteBool(b); WriteLn; END Array."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output, new StringReader("true" + Environment.NewLine + "false" + Environment.NewLine)); Assert.Equal($"{true}\n{false}\n", output.ToString().NlFix()); }
public void TestReadIntVar() { const string source = @"MODULE Array; VAR a: INTEGER; BEGIN ReadInt(a); WriteInt(a); WriteLn; END Array."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output, new StringReader("12" + Environment.NewLine)); Assert.Equal("12\n", output.ToString().NlFix()); }
public void TestIssue25Repeat() { const string source = @"MODULE Issue25; VAR x: BOOLEAN; BEGIN x := FALSE; REPEAT WriteString('Yes'); x := TRUE UNTIL x; WriteLn END Issue25."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("Yes\n", output.ToString().NlFix()); }
public void TestEpsilon() { string source = @"MODULE Test; CONST expected = 10.8511834932; VAR r, s, z: REAL; b, c: BOOLEAN; BEGIN r := 1.5; s := 7.2341223288; z := r * s; b := (r - expected) < EPSILON; c := r = expected; WriteBool(b); WriteString(','); WriteBool(c); WriteLn END Test."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal($"{true},{false}\n", output.ToString().NlFix()); }
public void TestReservedWordIssue23() { const string source = @"MODULE Test; VAR int: INTEGER; BEGIN int := 1; WriteInt(int); WriteLn END Test."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("1\n", output.ToString().NlFix()); }
public void SystemToStringReal() { const string source = @"MODULE ToStringTest; VAR s: STRING; BEGIN s := ToString(12.5); WriteString(s); WriteLn END ToStringTest."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("12.5\n", output.ToString().NlFix()); }
private object CompileList(CompileHelper helper, params string[] opers) { var list = new Vector(); CompileList(list, helper, opers); // Return the shortest code. if (list.Count == 0) { return(null); } else if (list.Count == 1) { return(list[0]); } else { object code = list[0]; for (var i = 1; i < list.Count; i += 2) { var lispOper = GetLispOperator(list[i]); if (SupportsMany(lispOper) && code is Cons && Runtime.First(code) == lispOper) { code = Runtime.Append((Cons)code, Runtime.MakeList(list[i + 1])); } else { code = Runtime.MakeList(lispOper, code, list[i + 1]); } } return(code); } }
public void TestAddVarConst() { const string source = @"MODULE Array; VAR a: INTEGER; BEGIN a := 1; WriteInt(a+2); WriteLn END Array."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("3\n", output.ToString().NlFix()); }
public void The_StringBuilder_Does_Not_Change_1() { const string functionText = "return true;"; string output = CompileHelper.ReplaceVirtualPropertyCalls(functionText); Assert.That(output, Is.EqualTo(functionText)); }
public ScriptOutput Run(ScriptInput input) { var co = new ScriptOutput(); co.Code = input.Code; co.Output = CompileHelper.CompileAndRun(string.Format(input.Language == Language.CSharp?CodeSamples.ScriptWrapperCSharp:CodeSamples.ScriptWrapperVBNet, input.Code), input.Language); return(co); }
public void The_StringBuilder_Does_Not_Change_2() { const string functionText = "entity.VirtualPropertyName.ToString();"; string output = CompileHelper.ReplaceVirtualPropertyCalls(functionText); Assert.That(output, Is.EqualTo(functionText)); }
public void The_Call_Is_Changed() { const string functionText = "entity.VirtualProperties.NameX;\nentity.VirtualProperties.NameX;"; const string expectedFunctionText = "entity.get_NameX();\nentity.get_NameX();"; string output = CompileHelper.ReplaceVirtualPropertyCalls(functionText); Assert.That(output, Is.EqualTo(expectedFunctionText)); }
private void MarkErrorWord(SyntaxEditor editor, int lineNumber, int characterPos, string message) { string text = editor.Document.Lines[lineNumber].Text; string compileText = CompileHelper.ReplaceUserOptionCalls(text); string preceedingText = characterPos <= compileText.Length ? compileText.Substring(0, characterPos) : ""; #region Find all GetUserOption calls and discount them int index = preceedingText.LastIndexOf("GetUserOption"); int offsetUO = "GetUserOptionValue('')".Length - "UserOptions.".Length; int castEndPos = 0; int castStartPos = 0; while (index >= 0) { characterPos -= offsetUO; while (preceedingText[index] != ')') { castEndPos = index; index -= 1; } while (preceedingText[index] != '(') { castStartPos = index; index -= 1; } characterPos -= castEndPos - castStartPos + 1; index = preceedingText.LastIndexOf("GetUserOption", index); } #endregion DocumentPosition position = new DocumentPosition(lineNumber, characterPos); int offset = editor.Document.PositionToOffset(position); DynamicToken token = (DynamicToken)editor.Document.Tokens.GetTokenAtOffset(offset); SpanIndicator indicator = new WaveLineSpanIndicator("ErrorIndicator", Color.Red); indicator.Tag = message; SpanIndicatorLayer indicatorLayer = editor.Document.SpanIndicatorLayers[ErrorLayerKey]; if (indicatorLayer == null) { indicatorLayer = new SpanIndicatorLayer(ErrorLayerKey, 1); editor.Document.SpanIndicatorLayers.Add(indicatorLayer); } int startOffset = Math.Min(token.StartOffset, indicatorLayer.Document.Length - 1); int length = Math.Max(token.Length, 1); SpanIndicator[] indicators = indicatorLayer.GetIndicatorsForTextRange(new TextRange(startOffset, startOffset + length)); foreach (SpanIndicator i in indicators) { // If there is already an error indicator on that word, don't add another one. if (i.TextRange.StartOffset == startOffset && i.TextRange.Length == length) return; } indicatorLayer.Add(indicator, startOffset, length); }
public ActionResult Run(CodeViewModel vm) { _logRepository.Insert(new Log { InputCode = vm.InputCode, IpAddress = Request.UserHostAddress }); return(new ContentResult { Content = CompileHelper.CompileAndRun(vm.InputCode) }); }
public static CompilationResult <TriggerDelegate> Compile(IReadOnlyDictionary <string, IReadOnlyDictionary <string, Type> > properties, string body) { var className = $"Generated_{Guid.NewGuid():N}"; body = GenerateExplosionCode(properties) + Environment.NewLine + body; var result = CompileHelper.CompileTo <TriggerDelegate>(Namespace, className, MethodName, body, ReferencedAssemblies, Usings); return(result); }
public ConsoleOutput Run(ConsoleInput input) { _logRepository.Insert(new Log { InputCode = input.Code, IpAddress = GetClientIp(Request) }); //TODO: use Roslyn to compile and run var co = new Models.ConsoleOutput(); co.Code = input.Code; co.Output = CompileHelper.CompileAndRun(input.Code, input.Language); co.Id = input.Id; return(co); }
private async void BtnRun_Click(object sender, RoutedEventArgs e) { if (Code.IsNullOrEmpty()) { return; } string lang = ""; switch (CodeLanguage) { case "python": lang = "py"; break; case "cplusplus": lang = "cpp"; break; case "c": lang = "c"; break; case "java": lang = "java"; break; default: break; } if (lang == "") { await DispatcherHelper.ExecuteOnUIThreadAsync(() => { RunResult = "暂不支持此语言"; }); } else { await DispatcherHelper.ExecuteOnUIThreadAsync(() => { RunResult = "正在编译运行……"; }); string result = await CompileHelper.Compile(lang, Code, new Random().Next().ToString()); await DispatcherHelper.ExecuteOnUIThreadAsync(() => { RunResult = result; }); } }
static void Main() { Console.WriteLine("Set firewall..."); Assembly assembly = Assembly.GetExecutingAssembly(); Starter.SetTiaPortalFirewall(assembly); TiaPortal tiaPortal = null; try { Console.WriteLine("Get instance..."); tiaPortal = Starter.GetInstance(TiaPortalMode.WithUserInterface); Console.WriteLine("Create project..."); string projectFullPath = Path.Combine(DataPath, "debug", "MyProject", "MyProject.ap15_1"); Project project = ProjectHelper.CreateProjectIfNotExists(tiaPortal, projectFullPath); Console.WriteLine("Insert devices..."); List <Device> devices = InsertDevices(project); Console.WriteLine("Get network interfaces..."); List <Node> nodes = GetNetworkInterfaces(devices); Console.WriteLine("Create Subnet..."); NetworkHelper.ConnectNodesToNetwork(project, "System:Subnet.Ethernet", "MySubnet", nodes); Console.WriteLine("Get software..."); List <Software> softwareList = GetSoftwareList(devices); Console.WriteLine("Insert blocks from SCL source..."); List <PlcSoftware> plcSoftwareList = softwareList.OfType <PlcSoftware>().ToList(); InsertBlocksFromSclSource(plcSoftwareList); Console.WriteLine("Compile..."); CompileHelper.Compile(softwareList); Console.WriteLine("Save project..."); project.Save(); Console.WriteLine("Show block in editor..."); var block = plcSoftwareList.FirstOrDefault()?.BlockGroup.Blocks .OfType <OB>() .FirstOrDefault(obj => obj.Number.Equals(1)); block?.ShowInEditor(); } finally { tiaPortal?.Dispose(); } }
/// <summary> /// /// </summary> /// <param name="fileName"></param> /// <returns>True if successfully compiled, false otherwise.</returns> public static bool CompileStzFile(string fileName) { Project.Instance.Clear(); Project.Instance.ProjectFileName = fileName; Project.Instance.CompileFolderName = ""; Project.Instance.Open(fileName); Settings.Default.CodeFile = System.IO.Path.GetTempFileName(); if (CompileHelper.Compile(false)) { return(true); } return(false); }
private object CompileBinary(CompileHelper leftHelper, CompileHelper rightHelper, params string[] opers) { var node1 = leftHelper(); var oper = FindOper(Token, opers); if (oper != null) { Accept(); var node2 = rightHelper(); return(Runtime.MakeList(GetLispOperator(oper), node1, node2)); } return(node1); }
public void SimpleArrayDefinition() { const string source = @"MODULE Array; VAR a: ARRAY 32 OF INTEGER; b: ARRAY 32 OF BOOLEAN; END Array."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); Assert.True(syntaxTree.CompileAndLoadAssembly(cg) != null); }
/// <summary> /// Invokes the Component async. /// </summary> /// <param name="org">Unique identifier of the organisation responsible for the app.</param> /// <param name="app">Application identifier which is unique within an organisation.</param> /// <param name="serviceMetadata">The service metadata.</param> /// <param name="codeCompilationResult">The code compilation result.</param> /// <returns>The <see cref="Task"/>.</returns> public async Task <IViewComponentResult> InvokeAsync( string org, string app, ServiceMetadata serviceMetadata = null, CodeCompilationResult codeCompilationResult = null) { var serviceIdentifier = new ServiceIdentifier { Org = org, Service = app }; var compilation = codeCompilationResult ?? await CompileHelper.CompileService(_compilation, serviceIdentifier); var metadata = serviceMetadata ?? await GetServiceMetadata(serviceIdentifier); var model = CreateModel(serviceIdentifier, compilation, metadata); return(View(model)); }
public void Multiply(int a, int b) { string source = @" MODULE MultiplyTest; VAR x, y, z: INTEGER; PROCEDURE Multiply(x: INTEGER; y: INTEGER; VAR z: INTEGER); VAR negate : BOOLEAN; BEGIN negate := x < 0; IF (negate) THEN x := -x END; z := 0; WHILE x > 0 DO IF x MOD 2 = 1 THEN z := z + y END; y := 2*y; x := x DIV 2 END ; IF (negate) THEN z := -z END; END Multiply; BEGIN ReadInt(x); ReadInt(y); Multiply(x, y, z); WriteInt(z); WriteLn END MultiplyTest. "; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output, new StringReader($"{a}{Environment.NewLine}{b}{Environment.NewLine}")); Assert.Equal($"{a * b}\n", output.ToString().NlFix()); }
public CompileHelper CompileTheCode( ITakeSample itakeSample, // pass this to executing code, which may not reference WPF asms string pathFileToExecute, CancellationToken token) { var compilerHelper = new CompileHelper(pathFileToExecute, _logger, itakeSample, token); try { compilerHelper.CompileTheCode(); } catch (Exception ex) { compilerHelper.CompileResults = ex.ToString(); } return(compilerHelper); }
public void TestProcedureBoolean() { string source = @" MODULE ProcTest; CONST true_ = TRUE; VAR x, y: BOOLEAN; i, j: BOOLEAN; PROCEDURE Test(x: BOOLEAN; y: BOOLEAN; VAR a: BOOLEAN; b: BOOLEAN); BEGIN x := FALSE; y := true_; a := TRUE; b := FALSE; END Test; BEGIN x := TRUE; y := x = true_; i := FALSE; j := FALSE; Test(y, x, i, j); WriteBool(x); WriteString(', '); WriteBool(y); WriteString(', '); WriteBool(i); WriteString(', '); WriteBool(j); WriteLn END ProcTest. "; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal($"{true}, {true}, {true}, {false}\n", output.ToString().NlFix()); }
private object CompileBinary(CompileHelper helper, params string[] opers) { return CompileBinary(helper, helper, opers); }
private object CompileBinary(CompileHelper leftHelper, CompileHelper rightHelper, params string[] opers) { var node1 = leftHelper(); var oper = Runtime.Find(Token, opers); if (oper != null) { Accept(); var node2 = rightHelper(); return Runtime.MakeList(GetLispOperator(oper), node1, node2); } return node1; }
private object CompileList(CompileHelper helper, params string[] opers) { var list = new Vector(); CompileList(list, helper, opers); // Return the shortest code. if (list.Count == 0) { return null; } else if (list.Count == 1) { return list[0]; } else { object code = list[0]; for (var i = 1; i < list.Count; i += 2) { var lispOper = GetLispOperator(list[i]); if (SupportsMany(lispOper) && code is Cons && Runtime.First(code) == lispOper) { code = Runtime.Append((Cons)code, Runtime.MakeList(list[i + 1])); } else { code = Runtime.MakeList(lispOper, code, list[i + 1]); } } return code; } }
private void CompileList(Vector list, CompileHelper helper, params string[] opers) { var node1 = helper(); if (node1 == null) { return; } list.Add(node1); if (Tokens.Count != 0) { var oper = Runtime.Find(Token, opers); if (oper != null) { Accept(); list.Add(oper); CompileList(list, helper, opers); } } }
private object CompileTests(CompileHelper helper, params string[] opers) { var list = new Vector(); CompileList(list, helper, opers); // Return the shortest code. if (list.Count == 0) { return null; } else if (list.Count == 1) { return list[0]; } else { // 3, 5, 8 etc: a==b==c becomes (and (= a b) (= b c)) var list2 = new Vector(); for (var i = 0; i + 1 < list.Count; i += 2) { var lispOper = GetLispOperator(list[i + 1]); list2.Add(Runtime.MakeList(lispOper, list[i], list[i + 2])); } if (list2.Count == 1) { return list2[0]; } else { return Runtime.MakeListStar(Symbols.And, list2); } } }