public static Command Split(int id, LispNode argument) { return(new Command( CommandType.Split, id, Common.Unflatten(argument))); }
private Command(CommandType type, int shipID, LispNode argument1 = null, LispNode argument2 = null) { ShipID = shipID; Type = type; Argument1 = argument1; Argument2 = argument2; }
public void TestFlatten() { var test = new LispNode() { new LispNode("0"), new LispNode("12345678"), new LispNode() { new LispNode("1"), new LispNode("2"), new LispNode() { new LispNode("3"), new LispNode("4") } } }; var notFlat = Common.Unflatten(test); var flat = Common.Flatten(notFlat); Console.WriteLine(test); Console.WriteLine(notFlat); Console.WriteLine(flat); Assert.AreEqual(test.ToString(), flat.ToString()); }
public async Task TestDefaultStartingParams() { var startingParams = new LispNode() { new LispNode(1), new LispNode(0), new LispNode(0), new LispNode(1), }; var create = Common.Flatten(await Common.Send(Common.Unflatten(Lisp.Parse("(1 6)")[0]))); var playerKey = create[1][0][1]; var tasks = new List <Task <LispNode> >(); tasks.Add(Common.Send(Common.Unflatten(new LispNode() { new LispNode(2), create[1][0][1], new LispNode() }))); //tasks.Add(Common.Send(Common.Unflatten(new LispNode() { new LispNode(2), playerKey, new LispNode() }))); Task.WaitAll(tasks.ToArray()); var join = Common.Flatten(tasks[0].Result); var start = Common.Flatten(await Common.Send(Common.Unflatten(new LispNode() { new LispNode(3), playerKey, startingParams }))); }
public static LispNode Interact( LispNode protocol, LispNode state, string x, string y, Dictionary <string, LispNode> symbols) { var vector = new LispNode( new LispNode( new LispNode("cons"), new LispNode(x)), new LispNode(y)); while (true) { var protocolResultRoot = new LispNode(); protocolResultRoot.Children.Add(new LispNode()); protocolResultRoot.Children.First().Children.Add(protocol); protocolResultRoot.Children.First().Children.Add(state); protocolResultRoot.Children.Add(vector); var result = Evaluate(protocolResultRoot, symbols); if (result.Children.First().Children.Last().Text == "0") { return(result); } state = result.Children.Last().Children.First().Children.Last(); var data = result.Children.Last().Children.Last().Children.First().Children.Last(); vector = Common.Send(data).Result; } }
public static string TreeToJson(LispNode node, int indent = 0) { var sb = new StringBuilder(); TreeToJsonInternal(sb, node, 0, indent); return(sb.ToString()); }
static LispNode EvalCons(LispNode a, LispNode b, Dictionary <string, LispNode> symbols) { var res = new LispNode(new LispNode(cons, Evaluate(a, symbols)), Evaluate(b, symbols)); res.Evaluated = res; return(res); }
static BigInteger AsNum(LispNode n) { if (n.Type == LispNodeType.Token) { return(BigInteger.Parse(n.Text)); } throw new Exception("not a number"); }
static void TreeToJsonInternal( StringBuilder sb, LispNode node, int depth, int indent) { if (node.Type == LispNodeType.Token) { sb.Append('"'); sb.Append(node.Text); sb.Append('"'); } else { if (indent > 0) { sb.Append(Environment.NewLine); } foreach (var i in Enumerable.Range(0, indent * depth)) { sb.Append(' '); } sb.Append('['); var firstChild = true; foreach (var child in node.Children) { if (!firstChild) { sb.Append(", "); } TreeToJsonInternal(sb, child, depth + 1, indent); firstChild = false; } sb.Append(']'); } }
public static LispNode Evaluate( LispNode expr, Dictionary <string, LispNode> symbols) { if (expr.Evaluated != null) { return(expr.Evaluated); } LispNode initialExpr = expr; while (true) { var result = TryEval(expr, symbols); if (result == expr) { initialExpr.Evaluated = result; return(result); } expr = result; } }
public GameState(LispNode node) { Tick = int.Parse(node[0].Text); Ships = node[2].Select(i => new Ship(i[0])).ToList(); }
static LispNode TryEval( LispNode expr, Dictionary <string, LispNode> symbols) { if (expr.Evaluated != null) { return(expr.Evaluated); } if (expr.Type == LispNodeType.Token && symbols.ContainsKey(expr.Text)) { return(symbols[expr.Text]); } if (expr.Type == LispNodeType.Open) { var fun = Evaluate(expr.Children.First(), symbols); var x = expr.Children.Last(); if (fun.Type == LispNodeType.Token) { if (fun.Text == "neg") { return(new LispNode(-AsNum(Evaluate(x, symbols)))); } if (fun.Text == "i") { return(x); } if (fun.Text == "nil") { return(t); } if (fun.Text == "isnil") { return(new LispNode(x, new LispNode(t, new LispNode(t, f)))); } if (fun.Text == "car") { return(new LispNode(x, t)); } if (fun.Text == "cdr") { return(new LispNode(x, f)); } if (fun.Text == "inc") { return(new LispNode(AsNum(Evaluate(x, symbols)) + 1)); } if (fun.Text == "dec") { return(new LispNode(AsNum(Evaluate(x, symbols)) - 1)); } } if (fun.Type == LispNodeType.Open) { var fun2 = Evaluate(fun.Children.First(), symbols); var y = fun.Children.Last(); if (fun2.Type == LispNodeType.Token) { if (fun2.Text == "t") { return(y); } if (fun2.Text == "f") { return(x); } if (fun2.Text == "add") { return(new LispNode(AsNum(Evaluate(x, symbols)) + AsNum(Evaluate(y, symbols)))); } if (fun2.Text == "mul") { return(new LispNode(AsNum(Evaluate(x, symbols)) * AsNum(Evaluate(y, symbols)))); } if (fun2.Text == "div") { return(new LispNode(AsNum(Evaluate(y, symbols)) / AsNum(Evaluate(x, symbols)))); } if (fun2.Text == "lt") { return(AsNum(Evaluate(y, symbols)) < AsNum(Evaluate(x, symbols)) ? t : f); } if (fun2.Text == "eq") { return(AsNum(Evaluate(x, symbols)) == AsNum(Evaluate(y, symbols)) ? t : f); } if (fun2.Text == "cons") { return(EvalCons(y, x, symbols)); } } if (fun2.Type == LispNodeType.Open) { var fun3 = Evaluate(fun2.Children.First(), symbols); var z = fun2.Children.Last(); if (fun3.Type == LispNodeType.Token) { if (fun3.Text == "s") { return(new LispNode(new LispNode(z, x), new LispNode(y, x))); } if (fun3.Text == "c") { return(new LispNode(new LispNode(z, x), y)); } if (fun3.Text == "b") { return(new LispNode(z, new LispNode(y, x))); } if (fun3.Text == "cons") { return(new LispNode(new LispNode(x, z), y)); } } } } } return(expr); }