public static ZilObject VALUE(Context ctx, [NotNull] ZilAtom atom, [NotNull] LocalEnvironment env) { var result = env.GetLocalVal(atom) ?? ctx.GetGlobalVal(atom); if (result == null) { throw new InterpreterError( InterpreterMessages._0_Atom_1_Has_No_2_Value, "VALUE", atom.ToStringContext(ctx, false), "local or global"); } return(result); }
public static ZilObject GVAL([NotNull] Context ctx, [NotNull] ZilAtom atom) { var result = ctx.GetGlobalVal(atom); if (result == null) { throw new InterpreterError( InterpreterMessages._0_Atom_1_Has_No_2_Value, "GVAL", atom.ToStringContext(ctx, false), "global"); } return(result); }
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); } }
public static ZilObject DEFMAC([NotNull] Context ctx, [NotNull] ZilAtom name, [CanBeNull][Optional] ZilAtom activationAtom, [ItemNotNull][NotNull] ZilList argList, [CanBeNull][Optional] ZilDecl decl, [NotNull][Required] ZilObject[] body) { if (!ctx.AllowRedefine && ctx.GetGlobalVal(name) != null) { throw new InterpreterError(InterpreterMessages._0_Already_Defined_1, "DEFMAC", name.ToStringContext(ctx, false)); } var func = new ZilFunction( "DEFMAC", name, activationAtom, argList, decl, body); var macro = new ZilEvalMacro(func) { SourceLine = ctx.TopFrame.SourceLine }; ctx.SetGlobalVal(name, macro); return(name); }
static ZilObject PerformDefine([NotNull][ProvidesContext] Context ctx, [NotNull] ZilAtom name, [CanBeNull] ZilAtom activationAtom, [NotNull] ZilList argList, ZilDecl decl, [NotNull] ZilObject[] body, [NotNull] string subrName) { if (!ctx.AllowRedefine && ctx.GetGlobalVal(name) != null) { throw new InterpreterError(InterpreterMessages._0_Already_Defined_1, subrName, name.ToStringContext(ctx, false)); } var func = new ZilFunction( subrName, name, activationAtom, argList, decl, body); ctx.SetGlobalVal(name, func); return(name); }
public static ZilObject NEWTYPE([NotNull] Context ctx, [NotNull] ZilAtom name, [NotNull] ZilAtom primtypeAtom, [CanBeNull] ZilObject decl = null) { if (ctx.IsRegisteredType(name)) { throw new InterpreterError(InterpreterMessages._0_Already_Defined_1, "NEWTYPE", name.ToStringContext(ctx, false)); } PrimType primtype; if (ctx.IsRegisteredType(primtypeAtom)) { primtype = ctx.GetTypePrim(primtypeAtom); } else { throw new InterpreterError(InterpreterMessages._0_Unrecognized_1_2, "NEWTYPE", "primtype", primtypeAtom.ToStringContext(ctx, false)); } ctx.RegisterType(name, primtype); ctx.PutProp(name, ctx.GetStdAtom(StdAtom.DECL), decl); return(name); }
public static ZilObject TYPEPRIM([NotNull] Context ctx, [NotNull] ZilAtom type) { if (!ctx.IsRegisteredType(type)) { throw new InterpreterError(InterpreterMessages._0_Unrecognized_1_2, "TYPEPRIM", "type", type.ToStringContext(ctx, false)); } return(ctx.GetStdAtom(PrimTypeToType(ctx.GetTypePrim(type)))); }