Exemplo n.º 1
0
        public ValueTuple <object, TypeBuilder> nativeStartType(string name, Runtime.Struct typeValue)
        {
            string      fullName    = name;
            TypeBuilder typeBuilder = RuntimeContext.CurrentContext.AssemblyBuilder.ModuleBuilder.DefineType(fullName, TypeAttributes.Public | TypeAttributes.Class);

            return(typeValue, typeBuilder);
        }
Exemplo n.º 2
0
        public Unit nativeFinishType(Runtime.Struct typeValue, TypeBuilder typeBuilder)
        {
            foreach (var field in typeValue.Fields)
            {
                typeBuilder.DefineField(field.Name, TypeBox.Unbox(field.Type), FieldAttributes.Public | FieldAttributes.InitOnly);
            }

            // Constructor
            var constuctor = typeBuilder.DefineConstructor(
                MethodAttributes.Public, CallingConventions.Standard,
                typeValue.Fields.Select(field => TypeBox.Unbox(field.Type)).ToArray()
                );

            ILGenerator ilgen = constuctor.GetILGenerator();

            for (int i = 0; i < typeValue.Fields.Count(); i++)
            {
                ilgen.Emit(OpCodes.Ldarg_0);
                ilgen.Emit(OpCodes.Ldarg, i + 1);
                ilgen.Emit(OpCodes.Stfld, typeBuilder.GetField(typeValue.Fields[i].Name));
            }
            ilgen.Emit(OpCodes.Ret);

            // TODO: ToString
            // TODO: structural equality
            // TODO: structural comparison
            // TODO: structural hashcode

            return(Unit.Instance);
        }
Exemplo n.º 3
0
        public IList <Ast.ModuleStmt> dataTypeMacro(string name, Runtime.Struct typeValue, Runtime.Params placeholderParams)
        {
            // TODO: generic

            var stmts = new List <Ast.ModuleStmt>();

            // Add field getters
            foreach (var field in typeValue.Fields)
            {
                stmts.Add(new Ast.ModuleFunStmt(
                              field.Name,
                              new Ast.FunDefExpr(
                                  new List <Ast.ParamDef>()
                {
                    new Ast.ParamDef("o",
                                     Ast.ParamDefKind.positional,
                                     type: Optional <Ast.Expr> .Some(new Ast.Name(name)))
                },
                                  new Ast.NativeGetField(new Ast.Name("o"), field.Name)))
                          );
            }

            // Add constructor
            string constuctorName = "make" + name;

            stmts.Add(new Ast.ModuleFunStmt(
                          constuctorName,
                          new Ast.FunDefExpr(
                              typeValue.Fields.Select(field => new Ast.ParamDef(
                                                          field.Name, Ast.ParamDefKind.named,
                                                          type: Optional.Some <Ast.Expr>(new Ast.NativeValue(field.Type)))).ToList(),
                              new Ast.NativeConstruct(new Ast.Name(name),
                                                      typeValue.Fields.Select(field => (Ast.Expr) new Ast.Name(field.Name)).ToList())
                              )
                          ));

            return(stmts);
        }