コード例 #1
0
ファイル: ZingaUtil.cs プロジェクト: Timwi/Zinga
        public static SucoEnvironment ConvertVariableValues(JsonDict variablesJson, JsonDict valuesJson)
        {
            var list = new List <(string name, object value)>();

            foreach (var(varName, varType) in variablesJson)
            {
                list.Add((varName, convertVariableValue(SucoType.Parse(varType.GetString()), valuesJson[varName])));
            }
            return(new SucoEnvironment(list));
        }
コード例 #2
0
ファイル: ZingaUtil.cs プロジェクト: Timwi/Zinga
        private static object convertList(JsonList list, SucoType elementType)
        {
            var result = elementType.CreateArray(list.Count);

            for (var i = 0; i < list.Count; i++)
            {
                result.SetValue(convertVariableValue(elementType, list[i]), i);
            }
            return(result);
        }
コード例 #3
0
 public SucoBinaryOperatorExpression(int startIndex, int endIndex, SucoExpression left, SucoExpression right, BinaryOperator op, SucoType type = null)
     : base(startIndex, endIndex, type)
 {
     Left     = left;
     Right    = right;
     Operator = op;
 }
コード例 #4
0
 public SucoBooleanLiteralExpression(int startIndex, int endIndex, bool literalValue, SucoType type = null)
     : base(startIndex, endIndex, type)
 {
     LiteralValue = literalValue;
 }
コード例 #5
0
 public SucoPositionExpression(int startIndex, int endIndex, string name, SucoType type)
     : base(startIndex, endIndex, type)
 {
     Name = name;
 }
コード例 #6
0
 public SucoLetExpression(int startIndex, int endIndex, string varName, SucoExpression valueExpr, SucoExpression innerExpr, SucoType type = null)
     : base(startIndex, endIndex, type)
 {
     VariableName    = varName;
     ValueExpression = valueExpr;
     InnerExpression = innerExpr;
 }
コード例 #7
0
ファイル: ZingaUtil.cs プロジェクト: Timwi/Zinga
 private static object convertVariableValue(SucoType type, JsonValue j) => type switch
 {
コード例 #8
0
 public SucoDecimalLiteralExpression(int startIndex, int endIndex, double numericalValue, SucoType type = null)
     : base(startIndex, endIndex, type)
 {
     LiteralValue = numericalValue;
 }
コード例 #9
0
ファイル: ZingaPuzzlePublish.cs プロジェクト: Timwi/Zinga
        public HttpResponse PuzzlePublish(HttpRequest req)
        {
            if (req.Method != HttpMethod.Post)
            {
                return(HttpResponse.Empty(HttpStatusCode._405_MethodNotAllowed));
            }

            var jsonRaw = req.Post["puzzle"].Value;

            if (jsonRaw == null || !JsonDict.TryParse(jsonRaw, out var json))
            {
                return(HttpResponse.PlainText("The data transmitted is not valid JSON.", HttpStatusCode._400_BadRequest));
            }

            try
            {
                var puzzle = new Puzzle();

                puzzle.Title   = json["title"].GetString();
                puzzle.Author  = json["author"].GetString();
                puzzle.Rules   = json["rules"].GetString();
                puzzle.UrlName = MD5.ComputeUrlName(jsonRaw);

                var givens = json["givens"].GetList().Select((v, ix) => (v, ix)).Where(tup => tup.v != null).Select(tup => (cell: tup.ix, value: tup.v.GetInt())).ToArray();
                if (givens.Any(given => given.cell < 0 || given.cell >= 81))
                {
                    return(HttpResponse.PlainText($"At least one given is out of range (cell {puzzle.Givens.First(given => given.cell < 0 || given.cell >= 81).cell}).", HttpStatusCode._400_BadRequest));
                }
                puzzle.Givens = givens.Length > 0 ? givens : null;

                var constraints           = json["constraints"].GetList();
                var customConstraintTypes = json["customConstraintTypes"].GetList();

                using var tr = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.Serializable });
                using var db = new Db();

                if (db.Puzzles.Any(p => p.UrlName == puzzle.UrlName))
                {
                    return(HttpResponse.PlainText(puzzle.UrlName));
                }

                var dbConstraintTypes = constraints.Select(c => c["type"].GetInt()).Where(c => c >= 0).Distinct().ToArray()
                                        .Apply(cIds => db.Constraints.Where(c => cIds.Contains(c.ConstraintID))).AsEnumerable().ToDictionary(c => c.ConstraintID);
                var faultyId = constraints.Select(c => c["type"].GetInt()).Where(c => c >= 0).FirstOrNull(c => !dbConstraintTypes.ContainsKey(c));
                if (faultyId != null)
                {
                    return(HttpResponse.PlainText($"Unknown constraint type ID: {faultyId.Value}.", HttpStatusCode._400_BadRequest));
                }

                // Add the custom constraint types into the database
                foreach (var constraint in constraints)
                {
                    if (constraint["type"].GetInt() is int typeId && typeId < 0 && !dbConstraintTypes.ContainsKey(typeId))
                    {
                        if (~typeId >= customConstraintTypes.Count || customConstraintTypes[~typeId] == null)
                        {
                            return(HttpResponse.PlainText($"Undefined custom constraint type: {typeId}. List has {customConstraintTypes.Count} entries.", HttpStatusCode._400_BadRequest));
                        }
                        var cType = customConstraintTypes[~typeId];
                        var kind  = EnumStrong.Parse <ConstraintKind>(cType["kind"].GetString());

                        // Some verifications:
                        // Make sure the variable types parse as valid Suco types
                        var env = new SucoTypeEnvironment();
                        foreach (var(varName, varType) in cType["variables"].GetDict())
                        {
                            if (!SucoType.TryParse(varType.GetString(), out var type))
                            {
                                return(HttpResponse.PlainText($"Unrecognized Suco type: {varType.GetString()}.", HttpStatusCode._400_BadRequest));
                            }
                            env = env.DeclareVariable(varName, type);
                        }
コード例 #10
0
 public abstract SucoListCondition DeduceTypes(SucoTypeEnvironment env, SucoContext context, SucoType elementType);
コード例 #11
0
 public SucoImplicitConversionExpression(int startIndex, int endIndex, SucoExpression inner, SucoType type)
     : base(startIndex, endIndex, type)
 {
     Expression = inner;
 }
コード例 #12
0
 public SucoIntegerLiteralExpression(int startIndex, int endIndex, int numericalValue, SucoType type = null)
     : base(startIndex, endIndex, type)
 {
     LiteralValue = numericalValue;
 }
コード例 #13
0
ファイル: SucoConstant.cs プロジェクト: Timwi/Zinga
 public SucoConstant(int startIndex, int endIndex, SucoType type, object value)
     : base(startIndex, endIndex, type)
 {
     Value = value;
 }
コード例 #14
0
 public SucoUnaryOperatorExpression(int startIndex, int endIndex, SucoExpression operand, UnaryOperator op, SucoType type = null)
     : base(startIndex, endIndex, type)
 {
     Operand  = operand;
     Operator = op;
 }
コード例 #15
0
        public override SucoListCondition DeduceTypes(SucoTypeEnvironment env, SucoContext context, SucoType elementType)
        {
            var innerExpression = Expression.DeduceTypes(env, context);

            if (!innerExpression.Type.ImplicitlyConvertibleTo(SucoType.Boolean))
            {
                throw new SucoCompileException($"A condition expression must be a boolean (or implicitly convertible to one).", StartIndex, EndIndex);
            }
            return(new SucoListExpressionCondition(StartIndex, EndIndex, innerExpression.ImplicitlyConvertTo(SucoType.Boolean)));
        }
コード例 #16
0
 public SucoIdentifierExpression(int startIndex, int endIndex, string name, SucoType type = null)
     : base(startIndex, endIndex, type)
 {
     Name = name;
 }
コード例 #17
0
 public SucoConditionalExpression(int startIndex, int endIndex, SucoExpression condition, SucoExpression truePart, SucoExpression falsePart, SucoType type = null)
     : base(startIndex, endIndex, type)
 {
     Condition = condition;
     True      = truePart;
     False     = falsePart;
 }
コード例 #18
0
 public SucoMemberAccessExpression(int startIndex, int endIndex, SucoExpression operand, string memberName, SucoType type = null)
     : base(startIndex, endIndex, type)
 {
     Operand    = operand;
     MemberName = memberName;
 }