Esempio n. 1
0
        public static ZilObject PRINC([NotNull] Context ctx, [NotNull] ZilObject value, ZilChannel channel = null)
        {
            if (channel == null)
            {
                channel =
                    ctx.GetLocalVal(ctx.GetStdAtom(StdAtom.OUTCHAN)) as ZilChannel ??
                    ctx.GetGlobalVal(ctx.GetStdAtom(StdAtom.OUTCHAN)) as ZilChannel;
                if (channel == null)
                {
                    throw new InterpreterError(InterpreterMessages._0_Bad_OUTCHAN, "PRINC");
                }
            }

            var str = value.ToStringContext(ctx, true);

            // TODO: check for I/O error
            channel.WriteString(str);

            return(value);
        }
Esempio n. 2
0
        public static ZilObject PRINT_MANY([NotNull] Context ctx, ZilChannel channel,
                                           [Decl("<OR ATOM APPLICABLE>")] ZilObject printer, [NotNull] ZilObject[] items)
        {
            if (printer is ZilAtom atom)
            {
                printer = ctx.GetGlobalVal(atom) ?? ctx.GetLocalVal(atom);
                if (printer == null)
                {
                    throw new InterpreterError(
                              InterpreterMessages._0_Atom_1_Has_No_2_Value,
                              "PRINT-MANY",
                              atom.ToStringContext(ctx, false),
                              "local or global");
                }
            }

            var applicablePrinter = printer.AsApplicable(ctx);

            if (applicablePrinter == null)
            {
                throw new InterpreterError(InterpreterMessages._0_Not_Applicable_1, "PRINT-MANY", printer.ToStringContext(ctx, false));
            }

            var crlf   = ctx.GetStdAtom(StdAtom.PRMANY_CRLF);
            var result = ctx.TRUE;

            using (var innerEnv = ctx.PushEnvironment())
            {
                innerEnv.Rebind(ctx.GetStdAtom(StdAtom.OUTCHAN), channel);

                var printArgs = new ZilObject[1];

                foreach (var item in items)
                {
                    result = item;

                    if (result == crlf)
                    {
                        CRLF(ctx);
                    }
                    else
                    {
                        printArgs[0] = result;
                        applicablePrinter.ApplyNoEval(ctx, printArgs);
                    }
                }
            }

            return(result);
        }
Esempio n. 3
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));
            }
        }
Esempio n. 4
0
 public DeclCheckError([NotNull] IProvideSourceLine src, [NotNull] Context ctx, [NotNull] ZilObject value,
                       [NotNull] ZilObject pattern, string usage)
     : base(src, DiagnosticCode, usage, pattern.ToStringContext(ctx, false), value.ToStringContext(ctx, false))
 {
 }
Esempio n. 5
0
 public DeclCheckError([NotNull] Context ctx, [NotNull] ZilObject value, [NotNull] ZilObject pattern,
                       [NotNull] string usage)
     : base(DiagnosticCode, usage, pattern.ToStringContext(ctx, false), value.ToStringContext(ctx, false))
 {
 }
Esempio n. 6
0
        static ZilObject PerformTypeHandler([NotNull] Context ctx, [NotNull] ZilAtom atom, [CanBeNull] ZilObject handler,
                                            string name,
                                            Func <Context, ZilAtom, ZilObject> getter,
                                            Func <Context, ZilAtom, ZilObject, Context.SetTypeHandlerResult> setter)
        {
            if (!ctx.IsRegisteredType(atom))
            {
                throw new InterpreterError(InterpreterMessages._0_Unrecognized_1_2, name, "type", atom.ToStringContext(ctx, false));
            }

            if (handler == null)
            {
                return(getter(ctx, atom) ?? ctx.FALSE);
            }

            var result = setter(ctx, atom, handler);

            switch (result)
            {
            case Context.SetTypeHandlerResult.OK:
                return(atom);

            case Context.SetTypeHandlerResult.BadHandlerType:
                // the caller should check the handler type, but just in case...
                throw new InterpreterError(InterpreterMessages._0_Must_Be_1, "handler", "atom or applicable value");

            case Context.SetTypeHandlerResult.OtherTypeNotRegistered:
                throw new InterpreterError(InterpreterMessages._0_Unrecognized_1_2, name, "type", handler.ToStringContext(ctx, false));

            case Context.SetTypeHandlerResult.OtherTypePrimDiffers:
                throw new InterpreterError(
                          InterpreterMessages._0_Primtypes_Of_1_And_2_Differ, name, atom.ToStringContext(ctx, false), handler.ToStringContext(ctx, false));

            default:
                throw UnhandledCaseException.FromEnum(result);
            }
        }
Esempio n. 7
0
        public static ZilObject UNPARSE([NotNull] Context ctx, [NotNull] ZilObject arg)
        {
            // in MDL, this takes an optional second argument (radix), but we don't bother

            return(ZilString.FromString(arg.ToStringContext(ctx, false)));
        }