public async Task TestForStatementNode() { Scope scope = new Scope(); Scope innerScope = new Scope(); List <float> loopTest = new List <float>(); for (float i = 1.0f; i <= 9.0f; i++) { loopTest.Add(i); } scope.Set("loopTest", loopTest); scope.Set("test", innerScope); ScriptTree scriptTree = new ScriptTree(@" test.foo = 1.0; foo = 1.0; for (var of loopTest) { test.foo += var; foo = var; } "); await scriptTree.Evaluate(scope); Assert.Equal(46.0f, innerScope.Get("foo")); Assert.Equal(9.0f, scope.Get("foo")); }
public async Task TestScopeNode() { ScriptTree scriptTree = new ScriptTree("foo.bar;"); Scope scope = new Scope(); Scope innerScope = new Scope(); scope.Set("foo", innerScope); innerScope.Set("bar", 5); DynamicReturnValue value = await scriptTree.Evaluate(scope); Console.WriteLine($"result {value.GetValue<int>()}"); Assert.Equal(5, value.GetValue <int>()); innerScope.Set("bar", false); value = await scriptTree.Evaluate(scope); Assert.Equal(false, value.GetValue <bool>()); scriptTree = new ScriptTree("foo.baz.nitch"); Scope lastScope = new Scope(); lastScope.Set("nitch", 101); innerScope.Set("baz", lastScope); value = await scriptTree.Evaluate(scope); Assert.Equal(101, value.GetValue <int>()); }
public static IfStatementNode Parse(AstTreeNode lastNode, ScriptToken scriptToken, List <ScriptToken> tokens) { if (tokens[1].Type != EScriptTokenType.L_PAREN) { Console.WriteLine("If statement needs to be followed by a condition encased in parenthesis."); return(null); } List <ConditionalNode> nodes = new List <ConditionalNode>(); nodes.Add(ParseConditional(tokens)); while (tokens.Count > 0 && EScriptTokenType.ELSE_IF == tokens[0].Type) { nodes.Add(ParseConditional(tokens)); } if (tokens.Count > 0 && EScriptTokenType.ELSE == tokens[0].Type) { tokens.RemoveAt(0); // Consume else nodes.Add(new ConditionalNode( new LiteralNode <bool>(true), ScriptTree.ProcessTokens(ScriptTree.GetBlockTokens(tokens, EBlockType.BRACE, false)[0]) )); } return(new IfStatementNode(nodes)); }
public void Load(ScriptTree tree, string name) { var p = GetSystem(tree.Nodes); nodes = p.Key; connections = p.Value; }
public static ForStatementNode Parse(AstTreeNode lastNode, ScriptToken scriptToken, List <ScriptToken> tokens) { if (tokens[1].Type != EScriptTokenType.L_PAREN) { Console.WriteLine("Syntax error: Missing ('"); return(null); } if (tokens[3].Type != EScriptTokenType.OF) { Console.WriteLine("Syntax error: Missing of"); return(null); } tokens.RemoveAt(0); // consume for List <ScriptToken> loopDef = ScriptTree.GetEnclosedTokens(tokens); ScriptToken variableName = loopDef[0]; loopDef.RemoveAt(0); // consume variable name loopDef.RemoveAt(0); // consume of AstTreeNode list = ScriptTree.ProcessTokens(loopDef); AstTreeNode block = ScriptTree.ProcessTokens(ScriptTree.GetBlockTokens(tokens, EBlockType.BRACE, false)[0]); return(new ForStatementNode( new LiteralNode <string>(variableName.Value), list, block )); }
public async Task TestStringLiteral() { ScriptTree scriptTree = new ScriptTree("'testing'"); DynamicReturnValue value = await scriptTree.Evaluate(new Scope()); Assert.Equal("testing", value.GetValue <string>()); scriptTree = new ScriptTree("\"testing double quotes\""); value = await scriptTree.Evaluate(new Scope()); Assert.Equal("testing double quotes", value.GetValue <string>()); }
public async Task TestBooleanLiteral() { ScriptTree scriptTree = new ScriptTree("true"); DynamicReturnValue value = await scriptTree.Evaluate(new Scope()); Assert.Equal(true, value.GetValue <bool>()); scriptTree = new ScriptTree("false"); value = await scriptTree.Evaluate(new Scope()); Assert.Equal(false, value.GetValue <bool>()); }
public async Task TestNumberLiteral() { ScriptTree scriptTree = new ScriptTree("5.52"); DynamicReturnValue value = await scriptTree.Evaluate(new Scope()); Assert.Equal(5.52f, value.GetValue <float>()); scriptTree = new ScriptTree("5"); value = await scriptTree.Evaluate(new Scope()); Assert.Equal(5, value.GetValue <int>()); }
public static void RedrawScripts() { ScriptTree.SafeAction(s => { s.BeginUpdate(); s.Nodes.Clear(); Recurse(s.Nodes, Config.GetUserDirectory("Scripts")); s.EndUpdate(); s.Refresh(); s.Update(); }); }
public async Task TestAsyncFunctionCallNode() { Function function = new Function(typeof(FunctionTest), "MyTestAsyncFunction"); ScriptTree scriptTree = new ScriptTree(@"test_function(words)"); Scope scope = new Scope(); string words = "pancakes are super tasty."; scope.Set("words", words); scope.Set("test_function", function); DynamicReturnValue r = await scriptTree.Evaluate(scope); Assert.Equal(words, r.GetValue <string>()); }
public async Task TestFunctionMultipleArgumentsCallNode() { Function function = new Function(typeof(FunctionTest), "MyTestFunctionWithMultipleArguments"); ScriptTree scriptTree = new ScriptTree(@"test_function(words, 'Cheese is also rather tasty.')"); Scope scope = new Scope(); string words = "pancakes are tasty."; scope.Set("words", words); scope.Set("test_function", function); DynamicReturnValue r = await scriptTree.Evaluate(scope); Assert.Equal($"{words} Cheese is also rather tasty.", r.GetValue <string>()); }
public async Task TestForStatementNodeWithInlineArray() { ScriptTree scriptTree = new ScriptTree(@" test = 0; for (var of [1, 2, 3, 4, 5, 6]) { test += var; } "); Scope scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(21, scope.Get("test")); }
public async Task TestArrayInitialization() { ScriptTree scriptTree = new ScriptTree("array = [0, 9, 200, 30];"); Scope scope = new Scope(); await scriptTree.Evaluate(scope); var array = scope.Get <List <int> >("array"); Assert.Equal(4, array.Count); Assert.Equal(0, array[0]); Assert.Equal(9, array[1]); Assert.Equal(200, array[2]); Assert.Equal(30, array[3]); }
public async Task TestArithmeticNode() { ScriptTree scriptTree = new ScriptTree("foo = 150 + 5.0"); Scope scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(155.0f, scope.Get("foo")); scriptTree = new ScriptTree("foo = 150 + 5"); scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(155, scope.Get("foo")); scriptTree = new ScriptTree("foo = 150 - 5.0"); scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(145f, scope.Get("foo")); scriptTree = new ScriptTree("foo = 150 - 5"); scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(145, scope.Get("foo")); scriptTree = new ScriptTree("foo = 150 * 5.0"); scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(750f, scope.Get("foo")); scriptTree = new ScriptTree("foo = 150 * 5"); scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(750, scope.Get("foo")); scriptTree = new ScriptTree("foo = 150 / 5.0"); scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(30f, scope.Get("foo")); scriptTree = new ScriptTree("foo = 150 / 5"); scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(30, scope.Get("foo")); }
public async Task TestGetEnclosedStatementTokens() { ScriptParser parser = new ScriptParser(); List <ScriptToken> tokens = parser.Tokenize("([1,2] + {1,2,3})"); List <ScriptToken> enclosedTokens = ScriptTree.GetEnclosedTokens(tokens); Assert.Equal(EScriptTokenType.L_BRACKET, enclosedTokens[0].Type); Assert.Equal(EScriptTokenType.R_BRACKET, enclosedTokens[4].Type); Assert.Equal(EScriptTokenType.ADD, enclosedTokens[6].Type); Assert.Equal(EScriptTokenType.L_BRACE, enclosedTokens[8].Type); Assert.Equal(EScriptTokenType.R_BRACE, enclosedTokens[14].Type); }
public static AssignmentNode Parse(AstTreeNode lastNode, ScriptToken scriptToken, List <ScriptToken> tokens) { if (!(lastNode is IScopeMemberNode)) { Console.WriteLine("Invalid assignment syntax."); return(null); } tokens.RemoveAt(0); // consume = List <ScriptToken> assignmentTokens = ScriptTree.GetStatementTokens(tokens, false); return(new AssignmentNode( (IScopeMemberNode)lastNode, ScriptTree.ProcessTokens(assignmentTokens) )); }
public async Task TestRootScopeNode() { ScriptTree scriptTree = new ScriptTree("foo"); Scope scope = new Scope(); scope.Set("foo", 5); DynamicReturnValue value = await scriptTree.Evaluate(scope); Assert.Equal(5, value.GetValue <int>()); scriptTree = new ScriptTree("foo"); scope.Set("foo", false); value = await scriptTree.Evaluate(scope); Assert.Equal(false, value.GetValue <bool>()); }
public static ArithmeticAssignmentNode Parse(AstTreeNode lastNode, ScriptToken scriptToken, List <ScriptToken> tokens) { if (lastNode == null || !(lastNode is IScopeMemberNode)) { Console.WriteLine("Invalid assignment syntax."); return(null); } tokens.RemoveAt(0); // consume += List <ScriptToken> assignmentTokens = ScriptTree.GetEnclosedTokens(tokens); return(new ArithmeticAssignmentNode( (IScopeMemberNode)Convert.ChangeType(lastNode, lastNode is RootScopeMemberNode ? typeof(RootScopeMemberNode) : typeof(ScopeMemberNode)), ScriptTree.ProcessTokens(assignmentTokens), scriptToken.Type )); }
private void InitMetaData() { var rootText = Resources.Load <TextAsset>("meta/root" + SceneManager.GetActiveScene().name); var docStream = new StringReader(rootText.text); RootGraph root = RootGraph.Construct(docStream); foreach (var clip in root.Clips) { //load clip var clipText = Resources.Load <TextAsset>("meta/" + clip.Path); var clipDocStream = new StringReader(clipText.text); ClipTree clipGraph = ClipTree.Construct(clipDocStream); var ser = new YamlDotNet.Serialization.Serializer(); clipGraphs.Add(clip.Name, clipGraph); var midiFile = Resources.Load <TextAsset>("clips/" + clipGraph.Midi); Debug.Log(clipGraph.Midi); AudioSource audioSource = AddAudioSourceToScene(clipGraph.Audio); //parse midi Midi midi = new MidiParser().Parse(midiFile.bytes); //debug Debug.Log(midi.Tracks[2].Bpm); foreach (var msg in midi.Tracks[2].Messages) { Debug.Log(msg); } Debug.Log(ser.Serialize(clipGraph)); clips.Add(clip.Name, new Assets.Core.Clip(audioSource, midi)); } foreach (var graph in root.Scriptgraphs) { var graphText = Resources.Load <TextAsset>("meta/" + graph.Path); Debug.Log(graphText); var graphDocStream = new StringReader(graphText.text); ScriptTree scriptGraph = ScriptTree.Construct(graphDocStream); var ser = new YamlDotNet.Serialization.Serializer(); scriptGraphs.Add(graph.Name, scriptGraph); Debug.Log("Constructed a script graph : " + graph.Name); Debug.Log(ser.Serialize(scriptGraph)); } }
public async Task TestIfStatementNode() { ScriptTree scriptTree = new ScriptTree(@" if (false) { result = false; } else if (1 > 2) { result = 'impossibru!'; } else if (2 > 1) { result = true; } else { result = 'not right'; } "); Scope scope = new Scope(); await scriptTree.Evaluate(scope); Console.WriteLine($"Result it Type {scope.GetType("result")}"); Assert.Equal(true, scope.Get("result")); }
public static ConditionalNode ParseConditional(List <ScriptToken> tokens) { List <EScriptTokenType> ifTokens = new List <EScriptTokenType>(); ifTokens.Add(EScriptTokenType.IF); ifTokens.Add(EScriptTokenType.ELSE_IF); if (!ifTokens.Contains(tokens[0].Type)) { Console.WriteLine("Invalid Syntax"); return(null); } tokens.RemoveAt(0); // consume if and else if return(new ConditionalNode( ScriptTree.ProcessTokens(ScriptTree.GetBlockTokens(tokens, EBlockType.PAREN, false)[0]), ScriptTree.ProcessTokens(ScriptTree.GetBlockTokens(tokens, EBlockType.BRACE, false)[0]) )); }
// Use only in editor mode public static void SaveScripts(string name, ScriptTree tree) { var rootText = Resources.Load <TextAsset>("meta/root"); var docStream = new StringReader(rootText.text); RootGraph root = RootGraph.Construct(docStream); var scriptGraph = root.Scriptgraphs.Find(sg => sg.Name == name); string fullPath = path + scriptGraph.Path + ".yaml"; var sr = new YamlDotNet.Serialization.SerializerBuilder() .WithNamingConvention(new YamlDotNet.Serialization.NamingConventions.CamelCaseNamingConvention()) .Build(); using (FileStream fs = new FileStream(fullPath, FileMode.Create)) { using (StreamWriter writer = new StreamWriter(fs)) { writer.Write(sr.Serialize(tree)); } } UnityEditor.AssetDatabase.Refresh(); }
public static ArrayNode Parse(AstTreeNode lastNode, ScriptToken scriptToken, List <ScriptToken> tokens) { List <ScriptToken> values = ScriptTree.GetEnclosedTokens(tokens); List <AstTreeNode> nodes = new List <AstTreeNode>(); for (int i = 0; i < values.Count; i++) { ScriptToken arg = values[i]; if (i % 2 == 0 && arg.Type != EScriptTokenType.COMMA) { nodes.Add(new IntegerLiteralNode(arg.Value)); } else { Console.WriteLine("Syntax Error."); } } return(new ArrayNode(nodes)); }
public async Task TestArithmeticOrderOfOperations() { ScriptTree scriptTree = new ScriptTree("foo = 1 + (5 + 1) * 2"); Scope scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(12.0f, scope.Get("foo")); scriptTree = new ScriptTree("foo = 150 + 5.0 / 5 + 1"); scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(152.0f, scope.Get("foo")); scriptTree = new ScriptTree("foo = 5 * 5 + 2 * 5"); scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(35, scope.Get("foo")); }
private static ScriptTree innerBuildScriptTree(TextReader tr) { try { ScriptTree tree = new ScriptTree(); ScriptSection section = readScriptSection(tr); while (section != null) { tree.Add(section); section = readScriptSection(tr); } return tree; } finally { if (tr != null) tr.Close(); } }
public SwordObject(byte[] data, int offset) { Data = data; Offset = offset; tree = new ScriptTree(data, offset + 108); bookmark = new ScriptTree(data, offset + 152); talk_table = new TalkOffset[6]; for (int i = 0; i < 6; i++) { talk_table[i] = new TalkOffset(data, offset + 232 + i * 8); } event_list = new OEventSlot[O_TOTAL_EVENTS]; for (int i = 0; i < O_TOTAL_EVENTS; i++) { event_list[i] = new OEventSlot(data, offset + 280 + i * 8); } route = new WalkData[O_WALKANIM_SIZE]; for (int i = 0; i < O_WALKANIM_SIZE; i++) { route[i] = new WalkData(data, offset + 340 + i * WalkData.Size); } }
public async Task TestScopeAssignmentNode() { ScriptTree scriptTree = new ScriptTree("foo = 150"); Scope scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(150, scope.Get("foo")); scriptTree = new ScriptTree("bar = 1.5;bar;"); DynamicReturnValue value = await scriptTree.Evaluate(scope); Assert.Equal(1.5f, value.GetValue <float>()); scope = new Scope(); scriptTree = new ScriptTree("foo.bar = 7.5;foo.bar;"); Scope innerScope = new Scope(); scope.Set("foo", innerScope); value = await scriptTree.Evaluate(scope); Assert.Equal(7.5f, value.GetValue <float>()); }
private static object executeScriptTree(ScriptTree tree) { List<ScriptNode> graph = tree.CreateScriptModel(); foreach (ScriptNode node in graph) { node.PreProcess(); } foreach (ScriptNode node in graph) { node.Process(); } ExecutionContext context = new ExecutionContext(); foreach (ScriptNode node in graph) { node.Execute(context); } return context.CurrentBlockWeb; }
protected void Save() { ScriptTree tree = ScriptTree.Construct(this); Meta.SaveScripts(currScriptName, tree); }
public static ComparisonNode Parse(AstTreeNode lastNode, ScriptToken scriptToken, List <ScriptToken> tokens) { tokens.RemoveAt(0); // Remove comparison operator return(new ComparisonNode(lastNode, ScriptTree.ProcessTokens(ScriptTree.GetEnclosedTokens(tokens)), scriptToken.Type)); }
private void processIncludeSection() { int temp = 0; string includeFile = Helper.ExtractToken(Contents, "<", ">", ref temp); Children = ScriptEngine.BuildScriptTreeFromFile(includeFile); }
private void OnSelectorChange(int selected) { Load(scriptTrees.ToList()[selected].Value, scriptTrees.ToList()[selected].Key); currScriptName = scriptTrees.ToList()[selected].Key; currTree = scriptTrees.ToList()[selected].Value; }