move code generation related functions from ZqIntegrator to ZqParser
This commit is contained in:
parent
16e0a821fa
commit
e2f691a0de
@ -153,13 +153,13 @@ public class ZqIntegrator
|
|||||||
{
|
{
|
||||||
if (OMS.IsInstanceOf(inst, OMS.GetInstance(KnownInstanceGuids.Classes.Class)))
|
if (OMS.IsInstanceOf(inst, OMS.GetInstance(KnownInstanceGuids.Classes.Class)))
|
||||||
{
|
{
|
||||||
string name = ZqNormalizeName(OMS.GetAttributeValue<string>(inst, OMS.GetInstance(KnownAttributeGuids.Text.Name)));
|
string name = Parser.NormalizeName(OMS.GetAttributeValue<string>(inst, OMS.GetInstance(KnownAttributeGuids.Text.Name)));
|
||||||
ZqClass cls = new ZqClass(name);
|
ZqClass cls = new ZqClass(name);
|
||||||
|
|
||||||
IEnumerable<InstanceHandle> insts = OMS.GetInstancesOf(inst);
|
IEnumerable<InstanceHandle> insts = OMS.GetInstancesOf(inst);
|
||||||
foreach (InstanceHandle inst2 in insts)
|
foreach (InstanceHandle inst2 in insts)
|
||||||
{
|
{
|
||||||
cls.Instances.Add(new ZqInstance() { InstanceKey = OMS.GetInstanceKey(inst2), Name = ZqNormalizeName(OMS.GetInstanceText(inst2)) });
|
cls.Instances.Add(new ZqInstance() { InstanceKey = OMS.GetInstanceKey(inst2), Name = Parser.NormalizeName(OMS.GetInstanceText(inst2)) });
|
||||||
}
|
}
|
||||||
|
|
||||||
return cls;
|
return cls;
|
||||||
@ -169,8 +169,8 @@ public class ZqIntegrator
|
|||||||
InstanceKey ik = OMS.GetInstanceKey(inst);
|
InstanceKey ik = OMS.GetInstanceKey(inst);
|
||||||
InstanceHandle destClass = OMS.GetRelatedInstance(inst, OMS.GetInstance(KnownRelationshipGuids.Relationship__has_destination__Class));
|
InstanceHandle destClass = OMS.GetRelatedInstance(inst, OMS.GetInstance(KnownRelationshipGuids.Relationship__has_destination__Class));
|
||||||
string relTypeName = OMS.GetAttributeValue<string>(inst, OMS.GetInstance(KnownAttributeGuids.Text.RelationshipType));
|
string relTypeName = OMS.GetAttributeValue<string>(inst, OMS.GetInstance(KnownAttributeGuids.Text.RelationshipType));
|
||||||
string destClassName = ZqNormalizeName(OMS.GetInstanceText(destClass));
|
string destClassName = Parser.NormalizeName(OMS.GetInstanceText(destClass));
|
||||||
string name = ZqNormalizeName(relTypeName + destClassName);
|
string name = Parser.NormalizeName(relTypeName + destClassName);
|
||||||
|
|
||||||
bool singular = OMS.GetAttributeValue<bool>(inst, OMS.GetInstance(KnownAttributeGuids.Boolean.Singular));
|
bool singular = OMS.GetAttributeValue<bool>(inst, OMS.GetInstance(KnownAttributeGuids.Boolean.Singular));
|
||||||
|
|
||||||
@ -183,7 +183,7 @@ public class ZqIntegrator
|
|||||||
string verb = OMS.GetAttributeValue<string>(inst, OMS.GetInstance(KnownAttributeGuids.Text.Verb));
|
string verb = OMS.GetAttributeValue<string>(inst, OMS.GetInstance(KnownAttributeGuids.Text.Verb));
|
||||||
string name = OMS.GetAttributeValue<string>(inst, OMS.GetInstance(KnownAttributeGuids.Text.Name));
|
string name = OMS.GetAttributeValue<string>(inst, OMS.GetInstance(KnownAttributeGuids.Text.Name));
|
||||||
string fullName = verb + name;
|
string fullName = verb + name;
|
||||||
fullName = ZqNormalizeName(fullName);
|
fullName = Parser.NormalizeName(fullName);
|
||||||
|
|
||||||
InstanceHandle ihReturnsRelationship = OMS.GetRelatedInstance(inst, OMS.GetInstance(KnownRelationshipGuids.Get_Relationship_Method__returns__Relationship));
|
InstanceHandle ihReturnsRelationship = OMS.GetRelatedInstance(inst, OMS.GetInstance(KnownRelationshipGuids.Get_Relationship_Method__returns__Relationship));
|
||||||
ZqRelationship rel = (ZqRelationship)Import(ihReturnsRelationship);
|
ZqRelationship rel = (ZqRelationship)Import(ihReturnsRelationship);
|
||||||
@ -191,111 +191,4 @@ public class ZqIntegrator
|
|||||||
}
|
}
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public string ZqNormalizeName(string fullName)
|
|
||||||
{
|
|
||||||
return fullName.Replace("-", "_").Replace(" ", "").Replace("(", "_").Replace(")", "_");
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetFunctionDefinition(ZqMethod method)
|
|
||||||
{
|
|
||||||
return String.Format("function {0}({1}) : {2}", method.Name, GenerateCode(method.Parameters), method.ReturnDataType);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string GenerateCode(IEnumerable<ZqParameter> parms)
|
|
||||||
{
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
foreach (ZqParameter parm in parms)
|
|
||||||
{
|
|
||||||
sb.Append(parm.Name);
|
|
||||||
if (!parm.DataType.Equals(ZqDataType.None))
|
|
||||||
{
|
|
||||||
sb.Append(" : ");
|
|
||||||
sb.Append(parm.DataType);
|
|
||||||
}
|
|
||||||
sb.Append(", ");
|
|
||||||
}
|
|
||||||
if (sb.Length > 2)
|
|
||||||
{
|
|
||||||
sb.Remove(sb.Length - 2, 2);
|
|
||||||
}
|
|
||||||
return sb.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public string GenerateCode(ZqAccessModifier accessModifier)
|
|
||||||
{
|
|
||||||
switch (accessModifier)
|
|
||||||
{
|
|
||||||
case ZqAccessModifier.None: return String.Empty;
|
|
||||||
// case ZqAccessModifier.Internal: return "internal";
|
|
||||||
case ZqAccessModifier.Private: return "private";
|
|
||||||
case ZqAccessModifier.Protected: return "protected";
|
|
||||||
// case ZqAccessModifier.ProtectedInternal: return "protected internal";
|
|
||||||
case ZqAccessModifier.Public: return "public";
|
|
||||||
}
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
public string GenerateCode(ZqObject? obj)
|
|
||||||
{
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
if (obj is ZqClass cls)
|
|
||||||
{
|
|
||||||
if (cls.AccessModifier != ZqAccessModifier.None)
|
|
||||||
{
|
|
||||||
sb.Append(GenerateCode(cls.AccessModifier));
|
|
||||||
sb.Append(' ');
|
|
||||||
}
|
|
||||||
sb.Append("class ");
|
|
||||||
sb.Append(cls.Name);
|
|
||||||
if (cls.InstanceKey != InstanceKey.Empty)
|
|
||||||
{
|
|
||||||
sb.Append(", ");
|
|
||||||
sb.Append(cls.InstanceKey);
|
|
||||||
}
|
|
||||||
sb.AppendLine(" {");
|
|
||||||
|
|
||||||
if (cls.Attributes.Count > 0)
|
|
||||||
{
|
|
||||||
sb.AppendLine("attributes:");
|
|
||||||
sb.AppendLine();
|
|
||||||
}
|
|
||||||
if (cls.Relationships.Count > 0)
|
|
||||||
{
|
|
||||||
sb.AppendLine("relationships:");
|
|
||||||
sb.AppendLine();
|
|
||||||
}
|
|
||||||
if (cls.Functions.Count > 0)
|
|
||||||
{
|
|
||||||
sb.AppendLine("functions:");
|
|
||||||
sb.AppendLine();
|
|
||||||
}
|
|
||||||
if (cls.Instances.Count > 0)
|
|
||||||
{
|
|
||||||
sb.AppendLine("instances:");
|
|
||||||
foreach (ZqInstance inst in cls.Instances)
|
|
||||||
{
|
|
||||||
sb.AppendLine(String.Format("\t{0}, {1}", inst.Name, inst.InstanceKey));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sb.AppendLine();
|
|
||||||
sb.AppendLine("}");
|
|
||||||
}
|
|
||||||
else if (obj is ZqMethod m)
|
|
||||||
{
|
|
||||||
sb.Append(GetFunctionDefinition(m));
|
|
||||||
sb.Append(" = ");
|
|
||||||
if (obj is ZqGetRelationshipMethod gr)
|
|
||||||
{
|
|
||||||
sb.Append("this.");
|
|
||||||
sb.Append(gr.Relationship.Name);
|
|
||||||
}
|
|
||||||
else if (obj is ZqSimpleReturnMethod m2)
|
|
||||||
{
|
|
||||||
sb.Append(m2.ReturnValue);
|
|
||||||
}
|
|
||||||
// sb.Append("{");
|
|
||||||
// sb.Append("}");
|
|
||||||
}
|
|
||||||
return sb.ToString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -974,4 +974,111 @@ public class ZqParser
|
|||||||
lastChar = text[i - 1];
|
lastChar = text[i - 1];
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string NormalizeName(string fullName)
|
||||||
|
{
|
||||||
|
return fullName.Replace("-", "_").Replace(" ", "").Replace("(", "_").Replace(")", "_");
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetFunctionDefinition(ZqMethod method)
|
||||||
|
{
|
||||||
|
return String.Format("function {0}({1}) : {2}", method.Name, GenerateCode(method.Parameters), method.ReturnDataType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GenerateCode(IEnumerable<ZqParameter> parms)
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
foreach (ZqParameter parm in parms)
|
||||||
|
{
|
||||||
|
sb.Append(parm.Name);
|
||||||
|
if (!parm.DataType.Equals(ZqDataType.None))
|
||||||
|
{
|
||||||
|
sb.Append(" : ");
|
||||||
|
sb.Append(parm.DataType);
|
||||||
|
}
|
||||||
|
sb.Append(", ");
|
||||||
|
}
|
||||||
|
if (sb.Length > 2)
|
||||||
|
{
|
||||||
|
sb.Remove(sb.Length - 2, 2);
|
||||||
|
}
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GenerateCode(ZqAccessModifier accessModifier)
|
||||||
|
{
|
||||||
|
switch (accessModifier)
|
||||||
|
{
|
||||||
|
case ZqAccessModifier.None: return String.Empty;
|
||||||
|
// case ZqAccessModifier.Internal: return "internal";
|
||||||
|
case ZqAccessModifier.Private: return "private";
|
||||||
|
case ZqAccessModifier.Protected: return "protected";
|
||||||
|
// case ZqAccessModifier.ProtectedInternal: return "protected internal";
|
||||||
|
case ZqAccessModifier.Public: return "public";
|
||||||
|
}
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
public string GenerateCode(ZqObject? obj)
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
if (obj is ZqClass cls)
|
||||||
|
{
|
||||||
|
if (cls.AccessModifier != ZqAccessModifier.None)
|
||||||
|
{
|
||||||
|
sb.Append(GenerateCode(cls.AccessModifier));
|
||||||
|
sb.Append(' ');
|
||||||
|
}
|
||||||
|
sb.Append("class ");
|
||||||
|
sb.Append(cls.Name);
|
||||||
|
if (cls.InstanceKey != InstanceKey.Empty)
|
||||||
|
{
|
||||||
|
sb.Append(", ");
|
||||||
|
sb.Append(cls.InstanceKey);
|
||||||
|
}
|
||||||
|
sb.AppendLine(" {");
|
||||||
|
|
||||||
|
if (cls.Attributes.Count > 0)
|
||||||
|
{
|
||||||
|
sb.AppendLine("attributes:");
|
||||||
|
sb.AppendLine();
|
||||||
|
}
|
||||||
|
if (cls.Relationships.Count > 0)
|
||||||
|
{
|
||||||
|
sb.AppendLine("relationships:");
|
||||||
|
sb.AppendLine();
|
||||||
|
}
|
||||||
|
if (cls.Functions.Count > 0)
|
||||||
|
{
|
||||||
|
sb.AppendLine("functions:");
|
||||||
|
sb.AppendLine();
|
||||||
|
}
|
||||||
|
if (cls.Instances.Count > 0)
|
||||||
|
{
|
||||||
|
sb.AppendLine("instances:");
|
||||||
|
foreach (ZqInstance inst in cls.Instances)
|
||||||
|
{
|
||||||
|
sb.AppendLine(String.Format("\t{0}, {1}", inst.Name, inst.InstanceKey));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.AppendLine();
|
||||||
|
sb.AppendLine("}");
|
||||||
|
}
|
||||||
|
else if (obj is ZqMethod m)
|
||||||
|
{
|
||||||
|
sb.Append(GetFunctionDefinition(m));
|
||||||
|
sb.Append(" = ");
|
||||||
|
if (obj is ZqGetRelationshipMethod gr)
|
||||||
|
{
|
||||||
|
sb.Append("this.");
|
||||||
|
sb.Append(gr.Relationship.Name);
|
||||||
|
}
|
||||||
|
else if (obj is ZqSimpleReturnMethod m2)
|
||||||
|
{
|
||||||
|
sb.Append(m2.ReturnValue);
|
||||||
|
}
|
||||||
|
// sb.Append("{");
|
||||||
|
// sb.Append("}");
|
||||||
|
}
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -246,11 +246,11 @@ public class IntegrationTests : Mocha.Core.Tests.OmsTestsBase
|
|||||||
ZqIntegrator zq = new ZqIntegrator(Oms);
|
ZqIntegrator zq = new ZqIntegrator(Oms);
|
||||||
|
|
||||||
ZqObject obj1 = zq.Import(Oms.GetInstance(KnownInstanceGuids.Classes.AccessModifier));
|
ZqObject obj1 = zq.Import(Oms.GetInstance(KnownInstanceGuids.Classes.AccessModifier));
|
||||||
string code1 = zq.GenerateCode(obj1);
|
string code1 = zq.Parser.GenerateCode(obj1);
|
||||||
|
|
||||||
|
|
||||||
ZqObject obj2 = zq.Import(Oms.GetInstance(KnownInstanceGuids.Classes.Instance));
|
ZqObject obj2 = zq.Import(Oms.GetInstance(KnownInstanceGuids.Classes.Instance));
|
||||||
string code2 = zq.GenerateCode(obj2);
|
string code2 = zq.Parser.GenerateCode(obj2);
|
||||||
|
|
||||||
// ZqObject obj2 = zq.Parser.Parse(code);
|
// ZqObject obj2 = zq.Parser.Parse(code);
|
||||||
// Assert.That(code, Is.EqualTo("function getParentClass() : Class* = this.forClass"));
|
// Assert.That(code, Is.EqualTo("function getParentClass() : Class* = this.forClass"));
|
||||||
@ -262,12 +262,12 @@ public class IntegrationTests : Mocha.Core.Tests.OmsTestsBase
|
|||||||
ZqIntegrator zq = new ZqIntegrator(Oms);
|
ZqIntegrator zq = new ZqIntegrator(Oms);
|
||||||
|
|
||||||
ZqObject obj = zq.Import(Oms.GetInstance(KnownInstanceGuids.Methods.GetRelationship.Instance__get__Parent_Class));
|
ZqObject obj = zq.Import(Oms.GetInstance(KnownInstanceGuids.Methods.GetRelationship.Instance__get__Parent_Class));
|
||||||
string code = zq.GenerateCode(obj);
|
string code = zq.Parser.GenerateCode(obj);
|
||||||
|
|
||||||
Assert.That(code, Is.EqualTo("function getParentClass() : Class* = this.forClass"));
|
Assert.That(code, Is.EqualTo("function getParentClass() : Class* = this.forClass"));
|
||||||
|
|
||||||
ZqObject obj2 = zq.Parser.Parse(code);
|
ZqObject obj2 = zq.Parser.Parse(code);
|
||||||
string code2 = zq.GenerateCode(obj2);
|
string code2 = zq.Parser.GenerateCode(obj2);
|
||||||
|
|
||||||
Assert.That(code2, Is.EqualTo(code));
|
Assert.That(code2, Is.EqualTo(code));
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user