Example #1
0
        public static bool Check([NotNull] Context ctx, [NotNull] ZilObject value, [NotNull] ZilObject pattern,
                                 bool ignoreErrors = false)
        {
            switch (pattern)
            {
            case ZilAtom atom:
                // ReSharper disable once SwitchStatementMissingSomeCases
                switch (atom.StdAtom)
                {
                case StdAtom.ANY:
                    return(true);

                case StdAtom.APPLICABLE:
                    return(value.IsApplicable(ctx));

                case StdAtom.STRUCTURED:
                    return(value is IStructure);

                case StdAtom.TUPLE:
                    // special case
                    return(value.StdTypeAtom == StdAtom.LIST);

                default:
                    // arbitrary atoms can be type names...
                    if (ctx.IsRegisteredType(atom))
                    {
                        var typeAtom = value.GetTypeAtom(ctx);

                        if (typeAtom == atom)
                        {
                            return(true);
                        }

                        // special cases: a raw TABLE value can substitute for a TABLE-based type, or VECTOR
                        return(typeAtom.StdAtom == StdAtom.TABLE &&
                               (atom.StdAtom == StdAtom.VECTOR || ctx.GetTypePrim(atom) == PrimType.TABLE));
                    }

                    // ...or aliases
                    if (IsNonCircularAlias(ctx, atom, out var aliased))
                    {
                        return(Check(ctx, value, aliased, ignoreErrors));
                    }

                    // special cases for GVAL and LVAL
                    // ReSharper disable once SwitchStatementMissingSomeCases
                    switch (atom.StdAtom)
                    {
                    case StdAtom.GVAL:
                        return(value.IsGVAL(out _));

                    case StdAtom.LVAL:
                        return(value.IsLVAL(out _));

                    default:
                        return(ignoreErrors
                                        ? false
                                        : throw new InterpreterError(
                                   InterpreterMessages.Unrecognized_0_1,
                                   "atom in DECL pattern",
                                   atom));
                    }
                }

            case ZilSegment seg:
                return(CheckFormOrSegment(ctx, value, seg.Form, true, ignoreErrors));

            case ZilForm form:
                return(CheckFormOrSegment(ctx, value, form, false, ignoreErrors));

            default:
                if (ignoreErrors)
                {
                    return(false);
                }

                throw new InterpreterError(
                          InterpreterMessages.Unrecognized_0_1,
                          "value in DECL pattern",
                          pattern.ToStringContext(ctx, false));
            }
        }
Example #2
0
 public override bool Allows(Context ctx, ZilObject arg)
 {
     return(arg.IsApplicable(ctx));
 }
Example #3
0
 public static ZilObject APPLICABLE_P([NotNull] Context ctx, ZilObject arg)
 {
     return(arg.IsApplicable(ctx) ? ctx.TRUE : ctx.FALSE);
 }