void Compile() { ClearLog(); richTextBoxOutput.Clear(); FinalExpr = null; FinalBlock = null; FinalExpressable = null; var enc = new UTF8Encoding(false); using (var sr = new StreamReader(new MemoryStream(enc.GetBytes(richTextBoxInput.Text)))) using (var sb = new SyntaxBuilder(Cfe, sr, SelectedScope)) { while (sb.Step()) { ; } foreach (var msg in sb.Warns) { Log(msg.Msg, Color.DarkOrange, msg.Location); } foreach (var msg in sb.Errors) { Log(msg.Msg, Color.LightPink, msg.Location); } if (!sb.Success) { Log("Compiling failed!", Color.Pink); return; } else { Log("Compile Successful!", Color.LightSeaGreen); if (SelectedScope == ScopeMode.Expression) { FinalExpr = sb.Syn.CreateFinalExpression(); } else if (SelectedScope == ScopeMode.Function) { FinalBlock = sb.Syn.CreateFinalFunction().RootStatement; } FinalExpressable = sb.GetExpressable(); richTextBoxOutput.Text = FinalExpressable.Express(0); } } }
static void Main(string[] args) { Character testr = new Character { Name = "Strange\",sp\"spspsp,,,LastName", Level = 5, Attributes = new List <CharacterAttribute> { new Strength(1) }, Balance = new Money { Gold = 10, Silver = 25, Copper = 67 } }; List <string> st = new List <string>(); string testPEMDAS = "5 + 3 - ((8/9)+1)"; string testmethodCreation = "Boolean LessThanForty(string xz){return xz.Length < 40;}"; string exampleBlock = "Character boi = new Character(\"maboi\",6);\r\nreturn boi.Level > 5;"; string ComplexBlocks = "int main(){\r\nvoid SetLevel(Character subject,int iLvl){ subject.Level = iLvl; }\r\n}"; Expressable.ExposeTypes = new List <Type> { typeof(Character), typeof(Money), typeof(CharacterAttribute), typeof(Strength), typeof(string), typeof(int), typeof(long), typeof(decimal), typeof(DateTime), typeof(bool) }; Expressable.ExposeMembers = new List <System.Reflection.MemberInfo> { typeof(string).GetProperty("Length") }; BlockExpression blockExpression = (BlockExpression)Expressable.ParseExpression(exampleBlock); Func <bool> compExpress = Expression.Lambda <Func <bool> >(blockExpression).Compile(); var result = compExpress(); }
public void SetUpNodes(Expressable block) { treeView1.Nodes.Clear(); if (block == null) { return; } Text = block is CodeDOMModule ? "Module Code Graph" : (block is FunctionDefinition ? "Statement Code Graph" : "Expression Code Graph"); var nodestack = new Stack <NodeProg>(); NodeProg current; if (block is Expression) { nodestack.Push(new NodeProg { It = (new Expression[] { block as Expression }).AsEnumerable().GetEnumerator(), Parent = treeView1.Nodes }); } else if (block is CodeDOMModule || block is FunctionDefinition) { IEnumerable <FunctionDefinition> target = block is CodeDOMModule ? (IEnumerable <FunctionDefinition>)(block as CodeDOMModule).GetDefinitions() : new FunctionDefinition[] { block as FunctionDefinition }; foreach (var fd in target) { var tn = new TreeNode() { Text = fd.Name, Tag = fd }; treeView1.Nodes.Add(tn); nodestack.Push(new NodeProg { It = fd.RootStatement.GetChildren().GetEnumerator(), Parent = tn.Nodes }); } List <TreeNode> readytosort = new List <TreeNode>(); foreach (TreeNode tn in treeView1.Nodes) { readytosort.Add(tn); } readytosort.Sort(new Comparison <TreeNode>((t1, t2) => t1.Text.CompareTo(t2.Text))); treeView1.Nodes.Clear(); treeView1.Nodes.AddRange(readytosort.ToArray()); } while (nodestack.Count > 0) { current = nodestack.Pop(); while (current.It.MoveNext()) { var c = current.It.Current; if (c == null) { continue; } var tn = CreateNode(c); current.Parent.Add(tn); if (c == null || c.GetChildren() == null) { continue; } nodestack.Push(current); current = new NodeProg() { Parent = tn.Nodes, It = c.GetChildren().GetEnumerator() }; } } }