private static void compileConstants(String consFile, String defsFile) { try { PrintWriter out = new PrintWriter(new FileWriter(consFile)); for (ConstExpression ce : Consts.getDeclaredConsts()) { String afterType = ""; if (ce instanceof ArrayConstant) { if (((ArrayConstant) ce).getValues().isEmpty()) { continue; } out.print("int32_t "); afterType = "[" + ((ArrayConstant) ce).size() + "]"; } else { Type ceType = ce.getType(); if (ce.getType() instanceof IntType) { out.print("int32_t "); } else if (ce.getType() instanceof FloatType) { out.print("double "); } else { throw new RuntimeException("No equivalent C++ type for " + ceType); } } out.print(ce.getName()); out.print(afterType); out.print(" = "); compileConstants_(out, ce); out.println(); } // Now check if any of the #defines can be copied over (have integer value) if (defsFile != null) { Scanner in = new Scanner(new File(defsFile)); while (in.hasNext()) { String def = in.next(); if (def.equals("#define")) { String name = in.next(); String value = in.nextLine().trim(); try { int ival = Integer.decode(value); out.println("int32_t " + name + " = " + ival); } catch (Throwable e) { // Ignore it. } } else { in.nextLine(); } } in.close(); } out.close(); } catch (IOException e) { throw new RuntimeException(e); } }
private static void compileConstants_(PrintWriter out, ConstExpression value) { if (value instanceof ArrayConstant) { ArrayConstant ac = (ArrayConstant) value; Iterator<ConstExpression> i = ac.getValues().iterator(); out.print("{ "); while (i.hasNext()) { compileConstants_(out, i.next()); if (i.hasNext()) { out.print(", "); } } out.print("} "); } else { out.print(value.toString().substring(1)); // remove the "C" if (value.getType() instanceof FloatType) { out.println(".0"); // to make sure that floating point division is used } } }