private void Format_OnClick(object sender, RoutedEventArgs e) { if (TabControl.SelectedItem == null) { return; } var tab = (EditorTab)TabControl.SelectedItem; tab.CompilerMessages.Clear(); var validated = ValidateCurrentEditorText(); if (validated == null) { return; } var settings = AppSettings.Settings.CurrentFormatterConfiguration.FormatterSettings; var formatter = new LSLCodeFormatter(settings); var str = new StringWriter(); formatter.Format(tab.SourceCode, validated, str); var st = str.ToString(); if (tab.SourceCode != st) { tab.ChangesPending = true; } tab.SourceCode = st; tab.CompilerMessages.Clear(); validated = ValidateCurrentEditorText(); if (validated != null) { tab.CompilerMessages.Add(new CompilerMessage(CompilerMessageType.General, "Notice", "No Syntax errors detected.", false) { Clickable = false }); } }
/// <summary> /// Builds a code document manually and uses <see cref="LSLCodeFormatter"/> to /// write the tree to standard output. /// </summary> private static void PrettyPrintExample() { Console.WriteLine("Pretty Print DOM Example."); Console.WriteLine(); Console.WriteLine("======================"); Console.WriteLine(); LSLCompilationUnitNode program = new LSLCompilationUnitNode(); var globalVar1 = LSLVariableDeclarationNode.CreateGlobalVar(LSLType.Rotation, "rot", new LSLRotationLiteralNode(new LSLFloatLiteralNode(3.0f), new LSLFloatLiteralNode(3.0f), new LSLFloatLiteralNode(3.0f), new LSLFloatLiteralNode(3.0f)) ); var globalVar2 = LSLVariableDeclarationNode.CreateGlobalVar(LSLType.Vector, "vec", new LSLVectorLiteralNode(new LSLFloatLiteralNode(3.0f), new LSLFloatLiteralNode(3.0f), new LSLFloatLiteralNode(3.0f)) ); var globalVar3 = LSLVariableDeclarationNode.CreateGlobalVar(LSLType.Integer, "int", new LSLIntegerLiteralNode(0)); program.Add(globalVar1); program.Add(globalVar2); program.Add(globalVar3); var llSay = LSLFunctionSignature.Parse("llSay(integer chan, string msg);"); var callSay = new LSLFunctionCallNode(llSay, new LSLIntegerLiteralNode(0), new LSLStringLiteralNode("hello world")); var callSay2 = new LSLFunctionCallNode(llSay, new LSLIntegerLiteralNode(0), new LSLStringLiteralNode("HELLO WORLD")); int scopeId = 0; var funCode = new LSLCodeScopeNode(++scopeId); { var init = new LSLBinaryExpressionNode(LSLType.Integer, globalVar3.CreateReference(), LSLBinaryOperationType.Assign, new LSLIntegerLiteralNode(0)); var cond = new LSLBinaryExpressionNode(LSLType.Integer, globalVar3.CreateReference(), LSLBinaryOperationType.LessThan, new LSLIntegerLiteralNode(100)); var after = new LSLPostfixOperationNode(LSLType.Integer, globalVar3.CreateReference(), LSLPostfixOperationType.Increment); funCode.AddStatement( new LSLForLoopNode(init, cond, after, new LSLCodeScopeNode(++scopeId, callSay.Clone())) ); funCode.AddStatement( new LSLDoLoopNode( new LSLCodeScopeNode(++scopeId, callSay2.Clone()), cond.Clone()) ); } var myFunc = new LSLFunctionDeclarationNode(LSLType.Void, "my_function", funCode); program.Add(myFunc); scopeId = 0; var eventCode = new LSLCodeScopeNode(++scopeId); { eventCode.AddStatement(LSLVariableDeclarationNode.CreateLocalVar(LSLType.List, "localVar", new LSLListLiteralNode(new LSLIntegerLiteralNode(3), new LSLIntegerLiteralNode(4)) )); var label = new LSLLabelStatementNode("label"); eventCode.PreDefineLabel(label); var ic = new LSLCodeScopeNode(++scopeId); { ic.AddStatement(new LSLJumpStatementNode(label)); ic.AddStatement(callSay.Clone()); ic.AddStatement(new LSLFunctionCallNode(myFunc)); ic.AddStatement(callSay.Clone()); ic.EndScope(); } eventCode.AddStatement(ic); eventCode.AddStatement(label); } eventCode.EndScope(); var e = new LSLEventHandlerNode("state_entry", eventCode); program.DefaultState.Add(e); var constant = LSLVariableDeclarationNode.CreateLibraryConstant(LSLType.Integer, "TRUE"); scopeId = 0; program.Add( new LSLStateScopeNode("my_state") { new LSLEventHandlerNode("state_entry", new LSLCodeScopeNode(++scopeId, new LSLExpressionStatementNode(new LSLFunctionCallNode(llSay, new LSLIntegerLiteralNode(0), new LSLStringLiteralNode("hello world"))), new LSLDoLoopNode( new LSLCodeScopeNode(++scopeId, new LSLFunctionCallNode(myFunc)) { IsSingleStatementScope = true }, constant.CreateReference() ), new LSLWhileLoopNode( constant.CreateReference(), new LSLCodeScopeNode(++scopeId, new LSLFunctionCallNode(myFunc)) ), new LSLControlStatementNode( new LSLIfStatementNode(constant.CreateReference(), new LSLCodeScopeNode(++scopeId, new LSLFunctionCallNode(myFunc)) { IsSingleStatementScope = true } ) ), new LSLControlStatementNode( new LSLIfStatementNode(constant.CreateReference(), new LSLCodeScopeNode(++scopeId, new LSLFunctionCallNode(myFunc) )), new [] { new LSLElseIfStatementNode(globalVar3.CreateReference(), new LSLCodeScopeNode(++scopeId, new LSLFunctionCallNode(myFunc), new LSLFunctionCallNode(myFunc)) ) }, new LSLElseStatementNode( new LSLCodeScopeNode(++scopeId, new LSLPrefixOperationNode(LSLType.Integer, LSLPrefixOperationType.Increment, globalVar3.CreateReference() ) ) ) ) ) ) }); var formatter = new LSLCodeFormatter(); Console.WriteLine("Print Compilation Unit:"); Console.WriteLine(); formatter.Format(program, Console.Out); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Print Specific Event Node:"); Console.WriteLine(); formatter.Format(program.DefaultState.EventHandlers.First(), Console.Out); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Print Specific Function Call:"); Console.WriteLine(); formatter.Format(callSay, Console.Out); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("======================"); }