void print_func(Output& out, const Func* func) { auto const finfo = find_func_info(func); if (func->isPseudoMain()) { out.fmtln(".main {{"); } else { out.fmtln(".function {}({}){}{{", func->name()->data(), func_param_list(finfo), func_flag_list(finfo)); } indented(out, [&] { print_func_directives(out, func); print_func_body(out, finfo); }); out.fmtln("}}"); out.nl(); }
void print_func(Output& out, const Func* func) { auto const finfo = find_func_info(func); if (func->isPseudoMain()) { out.fmtln(".main {{"); } else { out.fmtln(".function{} {}{}({}){}{{", opt_attrs(AttrContext::Func, func->attrs(), &func->userAttributes()), opt_type_info(func->returnUserType(), func->returnTypeConstraint()), func->name(), func_param_list(finfo), func_flag_list(finfo)); } indented(out, [&] { print_func_directives(out, finfo); print_func_body(out, finfo); }); out.fmtln("}}"); out.nl(); }
void bbindent(Bytebuffer* buf, const int n) { bbCat(buf,indented(n)); }
static void genj_writevar(Generator* generator, Symbol* vsym, Bytebuffer* code, int rank, size_t* start, size_t* count) { Dimset* dimset = &vsym->typ.dimset; int typecode = vsym->typ.basetype->typ.typecode; int i; codeline(""); codelined(1,"{"); /* Enclose in {...} for scoping */ if(rank == 0) { bbprintf0(stmt,"%sArray%s.D0 data = new Array%s.D0();\n", indented(1),jtypecap(typecode), jtypecap(typecode)); codedump(stmt); if(typecode == NC_CHAR) { /* Construct the data Array */ jquotestring(code,'\''); bbprintf0(stmt,"%sdata.set((char)%s);\n", indented(1),bbContents(code)); } else { commify(code); bbprintf0(stmt,"%sdata.set((%s)%s);\n", indented(1),jtype(typecode),bbContents(code)); } codedump(stmt); /* do the actual write */ bbprintf0(stmt,"%sncfile.write(\"%s\",data);\n", indented(1),jescapifyname(vsym->name)); codedump(stmt); } else { /* array */ Bytebuffer* dimbuf = bbNew(); /* Construct the dimension set*/ bbCat(dimbuf,"new int[]{"); for(i=0;i<rank;i++) { Symbol* dsym = dimset->dimsyms[i]; char tmp[32]; nprintf(tmp,sizeof(tmp),"%lu",dsym->dim.declsize); if(i>0) {bbCat(dimbuf,", ");} bbCat(dimbuf,tmp); } bbCat(dimbuf,"}"); /* Construct the data array and capture its index */ if(typecode == NC_CHAR) { jquotestring(code,'"'); bbprintf0(stmt,"%sString contents = ", indented(1)); } else { bbprintf0(stmt,"%s%s[] contents = new %s[] {", indented(1),jtype(typecode),jtype(typecode)); commify(code); } codedump(stmt); codedump(code); if(typecode != NC_CHAR) codepartial("}"); codeline(";"); bbprintf0(stmt,"%sArray%s data = new Array%s(%s);\n", indented(1), jtypecap(typecode), jtypecap(typecode), bbContents(dimbuf)); codedump(stmt); codelined(1,"IndexIterator iter = data.getIndexIterator();"); codelined(1,"int count = 0;"); codelined(1,"while(iter.hasNext())"); if(typecode == NC_CHAR) bbprintf0(stmt, "%siter.setCharNext(contents.charAt(count++));\n",indented(2)); else bbprintf0(stmt,"%siter.set%sNext(contents[count++]);\n", indented(2),jtypecap(typecode)); codedump(stmt); bbFree(dimbuf); /* Construct the origin set from the start set */ bbprintf0(stmt,"%sint[] origin = new int[]{",indented(1)); for(i=0;i<rank;i++) { bbprintf(stmt,"%s%lu",(i>0?", ":""),start[i]); } bbCat(stmt,"};\n"); codedump(stmt); /* do the actual write */ bbprintf0(stmt,"%sncfile.write(\"%s\",origin,data);\n", indented(1),jescapifyname(vsym->name)); codedump(stmt); } codelined(1,"}"); /* Enclose in {...} for scoping */ codeflush(); }
static void genj_writeattr(Generator* generator, Symbol* asym, Bytebuffer* code, int rank, size_t* start, size_t* count) { Symbol* basetype = asym->typ.basetype; nc_type typecode = basetype->typ.typecode; /* default assumption */ size_t len = asym->data == NULL?0:asym->data->length; codeprintf("%s/* attribute: %s */\n",indented(1),asym->name); /* Handle NC_CHAR specially */ if(typecode == NC_CHAR) { /* revise the length count */ len = bbLength(code); if(len == 0) { bbAppend(code,'\0'); len++; bbClear(code); bbCat(code,"\"\""); len++; } else jquotestring(code,'"'); bbNull(code); } else { /* not NC_CHAR*/ char* code2; commify(code); /* Convert to constant */ code2 = bbDup(code); bbClear(code); bbprintf0(stmt,"new %s[]", jarraytype(typecode)); bbCatbuf(code,stmt); bbCat(code,"{"); bbCat(code,code2); bbCat(code,"}"); efree(code2); } switch (typecode) { case NC_BYTE: case NC_SHORT: case NC_INT: case NC_FLOAT: case NC_DOUBLE: codelined(1,"{"); bbprintf0(stmt,"%sArray data = Array.factory(%s.class, new int[]{%lu}, ", indented(1), jtype(basetype->typ.typecode), len); codedump(stmt); codedump(code); codeline(");"); if(asym->att.var == NULL) { bbprintf0(stmt,"%sncfile.addGlobalAttribute(\"%s\",data);\n", indented(1),jescapifyname(asym->name)); } else { bbprintf0(stmt,"%sncfile.addVariableAttribute(\"%s\",\"%s\",data);\n", indented(1), jescapifyname(asym->att.var->name), jescapifyname(asym->name)); } codedump(stmt); codelined(1,"}"); codeflush(); break; case NC_CHAR: if(asym->att.var == NULL) { bbprintf0(stmt,"%sncfile.addGlobalAttribute(\"%s\",%s);\n", indented(1), jescapifyname(asym->name), bbContents(code)); } else { bbprintf0(stmt,"%sncfile.addVariableAttribute(\"%s\",\"%s\",%s);\n", indented(1), jescapifyname(asym->att.var->name), jescapifyname(asym->name), bbContents(code)); } codedump(stmt); codeflush(); break; default: break; } codeflush(); }
/* * Generate code for creating netCDF from in-memory structure. */ void gen_ncjava(const char *filename) { int idim, ivar, iatt, maxdims; int ndims, nvars, natts, ngatts; ndims = listlength(dimdefs); nvars = listlength(vardefs); natts = listlength(attdefs); ngatts = listlength(gattdefs); /* Construct the main class */ codeline("import java.util.*;"); codeline("import ucar.ma2.*;"); codeline("import ucar.nc2.*;"); codeline("import ucar.nc2.NetcdfFile.*;"); codeline(""); codepartial("public class "); codeline(mainname); codeline("{"); /* Now construct the main procedure*/ codeline(""); codeline("static public void main(String[] argv) throws Exception"); codeline("{"); /* create necessary declarations */ if(ndims > 0) { codeline(""); codelined(1,"/* dimension lengths */"); for(idim = 0; idim < ndims; idim++) { Symbol* dsym = (Symbol*)listget(dimdefs,idim); if(dsym->dim.declsize == NC_UNLIMITED) { bbprintf0(stmt,"%sfinal int %s_len = 0;\n", indented(1),jname(dsym)); } else { bbprintf0(stmt,"%sfinal int %s_len = %lu;\n", indented(1), jname(dsym), (unsigned long) dsym->dim.declsize); } codedump(stmt); } } codeflush(); maxdims = 0; /* most dimensions of any variable */ for(ivar = 0; ivar < nvars; ivar++) { Symbol* vsym = (Symbol*)listget(vardefs,ivar); if(vsym->typ.dimset.ndims > maxdims) maxdims = vsym->typ.dimset.ndims; } codeline(""); #ifdef EXCEPTWRAP codelined(1,"try {"); #endif /* create netCDF file, uses NC_CLOBBER mode */ codeline(""); codelined(1,"/* enter define mode */"); bbprintf0(stmt, "%sNetcdfFileWriteable ncfile = NetcdfFileWriteable.createNew(\"%s\", %s);\n", indented(1),filename,(nofill_flag?"false":"true")); codedump(stmt); codeflush(); /* define dimensions from info in dims array */ if(ndims > 0) { codeline(""); codelined(1,"/* define dimensions */"); for(idim = 0; idim < ndims; idim++) { Symbol* dsym = (Symbol*)listget(dimdefs,idim); if(dsym->dim.declsize == NC_UNLIMITED) { bbprintf0(stmt,"%sDimension %s_dim = ncfile.addUnlimitedDimension(\"%s\");\n", indented(1),jname(dsym),jescapifyname(dsym->name)); } else { bbprintf0(stmt,"%sDimension %s_dim = ncfile.addDimension(\"%s\", %s_len);\n", indented(1),jname(dsym),jescapifyname(dsym->name), jname(dsym)); } codedump(stmt); } codeflush(); } /* define variables from info in vars array */ if(nvars > 0) { codeline(""); codelined(1,"/* define variables */"); for(ivar = 0; ivar < nvars; ivar++) { Symbol* vsym = (Symbol*)listget(vardefs,ivar); Symbol* basetype = vsym->typ.basetype; Dimset* dimset = &vsym->typ.dimset; codeline(""); bbprintf0(stmt,"%sArrayList %s_dimlist = new ArrayList();\n", indented(1),jname(vsym)); codedump(stmt); if(dimset->ndims > 0) { for(idim = 0; idim < dimset->ndims; idim++) { Symbol* dsym = dimset->dimsyms[idim]; bbprintf0(stmt,"%s%s_dimlist.add(%s_dim);\n", indented(1),jname(vsym),jname(dsym)); codedump(stmt); } } bbprintf0(stmt, "%sncfile.addVariable(\"%s\", DataType.%s, %s_dimlist);\n", indented(1), jescapifyname(vsym->name), jtypeallcaps(basetype->typ.typecode), jname(vsym)); codedump(stmt); } codeflush(); } /* Define the global attributes*/ if(ngatts > 0) { codeline(""); codelined(1,"/* assign global attributes */"); for(iatt = 0; iatt < ngatts; iatt++) { Symbol* gasym = (Symbol*)listget(gattdefs,iatt); genj_defineattr(gasym); } codeline(""); codeflush(); } /* Define the variable specific attributes*/ if(natts > 0) { codeline(""); codelined(1,"/* assign per-variable attributes */"); for(iatt = 0; iatt < natts; iatt++) { Symbol* asym = (Symbol*)listget(attdefs,iatt); genj_defineattr(asym); } codeline(""); codeflush(); } codelined(1,"ncfile.create();"); /* equiv to nc_enddef */ if(!header_only) { /* Load values into those variables with defined data */ if(nvars > 0) { codeline(""); codelined(1,"/* assign variable data */"); for(ivar = 0; ivar < nvars; ivar++) { Symbol* vsym = (Symbol*)listget(vardefs,ivar); if(vsym->data != NULL) genj_definevardata(vsym); } codeline(""); } } codeflush(); }
std::string Value::iterateFormatted(const Value& value, const int indent) { std::stringstream stream; if (value.size() > 0) { // Stream a table begin if (indent > 0) { stream << "{\n"; } unsigned count = 0; for (auto& pair : value) { /// pair.first is the key, which is a string /// pair.second is the value, which may be a table // Set the initial indent stream << indented(indent); // Stream the key stream << pair.first << " = "; // Process any children this table has stream << iterateFormatted(pair.second, indent + 1); // Commas are required between elements, but not after the last one if (indent > 0 && count < value.size() - 1) { stream << ", "; } stream << "\n"; ++count; } // Stream a table end if (indent > 0) { stream << indented(indent - 1); stream << "}"; } } else { switch (value.type) { case Boolean: stream << std::boolalpha << value.b; break; case Number: stream << value.n; break; case String: stream << quoted(value.s); break; default: break; // wrong // also could be an empty table } } return stream.str(); }
/* * Generate code for creating netCDF from in-memory structure. */ void gen_ncjava_std(const char *filename) { int idim, ivar, iatt, maxdims; int ndims, nvars, natts, ngatts, ngrps, ntyps; char* cmode_string; jcode = bbNew(); bbSetalloc(jcode,C_MAX_STMT); ndims = listlength(dimdefs); nvars = listlength(vardefs); natts = listlength(attdefs); ngatts = listlength(gattdefs); ngrps = listlength(grpdefs); ntyps = listlength(typdefs); /* Construct the main class */ jline("import java.util.*;"); jline("import ucar.ma2.*;"); jline("import ucar.nc2.*;"); jline("import ucar.nc2.NetcdfFile.*;"); jline(""); jpartial("public class "); jline(mainname); jline("{"); /* Now construct the main procedure*/ jline(""); jline("static public void main(String[] argv) throws Exception"); jline("{"); /* create necessary declarations */ if (ndims > 0) { jline(""); jlined(1,"/* dimension lengths */"); for(idim = 0; idim < ndims; idim++) { Symbol* dsym = (Symbol*)listget(dimdefs,idim); if (dsym->dim.size == NC_UNLIMITED) { nprintf(stmt,sizeof(stmt),"%sfinal int %s_len = 0;", indented(1),jname(dsym)); } else { nprintf(stmt,sizeof(stmt),"%sfinal int %s_len = %lu;", indented(1), jname(dsym), (unsigned long) dsym->dim.size); } jline(stmt); } } jflush(); maxdims = 0; /* most dimensions of any variable */ for(ivar = 0; ivar < nvars; ivar++) { Symbol* vsym = (Symbol*)listget(vardefs,ivar); if(vsym->typ.dimset.ndims > maxdims) maxdims = vsym->typ.dimset.ndims; } jline(""); #ifdef DOTHROW jlined(1,"try {"); #endif /* create netCDF file, uses NC_CLOBBER mode */ jline(""); jlined(1,"/* enter define mode */"); if (!cmode_modifier) { cmode_string = "NC_CLOBBER"; } else if (cmode_modifier & NC_64BIT_OFFSET) { cmode_string = "NC_CLOBBER|NC_64BIT_OFFSET"; } else { derror("unknown cmode modifier"); cmode_string = "NC_CLOBBER"; } nprintf(stmt,sizeof(stmt), "NetcdfFileWriteable ncfile = NetcdfFileWriteable.createNew(\"%s\", %s);", filename,(nofill_flag?"false":"true")); jlined(1,stmt); jflush(); /* define dimensions from info in dims array */ if (ndims > 0) { jline(""); jlined(1,"/* define dimensions */"); for(idim = 0; idim < ndims; idim++) { Symbol* dsym = (Symbol*)listget(dimdefs,idim); if(dsym->dim.size == NC_UNLIMITED) { nprintf(stmt,sizeof(stmt),"Dimension %s_dim = ncfile.addUnlimitedDimension(\"%s\");", jname(dsym),jescapifyname(dsym->name)); } else { nprintf(stmt,sizeof(stmt),"Dimension %s_dim = ncfile.addDimension(\"%s\", %s_len);", jname(dsym),jescapifyname(dsym->name), jname(dsym)); } jlined(1,stmt); } jflush(); } /* define variables from info in vars array */ if (nvars > 0) { jline(""); jlined(1,"/* define variables */"); for(ivar = 0; ivar < nvars; ivar++) { Symbol* vsym = (Symbol*)listget(vardefs,ivar); Symbol* basetype = vsym->typ.basetype; Dimset* dimset = &vsym->typ.dimset; jline(""); nprintf(stmt,sizeof(stmt),"ArrayList %s_dimlist = new ArrayList();", jname(vsym)); jlined(1,stmt); if(dimset->ndims > 0) { for(idim = 0; idim < dimset->ndims; idim++) { Symbol* dsym = dimset->dimsyms[idim]; nprintf(stmt,sizeof(stmt),"%s_dimlist.add(%s_dim);", jname(vsym),jname(dsym)); jlined(1,stmt); } } nprintf(stmt,sizeof(stmt), "ncfile.addVariable(\"%s\", DataType.%s, %s_dimlist);", jescapifyname(vsym->name), jtypeallcaps(basetype->typ.typecode), jname(vsym)); jlined(1,stmt); } jflush(); } /* Define the global attributes*/ if(ngatts > 0) { jline(""); jlined(1,"/* assign global attributes */"); for(iatt = 0; iatt < ngatts; iatt++) { Symbol* gasym = (Symbol*)listget(gattdefs,iatt); genjstd_defineattribute(gasym); } jline(""); jflush(); } /* Define the variable specific attributes*/ if(natts > 0) { jline(""); jlined(1,"/* assign per-variable attributes */"); for(iatt = 0; iatt < natts; iatt++) { Symbol* asym = (Symbol*)listget(attdefs,iatt); genjstd_defineattribute(asym); } jline(""); jflush(); } jlined(1,"ncfile.create();"); /* equiv to nc_enddef */ /* Load values into those variables with defined data */ if(nvars > 0) { jline(""); jlined(1,"/* assign variable data */"); for(ivar = 0; ivar < nvars; ivar++) { Symbol* vsym = (Symbol*)listget(vardefs,ivar); if(vsym->data != NULL) genjstd_definevardata(vsym); } jline(""); /* compute the max actual size of the unlimited dimension*/ if(usingclassic) computemaxunlimited(); } jflush(); }
static void genjstd_definevardata(Symbol* vsym) { Dimset* dimset = &vsym->typ.dimset; Symbol* basetype = vsym->typ.basetype; int rank = dimset->ndims; int isscalar = (dimset->ndims == 0); Bytebuffer* code; nc_type typecode = basetype->typ.typecode; if(vsym->data == NULL) return; code = bbNew(); jlined(1,"{"); /* Handle special cases first*/ if(isscalar) { /* Construct the data Array */ nprintf(stmt,sizeof(stmt),"Array%s.D0 data = new Array%s.D0();", jtypecap(typecode), jtypecap(typecode)); jlined(1,stmt); /* Fill it */ genjstd_scalardata(vsym,code); if(typecode == NC_CHAR) { nprintf(stmt,sizeof(stmt),"data.set(%s.charAt(0));", bbContents(code)); } else { nprintf(stmt,sizeof(stmt),"data.set((%s)%s);", jtype(typecode),bbContents(code)); } jlined(1,stmt); } else { /* Non-scalar*/ int i; Bytebuffer* dimbuf = bbNew(); /* Store the data */ genjstd_arraydata(vsym,NULL,code); /* Construct the dimension set*/ bbCat(dimbuf,"new int[]{"); for(i=0;i<rank;i++) { Symbol* dsym = dimset->dimsyms[i]; char tmp[32]; if(i==0 && dsym->dim.size == NC_UNLIMITED) nprintf(tmp,sizeof(tmp),"%lu",dsym->dim.unlimitedsize); else nprintf(tmp,sizeof(tmp),"%lu",dsym->dim.size); if(i>0) {bbCat(dimbuf,", ");} bbCat(dimbuf,tmp); } bbCat(dimbuf,"}"); /* Construct the data array and capture its index */ if(typecode == NC_CHAR) nprintf(stmt,sizeof(stmt),"%sString contents = ", indented(1)); else nprintf(stmt,sizeof(stmt),"%s%s[] contents = new %s[] {", indented(1),jtype(typecode),jtype(typecode)); jpartial(stmt); commify(code); jprint(code); if(typecode != NC_CHAR) jpartial("}"); jline(";"); nprintf(stmt,sizeof(stmt),"Array%s data = new Array%s(%s);", jtypecap(typecode), jtypecap(typecode), bbContents(dimbuf)); jlined(1,stmt); jlined(1,"IndexIterator iter = data.getIndexIterator();"); jlined(1,"int count = 0;"); jlined(1,"while(iter.hasNext())"); if(typecode == NC_CHAR) nprintf(stmt,sizeof(stmt), "iter.setCharNext(contents.charAt(count++));"); else nprintf(stmt,sizeof(stmt),"iter.set%sNext(contents[count++]);", jtypecap(typecode)); jlined(2,stmt); bbFree(dimbuf); } /* do the actual write */ nprintf(stmt,sizeof(stmt),"ncfile.write(\"%s\",data);", jescapifyname(vsym->name)); jlined(1,stmt); bbFree(code); jlined(1,"}"); jflush(); }
static void genjstd_primattribute(Symbol* asym, Bytebuffer* code, unsigned long len) { Symbol* basetype = asym->typ.basetype; nc_type typecode = basetype->typ.typecode; /* Handle NC_CHAR specially */ if(typecode == NC_CHAR) { /* revise the length count */ len = bbLength(code); if(len == 0) {bbAppend(code,'\0'); len++;} jquotestring(code,'"'); } else { /* Convert to constant */ char* code2 = bbDup(code); bbClear(code); nprintf(stmt,sizeof(stmt),"new %s[]", jarraytype(typecode)); bbCat(code,stmt); bbCat(code,"{"); bbCat(code,code2); bbCat(code,"}"); efree(code2); } switch (typecode) { case NC_BYTE: case NC_SHORT: case NC_INT: case NC_FLOAT: case NC_DOUBLE: jlined(1,"{"); nprintf(stmt,sizeof(stmt),"%sArray data = Array.factory(%s.class, new int[]{%lu}, ", indented(1), jtype(basetype->typ.typecode), len); jpartial(stmt); jprint(code); jline(");"); if(asym->att.var == NULL) { nprintf(stmt,sizeof(stmt),"ncfile.addGlobalAttribute(\"%s\",data);", jescapifyname(asym->name)); } else { nprintf(stmt,sizeof(stmt),"ncfile.addVariableAttribute(\"%s\",\"%s\",data);", jescapifyname(asym->att.var->name), jescapifyname(asym->name)); } jlined(1,stmt); jlined(1,"}"); jflush(); break; case NC_CHAR: if(asym->att.var == NULL) { nprintf(stmt,sizeof(stmt),"ncfile.addGlobalAttribute(\"%s\",%s);", jescapifyname(asym->name), bbContents(code)); } else { nprintf(stmt,sizeof(stmt),"ncfile.addVariableAttribute(\"%s\",\"%s\",%s);", jescapifyname(asym->att.var->name), jescapifyname(asym->name), bbContents(code)); } jlined(1,stmt); jflush(); break; default: break; } }