当前位置: 首页>>代码示例>>C#>>正文


C# AST.Find方法代码示例

本文整理汇总了C#中AST.Find方法的典型用法代码示例。如果您正苦于以下问题:C# AST.Find方法的具体用法?C# AST.Find怎么用?C# AST.Find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在AST的用法示例。


在下文中一共展示了AST.Find方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetExprTypeEnv

        public Tuple<AST.Env, AST.ExprType> GetExprTypeEnv(Boolean is_struct, AST.Env env, Boolean is_const, Boolean is_volatile) {

            if (name == "") {
                // If no name is supplied: must be complete.
                // struct { ... } or union { ... }

                if (declns == null) {
                    throw new ArgumentNullException("Error: parser should ensure declns != null");
                }

                Tuple<AST.Env, List<Tuple<String, AST.ExprType>>> r_attribs = GetAttribs(env);
                env = r_attribs.Item1;

                if (is_struct) {
                    return new Tuple<AST.Env, AST.ExprType>(env, AST.TStructOrUnion.CreateStruct("<anonymous>", r_attribs.Item2, is_const, is_volatile));
                } else {
                    return new Tuple<AST.Env, AST.ExprType>(env, AST.TStructOrUnion.CreateUnion("<anonymous>", r_attribs.Item2, is_const, is_volatile));
                }

            } else {
                // If a name is supplied, split into 2 cases.

                String typename = is_struct ? $"struct {name}" : $"union {name}";

                if (declns == null) {
                    // Case 1: If no attribute list supplied, then we are either
                    //       1) mentioning an already-existed struct/union
                    //    or 2) creating an incomplete struct/union

                    Option<AST.Env.Entry> entry_opt = env.Find(typename);

                    if (entry_opt.IsNone) {
                        // If the struct/union is not in the current environment,
                        // then add an incomplete struct/union into the environment
                        AST.ExprType type =
                            is_struct
                            ? AST.TStructOrUnion.CreateIncompleteStruct(name, is_const, is_volatile)
                            : AST.TStructOrUnion.CreateIncompleteUnion(name, is_const, is_volatile);

                        env = env.PushEntry(AST.Env.EntryKind.TYPEDEF, typename, type);
                        return Tuple.Create(env, type);
                    }

                    if (entry_opt.Value.kind != AST.Env.EntryKind.TYPEDEF) {
                        throw new InvalidProgramException(typename + " is not a type? This should be my fault.");
                    }

                    // If the struct/union is found, return it.
                    return Tuple.Create(env, entry_opt.Value.type);

                } else {
                    // Case 2: If an attribute list is supplied.

                    // 1) Make sure there is no complete struct/union in the current environment.
                    Option<AST.Env.Entry> entry_opt = env.Find(typename);
                    if (entry_opt.IsSome && entry_opt.Value.type.kind == AST.ExprType.Kind.STRUCT_OR_UNION && ((AST.TStructOrUnion)entry_opt.Value.type).IsComplete) {
                        throw new InvalidOperationException($"Redefining {typename}");
                    }

                    // 2) Add an incomplete struct/union into the environment.
                    AST.TStructOrUnion type =
                        is_struct
                        ? AST.TStructOrUnion.CreateIncompleteStruct(name, is_const, is_volatile)
                        : AST.TStructOrUnion.CreateIncompleteUnion(name, is_const, is_volatile);
                    env = env.PushEntry(AST.Env.EntryKind.TYPEDEF, typename, type);

                    // 3) Iterate over the attributes.
                    Tuple<AST.Env, List<Tuple<String, AST.ExprType>>> r_attribs = GetAttribs(env);
                    env = r_attribs.Item1;

                    // 4) Make the type complete. This would also change the entry inside env.
                    if (is_struct) {
                        type.DefineStruct(r_attribs.Item2);
                    } else {
                        type.DefineUnion(r_attribs.Item2);
                    }

                    return new Tuple<AST.Env, AST.ExprType>(env, type);
                }
            }
        }
开发者ID:JianpingZeng,项目名称:C-Compiler,代码行数:81,代码来源:declarations.cs

示例2: GetExpr

        public override AST.Expr GetExpr(AST.Env env)
        {
            Option<AST.Env.Entry> entry_opt = env.Find(name);

            if (entry_opt.IsNone) {
                throw new InvalidOperationException($"Cannot find variable '{name}'");
            }

            AST.Env.Entry entry = entry_opt.Value;

            switch (entry.kind) {
                case AST.Env.EntryKind.TYPEDEF:
                    throw new InvalidOperationException($"Expected a variable '{name}', not a typedef.");
                case AST.Env.EntryKind.ENUM:
                    return new AST.ConstLong(entry.offset, env);
                case AST.Env.EntryKind.FRAME:
                case AST.Env.EntryKind.GLOBAL:
                case AST.Env.EntryKind.STACK:
                    return new AST.Variable(entry.type, name, env);
                default:
                    throw new InvalidOperationException($"Cannot find variable '{name}'");
            }
        }
开发者ID:JianpingZeng,项目名称:C-Compiler,代码行数:23,代码来源:expressions.cs


注:本文中的AST.Find方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。