Ejemplo n.º 1
0
        // Token: 0x06006B95 RID: 27541 RVA: 0x001E294C File Offset: 0x001E0B4C
        public static bool Get(JsonObject j, string key, ref PInt value)
        {
            object obj = JsonUtility.GetObj(j, key);

            if (obj == null)
            {
                return(false);
            }
            if (obj.GetType() != typeof(long))
            {
                return(false);
            }
            value = (int)((long)obj);
            return(true);
        }
Ejemplo n.º 2
0
        public override void Define()
        {
            Statement("requires", list => EType.Unit,
                      (c, list) => {
                c += $"if({string.Join(" && ", list.Skip(1).Select(x => $"!({GenerateExpression(x)})"))})";
                c++;
                c += $"goto {Core.NextLabel};";
                c--;
            }).Interpret(
                (list, state) =>
                list.Skip(1).Select(x => Extensions.AsBool(state.Evaluate(x))).Aggregate((a, b) => a && b)
                                                        ? true
                                                        : throw new BailoutException());

            Statement("block", list => list.Last().Type,
                      (c, list) => list.Skip(1).ForEach(x => GenerateStatement(c, (PList)x)))
            .Interpret((list, state) => state.Evaluate(list.Skip(1)));

            Expression("block", list => list.Last().Type,
                       list => $@"([=]() -> {GenerateType(list.Type)} {{
{string.Join('\n', list.Skip(1).Select((x, i) => {
					string code;
					if(x is PList xl) {
						var c = new CodeBuilder();
						GenerateStatement(c, xl);
						code = c.Code;
					} else
						code = GenerateExpression(x) + "                        ; ";
					if(i == list.Count - 2)
						return $"                         \ t \ treturn {
                code.Trim()
            } ";
					return $"                     \ t \ t {
                code.Trim()
            } ";
				}))}
	}})()"    ,
                       list => $@"([=]() -> {GenerateType(list.Type)} {{
{string.Join('\n', list.Skip(1).Select((x, i) => {
					string code;
					if(x is PList xl) {
						var c = new CodeBuilder();
						GenerateStatement(c, xl);
						code = c.Code;
					} else
						code = GenerateExpression(x) + "                        ; ";
					if(i == list.Count - 2)
						return $"                         \ t \ treturn({ code.Trim().TrimEnd(';') }).Store(); ";
					return $"                     \ t \ t {
                code.Trim()
            } ";
				}))}
	}})()"    ).Interpret((list, state) => state.Evaluate(list.Skip(1)));

            Statement("if",
                      list => list[2].Type.AsRuntime(list[1].Type.Runtime ||
                                                     !(list[2].Type is EUnit) && list[2].Type.Runtime ||
                                                     !(list[3].Type is EUnit) && list[3].Type.Runtime),
                      (c, list) => {
                c += $"if(({GenerateExpression(list[1])}) != 0) {{";
                c++;
                GenerateStatement(c, (PList)list[2]);
                c--;
                c += "} else {";
                c++;
                GenerateStatement(c, (PList)list[3]);
                c--;
                c += "}";
            }, (c, list) => {
                if (list[1].Type.Runtime)
                {
                    var if_label   = TempName();
                    var end_label  = TempName();
                    var else_label = TempName();
                    c += $"LabelTag {if_label} = DefineLabel(), {else_label} = DefineLabel(), {end_label} = DefineLabel();";
                    c += $"BranchIf({GenerateExpression(list[1])}, {if_label}, {else_label});";
                    c += $"Label({if_label});";
                    GenerateStatement(c, (PList)list[2]);
                    c += $"Branch({end_label});";
                    c += $"Label({else_label});";
                    GenerateStatement(c, (PList)list[3]);
                    c += $"Branch({end_label});";
                    c += $"Label({end_label});";
                }
                else
                {
                    c += $"if(({GenerateExpression(list[1])}) != 0) {{";
                    c++;
                    GenerateStatement(c, (PList)list[2]);
                    c--;
                    c += "} else {";
                    c++;
                    GenerateStatement(c, (PList)list[3]);
                    c--;
                    c += "}";
                }
            });

            Expression("if", list => list[2].Type, list => {
                var a = GenerateExpression(list[2]);
                var b = GenerateExpression(list[3]);
                if (!a.StartsWith("throw"))
                {
                    a = $"({a})";
                }
                if (!b.StartsWith("throw"))
                {
                    b = $"({b})";
                }
                return($"({GenerateExpression(list[1])} != 0) ? {a} : {b}");
            }, list => {
                var a = GenerateExpression(list[2]);
                var b = GenerateExpression(list[3]);

                if (list[1].Type.Runtime)
                {
                    if (a.StartsWith("throw"))
                    {
                        a = "null";
                    }
                    if (b.StartsWith("throw"))
                    {
                        b = "null";
                    }
                    return($"Ternary<{GenerateType(list[1].Type.AsCompiletime())}, {GenerateType(list[2].Type.AsCompiletime())}>((LlvmRuntimeValue<{GenerateType(list[1].Type.AsCompiletime())}>) ({GenerateExpression(list[1])}), {a}, {b})");
                }

                if (!a.StartsWith("throw"))
                {
                    a = $"({a})";
                }
                if (!b.StartsWith("throw"))
                {
                    b = $"({b})";
                }
                return($"({GenerateExpression(list[1])}) != 0 ? {a} : {b}");
            });

            Interpret("if", (list, state) => Extensions.AsBool(state.Evaluate(list[1])) ? state.Evaluate(list[2]) : state.Evaluate(list[3]));

            Statement("for", _ => EType.Unit,
                      (c, list) => {
                if (!(list[1] is PList dlist) || !(dlist[0] is PName vname))
                {
                    throw new NotSupportedException();
                }
                int start = 0, end = 0, step = 1;
                var name  = vname.Name;
                if (dlist.Count == 2)
                {
                    if (!(dlist[1] is PInt ei))
                    {
                        throw new NotSupportedException();
                    }
                    end = (int)ei.Value;
                }
                else if (dlist.Count == 3)
                {
                    if (!(dlist[1] is PInt si) || !(dlist[2] is PInt ei))
                    {
                        throw new NotSupportedException();
                    }
                    start = (int)si.Value;
                    end   = (int)ei.Value;
                }
                else if (dlist.Count == 4)
                {
                    if (!(dlist[1] is PInt si) || !(dlist[2] is PInt ei) || !(dlist[3] is PInt ti))
                    {
                        throw new NotSupportedException();
                    }
                    start = (int)si.Value;
                    end   = (int)ei.Value;
                    step  = (int)ti.Value;
                }
                else
                {
                    throw new NotSupportedException();
                }

                for (var i = start; i < end; i += step)
                {
                    var pi  = new PInt(i);
                    pi.Type = new EInt(true, 32);
                    list.Skip(2).ForEach(x => GenerateStatement(c, ((PList)x).MapLeaves(y => y is PName pn && pn.Name == name ? pi : y)));
                }
            }).Interpret((list, state) => {