From cda780032488adc0baa6567bd450f896e07c9cc3 Mon Sep 17 00:00:00 2001 From: Tripleo Date: Sun, 22 Sep 2024 17:12:46 -0400 Subject: [PATCH 1/7] Format --- .../elijah/stages/gen_c/GenerateC.java | 1802 ++++++++--------- 1 file changed, 900 insertions(+), 902 deletions(-) diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java b/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java index 9c6f0dd..e41e22c 100644 --- a/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java +++ b/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java @@ -44,939 +44,937 @@ * Created 10/8/20 7:13 AM */ public class GenerateC implements CodeGenerator, GenerateFiles { - private static final String PHASE = "GenerateC"; - final ErrSink errSink; - private final ElLog LOG; - - public GenerateC(final @NotNull OutputFileFactoryParams p) { - errSink = p.getErrSink(); - LOG = new ElLog(p.getModFileName(), p.getVerbosity(), PHASE); - // - p.addLog(LOG); - } - - public static @NotNull String getRealTargetName(final BaseGeneratedFunction gf, - final @NotNull VariableTableEntry varTableEntry) { - final String vte_name = varTableEntry.getName(); - if (varTableEntry.vtt == VariableTableType.TEMP) { - if (varTableEntry.getName() == null) { - return "vt" + varTableEntry.tempNum; - } else { - return "vt" + varTableEntry.getName(); - } - } else if (varTableEntry.vtt == VariableTableType.ARG) { - return "va" + vte_name; - } else if (SpecialVariables.contains(vte_name)) { - return SpecialVariables.get(vte_name); - } else if (isValue(gf, vte_name)) { - return "vsc->vsv"; - } else { - return Emit.emit("/*879*/") + "vv" + vte_name; - } - } - - private static boolean isValue(final BaseGeneratedFunction gf, final @NotNull String name) { - if (!name.equals("Value")) - return false; - // - final FunctionDef fd = (FunctionDef) gf.getFD(); - switch (fd.getSpecies()) { - case REG_FUN: - case DEF_FUN: - if (!(fd.getParent() instanceof ClassStatement)) - return false; - for (final AnnotationPart anno : ((ClassStatement) fd.getParent()).annotationIterable()) { - if (anno.annoClass().equals(Helpers.string_to_qualident("Primitive"))) { - return true; - } - } - return false; - case PROP_GET: - case PROP_SET: - return true; - default: - throw new IllegalStateException("Unexpected value: " + fd.getSpecies()); - } - } - - public GenerateResult generateCode(final Collection lgn, final WorkManager wm) { - final GenerateResult gr = new GenerateResult(); - - for (final GeneratedNode generatedNode : lgn) { - if (generatedNode instanceof final GeneratedFunction generatedFunction) { - final WorkList wl = new WorkList(); - generate_function(generatedFunction, gr, wl); - if (!wl.isEmpty()) - wm.addJobs(wl); - } else if (generatedNode instanceof final GeneratedContainerNC containerNC) { - containerNC.generateCode(this, gr); - } else if (generatedNode instanceof final GeneratedConstructor generatedConstructor) { - final WorkList wl = new WorkList(); - generate_constructor(generatedConstructor, gr, wl); - if (!wl.isEmpty()) - wm.addJobs(wl); - } - } - - return gr; - } - - @NotNull - public String getTypeName(final GeneratedNode aNode) { - if (aNode instanceof GeneratedClass) - return getTypeName((GeneratedClass) aNode); - if (aNode instanceof GeneratedNamespace) - return getTypeName((GeneratedNamespace) aNode); - throw new IllegalStateException("Must be class or namespace."); - } - - String getTypeName(@NotNull final GeneratedClass aGeneratedClass) { - return GetTypeName.forGenClass(aGeneratedClass); - } - - String getTypeName(@NotNull final GeneratedNamespace aGeneratedNamespace) { - return GetTypeName.forGenNamespace(aGeneratedNamespace); - } - - public void generate_function(final GeneratedFunction aGeneratedFunction, final GenerateResult gr, - final WorkList wl) { - generateCodeForMethod(aGeneratedFunction, gr, wl); - for (final IdentTableEntry identTableEntry : aGeneratedFunction.idte_list) { - if (identTableEntry.isResolved()) { - final GeneratedNode x = identTableEntry.resolvedType(); - - if (x instanceof GeneratedClass) { - generate_class((GeneratedClass) x, gr); - } else if (x instanceof GeneratedFunction) { - wl.addJob(new WlGenerateFunctionC((GeneratedFunction) x, gr, wl, this)); - } else { - LOG.err(x.toString()); - throw new NotImplementedException(); - } - } - } - for (final ProcTableEntry pte : aGeneratedFunction.prte_list) { + private static final String PHASE = "GenerateC"; + final ErrSink errSink; + private final ElLog LOG; + + public GenerateC(final @NotNull OutputFileFactoryParams p) { + errSink = p.getErrSink(); + LOG = new ElLog(p.getModFileName(), p.getVerbosity(), PHASE); + // + p.addLog(LOG); + } + + public static @NotNull String getRealTargetName(final BaseGeneratedFunction gf, + final @NotNull VariableTableEntry varTableEntry) { + final String vte_name = varTableEntry.getName(); + if (varTableEntry.vtt == VariableTableType.TEMP) { + if (varTableEntry.getName() == null) { + return "vt" + varTableEntry.tempNum; + } else { + return "vt" + varTableEntry.getName(); + } + } else if (varTableEntry.vtt == VariableTableType.ARG) { + return "va" + vte_name; + } else if (SpecialVariables.contains(vte_name)) { + return SpecialVariables.get(vte_name); + } else if (isValue(gf, vte_name)) { + return "vsc->vsv"; + } else { + return Emit.emit("/*879*/") + "vv" + vte_name; + } + } + + private static boolean isValue(final BaseGeneratedFunction gf, final @NotNull String name) { + if (!name.equals("Value")) + return false; + // + final FunctionDef fd = (FunctionDef) gf.getFD(); + switch (fd.getSpecies()) { + case REG_FUN: + case DEF_FUN: + if (!(fd.getParent() instanceof ClassStatement)) + return false; + for (final AnnotationPart anno : ((ClassStatement) fd.getParent()).annotationIterable()) { + if (anno.annoClass().equals(Helpers.string_to_qualident("Primitive"))) { + return true; + } + } + return false; + case PROP_GET: + case PROP_SET: + return true; + default: + throw new IllegalStateException("Unexpected value: " + fd.getSpecies()); + } + } + + public GenerateResult generateCode(final Collection lgn, final WorkManager wm) { + final GenerateResult gr = new GenerateResult(); + + for (final GeneratedNode generatedNode : lgn) { + if (generatedNode instanceof final GeneratedFunction generatedFunction) { + final WorkList wl = new WorkList(); + generate_function(generatedFunction, gr, wl); + if (!wl.isEmpty()) + wm.addJobs(wl); + } else if (generatedNode instanceof final GeneratedContainerNC containerNC) { + containerNC.generateCode(this, gr); + } else if (generatedNode instanceof final GeneratedConstructor generatedConstructor) { + final WorkList wl = new WorkList(); + generate_constructor(generatedConstructor, gr, wl); + if (!wl.isEmpty()) + wm.addJobs(wl); + } + } + + return gr; + } + + @NotNull + public String getTypeName(final GeneratedNode aNode) { + if (aNode instanceof GeneratedClass) + return getTypeName((GeneratedClass) aNode); + if (aNode instanceof GeneratedNamespace) + return getTypeName((GeneratedNamespace) aNode); + throw new IllegalStateException("Must be class or namespace."); + } + + String getTypeName(@NotNull final GeneratedClass aGeneratedClass) { + return GetTypeName.forGenClass(aGeneratedClass); + } + + String getTypeName(@NotNull final GeneratedNamespace aGeneratedNamespace) { + return GetTypeName.forGenNamespace(aGeneratedNamespace); + } + + public void generate_function(final GeneratedFunction aGeneratedFunction, final GenerateResult gr, + final WorkList wl) { + generateCodeForMethod(aGeneratedFunction, gr, wl); + for (final IdentTableEntry identTableEntry : aGeneratedFunction.idte_list) { + if (identTableEntry.isResolved()) { + final GeneratedNode x = identTableEntry.resolvedType(); + + if (x instanceof GeneratedClass) { + generate_class((GeneratedClass) x, gr); + } else if (x instanceof GeneratedFunction) { + wl.addJob(new WlGenerateFunctionC((GeneratedFunction) x, gr, wl, this)); + } else { + LOG.err(x.toString()); + throw new NotImplementedException(); + } + } + } + for (final ProcTableEntry pte : aGeneratedFunction.prte_list) { // ClassInvocation ci = pte.getClassInvocation(); - final FunctionInvocation fi = pte.getFunctionInvocation(); - if (fi == null) { - // TODO constructor - final int y = 2; - /* - * if (pte.getClassInvocation() == null) assert pte.getStatus() == - * BaseTableEntry.Status.UNKNOWN; - */ - } else { - final BaseGeneratedFunction gf = fi.getGenerated(); - if (gf != null) { - wl.addJob(new WlGenerateFunctionC(gf, gr, wl, this)); - } - } - } - } - - public void generate_constructor(final GeneratedConstructor aGeneratedConstructor, final GenerateResult gr, - final WorkList wl) { - generateCodeForConstructor(aGeneratedConstructor, gr, wl); - for (final IdentTableEntry identTableEntry : aGeneratedConstructor.idte_list) { - if (identTableEntry.isResolved()) { - final GeneratedNode x = identTableEntry.resolvedType(); - - if (x instanceof GeneratedClass) { - generate_class((GeneratedClass) x, gr); - } else if (x instanceof GeneratedFunction) { - wl.addJob(new WlGenerateFunctionC((GeneratedFunction) x, gr, wl, this)); - } else { - LOG.err(x.toString()); - throw new NotImplementedException(); - } - } - } - for (final ProcTableEntry pte : aGeneratedConstructor.prte_list) { + final FunctionInvocation fi = pte.getFunctionInvocation(); + if (fi == null) { + // TODO constructor + final int y = 2; + /* + * if (pte.getClassInvocation() == null) assert pte.getStatus() == + * BaseTableEntry.Status.UNKNOWN; + */ + } else { + final BaseGeneratedFunction gf = fi.getGenerated(); + if (gf != null) { + wl.addJob(new WlGenerateFunctionC(gf, gr, wl, this)); + } + } + } + } + + public void generate_constructor(final GeneratedConstructor aGeneratedConstructor, final GenerateResult gr, + final WorkList wl) { + generateCodeForConstructor(aGeneratedConstructor, gr, wl); + for (final IdentTableEntry identTableEntry : aGeneratedConstructor.idte_list) { + if (identTableEntry.isResolved()) { + final GeneratedNode x = identTableEntry.resolvedType(); + + if (x instanceof GeneratedClass) { + generate_class((GeneratedClass) x, gr); + } else if (x instanceof GeneratedFunction) { + wl.addJob(new WlGenerateFunctionC((GeneratedFunction) x, gr, wl, this)); + } else { + LOG.err(x.toString()); + throw new NotImplementedException(); + } + } + } + for (final ProcTableEntry pte : aGeneratedConstructor.prte_list) { // ClassInvocation ci = pte.getClassInvocation(); - final FunctionInvocation fi = pte.getFunctionInvocation(); - if (fi == null) { - // TODO constructor - final int y = 2; - } else { - final BaseGeneratedFunction gf = fi.getGenerated(); - if (gf != null) { - wl.addJob(new WlGenerateFunctionC(gf, gr, wl, this)); - } - } - } - } - - @Override - public void generate_namespace(final GeneratedNamespace x, final GenerateResult gr) { - if (x.generatedAlready) - return; - // TODO do we need `self' parameters for namespace? - final BufferTabbedOutputStream tosHdr = new BufferTabbedOutputStream(); - final BufferTabbedOutputStream tos = new BufferTabbedOutputStream(); - try { - if (x.varTable.size() > 0) { - tosHdr.put_string_ln("typedef struct {"); - tosHdr.incr_tabs(); + final FunctionInvocation fi = pte.getFunctionInvocation(); + if (fi == null) { + // TODO constructor + final int y = 2; + } else { + final BaseGeneratedFunction gf = fi.getGenerated(); + if (gf != null) { + wl.addJob(new WlGenerateFunctionC(gf, gr, wl, this)); + } + } + } + } + + @Override + public void generate_namespace(final GeneratedNamespace x, final GenerateResult gr) { + if (x.generatedAlready) + return; + // TODO do we need `self' parameters for namespace? + final BufferTabbedOutputStream tosHdr = new BufferTabbedOutputStream(); + final BufferTabbedOutputStream tos = new BufferTabbedOutputStream(); + try { + if (x.varTable.size() > 0) { + tosHdr.put_string_ln("typedef struct {"); + tosHdr.incr_tabs(); // tosHdr.put_string_ln("int _tag;"); - for (final GeneratedNamespace.VarTableEntry o : x.varTable) { - final String typeName = getTypeNameGNCForVarTableEntry(o); - - tosHdr.put_string_ln( - String.format("%s* vm%s;", o.varType == null ? "void " : typeName, o.nameToken)); - } - - final String class_name = getTypeName(x); - final int class_code = x.getCode(); - - tosHdr.dec_tabs(); - tosHdr.put_string_ln(""); - tosHdr.put_string_ln(String.format("} %s; // namespace `%s'", class_name, x.getName())); - // TODO "instance" namespaces - tosHdr.put_string_ln(""); - tosHdr.put_string_ln( - String.format("Z%d zN%s_instance; // namespace `%s'", class_code, class_name, x.getName())); - tosHdr.put_string_ln(""); - - tosHdr.put_string_ln(""); - tosHdr.put_string_ln(""); - tos.put_string_ln(String.format("%s* ZN%d() {", class_name, class_code)); - tos.incr_tabs(); - // TODO multiple calls of namespace function (need if/else statement) - tos.put_string_ln(String.format("%s* R = GC_malloc(sizeof(%s));", class_name, class_name)); + for (final GeneratedNamespace.VarTableEntry o : x.varTable) { + final String typeName = getTypeNameGNCForVarTableEntry(o); + + tosHdr.put_string_ln( + String.format("%s* vm%s;", o.varType == null ? "void " : typeName, o.nameToken)); + } + + final String class_name = getTypeName(x); + final int class_code = x.getCode(); + + tosHdr.dec_tabs(); + tosHdr.put_string_ln(""); + tosHdr.put_string_ln(String.format("} %s; // namespace `%s'", class_name, x.getName())); + // TODO "instance" namespaces + tosHdr.put_string_ln(""); + tosHdr.put_string_ln( + String.format("Z%d zN%s_instance; // namespace `%s'", class_code, class_name, x.getName())); + tosHdr.put_string_ln(""); + + tosHdr.put_string_ln(""); + tosHdr.put_string_ln(""); + tos.put_string_ln(String.format("%s* ZN%d() {", class_name, class_code)); + tos.incr_tabs(); + // TODO multiple calls of namespace function (need if/else statement) + tos.put_string_ln(String.format("%s* R = GC_malloc(sizeof(%s));", class_name, class_name)); // tos.put_string_ln(String.format("R->_tag = %d;", class_code)); - for (final GeneratedNamespace.VarTableEntry o : x.varTable) { + for (final GeneratedNamespace.VarTableEntry o : x.varTable) { // final String typeName = getTypeNameForVarTableEntry(o); - tosHdr.put_string_ln(String.format("R->vm%s = 0;", o.nameToken)); - } - tos.put_string_ln(""); - tos.put_string_ln(String.format("zN%s_instance = R;", class_name)); - tos.put_string_ln("return R;"); - tos.dec_tabs(); - tos.put_string_ln(String.format("} // namespace `%s'", x.getName())); - tos.put_string_ln(""); - tos.flush(); - } - } finally { - tos.close(); - tosHdr.close(); - if (x.varTable.size() > 0) { // TODO should we let this through? - final Buffer buf = tos.getBuffer(); + tosHdr.put_string_ln(String.format("R->vm%s = 0;", o.nameToken)); + } + tos.put_string_ln(""); + tos.put_string_ln(String.format("zN%s_instance = R;", class_name)); + tos.put_string_ln("return R;"); + tos.dec_tabs(); + tos.put_string_ln(String.format("} // namespace `%s'", x.getName())); + tos.put_string_ln(""); + tos.flush(); + } + } finally { + tos.close(); + tosHdr.close(); + if (x.varTable.size() > 0) { // TODO should we let this through? + final Buffer buf = tos.getBuffer(); // LOG.info(buf.getText()); - gr.addNamespace(GenerateResult.TY.IMPL, x, buf, x.module().getLsp()); - final Buffer buf2 = tosHdr.getBuffer(); + gr.addNamespace(GenerateResult.TY.IMPL, x, buf, x.module().getLsp()); + final Buffer buf2 = tosHdr.getBuffer(); // LOG.info(buf2.getText()); - gr.addNamespace(GenerateResult.TY.HEADER, x, buf2, x.module().getLsp()); - } - } - x.generatedAlready = true; - } - - @Override - public void forNode(final SM_Node aNode) { - final int y = 2; - if (aNode instanceof final SM_ClassDeclaration classDecl) { - // return classDecl; - } + gr.addNamespace(GenerateResult.TY.HEADER, x, buf2, x.module().getLsp()); + } + } + x.generatedAlready = true; + } + + @Override + public void forNode(final SM_Node aNode) { + final int y = 2; + if (aNode instanceof final SM_ClassDeclaration classDecl) { + // return classDecl; + } // return null; - } - - @NotNull - public String getTypeNameGNCForVarTableEntry(final GeneratedContainer.VarTableEntry o) { - final String typeName; - if (o.resolvedType() != null) { - final GeneratedNode xx = o.resolvedType(); - if (xx instanceof GeneratedClass) { - typeName = getTypeName((GeneratedClass) xx); - } else if (xx instanceof GeneratedNamespace) { - typeName = getTypeName((GeneratedNamespace) xx); - } else - throw new NotImplementedException(); - } else { - if (o.varType != null) - typeName = getTypeName(o.varType); - else - typeName = "void*/*null*/"; - } - return typeName; - } - - public GenerateResult resultsFromNodes(final List aNodes, final WorkManager wm) { - final GenerateC ggc = this; - - final GenerateResult gr2 = new GenerateResult(); - - for (final GeneratedNode generatedNode : aNodes) { + } + + @NotNull + public String getTypeNameGNCForVarTableEntry(final GeneratedContainer.VarTableEntry o) { + final String typeName; + if (o.resolvedType() != null) { + final GeneratedNode xx = o.resolvedType(); + if (xx instanceof GeneratedClass) { + typeName = getTypeName((GeneratedClass) xx); + } else if (xx instanceof GeneratedNamespace) { + typeName = getTypeName((GeneratedNamespace) xx); + } else + throw new NotImplementedException(); + } else { + if (o.varType != null) + typeName = getTypeName(o.varType); + else + typeName = "void*/*null*/"; + } + return typeName; + } + + public GenerateResult resultsFromNodes(final List aNodes, final WorkManager wm) { + final GenerateC ggc = this; + + final GenerateResult gr2 = new GenerateResult(); + + for (final GeneratedNode generatedNode : aNodes) { // if (generatedNode.module() != mod) continue; // README curious - if (generatedNode instanceof final GeneratedContainerNC nc) { - - nc.generateCode(ggc, gr2); - final @NotNull Collection gn1 = (nc.functionMap.values()).stream() - .map(x -> (GeneratedNode) x).collect(Collectors.toList()); - final GenerateResult gr3 = ggc.generateCode(gn1, wm); - gr2.results().addAll(gr3.results()); - final @NotNull Collection gn2 = (nc.classMap.values()).stream() - .map(x -> (GeneratedNode) x).collect(Collectors.toList()); - final GenerateResult gr4 = ggc.generateCode(gn2, wm); - gr2.results().addAll(gr4.results()); - } else { - SimplePrintLoggerToRemoveSoon.println2("2009 " + generatedNode.getClass().getName()); - } - } - - return gr2; - } - - private void generateCodeForMethod(final BaseGeneratedFunction gf, final GenerateResult gr, - final WorkList aWorkList) { - if (gf.getFD() == null) - return; - final Generate_Code_For_Method gcfm = new Generate_Code_For_Method(this, LOG); - gcfm.generateCodeForMethod(gf, gr, aWorkList); - } - - private void generateCodeForConstructor(final GeneratedConstructor gf, final GenerateResult gr, - final WorkList aWorkList) { - if (gf.getFD() == null) - return; - final Generate_Code_For_Method gcfm = new Generate_Code_For_Method(this, LOG); - gcfm.generateCodeForConstructor(gf, gr, aWorkList); - } - - String getTypeNameForGenClass(@NotNull final GeneratedNode aGenClass) { - return GetTypeName.getTypeNameForGenClass(aGenClass); - } - - String getTypeNameForVariableEntry(@NotNull final VariableTableEntry input) { - return GetTypeName.forVTE(input); - } - - String getTypeName(@NotNull final TypeTableEntry tte) { - return GetTypeName.forTypeTableEntry(tte); - } - - @Override - public void generate_class(final GeneratedClass x, final GenerateResult gr) { - if (x.generatedAlready) - return; - switch (x.getKlass().getType()) { - // Don't generate class definition for these three - case INTERFACE: - case SIGNATURE: - case ABSTRACT: - return; - } - final CClassDecl decl = new CClassDecl(x); - decl.evaluatePrimitive(); - final BufferTabbedOutputStream tosHdr = new BufferTabbedOutputStream(); - final BufferTabbedOutputStream tos = new BufferTabbedOutputStream(); - try { - tosHdr.put_string_ln("typedef struct {"); - tosHdr.incr_tabs(); - tosHdr.put_string_ln("int _tag;"); - if (!decl.prim) { - for (final GeneratedClass.VarTableEntry o : x.varTable) { - final String typeName = getTypeNameGNCForVarTableEntry(o); - tosHdr.put_string_ln(String.format("%s vm%s;", typeName, o.nameToken)); - } - } else { - tosHdr.put_string_ln(String.format("%s vsv;", decl.prim_decl)); - } - - final String class_name = getTypeName(x); - final int class_code = x.getCode(); - - tosHdr.dec_tabs(); - tosHdr.put_string_ln(""); + if (generatedNode instanceof final GeneratedContainerNC nc) { + + nc.generateCode(ggc, gr2); + final @NotNull Collection gn1 = (nc.functionMap.values()).stream() + .map(x -> (GeneratedNode) x).collect(Collectors.toList()); + final GenerateResult gr3 = ggc.generateCode(gn1, wm); + gr2.results().addAll(gr3.results()); + final @NotNull Collection gn2 = (nc.classMap.values()).stream() + .map(x -> (GeneratedNode) x).collect(Collectors.toList()); + final GenerateResult gr4 = ggc.generateCode(gn2, wm); + gr2.results().addAll(gr4.results()); + } else { + SimplePrintLoggerToRemoveSoon.println2("2009 " + generatedNode.getClass().getName()); + } + } + + return gr2; + } + + private void generateCodeForMethod(final BaseGeneratedFunction gf, final GenerateResult gr, + final WorkList aWorkList) { + if (gf.getFD() == null) + return; + final Generate_Code_For_Method gcfm = new Generate_Code_For_Method(this, LOG); + gcfm.generateCodeForMethod(gf, gr, aWorkList); + } + + private void generateCodeForConstructor(final GeneratedConstructor gf, final GenerateResult gr, + final WorkList aWorkList) { + if (gf.getFD() == null) + return; + final Generate_Code_For_Method gcfm = new Generate_Code_For_Method(this, LOG); + gcfm.generateCodeForConstructor(gf, gr, aWorkList); + } + + String getTypeNameForGenClass(@NotNull final GeneratedNode aGenClass) { + return GetTypeName.getTypeNameForGenClass(aGenClass); + } + + String getTypeName(@NotNull final TypeTableEntry tte) { + return GetTypeName.forTypeTableEntry(tte); + } + + String getTypeNameForVariableEntry(@NotNull final VariableTableEntry input) { + return GetTypeName.forVTE(input); + } + + @Override + public void generate_class(final GeneratedClass x, final GenerateResult gr) { + if (x.generatedAlready) + return; + switch (x.getKlass().getType()) { + // Don't generate class definition for these three + case INTERFACE: + case SIGNATURE: + case ABSTRACT: + return; + } + final CClassDecl decl = new CClassDecl(x); + decl.evaluatePrimitive(); + final BufferTabbedOutputStream tosHdr = new BufferTabbedOutputStream(); + final BufferTabbedOutputStream tos = new BufferTabbedOutputStream(); + try { + tosHdr.put_string_ln("typedef struct {"); + tosHdr.incr_tabs(); + tosHdr.put_string_ln("int _tag;"); + if (!decl.prim) { + for (final GeneratedClass.VarTableEntry o : x.varTable) { + final String typeName = getTypeNameGNCForVarTableEntry(o); + tosHdr.put_string_ln(String.format("%s vm%s;", typeName, o.nameToken)); + } + } else { + tosHdr.put_string_ln(String.format("%s vsv;", decl.prim_decl)); + } + + final String class_name = getTypeName(x); + final int class_code = x.getCode(); + + tosHdr.dec_tabs(); + tosHdr.put_string_ln(""); // tosHdr.put_string_ln(String.format("} %s;", class_name)); - tosHdr.put_string_ln( - String.format("} %s; // class %s%s", class_name, decl.prim ? "box " : "", x.getName())); - - tosHdr.put_string_ln(""); - tosHdr.put_string_ln(""); - - // TODO remove this block when constructors are added in dependent functions, - // etc in Deduce - { - // TODO what about named constructors and ctor$0 and "the debug stack" - tos.put_string_ln(String.format("%s* ZC%d() {", class_name, class_code)); - tos.incr_tabs(); - tos.put_string_ln(String.format("%s* R = GC_malloc(sizeof(%s));", class_name, class_name)); - tos.put_string_ln(String.format("R->_tag = %d;", class_code)); - if (decl.prim) { - // TODO consider NULL, and floats and longs, etc - if (!decl.prim_decl.equals("bool")) - tos.put_string_ln("R->vsv = 0;"); - else if (decl.prim_decl.equals("bool")) - tos.put_string_ln("R->vsv = false;"); - } else { - for (final GeneratedClass.VarTableEntry o : x.varTable) { + tosHdr.put_string_ln( + String.format("} %s; // class %s%s", class_name, decl.prim ? "box " : "", x.getName())); + + tosHdr.put_string_ln(""); + tosHdr.put_string_ln(""); + + // TODO remove this block when constructors are added in dependent functions, + // etc in Deduce + { + // TODO what about named constructors and ctor$0 and "the debug stack" + tos.put_string_ln(String.format("%s* ZC%d() {", class_name, class_code)); + tos.incr_tabs(); + tos.put_string_ln(String.format("%s* R = GC_malloc(sizeof(%s));", class_name, class_name)); + tos.put_string_ln(String.format("R->_tag = %d;", class_code)); + if (decl.prim) { + // TODO consider NULL, and floats and longs, etc + if (!decl.prim_decl.equals("bool")) + tos.put_string_ln("R->vsv = 0;"); + else if (decl.prim_decl.equals("bool")) + tos.put_string_ln("R->vsv = false;"); + } else { + for (final GeneratedClass.VarTableEntry o : x.varTable) { // final String typeName = getTypeNameForVarTableEntry(o); - // TODO this should be the result of getDefaultValue for each type - tos.put_string_ln(String.format("R->vm%s = 0;", o.nameToken)); - } - } - tos.put_string_ln("return R;"); - tos.dec_tabs(); - tos.put_string_ln(String.format("} // class %s%s", decl.prim ? "box " : "", x.getName())); - tos.put_string_ln(""); - } - tos.flush(); - } finally { - tos.close(); - tosHdr.close(); - final Buffer buf = tos.getBuffer(); + // TODO this should be the result of getDefaultValue for each type + tos.put_string_ln(String.format("R->vm%s = 0;", o.nameToken)); + } + } + tos.put_string_ln("return R;"); + tos.dec_tabs(); + tos.put_string_ln(String.format("} // class %s%s", decl.prim ? "box " : "", x.getName())); + tos.put_string_ln(""); + } + tos.flush(); + } finally { + tos.close(); + tosHdr.close(); + final Buffer buf = tos.getBuffer(); // LOG.info(buf.getText()); - gr.addClass(GenerateResult.TY.IMPL, x, buf, x.module().getLsp()); - final Buffer buf2 = tosHdr.getBuffer(); + gr.addClass(GenerateResult.TY.IMPL, x, buf, x.module().getLsp()); + final Buffer buf2 = tosHdr.getBuffer(); // LOG.info(buf2.getText()); - gr.addClass(GenerateResult.TY.HEADER, x, buf2, x.module().getLsp()); - } - x.generatedAlready = true; - } - - String getRealTargetName(final @NotNull BaseGeneratedFunction gf, final @NotNull IntegerIA target, - final Generate_Code_For_Method.AOG aog) { - final VariableTableEntry varTableEntry = gf.getVarTableEntry(target.getIndex()); - return getRealTargetName(gf, varTableEntry); - } - - @Deprecated - String getTypeName(final @NotNull OS_Type ty) { - return GetTypeName.forOSType(ty, LOG); - } - - @NotNull - List getArgumentStrings(final BaseGeneratedFunction gf, final Instruction instruction) { - final List sl3 = new ArrayList(); - final int args_size = instruction.getArgsSize(); - for (int i = 1; i < args_size; i++) { - final InstructionArgument ia = instruction.getArg(i); - if (ia instanceof IntegerIA) { + gr.addClass(GenerateResult.TY.HEADER, x, buf2, x.module().getLsp()); + } + x.generatedAlready = true; + } + + String getRealTargetName(final @NotNull BaseGeneratedFunction gf, final @NotNull IntegerIA target, + final Generate_Code_For_Method.AOG aog) { + final VariableTableEntry varTableEntry = gf.getVarTableEntry(target.getIndex()); + return getRealTargetName(gf, varTableEntry); + } + + @Deprecated + String getTypeName(final @NotNull OS_Type ty) { + return GetTypeName.forOSType(ty, LOG); + } + + @NotNull + List getArgumentStrings(final BaseGeneratedFunction gf, final Instruction instruction) { + final List sl3 = new ArrayList(); + final int args_size = instruction.getArgsSize(); + for (int i = 1; i < args_size; i++) { + final InstructionArgument ia = instruction.getArg(i); + if (ia instanceof IntegerIA) { // VariableTableEntry vte = gf.getVarTableEntry(DeduceTypes2.to_int(ia)); - final String realTargetName = getRealTargetName(gf, (IntegerIA) ia, Generate_Code_For_Method.AOG.GET); - sl3.add(Emit.emit("/*669*/") + realTargetName); - } else if (ia instanceof IdentIA) { - final CReference reference = new CReference(); - reference.getIdentIAPath((IdentIA) ia, Generate_Code_For_Method.AOG.GET, null); - final String text = reference.build(); - sl3.add(Emit.emit("/*673*/") + text); - } else if (ia instanceof final ConstTableIA c) { - final ConstantTableEntry cte = gf.getConstTableEntry(c.getIndex()); - final String s = GetAssignmentValue.const_to_string(cte.initialValue); - sl3.add(s); - final int y = 2; - } else if (ia instanceof ProcIA) { - LOG.err("740 ProcIA"); - throw new NotImplementedException(); - } else { - LOG.err(ia.getClass().getName()); - throw new IllegalStateException("Invalid InstructionArgument"); - } - } - return sl3; - } - - @Deprecated - String getTypeName(final @NotNull TypeName typeName) { - return GetTypeName.forTypeName(typeName, errSink); - } - - String getAssignmentValue(final VariableTableEntry aSelf, final Instruction aInstruction, - final ClassInvocation aClsinv, final BaseGeneratedFunction gf) { - final GetAssignmentValue gav = new GetAssignmentValue(); - return gav.forClassInvocation(aInstruction, aClsinv, gf, LOG); - } - - @NotNull - String getAssignmentValue(final VariableTableEntry value_of_this, final InstructionArgument value, - final BaseGeneratedFunction gf) { - final GetAssignmentValue gav = new GetAssignmentValue(); - if (value instanceof final FnCallArgs fca) { - return gav.FnCallArgs(fca, gf, LOG); - } - - if (value instanceof @NotNull final ConstTableIA constTableIA) { - return gav.ConstTableIA(constTableIA, gf); - } - - if (value instanceof final IntegerIA integerIA) { - return gav.IntegerIA(integerIA, gf); - } - - if (value instanceof final IdentIA identIA) { - return gav.IdentIA(identIA, gf); - } - - LOG.err(String.format("783 %s %s", value.getClass().getName(), value)); - return String.valueOf(value); - } - - @NotNull - List getArgumentStrings(final @NotNull Supplier> instructionSupplier) { - final @NotNull List sl3 = new ArrayList(); - final int args_size = instructionSupplier.get().size(); - for (int i = 1; i < args_size; i++) { - final InstructionArgument ia = instructionSupplier.get().get(i); - if (ia instanceof IntegerIA) { + final String realTargetName = getRealTargetName(gf, (IntegerIA) ia, Generate_Code_For_Method.AOG.GET); + sl3.add(Emit.emit("/*669*/") + realTargetName); + } else if (ia instanceof IdentIA) { + final CReference reference = new CReference(); + reference.getIdentIAPath((IdentIA) ia, Generate_Code_For_Method.AOG.GET, null); + final String text = reference.build(); + sl3.add(Emit.emit("/*673*/") + text); + } else if (ia instanceof final ConstTableIA c) { + final ConstantTableEntry cte = gf.getConstTableEntry(c.getIndex()); + final String s = GetAssignmentValue.const_to_string(cte.initialValue); + sl3.add(s); + final int y = 2; + } else if (ia instanceof ProcIA) { + LOG.err("740 ProcIA"); + throw new NotImplementedException(); + } else { + LOG.err(ia.getClass().getName()); + throw new IllegalStateException("Invalid InstructionArgument"); + } + } + return sl3; + } + + @Deprecated + String getTypeName(final @NotNull TypeName typeName) { + return GetTypeName.forTypeName(typeName, errSink); + } + + String getAssignmentValue(final VariableTableEntry aSelf, final Instruction aInstruction, + final ClassInvocation aClsinv, final BaseGeneratedFunction gf) { + final GetAssignmentValue gav = new GetAssignmentValue(); + return gav.forClassInvocation(aInstruction, aClsinv, gf, LOG); + } + + @NotNull + String getAssignmentValue(final VariableTableEntry value_of_this, final InstructionArgument value, + final BaseGeneratedFunction gf) { + final GetAssignmentValue gav = new GetAssignmentValue(); + if (value instanceof final FnCallArgs fca) { + return gav.FnCallArgs(fca, gf, LOG); + } + + if (value instanceof @NotNull final ConstTableIA constTableIA) { + return gav.ConstTableIA(constTableIA, gf); + } + + if (value instanceof final IntegerIA integerIA) { + return gav.IntegerIA(integerIA, gf); + } + + if (value instanceof final IdentIA identIA) { + return gav.IdentIA(identIA, gf); + } + + LOG.err(String.format("783 %s %s", value.getClass().getName(), value)); + return String.valueOf(value); + } + + @NotNull + List getArgumentStrings(final @NotNull Supplier> instructionSupplier) { + final @NotNull List sl3 = new ArrayList(); + final int args_size = instructionSupplier.get().size(); + for (int i = 1; i < args_size; i++) { + final InstructionArgument ia = instructionSupplier.get().get(i); + if (ia instanceof IntegerIA) { // VariableTableEntry vte = gf.getVarTableEntry(DeduceTypes2.to_int(ia)); - final String realTargetName = getRealTargetName((IntegerIA) ia, Generate_Code_For_Method.AOG.GET); - sl3.add(Emit.emit("/*669*/") + realTargetName); - } else if (ia instanceof IdentIA) { - final CReference reference = new CReference(); - reference.getIdentIAPath((IdentIA) ia, Generate_Code_For_Method.AOG.GET, null); - final String text = reference.build(); - sl3.add(Emit.emit("/*673*/") + text); - } else if (ia instanceof final ConstTableIA c) { - final ConstantTableEntry cte = c.getEntry(); - final String s = GetAssignmentValue.const_to_string(cte.initialValue); - sl3.add(s); - final int y = 2; - } else if (ia instanceof ProcIA) { - LOG.err("740 ProcIA"); - throw new NotImplementedException(); - } else { - LOG.err(ia.getClass().getName()); - throw new IllegalStateException("Invalid InstructionArgument"); - } - } - return sl3; - } - - String getRealTargetName(final @NotNull IntegerIA target, final Generate_Code_For_Method.AOG aog) { - final BaseGeneratedFunction gf = target.gf; - final VariableTableEntry varTableEntry = gf.getVarTableEntry(target.getIndex()); - return getRealTargetName(gf, varTableEntry); - } - - String getRealTargetName(final @NotNull BaseGeneratedFunction gf, final @NotNull IdentIA target, - final Generate_Code_For_Method.AOG aog, final String value) { - int state = 0, code = -1; - final IdentTableEntry identTableEntry = gf.getIdentTableEntry(target.getIndex()); - final LinkedList ls = new LinkedList(); - // TODO in Deduce set property lookupType to denote what type of lookup it is: - // MEMBER, LOCAL, or CLOSURE - InstructionArgument backlink = identTableEntry.getBacklink(); - final String text = identTableEntry.getIdent().getText(); - if (backlink == null) { - if (identTableEntry.getResolvedElement() instanceof final VariableStatement vs) { - final OS_Element parent = vs.getParent().getParent(); - if (parent != gf.getFD()) { - // we want identTableEntry.resolved which will be a GeneratedMember - // which will have a container which will be either be a function, - // statement (semantic block, loop, match, etc) or a GeneratedContainerNC - final int y = 2; - final GeneratedNode er = identTableEntry.externalRef; - if (er instanceof final GeneratedContainerNC nc) { - assert nc instanceof GeneratedNamespace; - final GeneratedNamespace ns = (GeneratedNamespace) nc; + final String realTargetName = getRealTargetName((IntegerIA) ia, Generate_Code_For_Method.AOG.GET); + sl3.add(Emit.emit("/*669*/") + realTargetName); + } else if (ia instanceof IdentIA) { + final CReference reference = new CReference(); + reference.getIdentIAPath((IdentIA) ia, Generate_Code_For_Method.AOG.GET, null); + final String text = reference.build(); + sl3.add(Emit.emit("/*673*/") + text); + } else if (ia instanceof final ConstTableIA c) { + final ConstantTableEntry cte = c.getEntry(); + final String s = GetAssignmentValue.const_to_string(cte.initialValue); + sl3.add(s); + final int y = 2; + } else if (ia instanceof ProcIA) { + LOG.err("740 ProcIA"); + throw new NotImplementedException(); + } else { + LOG.err(ia.getClass().getName()); + throw new IllegalStateException("Invalid InstructionArgument"); + } + } + return sl3; + } + + String getRealTargetName(final @NotNull IntegerIA target, final Generate_Code_For_Method.AOG aog) { + final BaseGeneratedFunction gf = target.gf; + final VariableTableEntry varTableEntry = gf.getVarTableEntry(target.getIndex()); + return getRealTargetName(gf, varTableEntry); + } + + String getRealTargetName(final @NotNull BaseGeneratedFunction gf, final @NotNull IdentIA target, + final Generate_Code_For_Method.AOG aog, final String value) { + int state = 0, code = -1; + final IdentTableEntry identTableEntry = gf.getIdentTableEntry(target.getIndex()); + final LinkedList ls = new LinkedList(); + // TODO in Deduce set property lookupType to denote what type of lookup it is: + // MEMBER, LOCAL, or CLOSURE + InstructionArgument backlink = identTableEntry.getBacklink(); + final String text = identTableEntry.getIdent().getText(); + if (backlink == null) { + if (identTableEntry.getResolvedElement() instanceof final VariableStatement vs) { + final OS_Element parent = vs.getParent().getParent(); + if (parent != gf.getFD()) { + // we want identTableEntry.resolved which will be a GeneratedMember + // which will have a container which will be either be a function, + // statement (semantic block, loop, match, etc) or a GeneratedContainerNC + final int y = 2; + final GeneratedNode er = identTableEntry.externalRef; + if (er instanceof final GeneratedContainerNC nc) { + assert nc instanceof GeneratedNamespace; + final GeneratedNamespace ns = (GeneratedNamespace) nc; // if (ns.isInstance()) {} - state = 1; - code = nc.getCode(); - } - } - } - switch (state) { - case 0: - ls.add(Emit.emit("/*912*/") + "vsc->vm" + text); // TODO blindly adding "vm" might not always work, also - // put in loop - break; - case 1: - ls.add(Emit.emit("/*845*/") + String.format("zNZ%d_instance->vm%s", code, text)); - break; - default: - throw new IllegalStateException("Can't be here"); - } - } else - ls.add(Emit.emit("/*872*/") + "vm" + text); // TODO blindly adding "vm" might not always work, also put in - // loop - while (backlink != null) { - if (backlink instanceof final IntegerIA integerIA) { - final String realTargetName = getRealTargetName(gf, integerIA, Generate_Code_For_Method.AOG.ASSIGN); - ls.addFirst(Emit.emit("/*892*/") + realTargetName); - backlink = null; - } else if (backlink instanceof final IdentIA identIA) { - final int identIAIndex = identIA.getIndex(); - final IdentTableEntry identTableEntry1 = gf.getIdentTableEntry(identIAIndex); - final String identTableEntryName = identTableEntry1.getIdent().getText(); - ls.addFirst(Emit.emit("/*885*/") + "vm" + identTableEntryName); // TODO blindly adding "vm" might not - // always be right - backlink = identTableEntry1.getBacklink(); - } else - throw new IllegalStateException("Invalid InstructionArgument for backlink"); - } - final CReference reference = new CReference(); - reference.getIdentIAPath(target, aog, value); - final String path = reference.build(); - LOG.info("932 " + path); - final String s = Helpers.String_join("->", ls); - LOG.info("933 " + s); - if (identTableEntry.getResolvedElement() instanceof ConstructorDef - || identTableEntry.getResolvedElement() instanceof PropertyStatement || value != null) - return path; - else - return s; - } - - static class WlGenerateFunctionC implements WorkJob { - - private final BaseGeneratedFunction gf; - private final GenerateResult gr; - private final WorkList wl; - private final GenerateC generateC; - private boolean _isDone = false; - - public WlGenerateFunctionC(final BaseGeneratedFunction aGf, final GenerateResult aGr, final WorkList aWl, - final GenerateC aGenerateC) { - gf = aGf; - gr = aGr; - wl = aWl; - generateC = aGenerateC; - } - - @Override - public void run(final WorkManager aWorkManager) { - if (gf instanceof GeneratedFunction) - generateC.generate_function((GeneratedFunction) gf, gr, wl); - else - generateC.generate_constructor((GeneratedConstructor) gf, gr, wl); - _isDone = true; - } - - @Override - public boolean isDone() { - return _isDone; - } - } - - static class GetTypeName { - static String forVTE(@NotNull final VariableTableEntry input) { - final OS_Type attached = input.type.getAttached(); - if (attached == null) - return Emit.emit("/*390*/") + "Z__Unresolved*"; // TODO remove this ASAP - // - // special case - // - if (input.type.genType.getNode() != null) - return Emit.emit("/*395*/") + getTypeNameForGenClass(input.type.genType.getNode()) + "*"; - // - if (input.getStatus() == BaseTableEntry.Status.UNCHECKED) - return "Error_UNCHECKED_Type"; - switch (attached.getType()) { - case USER_CLASS: - return attached.getClassOf().name(); - case USER: - final TypeName typeName = attached.getTypeName(); - final String name; - if (typeName instanceof NormalTypeName) - name = ((NormalTypeName) typeName).getName(); - else - name = typeName.toString(); - return String.format(Emit.emit("/*543*/") + "Z<%s>*", name); - default: - throw new NotImplementedException(); - } - } - - static String getTypeNameForGenClass(@NotNull final GeneratedNode aGenClass) { - final String ty; - if (aGenClass instanceof GeneratedClass) - ty = forGenClass((GeneratedClass) aGenClass); - else if (aGenClass instanceof GeneratedNamespace) - ty = forGenNamespace((GeneratedNamespace) aGenClass); - else - ty = "Error_Unknown_GenClass"; - return ty; - } - - static String forGenClass(@NotNull final GeneratedClass aGeneratedClass) { - final String z; - z = String.format("Z%d", aGeneratedClass.getCode()); - return z; - } - - static String forGenNamespace(@NotNull final GeneratedNamespace aGeneratedNamespace) { - final String z; - z = String.format("Z%d", aGeneratedNamespace.getCode()); - return z; - } - - static @NotNull String forTypeTableEntry(@NotNull final TypeTableEntry tte) { - final GeneratedNode res = tte.resolved(); - if (res instanceof final GeneratedContainerNC nc) { - final int code = nc.getCode(); - return "Z" + code; - } else - return "Z<-1>"; - } - - @Deprecated - static String forOSType(final @NotNull OS_Type ty, final ElLog LOG) { - if (ty == null) - throw new IllegalArgumentException("ty is null"); - // - final String z; - switch (ty.getType()) { - case USER_CLASS: - final ClassStatement el = ty.getClassOf(); - final String name; - if (ty instanceof NormalTypeName) - name = ((NormalTypeName) ty).getName(); - else - name = el.getName(); - z = Emit.emit("/*443*/") + String.format("Z%d/*%s*/", el._a.getCode(), name);// .getName(); - break; - case FUNCTION: - z = ""; - break; - case FUNC_EXPR: { - z = ""; - final OS_FuncExprType fe = (OS_FuncExprType) ty; - final int y = 2; - } - break; - case USER: - final TypeName typeName = ty.getTypeName(); - LOG.err("Warning: USER TypeName in GenerateC " + typeName); - final String s = typeName.toString(); - if (s.equals("Unit")) - z = "void"; - else - z = String.format("Z", s); - break; - case BUILT_IN: - LOG.err("Warning: BUILT_IN TypeName in GenerateC"); - z = "Z" + ty.getBType().getCode(); // README should not even be here, but look at .name() for other code - // gen schemes - break; - case UNIT_TYPE: - z = "void"; - break; - default: - throw new IllegalStateException("Unexpected value: " + ty.getType()); - } - return z; - } - - @Deprecated - static String forTypeName(final @NotNull TypeName typeName, final @NotNull ErrSink errSink) { - if (typeName instanceof RegularTypeName) { - final String name = ((RegularTypeName) typeName).getName(); // TODO convert to Z-name - - return String.format("Z<%s>/*kklkl*/", name); + state = 1; + code = nc.getCode(); + } + } + } + switch (state) { + case 0: + ls.add(Emit.emit("/*912*/") + "vsc->vm" + text); // TODO blindly adding "vm" might not always work, also + // put in loop + break; + case 1: + ls.add(Emit.emit("/*845*/") + String.format("zNZ%d_instance->vm%s", code, text)); + break; + default: + throw new IllegalStateException("Can't be here"); + } + } else + ls.add(Emit.emit("/*872*/") + "vm" + text); // TODO blindly adding "vm" might not always work, also put in + // loop + while (backlink != null) { + if (backlink instanceof final IntegerIA integerIA) { + final String realTargetName = getRealTargetName(gf, integerIA, Generate_Code_For_Method.AOG.ASSIGN); + ls.addFirst(Emit.emit("/*892*/") + realTargetName); + backlink = null; + } else if (backlink instanceof final IdentIA identIA) { + final int identIAIndex = identIA.getIndex(); + final IdentTableEntry identTableEntry1 = gf.getIdentTableEntry(identIAIndex); + final String identTableEntryName = identTableEntry1.getIdent().getText(); + ls.addFirst(Emit.emit("/*885*/") + "vm" + identTableEntryName); // TODO blindly adding "vm" might not + // always be right + backlink = identTableEntry1.getBacklink(); + } else + throw new IllegalStateException("Invalid InstructionArgument for backlink"); + } + final CReference reference = new CReference(); + reference.getIdentIAPath(target, aog, value); + final String path = reference.build(); + LOG.info("932 " + path); + final String s = Helpers.String_join("->", ls); + LOG.info("933 " + s); + if (identTableEntry.getResolvedElement() instanceof ConstructorDef + || identTableEntry.getResolvedElement() instanceof PropertyStatement || value != null) + return path; + else + return s; + } + + static class WlGenerateFunctionC implements WorkJob { + + private final BaseGeneratedFunction gf; + private final GenerateResult gr; + private final WorkList wl; + private final GenerateC generateC; + private boolean _isDone = false; + + public WlGenerateFunctionC(final BaseGeneratedFunction aGf, final GenerateResult aGr, final WorkList aWl, + final GenerateC aGenerateC) { + gf = aGf; + gr = aGr; + wl = aWl; + generateC = aGenerateC; + } + + @Override + public void run(final WorkManager aWorkManager) { + if (gf instanceof GeneratedFunction) + generateC.generate_function((GeneratedFunction) gf, gr, wl); + else + generateC.generate_constructor((GeneratedConstructor) gf, gr, wl); + _isDone = true; + } + + @Override + public boolean isDone() { + return _isDone; + } + } + + static class GetTypeName { + static String forVTE(@NotNull final VariableTableEntry input) { + final OS_Type attached = input.type.getAttached(); + if (attached == null) + return Emit.emit("/*390*/") + "Z__Unresolved*"; // TODO remove this ASAP + // + // special case + // + if (input.type.genType.getNode() != null) + return Emit.emit("/*395*/") + getTypeNameForGenClass(input.type.genType.getNode()) + "*"; + // + if (input.getStatus() == BaseTableEntry.Status.UNCHECKED) + return "Error_UNCHECKED_Type"; + switch (attached.getType()) { + case USER_CLASS: + return attached.getClassOf().name(); + case USER: + final TypeName typeName = attached.getTypeName(); + final String name; + if (typeName instanceof NormalTypeName) + name = ((NormalTypeName) typeName).getName(); + else + name = typeName.toString(); + return String.format(Emit.emit("/*543*/") + "Z<%s>*", name); + default: + throw new NotImplementedException(); + } + } + + static String getTypeNameForGenClass(@NotNull final GeneratedNode aGenClass) { + final String ty; + if (aGenClass instanceof GeneratedClass) + ty = forGenClass((GeneratedClass) aGenClass); + else if (aGenClass instanceof GeneratedNamespace) + ty = forGenNamespace((GeneratedNamespace) aGenClass); + else + ty = "Error_Unknown_GenClass"; + return ty; + } + + static String forGenClass(@NotNull final GeneratedClass aGeneratedClass) { + final String z; + z = String.format("Z%d", aGeneratedClass.getCode()); + return z; + } + + static String forGenNamespace(@NotNull final GeneratedNamespace aGeneratedNamespace) { + final String z; + z = String.format("Z%d", aGeneratedNamespace.getCode()); + return z; + } + + static @NotNull String forTypeTableEntry(@NotNull final TypeTableEntry tte) { + final GeneratedNode res = tte.resolved(); + if (res instanceof final GeneratedContainerNC nc) { + final int code = nc.getCode(); + return "Z" + code; + } else + return "Z<-1>"; + } + + @Deprecated + static String forOSType(final @NotNull OS_Type ty, final ElLog LOG) { + if (ty == null) + throw new IllegalArgumentException("ty is null"); + // + final String z; + switch (ty.getType()) { + case USER_CLASS: + final ClassStatement el = ty.getClassOf(); + final String name; + if (ty instanceof NormalTypeName) + name = ((NormalTypeName) ty).getName(); + else + name = el.getName(); + z = Emit.emit("/*443*/") + String.format("Z%d/*%s*/", el._a.getCode(), name);// .getName(); + break; + case FUNCTION: + z = ""; + break; + case FUNC_EXPR: { + z = ""; + final OS_FuncExprType fe = (OS_FuncExprType) ty; + final int y = 2; + } + break; + case USER: + final TypeName typeName = ty.getTypeName(); + LOG.err("Warning: USER TypeName in GenerateC " + typeName); + final String s = typeName.toString(); + if (s.equals("Unit")) + z = "void"; + else + z = String.format("Z", s); + break; + case BUILT_IN: + LOG.err("Warning: BUILT_IN TypeName in GenerateC"); + z = "Z" + ty.getBType().getCode(); // README should not even be here, but look at .name() for other code + // gen schemes + break; + case UNIT_TYPE: + z = "void"; + break; + default: + throw new IllegalStateException("Unexpected value: " + ty.getType()); + } + return z; + } + + @Deprecated + static String forTypeName(final @NotNull TypeName typeName, final @NotNull ErrSink errSink) { + if (typeName instanceof RegularTypeName) { + final String name = ((RegularTypeName) typeName).getName(); // TODO convert to Z-name + + return String.format("Z<%s>/*kklkl*/", name); // return getTypeName(new OS_Type(typeName)); - } - errSink.reportError("Type is not fully deduced " + typeName); - return String.format("Z<%s>/*kllkk*/", typeName); // TODO type is not fully deduced - } - } + } + errSink.reportError("Type is not fully deduced " + typeName); + return String.format("Z<%s>/*kllkk*/", typeName); // TODO type is not fully deduced + } + } // @NotNull List getArgumentStrings(final BaseGeneratedFunction gf, final Instruction instruction) { // return getArgumentStrings(gf, () -> new InstructionFixedList(instruction)); // } - static class GetAssignmentValue { - - private static String const_to_string(final IExpression expression) { - if (expression instanceof NumericExpression) { - return String.valueOf(((NumericExpression) expression).getValue()); - } - if (expression instanceof CharLitExpression) { - return String.format("'%s'", expression); - } - if (expression instanceof StringExpression) { - // TODO triple quoted strings and other escaping concerns - return String.format("\"%s\"", ((StringExpression) expression).getText()); - } - - // FloatLitExpression - // BooleanExpression - throw new NotImplementedException(); - } - - public String FnCallArgs(final @NotNull FnCallArgs fca, final @NotNull BaseGeneratedFunction gf, - final ElLog LOG) { - final StringBuilder sb = new StringBuilder(); - final Instruction inst = fca.getExpression(); + static class GetAssignmentValue { + + private static String const_to_string(final IExpression expression) { + if (expression instanceof NumericExpression) { + return String.valueOf(((NumericExpression) expression).getValue()); + } + if (expression instanceof CharLitExpression) { + return String.format("'%s'", expression); + } + if (expression instanceof StringExpression) { + // TODO triple quoted strings and other escaping concerns + return String.format("\"%s\"", ((StringExpression) expression).getText()); + } + + // FloatLitExpression + // BooleanExpression + throw new NotImplementedException(); + } + + public String FnCallArgs(final @NotNull FnCallArgs fca, final @NotNull BaseGeneratedFunction gf, + final ElLog LOG) { + final StringBuilder sb = new StringBuilder(); + final Instruction inst = fca.getExpression(); // LOG.err("9000 "+inst.getName()); - final InstructionArgument x = inst.getArg(0); - assert x instanceof ProcIA; - final ProcTableEntry pte = gf.getProcTableEntry(to_int(x)); + final InstructionArgument x = inst.getArg(0); + assert x instanceof ProcIA; + final ProcTableEntry pte = gf.getProcTableEntry(to_int(x)); // LOG.err("9000-2 "+pte); - switch (inst.getName()) { - case CALL: { - if (pte.expression_num == null) { + switch (inst.getName()) { + case CALL: { + if (pte.expression_num == null) { // assert false; // TODO synthetic methods - final IdentExpression ptex = (IdentExpression) pte.expression; - sb.append(ptex.getText()); - sb.append(Emit.emit("/*671*/") + "("); - - final List sll = getAssignmentValueArgs(inst, gf, LOG); - sb.append(Helpers.String_join(", ", sll)); - - sb.append(")"); - } else { - final IdentIA ia2 = (IdentIA) pte.expression_num; - final IdentTableEntry idte = ia2.getEntry(); - if (idte.getStatus() == BaseTableEntry.Status.KNOWN) { - final CReference reference = new CReference(); - final FunctionInvocation functionInvocation = pte.getFunctionInvocation(); - if (functionInvocation == null - || functionInvocation.getFunction() == ConstructorDef.defaultVirtualCtor) { - reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); - final List sll = getAssignmentValueArgs(inst, gf, LOG); - reference.args(sll); - final String path = reference.build(); - sb.append(Emit.emit("/*829*/") + path); - } else { - final BaseGeneratedFunction pte_generated = functionInvocation.getGenerated(); - if (idte.resolvedType() == null && pte_generated != null) - idte.resolveTypeToClass(pte_generated); - reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); - final List sll = getAssignmentValueArgs(inst, gf, LOG); - reference.args(sll); - final String path = reference.build(); - sb.append(Emit.emit("/*827*/") + path); - } - } else { - final String path = gf.getIdentIAPathNormal(ia2); - sb.append(Emit.emit("/*828*/") + String.format("%s is UNKNOWN", path)); - } - } - return sb.toString(); - } - case CALLS: { - CReference reference = null; - if (pte.expression_num == null) { - final int y = 2; - final IdentExpression ptex = (IdentExpression) pte.expression; - sb.append(Emit.emit("/*684*/")); - sb.append(ptex.getText()); - } else { - // TODO Why not expression_num? - reference = new CReference(); - final IdentIA ia2 = (IdentIA) pte.expression_num; - reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); - final List sll = getAssignmentValueArgs(inst, gf, LOG); - reference.args(sll); - final String path = reference.build(); - sb.append(Emit.emit("/*807*/") + path); - - final IExpression ptex = pte.expression; - if (ptex instanceof IdentExpression) { - sb.append(Emit.emit("/*803*/")); - sb.append(((IdentExpression) ptex).getText()); - } else if (ptex instanceof ProcedureCallExpression) { - sb.append(Emit.emit("/*806*/")); - sb.append(ptex.getLeft()); // TODO Qualident, IdentExpression, DotExpression - } - } - if (true /* reference == null */) { - sb.append(Emit.emit("/*810*/") + "("); - { - final List sll = getAssignmentValueArgs(inst, gf, LOG); - sb.append(Helpers.String_join(", ", sll)); - } - sb.append(");"); - } - return sb.toString(); - } - default: - throw new IllegalStateException("Unexpected value: " + inst.getName()); - } - } - - @NotNull - private List getAssignmentValueArgs(final @NotNull Instruction inst, final BaseGeneratedFunction gf, - final ElLog LOG) { - final int args_size = inst.getArgsSize(); - final List sll = new ArrayList(); - for (int i = 1; i < args_size; i++) { - final InstructionArgument ia = inst.getArg(i); - final int y = 2; + final IdentExpression ptex = (IdentExpression) pte.expression; + sb.append(ptex.getText()); + sb.append(Emit.emit("/*671*/") + "("); + + final List sll = getAssignmentValueArgs(inst, gf, LOG); + sb.append(Helpers.String_join(", ", sll)); + + sb.append(")"); + } else { + final IdentIA ia2 = (IdentIA) pte.expression_num; + final IdentTableEntry idte = ia2.getEntry(); + if (idte.getStatus() == BaseTableEntry.Status.KNOWN) { + final CReference reference = new CReference(); + final FunctionInvocation functionInvocation = pte.getFunctionInvocation(); + if (functionInvocation == null + || functionInvocation.getFunction() == ConstructorDef.defaultVirtualCtor) { + reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); + final List sll = getAssignmentValueArgs(inst, gf, LOG); + reference.args(sll); + final String path = reference.build(); + sb.append(Emit.emit("/*829*/") + path); + } else { + final BaseGeneratedFunction pte_generated = functionInvocation.getGenerated(); + if (idte.resolvedType() == null && pte_generated != null) + idte.resolveTypeToClass(pte_generated); + reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); + final List sll = getAssignmentValueArgs(inst, gf, LOG); + reference.args(sll); + final String path = reference.build(); + sb.append(Emit.emit("/*827*/") + path); + } + } else { + final String path = gf.getIdentIAPathNormal(ia2); + sb.append(Emit.emit("/*828*/") + String.format("%s is UNKNOWN", path)); + } + } + return sb.toString(); + } + case CALLS: { + CReference reference = null; + if (pte.expression_num == null) { + final int y = 2; + final IdentExpression ptex = (IdentExpression) pte.expression; + sb.append(Emit.emit("/*684*/")); + sb.append(ptex.getText()); + } else { + // TODO Why not expression_num? + reference = new CReference(); + final IdentIA ia2 = (IdentIA) pte.expression_num; + reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); + final List sll = getAssignmentValueArgs(inst, gf, LOG); + reference.args(sll); + final String path = reference.build(); + sb.append(Emit.emit("/*807*/") + path); + + final IExpression ptex = pte.expression; + if (ptex instanceof IdentExpression) { + sb.append(Emit.emit("/*803*/")); + sb.append(((IdentExpression) ptex).getText()); + } else if (ptex instanceof ProcedureCallExpression) { + sb.append(Emit.emit("/*806*/")); + sb.append(ptex.getLeft()); // TODO Qualident, IdentExpression, DotExpression + } + } + if (true /* reference == null */) { + sb.append(Emit.emit("/*810*/") + "("); + { + final List sll = getAssignmentValueArgs(inst, gf, LOG); + sb.append(Helpers.String_join(", ", sll)); + } + sb.append(");"); + } + return sb.toString(); + } + default: + throw new IllegalStateException("Unexpected value: " + inst.getName()); + } + } + + @NotNull + private List getAssignmentValueArgs(final @NotNull Instruction inst, final BaseGeneratedFunction gf, + final ElLog LOG) { + final int args_size = inst.getArgsSize(); + final List sll = new ArrayList(); + for (int i = 1; i < args_size; i++) { + final InstructionArgument ia = inst.getArg(i); + final int y = 2; // LOG.err("7777 " +ia); - if (ia instanceof ConstTableIA) { - final ConstantTableEntry constTableEntry = gf.getConstTableEntry(((ConstTableIA) ia).getIndex()); - sll.add(const_to_string(constTableEntry.initialValue)); - } else if (ia instanceof IntegerIA) { - final VariableTableEntry variableTableEntry = gf.getVarTableEntry(((IntegerIA) ia).getIndex()); - sll.add(Emit.emit("/*853*/") + getRealTargetName(gf, variableTableEntry)); - } else if (ia instanceof IdentIA) { - final String path = gf.getIdentIAPathNormal((IdentIA) ia); // return x.y.z - final IdentTableEntry ite = gf.getIdentTableEntry(to_int(ia)); - if (ite.getStatus() == BaseTableEntry.Status.UNKNOWN) { - sll.add(String.format("%s is UNKNOWN", path)); - } else { - final CReference reference = new CReference(); - reference.getIdentIAPath((IdentIA) ia, Generate_Code_For_Method.AOG.GET, null); - final String path2 = reference.build(); // return ZP105get_z(vvx.vmy) - if (path.equals(path2)) { - // should always fail - // throw new AssertionError(); - LOG.err(String.format("864 should always fail but didn't %s %s", path, path2)); - } + if (ia instanceof ConstTableIA) { + final ConstantTableEntry constTableEntry = gf.getConstTableEntry(((ConstTableIA) ia).getIndex()); + sll.add(const_to_string(constTableEntry.initialValue)); + } else if (ia instanceof IntegerIA) { + final VariableTableEntry variableTableEntry = gf.getVarTableEntry(((IntegerIA) ia).getIndex()); + sll.add(Emit.emit("/*853*/") + getRealTargetName(gf, variableTableEntry)); + } else if (ia instanceof IdentIA) { + final String path = gf.getIdentIAPathNormal((IdentIA) ia); // return x.y.z + final IdentTableEntry ite = gf.getIdentTableEntry(to_int(ia)); + if (ite.getStatus() == BaseTableEntry.Status.UNKNOWN) { + sll.add(String.format("%s is UNKNOWN", path)); + } else { + final CReference reference = new CReference(); + reference.getIdentIAPath((IdentIA) ia, Generate_Code_For_Method.AOG.GET, null); + final String path2 = reference.build(); // return ZP105get_z(vvx.vmy) + if (path.equals(path2)) { + // should always fail + // throw new AssertionError(); + LOG.err(String.format("864 should always fail but didn't %s %s", path, path2)); + } // assert ident != null; // IdentTableEntry ite = gf.getIdentTableEntry(((IdentIA) ia).getIndex()); // sll.add(Emit.emit("/*748*/")+""+ite.getIdent().getText()); - sll.add(Emit.emit("/*748*/") + path2); - LOG.info("743 " + path2 + " " + path); - } - } else if (ia instanceof ProcIA) { - LOG.err("863 ProcIA"); - throw new NotImplementedException(); - } else { - throw new IllegalStateException("Cant be here: Invalid InstructionArgument"); - } - } - return sll; - } - - public String ConstTableIA(final @NotNull ConstTableIA constTableIA, final @NotNull BaseGeneratedFunction gf) { - final ConstantTableEntry cte = gf.getConstTableEntry(constTableIA.getIndex()); + sll.add(Emit.emit("/*748*/") + path2); + LOG.info("743 " + path2 + " " + path); + } + } else if (ia instanceof ProcIA) { + LOG.err("863 ProcIA"); + throw new NotImplementedException(); + } else { + throw new IllegalStateException("Cant be here: Invalid InstructionArgument"); + } + } + return sll; + } + + public String ConstTableIA(final @NotNull ConstTableIA constTableIA, final @NotNull BaseGeneratedFunction gf) { + final ConstantTableEntry cte = gf.getConstTableEntry(constTableIA.getIndex()); // LOG.err(("9001-3 "+cte.initialValue)); - switch (cte.initialValue.getKind()) { - case NUMERIC: - return const_to_string(cte.initialValue); - case STRING_LITERAL: - return const_to_string(cte.initialValue); - case IDENT: - final String text = ((IdentExpression) cte.initialValue).getText(); - if (BuiltInTypes.isBooleanText(text)) - return text; - else - throw new NotImplementedException(); - default: - throw new NotImplementedException(); - } - } - - public String IntegerIA(final @NotNull IntegerIA integerIA, final @NotNull BaseGeneratedFunction gf) { - final VariableTableEntry vte = gf.getVarTableEntry(integerIA.getIndex()); - final String x = getRealTargetName(gf, vte); - return x; - } - - public String IdentIA(final @NotNull IdentIA identIA, final BaseGeneratedFunction gf) { - assert gf == identIA.gf; - final CReference reference = new CReference(); - reference.getIdentIAPath(identIA, Generate_Code_For_Method.AOG.GET, null); - return reference.build(); - } - - public String forClassInvocation(final @NotNull Instruction aInstruction, final ClassInvocation aClsinv, - final @NotNull BaseGeneratedFunction gf, final ElLog LOG) { - final int y = 2; - final InstructionArgument _arg0 = aInstruction.getArg(0); - @NotNull - final ProcTableEntry pte = gf.getProcTableEntry(((ProcIA) _arg0).getIndex()); - final CtorReference reference = new CtorReference(); - reference.getConstructorPath(pte.expression_num, gf); - @NotNull - final List x = getAssignmentValueArgs(aInstruction, gf, LOG); - reference.args(x); - return reference.build(aClsinv); - } - } + switch (cte.initialValue.getKind()) { + case NUMERIC: + return const_to_string(cte.initialValue); + case STRING_LITERAL: + return const_to_string(cte.initialValue); + case IDENT: + final String text = ((IdentExpression) cte.initialValue).getText(); + if (BuiltInTypes.isBooleanText(text)) + return text; + else + throw new NotImplementedException(); + default: + throw new NotImplementedException(); + } + } + + public String IntegerIA(final @NotNull IntegerIA integerIA, final @NotNull BaseGeneratedFunction gf) { + final VariableTableEntry vte = gf.getVarTableEntry(integerIA.getIndex()); + final String x = getRealTargetName(gf, vte); + return x; + } + + public String IdentIA(final @NotNull IdentIA identIA, final BaseGeneratedFunction gf) { + assert gf == identIA.gf; + final CReference reference = new CReference(); + reference.getIdentIAPath(identIA, Generate_Code_For_Method.AOG.GET, null); + return reference.build(); + } + + public String forClassInvocation(final @NotNull Instruction aInstruction, final ClassInvocation aClsinv, + final @NotNull BaseGeneratedFunction gf, final ElLog LOG) { + final int y = 2; + final InstructionArgument _arg0 = aInstruction.getArg(0); + @NotNull final ProcTableEntry pte = gf.getProcTableEntry(((ProcIA) _arg0).getIndex()); + final CtorReference reference = new CtorReference(); + reference.getConstructorPath(pte.expression_num, gf); + @NotNull final List x = getAssignmentValueArgs(aInstruction, gf, LOG); + reference.args(x); + return reference.build(aClsinv); + } + } } // From e352ce66036cafbc784e3943deb2c1e6bace6d25 Mon Sep 17 00:00:00 2001 From: Tripleo Date: Sun, 22 Sep 2024 17:27:28 -0400 Subject: [PATCH 2/7] Add Gern* --- .../tripleo/elijah/stages/gen_c/GernNode.java | 25 ++++++++ .../elijah/stages/gen_c/GernNodeClass.java | 15 +++++ .../stages/gen_c/GernNodeConstructor.java | 14 +++++ .../stages/gen_c/GernNodeContainer.java | 62 +++++++++++++++++++ .../elijah/stages/gen_c/GernNodeFunction.java | 14 +++++ .../stages/gen_c/GernNodeNamespace.java | 16 +++++ .../tripleo/elijah/stages/gen_c/Gerns.java | 8 +++ 7 files changed, 154 insertions(+) create mode 100644 src/main/java/tripleo/elijah/stages/gen_c/GernNode.java create mode 100644 src/main/java/tripleo/elijah/stages/gen_c/GernNodeClass.java create mode 100644 src/main/java/tripleo/elijah/stages/gen_c/GernNodeConstructor.java create mode 100644 src/main/java/tripleo/elijah/stages/gen_c/GernNodeContainer.java create mode 100644 src/main/java/tripleo/elijah/stages/gen_c/GernNodeFunction.java create mode 100644 src/main/java/tripleo/elijah/stages/gen_c/GernNodeNamespace.java create mode 100644 src/main/java/tripleo/elijah/stages/gen_c/Gerns.java diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GernNode.java b/src/main/java/tripleo/elijah/stages/gen_c/GernNode.java new file mode 100644 index 0000000..58fa4d4 --- /dev/null +++ b/src/main/java/tripleo/elijah/stages/gen_c/GernNode.java @@ -0,0 +1,25 @@ +package tripleo.elijah.stages.gen_c; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import tripleo.elijah.stages.gen_fn.*; + +public interface GernNode { + @NotNull + GeneratedNode carrier(); + + @Nullable + GeneratedFunction asFunction(); + + @Nullable + GeneratedContainerNC asContainer(); + + @Nullable + GeneratedClass asClass(); + + @Nullable + GeneratedNamespace asNamespace(); + + @Nullable + GeneratedConstructor asConstructor(); +} diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GernNodeClass.java b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeClass.java new file mode 100644 index 0000000..d83fdcf --- /dev/null +++ b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeClass.java @@ -0,0 +1,15 @@ +package tripleo.elijah.stages.gen_c; + +import tripleo.elijah.stages.gen_fn.GeneratedClass; +import tripleo.elijah.stages.gen_fn.GeneratedNode; + +class GernNodeClass extends AbstractGernNode { + public GernNodeClass(final GeneratedNode aGeneratedNode) { + setNode(aGeneratedNode); + } + + @Override + public GeneratedClass asClass() { + return (GeneratedClass) generatedNode; + } +} diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GernNodeConstructor.java b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeConstructor.java new file mode 100644 index 0000000..9bbd0bf --- /dev/null +++ b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeConstructor.java @@ -0,0 +1,14 @@ +package tripleo.elijah.stages.gen_c; + +import tripleo.elijah.stages.gen_fn.GeneratedConstructor; +import tripleo.elijah.stages.gen_fn.GeneratedNode; + +class GernNodeConstructor extends AbstractGernNode { + public GernNodeConstructor(final GeneratedNode aGeneratedNode) { + setNode(aGeneratedNode); + } + + public GeneratedConstructor asConstructor() { + return (GeneratedConstructor) generatedNode; + } +} diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GernNodeContainer.java b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeContainer.java new file mode 100644 index 0000000..1c77afd --- /dev/null +++ b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeContainer.java @@ -0,0 +1,62 @@ +package tripleo.elijah.stages.gen_c; + +import com.google.common.base.Preconditions; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import tripleo.elijah.stages.gen_fn.GeneratedClass; +import tripleo.elijah.stages.gen_fn.GeneratedContainerNC; +import tripleo.elijah.stages.gen_fn.GeneratedFunction; +import tripleo.elijah.stages.gen_fn.GeneratedNode; +import tripleo.elijah.stages.gen_generic.GenerateResult; +import tripleo.elijah.work.WorkManager; + +import java.util.ArrayList; +import java.util.Collection; + +class GernNodeContainer extends AbstractGernNode { + public GernNodeContainer(final GeneratedNode aGeneratedNode) { + setNode(aGeneratedNode); + } + + @Override + public @Nullable GeneratedContainerNC asContainer() { + return (GeneratedContainerNC) generatedNode; + } + + public void generateCode(final GenerateC aGenerateC, final GenerateResult aGenerateResult) { + final GeneratedContainerNC container = asContainer(); + Preconditions.checkNotNull(container); + container.generateCode(aGenerateC, aGenerateResult); + } + + GenerateResult generateCodeForFunctions(final WorkManager wm, + final @NotNull GeneratedContainerNC nc, + final GenerateC ggc, + final GenerateC aGenerateC) { + if (ggc != aGenerateC) { + throw new AssertionError(); + } + + final Collection values = nc.functionMap.values(); + final @NotNull Collection gn1 = new ArrayList<>(); + for (GeneratedFunction node : values) { + //noinspection UseBulkOperation + gn1.add(node); + } + //noinspection UnnecessaryLocalVariable + final GenerateResult gr3 = aGenerateC.gern(gn1).generateCode(ggc, wm); + return gr3; + } + + GenerateResult generateCodeForContainers(final WorkManager wm, final @NotNull GeneratedContainerNC nc, final GenerateC ggc, final GenerateC aGenerateC) { + final Collection values = nc.classMap.values(); + final @NotNull Collection gn2 = new ArrayList<>(); + for (GeneratedClass node : values) { + //noinspection UseBulkOperation + gn2.add(node); + } + //noinspection UnnecessaryLocalVariable + final GenerateResult gr4 = aGenerateC.gern(gn2).generateCode(ggc, wm); + return gr4; + } +} diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GernNodeFunction.java b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeFunction.java new file mode 100644 index 0000000..060bde9 --- /dev/null +++ b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeFunction.java @@ -0,0 +1,14 @@ +package tripleo.elijah.stages.gen_c; + +import tripleo.elijah.stages.gen_fn.GeneratedFunction; +import tripleo.elijah.stages.gen_fn.GeneratedNode; + +class GernNodeFunction extends AbstractGernNode { + public GernNodeFunction(final GeneratedNode aGeneratedNode) { + setNode(aGeneratedNode); + } + + public GeneratedFunction asFunction() { + return (GeneratedFunction) generatedNode; + } +} diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GernNodeNamespace.java b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeNamespace.java new file mode 100644 index 0000000..6b30ffc --- /dev/null +++ b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeNamespace.java @@ -0,0 +1,16 @@ +package tripleo.elijah.stages.gen_c; + +import org.jetbrains.annotations.Nullable; +import tripleo.elijah.stages.gen_fn.GeneratedNamespace; +import tripleo.elijah.stages.gen_fn.GeneratedNode; + +class GernNodeNamespace extends AbstractGernNode { +public GernNodeNamespace(final GeneratedNode aGeneratedNode) { + setNode(aGeneratedNode); +} + +@Override +public @Nullable GeneratedNamespace asNamespace() { + return (GeneratedNamespace) generatedNode; +} +} diff --git a/src/main/java/tripleo/elijah/stages/gen_c/Gerns.java b/src/main/java/tripleo/elijah/stages/gen_c/Gerns.java new file mode 100644 index 0000000..c6d88ee --- /dev/null +++ b/src/main/java/tripleo/elijah/stages/gen_c/Gerns.java @@ -0,0 +1,8 @@ +package tripleo.elijah.stages.gen_c; + +import tripleo.elijah.stages.gen_generic.GenerateResult; +import tripleo.elijah.work.WorkManager; + +public interface Gerns extends Iterable { + GenerateResult generateCode(GenerateC aGgc, WorkManager aWm); +} From 8e9419fa6c7d0ac6f7d6b31d4cb37309bcb1e6b2 Mon Sep 17 00:00:00 2001 From: Tripleo Date: Sun, 22 Sep 2024 17:29:14 -0400 Subject: [PATCH 3/7] Add Gern* (II) --- .../elijah/stages/gen_c/AbstractGernNode.java | 52 +++++++++++++++++++ .../elijah_fluffy/anno/ElLateInit.java | 4 ++ 2 files changed, 56 insertions(+) create mode 100644 src/main/java/tripleo/elijah/stages/gen_c/AbstractGernNode.java create mode 100644 src/main/java/tripleo/elijah_fluffy/anno/ElLateInit.java diff --git a/src/main/java/tripleo/elijah/stages/gen_c/AbstractGernNode.java b/src/main/java/tripleo/elijah/stages/gen_c/AbstractGernNode.java new file mode 100644 index 0000000..b0be733 --- /dev/null +++ b/src/main/java/tripleo/elijah/stages/gen_c/AbstractGernNode.java @@ -0,0 +1,52 @@ +package tripleo.elijah.stages.gen_c; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import tripleo.elijah.stages.gen_fn.*; +import tripleo.elijah_fluffy.anno.ElLateInit; + +class AbstractGernNode implements GernNode { + protected /*final*/ @ElLateInit GeneratedNode generatedNode; + + public void setNode(final GeneratedNode aGeneratedNode) { + generatedNode = aGeneratedNode; + } + + @Override + public @NotNull GeneratedNode carrier() { + return generatedNode; + } + + @Override + public @Nullable GeneratedFunction asFunction() { + if (generatedNode instanceof GeneratedFunction) + return (GeneratedFunction) generatedNode; + return null; + } + + @Override + public @Nullable GeneratedContainerNC asContainer() { + if (generatedNode instanceof GeneratedContainerNC) + return (GeneratedContainerNC) generatedNode; + return null; + } + @Override + public @Nullable GeneratedClass asClass() { + if (generatedNode instanceof GeneratedClass) + return (GeneratedClass) generatedNode; + return null; + } + @Override + public @Nullable GeneratedNamespace asNamespace() { + if (generatedNode instanceof GeneratedNamespace) + return (GeneratedNamespace) generatedNode; + return null; + } + + @Override + public @Nullable GeneratedConstructor asConstructor() { + if (generatedNode instanceof GeneratedConstructor) + return (GeneratedConstructor) generatedNode; + return null; + } +} diff --git a/src/main/java/tripleo/elijah_fluffy/anno/ElLateInit.java b/src/main/java/tripleo/elijah_fluffy/anno/ElLateInit.java new file mode 100644 index 0000000..f904200 --- /dev/null +++ b/src/main/java/tripleo/elijah_fluffy/anno/ElLateInit.java @@ -0,0 +1,4 @@ +package tripleo.elijah_fluffy.anno; + +public @interface ElLateInit { +} From 513dad7dc4744243419b33d8b6c597b0d78b572b Mon Sep 17 00:00:00 2001 From: Tripleo Date: Sun, 22 Sep 2024 17:30:13 -0400 Subject: [PATCH 4/7] Refactor and add Gern --- .../elijah/stages/gen_c/GenerateC.java | 594 +++++++----------- .../gen_c/Generate_Code_For_Method.java | 14 +- .../stages/gen_c/GetAssignmentValue.java | 218 +++++++ .../elijah/stages/gen_c/GetTypeName.java | 131 ++++ .../stages/gen_c/WlGenerateFunctionC.java | 40 ++ 5 files changed, 634 insertions(+), 363 deletions(-) create mode 100644 src/main/java/tripleo/elijah/stages/gen_c/GetAssignmentValue.java create mode 100644 src/main/java/tripleo/elijah/stages/gen_c/GetTypeName.java create mode 100644 src/main/java/tripleo/elijah/stages/gen_c/WlGenerateFunctionC.java diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java b/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java index e41e22c..bf0eb6f 100644 --- a/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java +++ b/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java @@ -8,7 +8,9 @@ */ package tripleo.elijah.stages.gen_c; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import tripleo.elijah.comp.i.ErrSink; import tripleo.elijah.lang.*; import tripleo.elijah.lang.types.OS_FuncExprType; @@ -16,6 +18,9 @@ import tripleo.elijah.lang2.SpecialVariables; import tripleo.elijah.nextgen.model.SM_ClassDeclaration; import tripleo.elijah.nextgen.model.SM_Node; +import tripleo.elijah.nextgen.outputstatement.EG_SingleStatement; +import tripleo.elijah.nextgen.outputstatement.EG_Statement; +import tripleo.elijah.nextgen.outputstatement.EX_Explanation; import tripleo.elijah.stages.deduce.ClassInvocation; import tripleo.elijah.stages.deduce.FunctionInvocation; import tripleo.elijah.stages.gen_fn.*; @@ -31,10 +36,7 @@ import tripleo.elijah_fluffy.util.*; import tripleo.util.buffer.Buffer; -import java.util.ArrayList; -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; +import java.util.*; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -47,6 +49,7 @@ public class GenerateC implements CodeGenerator, GenerateFiles { private static final String PHASE = "GenerateC"; final ErrSink errSink; private final ElLog LOG; + Map gerns = new HashMap<>(); public GenerateC(final @NotNull OutputFileFactoryParams p) { errSink = p.getErrSink(); @@ -99,25 +102,92 @@ private static boolean isValue(final BaseGeneratedFunction gf, final @NotNull St } } - public GenerateResult generateCode(final Collection lgn, final WorkManager wm) { - final GenerateResult gr = new GenerateResult(); + private static EG_Statement _CALL_stmt(final GetAssignmentValue gav, + final @NotNull BaseGeneratedFunction gf, + final ElLog LOG, + final ProcTableEntry pte, + final Instruction inst) { + final CALL_stmt r = new CALL_stmt(); + r.set(gav, gf, LOG, pte, inst); + return r.getStatement(); + } - for (final GeneratedNode generatedNode : lgn) { - if (generatedNode instanceof final GeneratedFunction generatedFunction) { + private static @NotNull EG_Statement _CALLS_stmt(final GetAssignmentValue b, final @NotNull BaseGeneratedFunction gf, final ElLog LOG, final ProcTableEntry pte, final StringBuilder sb, final Instruction inst) { + CReference reference = null; + if (pte.expression_num == null) { + final int y = 2; + final IdentExpression ptex = (IdentExpression) pte.expression; + sb.append(Emit.emit("/*684*/")); + sb.append(ptex.getText()); + } else { + // TODO Why not expression_num? + reference = new CReference(); + final IdentIA ia2 = (IdentIA) pte.expression_num; + reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); + final List sll = b.getAssignmentValueArgs(inst, gf, LOG); + reference.args(sll); + final String path = reference.build(); + sb.append(Emit.emit("/*807*/") + path); + + final IExpression ptex = pte.expression; + if (ptex instanceof IdentExpression) { + sb.append(Emit.emit("/*803*/")); + sb.append(((IdentExpression) ptex).getText()); + } else if (ptex instanceof ProcedureCallExpression) { + sb.append(Emit.emit("/*806*/")); + sb.append(ptex.getLeft()); // TODO Qualident, IdentExpression, DotExpression + } + } + if (true /* reference == null */) { + sb.append(Emit.emit("/*810*/") + "("); + { + final List sll = b.getAssignmentValueArgs(inst, gf, LOG); + sb.append(Helpers.String_join(", ", sll)); + } + sb.append(");"); + } + EG_Statement st = new EG_SingleStatement(sb.toString(), EX_Explanation.withMessage("_CALLS_stmt")); + return st; + } + + public GenerateResult generateCode1(final Collection nodes, final Collection lgn, final WorkManager wm) { + final GenerateResult gr = new GenerateResult(); + if (nodes != null) for (GernNode gerN : nodes) { + if (gerN.asFunction() != null) { + final GeneratedFunction generatedFunction = gerN.asFunction(); final WorkList wl = new WorkList(); generate_function(generatedFunction, gr, wl); if (!wl.isEmpty()) wm.addJobs(wl); - } else if (generatedNode instanceof final GeneratedContainerNC containerNC) { + } else if (gerN.asContainer() != null) { + final GeneratedContainerNC containerNC = gerN.asContainer(); + assert containerNC != null; containerNC.generateCode(this, gr); - } else if (generatedNode instanceof final GeneratedConstructor generatedConstructor) { + } else if (gerN.asConstructor() != null) { + final GeneratedConstructor generatedConstructor = gerN.asConstructor(); final WorkList wl = new WorkList(); generate_constructor(generatedConstructor, gr, wl); if (!wl.isEmpty()) wm.addJobs(wl); + } else assert false; + } + else { + for (final GeneratedNode generatedNode : lgn) { + if (generatedNode instanceof final GeneratedFunction generatedFunction) { + final WorkList wl = new WorkList(); + generate_function(generatedFunction, gr, wl); + if (!wl.isEmpty()) + wm.addJobs(wl); + } else if (generatedNode instanceof final GeneratedContainerNC containerNC) { + containerNC.generateCode(this, gr); + } else if (generatedNode instanceof final GeneratedConstructor generatedConstructor) { + final WorkList wl = new WorkList(); + generate_constructor(generatedConstructor, gr, wl); + if (!wl.isEmpty()) + wm.addJobs(wl); + } } } - return gr; } @@ -161,10 +231,7 @@ public void generate_function(final GeneratedFunction aGeneratedFunction, final if (fi == null) { // TODO constructor final int y = 2; - /* - * if (pte.getClassInvocation() == null) assert pte.getStatus() == - * BaseTableEntry.Status.UNKNOWN; - */ + assert false; } else { final BaseGeneratedFunction gf = fi.getGenerated(); if (gf != null) { @@ -271,6 +338,11 @@ public void generate_namespace(final GeneratedNamespace x, final GenerateResult x.generatedAlready = true; } + @Override + public GenerateResult generateCode(final @NotNull Collection aNodeCollection, final @NotNull WorkManager aWorkManager) { + return generateCode1(null, aNodeCollection, aWorkManager); + } + @Override public void forNode(final SM_Node aNode) { final int y = 2; @@ -313,11 +385,11 @@ public GenerateResult resultsFromNodes(final List aNodes, final W nc.generateCode(ggc, gr2); final @NotNull Collection gn1 = (nc.functionMap.values()).stream() .map(x -> (GeneratedNode) x).collect(Collectors.toList()); - final GenerateResult gr3 = ggc.generateCode(gn1, wm); + final GenerateResult gr3 = gern(gn1).generateCode(ggc, wm); gr2.results().addAll(gr3.results()); final @NotNull Collection gn2 = (nc.classMap.values()).stream() .map(x -> (GeneratedNode) x).collect(Collectors.toList()); - final GenerateResult gr4 = ggc.generateCode(gn2, wm); + final GenerateResult gr4 = gern(gn2).generateCode(ggc, wm); gr2.results().addAll(gr4.results()); } else { SimplePrintLoggerToRemoveSoon.println2("2009 " + generatedNode.getClass().getName()); @@ -327,8 +399,79 @@ public GenerateResult resultsFromNodes(final List aNodes, final W return gr2; } - private void generateCodeForMethod(final BaseGeneratedFunction gf, final GenerateResult gr, + public GenerateResult resultsFromNodes(final Gerns aNodes, final WorkManager wm) { + final GenerateC ggc = this; + + final GenerateResult gr2 = new GenerateResult(); + + for (GernNode gernNode : aNodes) { + final GeneratedNode generatedNode = gernNode.carrier(); + + // if (generatedNode.module() != mod) continue; // README curious + + if (gernNode.asContainer() != null) { + // FIXME idea bad + @Nullable final GeneratedContainerNC nc = Objects.requireNonNull(gernNode.asContainer()); + final GernNodeContainer nc2 = ((GernNodeContainer) gernNode); + + nc2.generateCode(ggc, gr2); + + // FIXME Simplify and preserve these two + final GenerateResult gr3 = nc2.generateCodeForFunctions(wm, nc, ggc, this); + gr2.results().addAll(gr3.results()); + final GenerateResult gr4 = nc2.generateCodeForContainers(wm, nc, ggc, this); + gr2.results().addAll(gr4.results()); + } else { + + SimplePrintLoggerToRemoveSoon.println2("2009 " + generatedNode.getClass().getName()); + } + } + + return gr2; + } + + private GenerateResult generateCode77(final Collection aGern, final WorkManager aWm) { + final Collection q = new ArrayList<>(); + for (GernNode gernNode : aGern) { + q.add(gernNode.carrier()); + } + return generateCode(q, aWm); + } + + public @NotNull Gerns gern(final @NotNull Collection aGeneratedNodes) { + return new Gerns() { + final Collection res = aGeneratedNodes.stream() + .map(generatedNode -> gern(generatedNode)) + .toList(); + + @NotNull + @Override + public Iterator iterator() { + return res.iterator(); + } + + @Override + public GenerateResult generateCode(final GenerateC aGgc, final WorkManager aWm) { + return generateCode1(res, aGeneratedNodes, aWm); + } + }; + } + + @Contract(value = "_ -> new", pure = true) + private @NotNull GernNode gern(final GeneratedNode aGeneratedNode) { + return new GernNodeFunction(aGeneratedNode); + } + + private void generateCodeForMethod(final @NotNull BaseGeneratedFunction gf, + final GenerateResult gr, + final WorkList aWorkList) { + generateCodeForMethod(gern(gf), gr, aWorkList); + } + + private void generateCodeForMethod(final @NotNull GernNode gn, + final GenerateResult gr, final WorkList aWorkList) { + BaseGeneratedFunction gf = (BaseGeneratedFunction) gn.carrier(); if (gf.getFD() == null) return; final Generate_Code_For_Method gcfm = new Generate_Code_For_Method(this, LOG); @@ -347,14 +490,14 @@ String getTypeNameForGenClass(@NotNull final GeneratedNode aGenClass) { return GetTypeName.getTypeNameForGenClass(aGenClass); } - String getTypeName(@NotNull final TypeTableEntry tte) { - return GetTypeName.forTypeTableEntry(tte); - } - String getTypeNameForVariableEntry(@NotNull final VariableTableEntry input) { return GetTypeName.forVTE(input); } + String getTypeName(@NotNull final TypeTableEntry tte) { + return GetTypeName.forTypeTableEntry(tte); + } + @Override public void generate_class(final GeneratedClass x, final GenerateResult gr) { if (x.generatedAlready) @@ -612,369 +755,108 @@ String getRealTargetName(final @NotNull BaseGeneratedFunction gf, final @NotNull final String s = Helpers.String_join("->", ls); LOG.info("933 " + s); if (identTableEntry.getResolvedElement() instanceof ConstructorDef - || identTableEntry.getResolvedElement() instanceof PropertyStatement || value != null) + || identTableEntry.getResolvedElement() instanceof PropertyStatement || value != null) { return path; - else + } else { return s; + } } - static class WlGenerateFunctionC implements WorkJob { - - private final BaseGeneratedFunction gf; - private final GenerateResult gr; - private final WorkList wl; - private final GenerateC generateC; - private boolean _isDone = false; - - public WlGenerateFunctionC(final BaseGeneratedFunction aGf, final GenerateResult aGr, final WorkList aWl, - final GenerateC aGenerateC) { - gf = aGf; - gr = aGr; - wl = aWl; - generateC = aGenerateC; - } + @SuppressWarnings("StringConcatenationInsideStringBufferAppend") + public static class CALL_stmt implements EG_Statement { + private final StringBuilder sb = new StringBuilder(); + private GetAssignmentValue gav; + private BaseGeneratedFunction gf; + private ElLog LOG; + private ProcTableEntry pte; + private Instruction inst; + private CReference reference; @Override - public void run(final WorkManager aWorkManager) { - if (gf instanceof GeneratedFunction) - generateC.generate_function((GeneratedFunction) gf, gr, wl); - else - generateC.generate_constructor((GeneratedConstructor) gf, gr, wl); - _isDone = true; + public String getText() { + return sb.toString(); } @Override - public boolean isDone() { - return _isDone; - } - } - - static class GetTypeName { - static String forVTE(@NotNull final VariableTableEntry input) { - final OS_Type attached = input.type.getAttached(); - if (attached == null) - return Emit.emit("/*390*/") + "Z__Unresolved*"; // TODO remove this ASAP - // - // special case - // - if (input.type.genType.getNode() != null) - return Emit.emit("/*395*/") + getTypeNameForGenClass(input.type.genType.getNode()) + "*"; - // - if (input.getStatus() == BaseTableEntry.Status.UNCHECKED) - return "Error_UNCHECKED_Type"; - switch (attached.getType()) { - case USER_CLASS: - return attached.getClassOf().name(); - case USER: - final TypeName typeName = attached.getTypeName(); - final String name; - if (typeName instanceof NormalTypeName) - name = ((NormalTypeName) typeName).getName(); - else - name = typeName.toString(); - return String.format(Emit.emit("/*543*/") + "Z<%s>*", name); - default: - throw new NotImplementedException(); - } + public EX_Explanation getExplanation() { + return EX_Explanation.withMessage("_CALL_stmt"); } - static String getTypeNameForGenClass(@NotNull final GeneratedNode aGenClass) { - final String ty; - if (aGenClass instanceof GeneratedClass) - ty = forGenClass((GeneratedClass) aGenClass); - else if (aGenClass instanceof GeneratedNamespace) - ty = forGenNamespace((GeneratedNamespace) aGenClass); - else - ty = "Error_Unknown_GenClass"; - return ty; - } + public void set(final GetAssignmentValue aGav, + final BaseGeneratedFunction aGf, + final ElLog aLOG, + final ProcTableEntry aPte, + final Instruction aInst) { + gav = aGav; + gf = aGf; + LOG = aLOG; + pte = aPte; + inst = aInst; - static String forGenClass(@NotNull final GeneratedClass aGeneratedClass) { - final String z; - z = String.format("Z%d", aGeneratedClass.getCode()); - return z; + if (pte.expression_num == null) { + calc_expression(); + } else { + calc_expression_num(); + } } - static String forGenNamespace(@NotNull final GeneratedNamespace aGeneratedNamespace) { - final String z; - z = String.format("Z%d", aGeneratedNamespace.getCode()); - return z; + public @Nullable CReference getReference() { + return reference; } - static @NotNull String forTypeTableEntry(@NotNull final TypeTableEntry tte) { - final GeneratedNode res = tte.resolved(); - if (res instanceof final GeneratedContainerNC nc) { - final int code = nc.getCode(); - return "Z" + code; - } else - return "Z<-1>"; - } - - @Deprecated - static String forOSType(final @NotNull OS_Type ty, final ElLog LOG) { - if (ty == null) - throw new IllegalArgumentException("ty is null"); - // - final String z; - switch (ty.getType()) { - case USER_CLASS: - final ClassStatement el = ty.getClassOf(); - final String name; - if (ty instanceof NormalTypeName) - name = ((NormalTypeName) ty).getName(); - else - name = el.getName(); - z = Emit.emit("/*443*/") + String.format("Z%d/*%s*/", el._a.getCode(), name);// .getName(); - break; - case FUNCTION: - z = ""; - break; - case FUNC_EXPR: { - z = ""; - final OS_FuncExprType fe = (OS_FuncExprType) ty; - final int y = 2; + private void calc_expression_num() { + final IdentIA ia2 = (IdentIA) pte.expression_num; // this is a name + final IdentTableEntry idte = ia2.getEntry(); + if (idte.getStatus() == BaseTableEntry.Status.KNOWN) { + reference = new CReference(); + final FunctionInvocation functionInvocation = pte.getFunctionInvocation(); + if (functionInvocation == null + || functionInvocation.getFunction() == ConstructorDef.defaultVirtualCtor) { + reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); + final List sll = gav.getAssignmentValueArgs(inst, gf, LOG); + reference.args(sll); + final String path = reference.build(); + emit(("/*829*/"), path); + } else { + final BaseGeneratedFunction pte_generated = functionInvocation.getGenerated(); + if (idte.resolvedType() == null && pte_generated != null) + idte.resolveTypeToClass(pte_generated); + reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); + final List sll = gav.getAssignmentValueArgs(inst, gf, LOG); + reference.args(sll); + final String path = reference.build(); + emit(("/*827*/"), path); } - break; - case USER: - final TypeName typeName = ty.getTypeName(); - LOG.err("Warning: USER TypeName in GenerateC " + typeName); - final String s = typeName.toString(); - if (s.equals("Unit")) - z = "void"; - else - z = String.format("Z", s); - break; - case BUILT_IN: - LOG.err("Warning: BUILT_IN TypeName in GenerateC"); - z = "Z" + ty.getBType().getCode(); // README should not even be here, but look at .name() for other code - // gen schemes - break; - case UNIT_TYPE: - z = "void"; - break; - default: - throw new IllegalStateException("Unexpected value: " + ty.getType()); + } else { + final String path = gf.getIdentIAPathNormal(ia2); + emit(("/*828*/"), String.format("%s is UNKNOWN", path)); } - return z; } - @Deprecated - static String forTypeName(final @NotNull TypeName typeName, final @NotNull ErrSink errSink) { - if (typeName instanceof RegularTypeName) { - final String name = ((RegularTypeName) typeName).getName(); // TODO convert to Z-name - - return String.format("Z<%s>/*kklkl*/", name); -// return getTypeName(new OS_Type(typeName)); - } - errSink.reportError("Type is not fully deduced " + typeName); - return String.format("Z<%s>/*kllkk*/", typeName); // TODO type is not fully deduced + private void emit(final String one, final String two) { + sb.append(Emit.emit(one) + two); } - } -// @NotNull List getArgumentStrings(final BaseGeneratedFunction gf, final Instruction instruction) { -// return getArgumentStrings(gf, () -> new InstructionFixedList(instruction)); -// } + private void calc_expression() { + if (true) { + final IdentExpression ptex = (IdentExpression) pte.expression; + sb.append(ptex.getText()); + emit(("/*671*/"), "("); - static class GetAssignmentValue { + final List sll = gav.getAssignmentValueArgs(inst, gf, LOG); + sb.append(Helpers.String_join(", ", sll)); - private static String const_to_string(final IExpression expression) { - if (expression instanceof NumericExpression) { - return String.valueOf(((NumericExpression) expression).getValue()); - } - if (expression instanceof CharLitExpression) { - return String.format("'%s'", expression); - } - if (expression instanceof StringExpression) { - // TODO triple quoted strings and other escaping concerns - return String.format("\"%s\"", ((StringExpression) expression).getText()); - } - - // FloatLitExpression - // BooleanExpression - throw new NotImplementedException(); - } - - public String FnCallArgs(final @NotNull FnCallArgs fca, final @NotNull BaseGeneratedFunction gf, - final ElLog LOG) { - final StringBuilder sb = new StringBuilder(); - final Instruction inst = fca.getExpression(); -// LOG.err("9000 "+inst.getName()); - final InstructionArgument x = inst.getArg(0); - assert x instanceof ProcIA; - final ProcTableEntry pte = gf.getProcTableEntry(to_int(x)); -// LOG.err("9000-2 "+pte); - switch (inst.getName()) { - case CALL: { - if (pte.expression_num == null) { -// assert false; // TODO synthetic methods - final IdentExpression ptex = (IdentExpression) pte.expression; - sb.append(ptex.getText()); - sb.append(Emit.emit("/*671*/") + "("); - - final List sll = getAssignmentValueArgs(inst, gf, LOG); - sb.append(Helpers.String_join(", ", sll)); - - sb.append(")"); - } else { - final IdentIA ia2 = (IdentIA) pte.expression_num; - final IdentTableEntry idte = ia2.getEntry(); - if (idte.getStatus() == BaseTableEntry.Status.KNOWN) { - final CReference reference = new CReference(); - final FunctionInvocation functionInvocation = pte.getFunctionInvocation(); - if (functionInvocation == null - || functionInvocation.getFunction() == ConstructorDef.defaultVirtualCtor) { - reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); - final List sll = getAssignmentValueArgs(inst, gf, LOG); - reference.args(sll); - final String path = reference.build(); - sb.append(Emit.emit("/*829*/") + path); - } else { - final BaseGeneratedFunction pte_generated = functionInvocation.getGenerated(); - if (idte.resolvedType() == null && pte_generated != null) - idte.resolveTypeToClass(pte_generated); - reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); - final List sll = getAssignmentValueArgs(inst, gf, LOG); - reference.args(sll); - final String path = reference.build(); - sb.append(Emit.emit("/*827*/") + path); - } - } else { - final String path = gf.getIdentIAPathNormal(ia2); - sb.append(Emit.emit("/*828*/") + String.format("%s is UNKNOWN", path)); - } - } - return sb.toString(); - } - case CALLS: { - CReference reference = null; - if (pte.expression_num == null) { - final int y = 2; - final IdentExpression ptex = (IdentExpression) pte.expression; - sb.append(Emit.emit("/*684*/")); - sb.append(ptex.getText()); - } else { - // TODO Why not expression_num? - reference = new CReference(); - final IdentIA ia2 = (IdentIA) pte.expression_num; - reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); - final List sll = getAssignmentValueArgs(inst, gf, LOG); - reference.args(sll); - final String path = reference.build(); - sb.append(Emit.emit("/*807*/") + path); - - final IExpression ptex = pte.expression; - if (ptex instanceof IdentExpression) { - sb.append(Emit.emit("/*803*/")); - sb.append(((IdentExpression) ptex).getText()); - } else if (ptex instanceof ProcedureCallExpression) { - sb.append(Emit.emit("/*806*/")); - sb.append(ptex.getLeft()); // TODO Qualident, IdentExpression, DotExpression - } - } - if (true /* reference == null */) { - sb.append(Emit.emit("/*810*/") + "("); - { - final List sll = getAssignmentValueArgs(inst, gf, LOG); - sb.append(Helpers.String_join(", ", sll)); - } - sb.append(");"); - } - return sb.toString(); - } - default: - throw new IllegalStateException("Unexpected value: " + inst.getName()); - } - } - - @NotNull - private List getAssignmentValueArgs(final @NotNull Instruction inst, final BaseGeneratedFunction gf, - final ElLog LOG) { - final int args_size = inst.getArgsSize(); - final List sll = new ArrayList(); - for (int i = 1; i < args_size; i++) { - final InstructionArgument ia = inst.getArg(i); - final int y = 2; -// LOG.err("7777 " +ia); - if (ia instanceof ConstTableIA) { - final ConstantTableEntry constTableEntry = gf.getConstTableEntry(((ConstTableIA) ia).getIndex()); - sll.add(const_to_string(constTableEntry.initialValue)); - } else if (ia instanceof IntegerIA) { - final VariableTableEntry variableTableEntry = gf.getVarTableEntry(((IntegerIA) ia).getIndex()); - sll.add(Emit.emit("/*853*/") + getRealTargetName(gf, variableTableEntry)); - } else if (ia instanceof IdentIA) { - final String path = gf.getIdentIAPathNormal((IdentIA) ia); // return x.y.z - final IdentTableEntry ite = gf.getIdentTableEntry(to_int(ia)); - if (ite.getStatus() == BaseTableEntry.Status.UNKNOWN) { - sll.add(String.format("%s is UNKNOWN", path)); - } else { - final CReference reference = new CReference(); - reference.getIdentIAPath((IdentIA) ia, Generate_Code_For_Method.AOG.GET, null); - final String path2 = reference.build(); // return ZP105get_z(vvx.vmy) - if (path.equals(path2)) { - // should always fail - // throw new AssertionError(); - LOG.err(String.format("864 should always fail but didn't %s %s", path, path2)); - } -// assert ident != null; -// IdentTableEntry ite = gf.getIdentTableEntry(((IdentIA) ia).getIndex()); -// sll.add(Emit.emit("/*748*/")+""+ite.getIdent().getText()); - sll.add(Emit.emit("/*748*/") + path2); - LOG.info("743 " + path2 + " " + path); - } - } else if (ia instanceof ProcIA) { - LOG.err("863 ProcIA"); - throw new NotImplementedException(); - } else { - throw new IllegalStateException("Cant be here: Invalid InstructionArgument"); - } - } - return sll; - } - - public String ConstTableIA(final @NotNull ConstTableIA constTableIA, final @NotNull BaseGeneratedFunction gf) { - final ConstantTableEntry cte = gf.getConstTableEntry(constTableIA.getIndex()); -// LOG.err(("9001-3 "+cte.initialValue)); - switch (cte.initialValue.getKind()) { - case NUMERIC: - return const_to_string(cte.initialValue); - case STRING_LITERAL: - return const_to_string(cte.initialValue); - case IDENT: - final String text = ((IdentExpression) cte.initialValue).getText(); - if (BuiltInTypes.isBooleanText(text)) - return text; - else - throw new NotImplementedException(); - default: - throw new NotImplementedException(); + sb.append(")"); + } else { + throw new AssertionError(); // TODO synthetic methods } } - public String IntegerIA(final @NotNull IntegerIA integerIA, final @NotNull BaseGeneratedFunction gf) { - final VariableTableEntry vte = gf.getVarTableEntry(integerIA.getIndex()); - final String x = getRealTargetName(gf, vte); - return x; - } - - public String IdentIA(final @NotNull IdentIA identIA, final BaseGeneratedFunction gf) { - assert gf == identIA.gf; - final CReference reference = new CReference(); - reference.getIdentIAPath(identIA, Generate_Code_For_Method.AOG.GET, null); - return reference.build(); - } - - public String forClassInvocation(final @NotNull Instruction aInstruction, final ClassInvocation aClsinv, - final @NotNull BaseGeneratedFunction gf, final ElLog LOG) { - final int y = 2; - final InstructionArgument _arg0 = aInstruction.getArg(0); - @NotNull final ProcTableEntry pte = gf.getProcTableEntry(((ProcIA) _arg0).getIndex()); - final CtorReference reference = new CtorReference(); - reference.getConstructorPath(pte.expression_num, gf); - @NotNull final List x = getAssignmentValueArgs(aInstruction, gf, LOG); - reference.args(x); - return reference.build(aClsinv); + public EG_Statement getStatement() { + return this; } } + } // diff --git a/src/main/java/tripleo/elijah/stages/gen_c/Generate_Code_For_Method.java b/src/main/java/tripleo/elijah/stages/gen_c/Generate_Code_For_Method.java index 638f993..d188207 100644 --- a/src/main/java/tripleo/elijah/stages/gen_c/Generate_Code_For_Method.java +++ b/src/main/java/tripleo/elijah/stages/gen_c/Generate_Code_For_Method.java @@ -541,7 +541,7 @@ private Operation2 _action_DECL(final @NotNull Instruction instruc final GeneratedNode res = vte.resolvedType(); if (res instanceof GeneratedClass) { - final String z = GenerateC.GetTypeName.forGenClass((GeneratedClass) res); + final String z = GetTypeName.forGenClass((GeneratedClass) res); final String s = String.format("%s* %s;", z, target_name); return Operation2.success(new EG_SingleStatement(s, null)); } @@ -549,7 +549,7 @@ private Operation2 _action_DECL(final @NotNull Instruction instruc if (x != null) { switch (x.getType()) { case USER_CLASS: - final String z = GenerateC.GetTypeName.forOSType(x, LOG); + final String z = GetTypeName.forOSType(x, LOG); final String s = String.format("%s* %s;", z, target_name); return Operation2.success(new EG_SingleStatement(s, null)); case USER: @@ -559,7 +559,7 @@ private Operation2 _action_DECL(final @NotNull Instruction instruc if (((NormalTypeName) typeName).getName().equals("Any")) z2 = "void *"; // TODO Technically this is wrong else - z2 = GenerateC.GetTypeName.forTypeName(typeName, gc.errSink); + z2 = GetTypeName.forTypeName(typeName, gc.errSink); final String s1 = String.format("%s %s;", z2, target_name); return Operation2.success(new EG_SingleStatement(s1, null)); } @@ -732,7 +732,7 @@ void generateCodeForConstructor(final @NotNull GeneratedConstructor gf, final Ge final CClassDecl decl = new CClassDecl(x); decl.evaluatePrimitive(); - final String class_name = GenerateC.GetTypeName.forGenClass(x); + final String class_name = GetTypeName.forGenClass(x); final int class_code = x.getCode(); assert gf.cd != null; @@ -864,7 +864,7 @@ EG_Statement find_args_statement(final @NotNull BaseGeneratedFunction gf) { final List args_list = gf.vte_list.stream().filter(input -> input.vtt == VariableTableType.ARG) // rule=vte:args_at - .map(input -> String.format("%s va%s", GenerateC.GetTypeName.forVTE(input), input.getName())) + .map(input -> String.format("%s va%s", GetTypeName.forVTE(input), input.getName())) .collect(Collectors.toList()); final EG_Statement args = new EG_DottedStatement(", ", args_list, new EX_Rule(rule)); @@ -907,7 +907,7 @@ String __find_header_string(final BaseGeneratedFunction gf, final ElLog LOG) { final GeneratedClass st = (GeneratedClass) parent; @NotNull - final C_HeaderString chs = C_HeaderString.forClass(st, () -> GenerateC.GetTypeName.forGenClass(st), + final C_HeaderString chs = C_HeaderString.forClass(st, () -> GetTypeName.forGenClass(st), return_type, name, args_string, LOG); result = chs.getResult(); @@ -916,7 +916,7 @@ String __find_header_string(final BaseGeneratedFunction gf, final ElLog LOG) { @NotNull final C_HeaderString chs = C_HeaderString.forNamespace(st, - () -> GenerateC.GetTypeName.forGenNamespace(st), return_type, name, args_string, LOG); + () -> GetTypeName.forGenNamespace(st), return_type, name, args_string, LOG); result = chs.getResult(); } else { @NotNull diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GetAssignmentValue.java b/src/main/java/tripleo/elijah/stages/gen_c/GetAssignmentValue.java new file mode 100644 index 0000000..31923dd --- /dev/null +++ b/src/main/java/tripleo/elijah/stages/gen_c/GetAssignmentValue.java @@ -0,0 +1,218 @@ +package tripleo.elijah.stages.gen_c; + +import org.jetbrains.annotations.NotNull; +import tripleo.elijah.lang.*; +import tripleo.elijah.lang2.BuiltInTypes; +import tripleo.elijah.stages.deduce.ClassInvocation; +import tripleo.elijah.stages.deduce.FunctionInvocation; +import tripleo.elijah.stages.gen_fn.*; +import tripleo.elijah.stages.instructions.*; +import tripleo.elijah.stages.logging.ElLog; +import tripleo.elijah_fluffy.util.Helpers; +import tripleo.elijah_fluffy.util.NotImplementedException; + +import java.util.ArrayList; +import java.util.List; + +import static tripleo.elijah.stages.deduce.DeduceTypes2.to_int; + +class GetAssignmentValue { + + static String const_to_string(final IExpression expression) { + if (expression instanceof NumericExpression) { + return String.valueOf(((NumericExpression) expression).getValue()); + } + if (expression instanceof CharLitExpression) { + return String.format("'%s'", expression); + } + if (expression instanceof StringExpression) { + // TODO triple quoted strings and other escaping concerns + return String.format("\"%s\"", ((StringExpression) expression).getText()); + } + + // FloatLitExpression + // BooleanExpression + throw new NotImplementedException(); + } + + public String FnCallArgs(final @NotNull FnCallArgs fca, final @NotNull BaseGeneratedFunction gf, + final ElLog LOG) { + final StringBuilder sb = new StringBuilder(); + final Instruction inst = fca.getExpression(); +// LOG.err("9000 "+inst.getName()); + final InstructionArgument x = inst.getArg(0); + assert x instanceof ProcIA; + final ProcTableEntry pte = gf.getProcTableEntry(to_int(x)); +// LOG.err("9000-2 "+pte); + switch (inst.getName()) { + case CALL: { + if (pte.expression_num == null) { +// assert false; // TODO synthetic methods + final IdentExpression ptex = (IdentExpression) pte.expression; + sb.append(ptex.getText()); + sb.append(Emit.emit("/*671*/") + "("); + + final List sll = getAssignmentValueArgs(inst, gf, LOG); + sb.append(Helpers.String_join(", ", sll)); + + sb.append(")"); + } else { + final IdentIA ia2 = (IdentIA) pte.expression_num; + final IdentTableEntry idte = ia2.getEntry(); + if (idte.getStatus() == BaseTableEntry.Status.KNOWN) { + final CReference reference = new CReference(); + final FunctionInvocation functionInvocation = pte.getFunctionInvocation(); + if (functionInvocation == null + || functionInvocation.getFunction() == ConstructorDef.defaultVirtualCtor) { + reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); + final List sll = getAssignmentValueArgs(inst, gf, LOG); + reference.args(sll); + final String path = reference.build(); + sb.append(Emit.emit("/*829*/") + path); + } else { + final BaseGeneratedFunction pte_generated = functionInvocation.getGenerated(); + if (idte.resolvedType() == null && pte_generated != null) + idte.resolveTypeToClass(pte_generated); + reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); + final List sll = getAssignmentValueArgs(inst, gf, LOG); + reference.args(sll); + final String path = reference.build(); + sb.append(Emit.emit("/*827*/") + path); + } + } else { + final String path = gf.getIdentIAPathNormal(ia2); + sb.append(Emit.emit("/*828*/") + String.format("%s is UNKNOWN", path)); + } + } + return sb.toString(); + } + case CALLS: { + CReference reference = null; + if (pte.expression_num == null) { + final int y = 2; + final IdentExpression ptex = (IdentExpression) pte.expression; + sb.append(Emit.emit("/*684*/")); + sb.append(ptex.getText()); + } else { + // TODO Why not expression_num? + reference = new CReference(); + final IdentIA ia2 = (IdentIA) pte.expression_num; + reference.getIdentIAPath(ia2, Generate_Code_For_Method.AOG.GET, null); + final List sll = getAssignmentValueArgs(inst, gf, LOG); + reference.args(sll); + final String path = reference.build(); + sb.append(Emit.emit("/*807*/") + path); + + final IExpression ptex = pte.expression; + if (ptex instanceof IdentExpression) { + sb.append(Emit.emit("/*803*/")); + sb.append(((IdentExpression) ptex).getText()); + } else if (ptex instanceof ProcedureCallExpression) { + sb.append(Emit.emit("/*806*/")); + sb.append(ptex.getLeft()); // TODO Qualident, IdentExpression, DotExpression + } + } + if (true /* reference == null */) { + sb.append(Emit.emit("/*810*/") + "("); + { + final List sll = getAssignmentValueArgs(inst, gf, LOG); + sb.append(Helpers.String_join(", ", sll)); + } + sb.append(");"); + } + return sb.toString(); + } + default: + throw new IllegalStateException("Unexpected value: " + inst.getName()); + } + } + + @NotNull + List getAssignmentValueArgs(final @NotNull Instruction inst, final BaseGeneratedFunction gf, + final ElLog LOG) { + final int args_size = inst.getArgsSize(); + final List sll = new ArrayList(); + for (int i = 1; i < args_size; i++) { + final InstructionArgument ia = inst.getArg(i); + final int y = 2; +// LOG.err("7777 " +ia); + if (ia instanceof ConstTableIA) { + final ConstantTableEntry constTableEntry = gf.getConstTableEntry(((ConstTableIA) ia).getIndex()); + sll.add(const_to_string(constTableEntry.initialValue)); + } else if (ia instanceof IntegerIA) { + final VariableTableEntry variableTableEntry = gf.getVarTableEntry(((IntegerIA) ia).getIndex()); + sll.add(Emit.emit("/*853*/") + GenerateC.getRealTargetName(gf, variableTableEntry)); + } else if (ia instanceof IdentIA) { + final String path = gf.getIdentIAPathNormal((IdentIA) ia); // return x.y.z + final IdentTableEntry ite = gf.getIdentTableEntry(to_int(ia)); + if (ite.getStatus() == BaseTableEntry.Status.UNKNOWN) { + sll.add(String.format("%s is UNKNOWN", path)); + } else { + final CReference reference = new CReference(); + reference.getIdentIAPath((IdentIA) ia, Generate_Code_For_Method.AOG.GET, null); + final String path2 = reference.build(); // return ZP105get_z(vvx.vmy) + if (path.equals(path2)) { + // should always fail + // throw new AssertionError(); + LOG.err(String.format("864 should always fail but didn't %s %s", path, path2)); + } +// assert ident != null; +// IdentTableEntry ite = gf.getIdentTableEntry(((IdentIA) ia).getIndex()); +// sll.add(Emit.emit("/*748*/")+""+ite.getIdent().getText()); + sll.add(Emit.emit("/*748*/") + path2); + LOG.info("743 " + path2 + " " + path); + } + } else if (ia instanceof ProcIA) { + LOG.err("863 ProcIA"); + throw new NotImplementedException(); + } else { + throw new IllegalStateException("Cant be here: Invalid InstructionArgument"); + } + } + return sll; + } + + public String ConstTableIA(final @NotNull ConstTableIA constTableIA, final @NotNull BaseGeneratedFunction gf) { + final ConstantTableEntry cte = gf.getConstTableEntry(constTableIA.getIndex()); +// LOG.err(("9001-3 "+cte.initialValue)); + switch (cte.initialValue.getKind()) { + case NUMERIC: + return const_to_string(cte.initialValue); + case STRING_LITERAL: + return const_to_string(cte.initialValue); + case IDENT: + final String text = ((IdentExpression) cte.initialValue).getText(); + if (BuiltInTypes.isBooleanText(text)) + return text; + else + throw new NotImplementedException(); + default: + throw new NotImplementedException(); + } + } + + public String IntegerIA(final @NotNull IntegerIA integerIA, final @NotNull BaseGeneratedFunction gf) { + final VariableTableEntry vte = gf.getVarTableEntry(integerIA.getIndex()); + final String x = GenerateC.getRealTargetName(gf, vte); + return x; + } + + public String IdentIA(final @NotNull IdentIA identIA, final BaseGeneratedFunction gf) { + assert gf == identIA.gf; + final CReference reference = new CReference(); + reference.getIdentIAPath(identIA, Generate_Code_For_Method.AOG.GET, null); + return reference.build(); + } + + public String forClassInvocation(final @NotNull Instruction aInstruction, final ClassInvocation aClsinv, + final @NotNull BaseGeneratedFunction gf, final ElLog LOG) { + final int y = 2; + final InstructionArgument _arg0 = aInstruction.getArg(0); + @NotNull final ProcTableEntry pte = gf.getProcTableEntry(((ProcIA) _arg0).getIndex()); + final CtorReference reference = new CtorReference(); + reference.getConstructorPath(pte.expression_num, gf); + @NotNull final List x = getAssignmentValueArgs(aInstruction, gf, LOG); + reference.args(x); + return reference.build(aClsinv); + } +} diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GetTypeName.java b/src/main/java/tripleo/elijah/stages/gen_c/GetTypeName.java new file mode 100644 index 0000000..69eb1bd --- /dev/null +++ b/src/main/java/tripleo/elijah/stages/gen_c/GetTypeName.java @@ -0,0 +1,131 @@ +package tripleo.elijah.stages.gen_c; + +import org.jetbrains.annotations.NotNull; +import tripleo.elijah.comp.i.ErrSink; +import tripleo.elijah.lang.*; +import tripleo.elijah.lang.types.OS_FuncExprType; +import tripleo.elijah.stages.gen_fn.*; +import tripleo.elijah.stages.logging.ElLog; +import tripleo.elijah_fluffy.util.NotImplementedException; + +class GetTypeName { + static String forVTE(@NotNull final VariableTableEntry input) { + final OS_Type attached = input.type.getAttached(); + if (attached == null) + return Emit.emit("/*390*/") + "Z__Unresolved*"; // TODO remove this ASAP + // + // special case + // + if (input.type.genType.getNode() != null) + return Emit.emit("/*395*/") + getTypeNameForGenClass(input.type.genType.getNode()) + "*"; + // + if (input.getStatus() == BaseTableEntry.Status.UNCHECKED) + return "Error_UNCHECKED_Type"; + switch (attached.getType()) { + case USER_CLASS: + return attached.getClassOf().name(); + case USER: + final TypeName typeName = attached.getTypeName(); + final String name; + if (typeName instanceof NormalTypeName) + name = ((NormalTypeName) typeName).getName(); + else + name = typeName.toString(); + return String.format(Emit.emit("/*543*/") + "Z<%s>*", name); + default: + throw new NotImplementedException(); + } + } + + static String getTypeNameForGenClass(@NotNull final GeneratedNode aGenClass) { + final String ty; + if (aGenClass instanceof GeneratedClass) + ty = forGenClass((GeneratedClass) aGenClass); + else if (aGenClass instanceof GeneratedNamespace) + ty = forGenNamespace((GeneratedNamespace) aGenClass); + else + ty = "Error_Unknown_GenClass"; + return ty; + } + + static String forGenClass(@NotNull final GeneratedClass aGeneratedClass) { + final String z; + z = String.format("Z%d", aGeneratedClass.getCode()); + return z; + } + + static String forGenNamespace(@NotNull final GeneratedNamespace aGeneratedNamespace) { + final String z; + z = String.format("Z%d", aGeneratedNamespace.getCode()); + return z; + } + + static @NotNull String forTypeTableEntry(@NotNull final TypeTableEntry tte) { + final GeneratedNode res = tte.resolved(); + if (res instanceof final GeneratedContainerNC nc) { + final int code = nc.getCode(); + return "Z" + code; + } else + return "Z<-1>"; + } + + @Deprecated + static String forOSType(final @NotNull OS_Type ty, final ElLog LOG) { + if (ty == null) + throw new IllegalArgumentException("ty is null"); + // + final String z; + switch (ty.getType()) { + case USER_CLASS: + final ClassStatement el = ty.getClassOf(); + final String name; + if (ty instanceof NormalTypeName) + name = ((NormalTypeName) ty).getName(); + else + name = el.getName(); + z = Emit.emit("/*443*/") + String.format("Z%d/*%s*/", el._a.getCode(), name);// .getName(); + break; + case FUNCTION: + z = ""; + break; + case FUNC_EXPR: { + z = ""; + final OS_FuncExprType fe = (OS_FuncExprType) ty; + final int y = 2; + } + break; + case USER: + final TypeName typeName = ty.getTypeName(); + LOG.err("Warning: USER TypeName in GenerateC " + typeName); + final String s = typeName.toString(); + if (s.equals("Unit")) + z = "void"; + else + z = String.format("Z", s); + break; + case BUILT_IN: + LOG.err("Warning: BUILT_IN TypeName in GenerateC"); + z = "Z" + ty.getBType().getCode(); // README should not even be here, but look at .name() for other code + // gen schemes + break; + case UNIT_TYPE: + z = "void"; + break; + default: + throw new IllegalStateException("Unexpected value: " + ty.getType()); + } + return z; + } + + @Deprecated + static String forTypeName(final @NotNull TypeName typeName, final @NotNull ErrSink errSink) { + if (typeName instanceof RegularTypeName) { + final String name = ((RegularTypeName) typeName).getName(); // TODO convert to Z-name + + return String.format("Z<%s>/*kklkl*/", name); +// return getTypeName(new OS_Type(typeName)); + } + errSink.reportError("Type is not fully deduced " + typeName); + return String.format("Z<%s>/*kllkk*/", typeName); // TODO type is not fully deduced + } +} diff --git a/src/main/java/tripleo/elijah/stages/gen_c/WlGenerateFunctionC.java b/src/main/java/tripleo/elijah/stages/gen_c/WlGenerateFunctionC.java new file mode 100644 index 0000000..6f8f543 --- /dev/null +++ b/src/main/java/tripleo/elijah/stages/gen_c/WlGenerateFunctionC.java @@ -0,0 +1,40 @@ +package tripleo.elijah.stages.gen_c; + +import tripleo.elijah.stages.gen_fn.BaseGeneratedFunction; +import tripleo.elijah.stages.gen_fn.GeneratedConstructor; +import tripleo.elijah.stages.gen_fn.GeneratedFunction; +import tripleo.elijah.stages.gen_generic.GenerateResult; +import tripleo.elijah.work.WorkJob; +import tripleo.elijah.work.WorkList; +import tripleo.elijah.work.WorkManager; + +class WlGenerateFunctionC implements WorkJob { + + private final BaseGeneratedFunction gf; + private final GenerateResult gr; + private final WorkList wl; + private final GenerateC generateC; + private boolean _isDone = false; + + public WlGenerateFunctionC(final BaseGeneratedFunction aGf, final GenerateResult aGr, final WorkList aWl, + final GenerateC aGenerateC) { + gf = aGf; + gr = aGr; + wl = aWl; + generateC = aGenerateC; + } + + @Override + public void run(final WorkManager aWorkManager) { + if (gf instanceof GeneratedFunction) + generateC.generate_function((GeneratedFunction) gf, gr, wl); + else + generateC.generate_constructor((GeneratedConstructor) gf, gr, wl); + _isDone = true; + } + + @Override + public boolean isDone() { + return _isDone; + } +} From a0a6e3d62c63dead008249b3cd73d09e53caf7eb Mon Sep 17 00:00:00 2001 From: Tripleo Date: Sun, 22 Sep 2024 17:44:25 -0400 Subject: [PATCH 5/7] GernNodeFunction::generateCodeForMethod --- .../elijah/stages/gen_c/GenerateC.java | 12 ++-- .../tripleo/elijah/stages/gen_c/GernNode.java | 15 +++++ .../elijah/stages/gen_c/GernNodeFunction.java | 57 +++++++++++++++++-- 3 files changed, 73 insertions(+), 11 deletions(-) diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java b/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java index bf0eb6f..32f9303 100644 --- a/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java +++ b/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java @@ -465,17 +465,19 @@ public GenerateResult generateCode(final GenerateC aGgc, final WorkManager aWm) private void generateCodeForMethod(final @NotNull BaseGeneratedFunction gf, final GenerateResult gr, final WorkList aWorkList) { - generateCodeForMethod(gern(gf), gr, aWorkList); + final GernNode gern = gern(gf); + if (gern instanceof GernNodeFunction) { + generateCodeForMethod(gern, gr, aWorkList); + } else { + throw new AssertionError(); + } } private void generateCodeForMethod(final @NotNull GernNode gn, final GenerateResult gr, final WorkList aWorkList) { BaseGeneratedFunction gf = (BaseGeneratedFunction) gn.carrier(); - if (gf.getFD() == null) - return; - final Generate_Code_For_Method gcfm = new Generate_Code_For_Method(this, LOG); - gcfm.generateCodeForMethod(gf, gr, aWorkList); + gn.generateCodeForMethod(gf, gr, aWorkList, this, LOG); } private void generateCodeForConstructor(final GeneratedConstructor gf, final GenerateResult gr, diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GernNode.java b/src/main/java/tripleo/elijah/stages/gen_c/GernNode.java index 58fa4d4..08d665b 100644 --- a/src/main/java/tripleo/elijah/stages/gen_c/GernNode.java +++ b/src/main/java/tripleo/elijah/stages/gen_c/GernNode.java @@ -3,6 +3,9 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import tripleo.elijah.stages.gen_fn.*; +import tripleo.elijah.stages.gen_generic.GenerateResult; +import tripleo.elijah.stages.logging.ElLog; +import tripleo.elijah.work.WorkList; public interface GernNode { @NotNull @@ -22,4 +25,16 @@ public interface GernNode { @Nullable GeneratedConstructor asConstructor(); + + default boolean isAttached() { + return false; + } + + default String identityString() { + return "mskjdlasnjdnasjkldansjdlkas"; + } + + default void generateCodeForMethod(BaseGeneratedFunction aGf, GenerateResult aGr, WorkList aWorkList, GenerateC aGenerateC, ElLog aLOG) { + throw new Error(); + } } diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GernNodeFunction.java b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeFunction.java index 060bde9..c3ce1f2 100644 --- a/src/main/java/tripleo/elijah/stages/gen_c/GernNodeFunction.java +++ b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeFunction.java @@ -1,14 +1,59 @@ package tripleo.elijah.stages.gen_c; +import com.google.common.base.Preconditions; +import tripleo.elijah.stages.gen_fn.BaseGeneratedFunction; +import tripleo.elijah.stages.gen_fn.GeneratedConstructor; import tripleo.elijah.stages.gen_fn.GeneratedFunction; import tripleo.elijah.stages.gen_fn.GeneratedNode; +import tripleo.elijah.stages.gen_generic.GenerateResult; +import tripleo.elijah.stages.logging.ElLog; +import tripleo.elijah.work.WorkList; +import tripleo.elijah_fluffy.util.SimplePrintLoggerToRemoveSoon; + +import java.util.Objects; class GernNodeFunction extends AbstractGernNode { - public GernNodeFunction(final GeneratedNode aGeneratedNode) { - setNode(aGeneratedNode); - } + public GernNodeFunction(final GeneratedNode aGeneratedNode) { + setNode(aGeneratedNode); + } + + public GeneratedFunction asFunction() { + return (GeneratedFunction) generatedNode; + } + + @Override + public boolean isAttached() { + final GeneratedFunction gf = asFunction(); + + Preconditions.checkNotNull((gf)); + + // nice catch, idea + if (gf.getFD() == null) + return false; + + return true; + } + + @Override + public String identityString() { + return Objects.requireNonNull(asFunction()).identityString(); + } + + @Override + public void generateCodeForMethod(final BaseGeneratedFunction aGf, final GenerateResult gr, final WorkList aWorkList, final GenerateC aGenerateC, final ElLog LOG) { + if (aGf instanceof GeneratedConstructor) { + LOG.err("Should not pass Constructor to GernNodeFunction::generateCodeForMethod " + aGf.identityString()); + return; + } + + if (!this.isAttached()) { + SimplePrintLoggerToRemoveSoon.println_out("** Skipping generateCodeForMethod for " + this.identityString()); + return; + } + + final GeneratedFunction gf = asFunction(); + final Generate_Code_For_Method gcfm = new Generate_Code_For_Method(aGenerateC, LOG); - public GeneratedFunction asFunction() { - return (GeneratedFunction) generatedNode; - } + gcfm.generateCodeForMethod(gf, gr, aWorkList); + } } From 057143503dae8bf88eb094d31a399f6597f4f210 Mon Sep 17 00:00:00 2001 From: Tripleo Date: Sun, 22 Sep 2024 17:59:22 -0400 Subject: [PATCH 6/7] Move generateCodeForMethod --- .../elijah/stages/gen_c/GenerateC.java | 10 +++--- .../gen_c/Generate_Code_For_Method.java | 14 +++++++-- .../stages/gen_c/GernNodeConstructor.java | 31 +++++++++++++++++++ .../elijah/stages/gen_c/GernNodeFunction.java | 12 +++---- 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java b/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java index 32f9303..20052a3 100644 --- a/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java +++ b/src/main/java/tripleo/elijah/stages/gen_c/GenerateC.java @@ -482,10 +482,12 @@ private void generateCodeForMethod(final @NotNull GernNode gn, private void generateCodeForConstructor(final GeneratedConstructor gf, final GenerateResult gr, final WorkList aWorkList) { - if (gf.getFD() == null) - return; - final Generate_Code_For_Method gcfm = new Generate_Code_For_Method(this, LOG); - gcfm.generateCodeForConstructor(gf, gr, aWorkList); + final GernNode gern = gern(gf); + if (gern instanceof GernNodeConstructor) { + generateCodeForMethod(gern, gr, aWorkList); + } else { + throw new AssertionError(); + } } String getTypeNameForGenClass(@NotNull final GeneratedNode aGenClass) { diff --git a/src/main/java/tripleo/elijah/stages/gen_c/Generate_Code_For_Method.java b/src/main/java/tripleo/elijah/stages/gen_c/Generate_Code_For_Method.java index d188207..b664186 100644 --- a/src/main/java/tripleo/elijah/stages/gen_c/Generate_Code_For_Method.java +++ b/src/main/java/tripleo/elijah/stages/gen_c/Generate_Code_For_Method.java @@ -63,7 +63,12 @@ public Generate_Code_For_Method(@NotNull final GenerateC aGenerateC, final ElLog LOG = aLog; // use log from GenerateC } - void generateCodeForMethod(final BaseGeneratedFunction gf, final GenerateResult gr, final WorkList aWorkList) { + void generateCodeForMethod(final GernNodeFunction gn, final GenerateResult gr, final WorkList aWorkList) { + final GeneratedFunction gf = gn.asFunction(); + if (gf == null) { + throw new AssertionError(); + } + final Generate_Method_Header gmh = new Generate_Method_Header(gf, gc, LOG); tos.put_string_ln(String.format("%s {", gmh.header_string)); @@ -718,8 +723,11 @@ private void __action_DECL(final @NotNull Instruction instruction, final BufferT LOG.err("8886 y is null (No typename specified)"); } - void generateCodeForConstructor(final @NotNull GeneratedConstructor gf, final GenerateResult gr, - final WorkList aWorkList) { + void generateCodeForConstructor(final GernNodeConstructor gn, + final GenerateResult gr, + final WorkList aWorkList) { + final @NotNull GeneratedConstructor gf = gn.asConstructor(); + // TODO this code is only correct for classes and not meant for namespaces final GeneratedClass x = (GeneratedClass) gf.getGenClass(); switch (x.getKlass().getType()) { diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GernNodeConstructor.java b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeConstructor.java index 9bbd0bf..7c64a5b 100644 --- a/src/main/java/tripleo/elijah/stages/gen_c/GernNodeConstructor.java +++ b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeConstructor.java @@ -1,7 +1,13 @@ package tripleo.elijah.stages.gen_c; +import tripleo.elijah.stages.gen_fn.BaseGeneratedFunction; import tripleo.elijah.stages.gen_fn.GeneratedConstructor; +import tripleo.elijah.stages.gen_fn.GeneratedFunction; import tripleo.elijah.stages.gen_fn.GeneratedNode; +import tripleo.elijah.stages.gen_generic.GenerateResult; +import tripleo.elijah.stages.logging.ElLog; +import tripleo.elijah.work.WorkList; +import tripleo.elijah_fluffy.util.SimplePrintLoggerToRemoveSoon; class GernNodeConstructor extends AbstractGernNode { public GernNodeConstructor(final GeneratedNode aGeneratedNode) { @@ -11,4 +17,29 @@ public GernNodeConstructor(final GeneratedNode aGeneratedNode) { public GeneratedConstructor asConstructor() { return (GeneratedConstructor) generatedNode; } + + @Override + public boolean isAttached() { + return generatedNode instanceof GeneratedConstructor; + } + + @Override + public void generateCodeForMethod(final BaseGeneratedFunction gf0, + final GenerateResult gr, + final WorkList aWorkList, + final GenerateC aGenerateC, + final ElLog LOG) { + if (gf0 instanceof GeneratedFunction) { + LOG.err("Should not pass Function to GernNodeConstructor::generateCodeForMethod " + gf0.identityString()); + return; + } + + if (!this.isAttached()) { + SimplePrintLoggerToRemoveSoon.println_out("** Skipping generateCodeForMethod for " + this.identityString()); + return; + } + + final Generate_Code_For_Method gcfm = new Generate_Code_For_Method(aGenerateC, LOG); + gcfm.generateCodeForConstructor(this, gr, aWorkList); + } } diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GernNodeFunction.java b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeFunction.java index c3ce1f2..85e3727 100644 --- a/src/main/java/tripleo/elijah/stages/gen_c/GernNodeFunction.java +++ b/src/main/java/tripleo/elijah/stages/gen_c/GernNodeFunction.java @@ -27,11 +27,9 @@ public boolean isAttached() { Preconditions.checkNotNull((gf)); - // nice catch, idea - if (gf.getFD() == null) - return false; - - return true; + // NOTE defensive + // noinspection ConstantValue + return gf.getFD() != null; } @Override @@ -51,9 +49,7 @@ public void generateCodeForMethod(final BaseGeneratedFunction aGf, final Generat return; } - final GeneratedFunction gf = asFunction(); final Generate_Code_For_Method gcfm = new Generate_Code_For_Method(aGenerateC, LOG); - - gcfm.generateCodeForMethod(gf, gr, aWorkList); + gcfm.generateCodeForMethod(this, gr, aWorkList); } } From 9345bb9a0ac92a6b1c10867bb07d28d378f620be Mon Sep 17 00:00:00 2001 From: Tripleo Date: Sun, 22 Sep 2024 18:18:12 -0400 Subject: [PATCH 7/7] Is this killing actions? --- src/main/java/tripleo/elijah/stages/gen_c/GernNode.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/tripleo/elijah/stages/gen_c/GernNode.java b/src/main/java/tripleo/elijah/stages/gen_c/GernNode.java index 08d665b..ebf6c7e 100644 --- a/src/main/java/tripleo/elijah/stages/gen_c/GernNode.java +++ b/src/main/java/tripleo/elijah/stages/gen_c/GernNode.java @@ -31,10 +31,10 @@ default boolean isAttached() { } default String identityString() { - return "mskjdlasnjdnasjkldansjdlkas"; + throw new RuntimeException("identityString"); } default void generateCodeForMethod(BaseGeneratedFunction aGf, GenerateResult aGr, WorkList aWorkList, GenerateC aGenerateC, ElLog aLOG) { - throw new Error(); + throw new RuntimeException("generateCodeForMethod"); } }