static void TestRewriteRefine() { var spec = new Racr.Specification(); spec.AstRule("S->A"); spec.AstRule("A->a"); spec.AstRule("Aa:A->b-c"); spec.CompileAstSpecifications("S"); spec.CompileAgSpecifications(); var ast = new Racr.AstNode(spec, "S", new Racr.AstNode(spec, "A", 1)); var A = ast.Child("A"); Console.WriteLine("{0}", A.NumChildren() == 1); Console.WriteLine("{0}", A.NodeType() == "A"); A.RewriteRefine("Aa", 2, 3); Console.WriteLine("{0}", A.NumChildren() == 3); Console.WriteLine("{0}", A.NodeType() == "Aa"); foreach (var c in A.Children()) { Console.WriteLine(c); } }
// Widgets: [AgRule("Widget", "Form")] static Control FormWidget(Ast n) { var form = new System.Windows.Forms.Form(); form.Text = "Questionnaire"; form.Closed += (object sender, EventArgs e) => { Application.Exit(); }; var file = new MenuItem("&File"); var save = new MenuItem("&Save"); var quit = new MenuItem("&Quit"); save.Click += (object sender, EventArgs e) => { SaveQuestionnaire(n); }; quit.Click += (object sender, EventArgs e) => { Application.Exit(); }; file.MenuItems.Add(save); file.MenuItems.Add(quit); form.Menu = new MainMenu(); form.Menu.MenuItems.Add(file); var contentBox = new FlowLayoutPanel(); contentBox.AutoSize = true; contentBox.AutoScroll = true; contentBox.Dock = DockStyle.Fill; contentBox.FlowDirection = FlowDirection.TopDown; contentBox.WrapContents = false; form.Controls.Add(contentBox); form.Show(); return(contentBox); }
private static bool OpenQuestionnaire() { var fd = new OpenFileDialog(); Ast next = null; while (fd.ShowDialog() == DialogResult.OK) { try { var parser = new Parser(QL, File.ReadAllText(fd.FileName)); next = parser.ParseAst(); break; } catch { } } fd.Dispose(); if (next != null) { UpdateQuestions(next); next.Render(); return(true); } else { return(false); } }
[Test] public void TestRewriteSubtree() { var ast = new Racr.AstNode(spec, "S", new Racr.AstNode(spec, "A", 42, new Racr.AstNode(spec, "B"))); var A = ast.Child("A"); var B1 = A.Child("B1"); Assert.IsTrue(A.HasParent()); Assert.AreEqual(A.NumChildren(), 2); Assert.AreEqual(A.NodeType(), "A"); Assert.AreEqual(A.Child <int>(1), 42); Assert.AreEqual(A.Child(2), B1); A.RewriteSubtree(new Racr.AstNode(spec, "Aa", 1, new Racr.AstNode(spec, "B"), new Racr.AstNode(spec, "B"), 42)); Assert.IsFalse(A.HasParent()); Assert.AreEqual(A.NumChildren(), 2); Assert.AreEqual(A.NodeType(), "A"); Assert.AreEqual(A.Child <int>(1), 42); Assert.AreEqual(A.Child(2), B1); A = ast.Child("A"); Assert.IsTrue(A.HasParent()); Assert.AreEqual(A.NumChildren(), 4); Assert.AreEqual(A.NodeType(), "Aa"); Assert.AreEqual(A.Child <int>(1), 1); Assert.AreNotEqual(A.Child(2), B1); Assert.AreEqual(A.Child(2).NodeType(), "B"); Assert.AreEqual(A.Child(3).NodeType(), "B"); Assert.AreEqual(A.Child <int>(4), 42); }
static ValueTypes ComputationType(Racr.AstNode n) { var op = n.GetOperator(); var inType = ValueTypes.ErrorType; var outType = ValueTypes.ErrorType; var operands = n.GetOperands().Children(); if (op == "&&" || op == "//" || op == "not") { inType = outType = ValueTypes.Boolean; } else if (op == "=" || op == "<" || op == ">" || op == "<=" || op == ">=" || op == "!=") { inType = ValueTypes.Number; outType = ValueTypes.Boolean; } else if (op == "string=?" || op == "string<?" || op == "string>?" || op == "string<=?" || op == "string>=?") { inType = ValueTypes.String; outType = ValueTypes.Boolean; } else if (op == "+" || op == "-" || op == "*" || op == "/") { inType = outType = ValueTypes.Number; } else if (op == "string-append") { inType = outType = ValueTypes.String; } if (operands.Any(x => ((Racr.AstNode)x).Type() != inType)) { return(ValueTypes.ErrorType); } return(outType); }
[Test] public void TestRewriteAbstract() { var ast = new Racr.AstNode(spec, "S", new Racr.AstNode(spec, "Aa", 1, new Racr.AstNode(spec, "B"), new Racr.AstNode(spec, "B"), 4)); var A = ast.Child("A"); var B1 = A.Child("B1"); var B2 = A.Child("B2"); Assert.IsTrue(A.HasParent()); Assert.AreEqual(A.NumChildren(), 4); Assert.AreEqual(A.NodeType(), "Aa"); Assert.AreEqual(A.Child <int>(1), 1); Assert.AreEqual(A.Child(2), B1); Assert.AreEqual(A.Child(3), B2); Assert.AreEqual(A.Child <int>(4), 4); var c = A.RewriteAbstract("A"); Assert.AreEqual(c.Length, 2); Assert.AreEqual(c[0], B2); Assert.AreEqual(c[1], 4); Assert.IsTrue(A.HasParent()); Assert.AreEqual(A.NumChildren(), 2); Assert.AreEqual(A.NodeType(), "A"); Assert.AreEqual(A.Child <int>(1), 1); Assert.AreEqual(A.Child(2), B1); }
[AgRule("Widget", "OrdinaryQuestion")] static Control OrdinaryQuestionWidget(Ast n) { QDialog dialog = n.Dialog(); ((Control)dialog).Enabled = true; EventHandler h = (object o, EventArgs a) => { if (!((Control)o).ContainsFocus) { return; // TODO: For which reason is this check needed? } n.SetValue(dialog.GetValue()); n.Root().Render(); }; dialog.AddEventHandler(h); var contentBox = new FlowLayoutPanel(); contentBox.AutoSize = true; contentBox.WrapContents = false; contentBox.FlowDirection = FlowDirection.LeftToRight; var label = new Label(); label.Text = n.GetLabel(); label.AutoSize = true; label.Anchor = AnchorStyles.Left; contentBox.Controls.Add(label); contentBox.Controls.Add((Control)dialog); n.Parent().Widget().Controls.Add(contentBox); return(contentBox); }
static void TestRewriteAbstract() { var spec = new Racr.Specification(); spec.AstRule("S->A"); spec.AstRule("A->a"); spec.AstRule("Aa:A->b-c"); spec.CompileAstSpecifications("S"); spec.CompileAgSpecifications(); var ast = new Racr.AstNode(spec, "S", new Racr.AstNode(spec, "Aa", 1, 2, 3)); var A = ast.Child("A"); Console.WriteLine(A.NumChildren() == 3); Console.WriteLine(A.NodeType() == "Aa"); var c = A.RewriteAbstract("A"); foreach (var x in c) { Console.WriteLine(x); } Console.WriteLine(A.NumChildren() == 1); Console.WriteLine(A.NodeType() == "A"); }
static Racr.AstNode QuestionLLookup(Racr.AstNode n, string name) { if (n.GetName() == name) { return(n); } return(null); }
[AgRule("LLookup", "Question")] static Ast QuestionLLookup(Ast n, string name) { if (n.GetName() == name) { return(n); } return(null); }
private static Racr.AstList MakeDefinitions(int constants) { Racr.AstNode[] defs = new Racr.AstNode[constants]; for (var i = 0; i < constants; i++) { defs[i] = CL.CreateAst("Definition", "d" + i, i / 10.0); } return(CL.CreateAstList(defs)); }
[AgRule("IsLValid", "Question")] static bool QuestionIsLValid(Ast n) { if (n.Type() == Types.ErrorType) { return(false); } var prev = n.GLookup(n.GetName()); return(prev.IsErrorQuestion() || n.Type() == prev.Type()); }
static bool QuestionIsLValid(Racr.AstNode n) { if (n.Type() == ValueTypes.ErrorType) { return(false); } var prev = n.GLookup(n.GetName()); return(prev.IsErrorQuestion() || n.Type() == prev.Type()); }
static object ComputationValue(Racr.AstNode n) { var op = n.GetOperator(); var operands = n.GetOperands().Children(); var args = operands.Select(p => ((Racr.AstNode)p).Value()).ToArray(); var func = opTable[op]; try { return(func(args)); } catch { return(null); } }
static Racr.AstNode ElementFindActive(Racr.AstNode n, string name) { var current = n.GLookup(name); while (!current.IsActive()) { current = current.GLookup(name); } return(current); }
[AgRule("Value", "Use")] static object UseValue(Ast n) { if (n.Type() == Types.ErrorType) { return(ErrorValue); } var active = n.FindActive(n.GetName()); return(active.Type() == n.Type() ? active.Value() : ErrorValue); }
[AgRule("Value", "OrdinaryQuestion")] static object OrdinaryQuestionValue(Ast n) { if (n.Type() == Types.ErrorType) { return(ErrorValue); } var acceptor = TypeToAcceptor(n.Type()); // acceptor is never null return(acceptor(n.GetValue()) ? n.GetValue() : ErrorValue); }
// Interpretation: [AgRule("FindActive", "Element")] static Ast ElementFindActive(Ast n, string name) { var current = n.GLookup(name); while (!current.IsActive()) { current = current.GLookup(name); } return(current); }
private static void SaveQuestionnaire(Ast current) { var fd = new SaveFileDialog(); if (fd.ShowDialog() == DialogResult.OK && fd.FileName != "") { File.WriteAllText(fd.FileName, current.SExpr()); } fd.Dispose(); }
static bool GroupIsActive(Racr.AstNode n) { var v = n.GetExpression().Value(); if (v == null) { return(false); } return((bool)v); }
private static void AddNode(Racr.AstNode n) { Racr.AstNode c = FlipCoin() ? n.GetOp1() : n.GetOp2(); if (c.IsBudNode()) { c.RewriteSubtree(NewNode()); } else { AddNode(c); } }
// Name Analysis: static Ast findL(string name, Ast l, int i) { for (int j = 1; j <= i; j++) { var r = l.Child(j).LLookup(name); if (r != null) { return(r); } } return(null); }
[AgRule("Widget", "Group")] static Control GroupWidget(Ast n) { var contentBox = new FlowLayoutPanel(); contentBox.AutoSize = true; contentBox.BorderStyle = BorderStyle.Fixed3D; contentBox.Dock = DockStyle.Fill; contentBox.FlowDirection = FlowDirection.TopDown; contentBox.WrapContents = false; n.Parent().Widget().Controls.Add(contentBox); return(contentBox); }
static Control GroupWidget(Racr.AstNode n) { var panel = new FlowLayoutPanel(); panel.AutoSize = true; panel.BorderStyle = BorderStyle.Fixed3D; panel.Dock = DockStyle.Fill; panel.FlowDirection = FlowDirection.TopDown; panel.WrapContents = false; n.Parent().Widget().Controls.Add(panel); return(panel); }
private void test(int nodes, int constants, int rewrites) { CalculatorProfiler.InitialiseRandom(); Racr.AstNode c1 = CalculatorProfiler.ProfileRacrNet(nodes, constants, rewrites, false); CalculatorProfiler.InitialiseRandom(); Object c2 = CalculatorProfiler.ProfileRacrScheme(nodes, constants, rewrites, false); Assert.AreEqual( c1.PrintAst(), @"(call-with-string-output-port (lambda (port) (print-ast {0} (list) port)))" .Eval(c2)); Assert.AreEqual(c1.Eval(), (double)"(=eval {0})".Eval(c2)); }
static Control FormWidget(Racr.AstNode n) { var form = new System.Windows.Forms.Form(); form.Text = "Questionnaire"; var file = new MenuItem("&File"); var open = new MenuItem("&Open"); var save = new MenuItem("&Save"); var quit = new MenuItem("&Quit"); open.Click += (object sender, EventArgs e) => { var ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { var parser = new Parser(QL.Ql, File.ReadAllText(ofd.FileName)); var ast = parser.ParseAst(); ast.Render(); Questionnaire.UpdateQuestions(ast); form.Closed -= FormClosed; form.Close(); } }; save.Click += (object sender, EventArgs e) => { var sfd = new SaveFileDialog(); if (sfd.ShowDialog() == DialogResult.OK && sfd.FileName != "") { File.WriteAllText(sfd.FileName, n.SExpr()); } }; quit.Click += (object sender, EventArgs e) => form.Close(); form.Closed += FormClosed; file.MenuItems.Add(open); file.MenuItems.Add(save); file.MenuItems.Add(quit); form.Menu = new MainMenu(); form.Menu.MenuItems.Add(file); var panel = new FlowLayoutPanel(); panel.AutoSize = true; panel.AutoScroll = true; panel.Dock = DockStyle.Fill; panel.FlowDirection = FlowDirection.TopDown; panel.WrapContents = false; form.Controls.Add(panel); form.Show(); return(panel); }
static Control ComputedQuestionWidget(Racr.AstNode n) { Widget w; if (n.Type() == ValueTypes.Boolean) { w = new CheckWidget(n.GetLabel(), false); } else { w = new TextWidget(n.GetLabel(), false); } n.Parent().Widget().Controls.Add(w); return(w); }
private static void SaveQuestionnaire(Ast current) { FileChooserDialog fc = new FileChooserDialog( "Select where to save questionnaire", null, FileChooserAction.Save, "Cancel", ResponseType.Cancel, "Save questionnaire", ResponseType.Accept); fc.SetPosition(WindowPosition.Center); if (fc.Run() == (int)ResponseType.Accept) { File.WriteAllText(fc.Filename, current.SExpr()); } fc.Destroy(); }
// Question dialogs: [AgRule("Dialog", "Question")] static QDialog QuestionDialogType(Ast n) { switch (n.Type()) { case Types.Boolean: return(new QCheckBoxDialog()); case Types.Number: case Types.String: case Types.ErrorType: return(new QTextFieldDialog(n)); default: throw new NotImplementedException( "No dialog for [" + n.Type() + "] implemented."); } }
[AgRule("Render", "Group")] static bool GroupRender(Ast n) { foreach (Ast c in n.GetBody().Children()) { if (c.IsShown()) { c.Render(); c.Widget().Show(); } else { c.Widget().Hide(); } } n.Widget().Show(); return(true); }
static void TestRewriteRefine() { var spec = new Racr.Specification(); spec.AstRule("S->A"); spec.AstRule("A->a"); spec.AstRule("Aa:A->b-c"); spec.CompileAstSpecifications("S"); spec.CompileAgSpecifications(); var ast = new Racr.AstNode(spec, "S", new Racr.AstNode(spec, "A", 1)); var A = ast.Child("A"); Console.WriteLine("{0}", A.NumChildren() == 1); Console.WriteLine("{0}", A.NodeType() == "A"); A.RewriteRefine("Aa", 2, 3); Console.WriteLine("{0}", A.NumChildren() == 3); Console.WriteLine("{0}", A.NodeType() == "Aa"); foreach (var c in A.Children()) Console.WriteLine(c); }
static void TestRewriteAbstract() { var spec = new Racr.Specification(); spec.AstRule("S->A"); spec.AstRule("A->a"); spec.AstRule("Aa:A->b-c"); spec.CompileAstSpecifications("S"); spec.CompileAgSpecifications(); var ast = new Racr.AstNode(spec, "S", new Racr.AstNode(spec, "Aa", 1, 2, 3)); var A = ast.Child("A"); Console.WriteLine(A.NumChildren() == 3); Console.WriteLine(A.NodeType() == "Aa"); var c = A.RewriteAbstract("A"); foreach (var x in c) Console.WriteLine(x); Console.WriteLine(A.NumChildren() == 1); Console.WriteLine(A.NodeType() == "A"); }
static void TestRewriteSubtree() { var spec = new Racr.Specification(); spec.AstRule("S->A"); spec.AstRule("A->a"); spec.AstRule("Aa:A->b-c"); spec.CompileAstSpecifications("S"); spec.CompileAgSpecifications(); var ast = new Racr.AstNode(spec, "S", new Racr.AstNode(spec, "A", 42)); var A = ast.Child("A"); Console.WriteLine(A.HasParent()); Console.WriteLine(ast.Child("A").NodeType()); A.RewriteSubtree(new Racr.AstNode(spec, "Aa", 1, 2, 3)); Console.WriteLine(A.HasParent()); Console.WriteLine(ast.Child("A").NodeType()); }
static void TestFoo() { var spec = new MySpec(); var root = new Racr.AstNode(spec, "A", new Racr.AstList( new B(spec, "abc"), new B(spec, "123"), new B(spec, "xyz")), new Racr.AstNode(spec, "C"), "hiya"); root.ForEachChild((i, o) => { var node = o as Racr.AstNode; if (node != null) Console.WriteLine("{0}: {1}", i, node.NodeType()); else Console.WriteLine("{0}: {1}", i, o); }, new Racr.Range(2)); Console.WriteLine("---"); Console.WriteLine("M: {0}", root.M()); Console.WriteLine("N: {0}", root.N()); Console.WriteLine("a: {0}", root.AttValue("a")); Console.WriteLine("b: {0}", root.AttValue("b", 3)); Console.WriteLine("kids: {0}", root.AttValue("kids")); var index = root.GetList().Child(1).AttValue<int>("bar", 3); Console.WriteLine("index: {0}", index); Console.WriteLine("---"); var c = root.FindChild((i, o) => { return i == 2; }) as Racr.AstNode; Console.WriteLine("{0}", c.NodeType()); Console.WriteLine("---"); Console.WriteLine(root); Console.WriteLine("NodeType: {0}", root.NodeType()); Console.WriteLine("IsNode: {0}", root.IsNode()); Console.WriteLine("HasParent: {0}", root.HasParent()); Console.WriteLine("NumChildren: {0}", root.NumChildren()); Console.WriteLine("HasChild 'B: {0}", root.HasChild("B")); Console.WriteLine("HasChild 'List: {0}", root.HasChild("List")); Console.WriteLine(""); var child = root.GetList(); Console.WriteLine(child); Console.WriteLine("Child 1 -> t: {0}", child.Child(1).GetT()); Console.WriteLine("IsNode: {0}", child.IsNode()); Console.WriteLine("HasParent: {0}", child.HasParent()); Console.WriteLine("ChildIndex: {0}", child.ChildIndex()); Console.WriteLine("NumChildren: {0}", child.NumChildren()); }