public override void CaseADelegateExp(ADelegateExp node) { /* Replace delegate<Type>(method) * With * * "method" * * or * * "method:reciever" */ AMethodDecl method = finalTrans.data.DelegateCreationMethod[node]; string d = GetName(method); PExp replacer; APointerLvalue reciever = finalTrans.data.DelegateRecieveres[node]; if (reciever != null) { d += ":"; AStringConstExp leftSide = new AStringConstExp(new TStringLiteral("\"" + d + "\"")); replacer = new ABinopExp(leftSide, new APlusBinop(new TPlus("+")), reciever.GetBase()); finalTrans.data.ExpTypes[leftSide] = finalTrans.data.ExpTypes[replacer] = new ANamedType(new TIdentifier("string"), null); if (Util.IsIntPointer(reciever, data.LvalueTypes[reciever], data)) { ASimpleInvokeExp intToStringInvoke = new ASimpleInvokeExp(new TIdentifier("IntToString"), new ArrayList() { ((ABinopExp)replacer).GetRight() }); ((ABinopExp)replacer).SetRight(intToStringInvoke); finalTrans.data.SimpleMethodLinks[intToStringInvoke] = data.Libraries.Methods.First(m => m.GetName().Text == intToStringInvoke.GetName().Text); data.ExpTypes[intToStringInvoke] = new ANamedType(new TIdentifier("string"), null); } } else { replacer = new AStringConstExp(new TStringLiteral("\"" + d + "\"")); finalTrans.data.ExpTypes[replacer] = new ANamedType(new TIdentifier("string"), null); } MoveMethodDeclsOut mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data); node.Apply(mover); node.ReplaceBy(replacer); foreach (PStm stm in mover.NewStatements) { stm.Apply(this); } }
public override void CaseAStringConstExp(AStringConstExp node) { List <string> strings = new List <string>(); StringBuilder name = new StringBuilder(""); bool previousWasUnicode = false; foreach (char c in node.GetStringLiteral().Text) { if (c > 0xFF) { byte[] utf8Bytes = Encoding.UTF8.GetBytes(c.ToString()); foreach (byte b in utf8Bytes) { name.Append("\\x"); name.AppendFormat("{0:x2}", b); } previousWasUnicode = true; } else { if (previousWasUnicode && ((c >= '0' && c <= '9') || (char.ToLower(c) >= 'a' && char.ToLower(c) <= 'f'))) { strings.Add(name.ToString()); name.Clear(); } name.Append(c); previousWasUnicode = false; } } strings.Add(name.ToString()); if (strings.Count == 1) { node.GetStringLiteral().Text = name.ToString(); } else { strings[0] = strings[0].Remove(0, 1); strings[strings.Count - 1] = strings[strings.Count - 1].Substring(0, strings[strings.Count - 1].Length - 1); AStringConstExp left = new AStringConstExp(new TStringLiteral("\"" + strings[0] + "\"")); strings.RemoveAt(0); data.StringsDontJoinRight.Add(left); data.ExpTypes[left] = new ANamedType(new TIdentifier("string"), null); node.ReplaceBy(Combine(left, strings)); } }
private PExp Combine(PExp left, List <string> strings) { AStringConstExp right = new AStringConstExp(new TStringLiteral("\"" + strings[0] + "\"")); strings.RemoveAt(0); ABinopExp binop = new ABinopExp(left, new APlusBinop(new TPlus("+")), right); data.StringsDontJoinRight.Add(right); data.ExpTypes[right] = data.ExpTypes[binop] = new ANamedType(new TIdentifier("string"), null); if (strings.Count > 0) { return(Combine(binop, strings)); } return(binop); }
//Join string + string to string public override void CaseABinopExp(ABinopExp node) { if (node.GetBinop() is APlusBinop) { PType type = data.ExpTypes[node]; if (type is ANamedType && ((ANamedType)type).IsPrimitive("string")) { PExp other = null; if (node.GetLeft() is ANullExp) { other = node.GetRight(); } if (node.GetRight() is ANullExp) { other = node.GetLeft(); } if (other != null) { node.ReplaceBy(other); other.Apply(this); return; } } //Case (string + string) if (node.GetLeft() is AStringConstExp && node.GetRight() is AStringConstExp) { AStringConstExp left = (AStringConstExp)node.GetLeft(); AStringConstExp right = (AStringConstExp)node.GetRight(); if (!IsJoinAllowed(left.GetStringLiteral().Text, right.GetStringLiteral().Text)) { base.CaseABinopExp(node); return; } left.GetStringLiteral().Text = left.GetStringLiteral().Text.Substring(0, left.GetStringLiteral().Text. Length - 1); left.GetStringLiteral().Text += right.GetStringLiteral().Text.Substring(1); node.ReplaceBy(left); CaseAStringConstExp(left); return; } //Case (<exp> + string) + string if (node.GetLeft() is ABinopExp && node.GetRight() is AStringConstExp) { ABinopExp leftBinop = (ABinopExp)node.GetLeft(); if (leftBinop.GetBinop() is APlusBinop && leftBinop.GetRight() is AStringConstExp) { AStringConstExp left = (AStringConstExp)leftBinop.GetRight(); AStringConstExp right = (AStringConstExp)node.GetRight(); if (!IsJoinAllowed(left.GetStringLiteral().Text, right.GetStringLiteral().Text)) { base.CaseABinopExp(node); return; } left.GetStringLiteral().Text = left.GetStringLiteral().Text.Substring(0, left.GetStringLiteral(). Text. Length - 1); left.GetStringLiteral().Text += right.GetStringLiteral().Text.Substring(1); node.ReplaceBy(leftBinop); CaseABinopExp(leftBinop); return; } } //Case string + (string + <exp>) //Case (<exp> + string) + (string + <exp>) } //Case (int + int) /*if (node.GetLeft() is AIntConstExp && node.GetRight() is AIntConstExp) * { * AIntConstExp left = (AIntConstExp) node.GetLeft(); * AIntConstExp right = (AIntConstExp) node.GetRight(); * * int a = int.Parse(left.GetIntegerLiteral().Text); * int b = int.Parse(right.GetIntegerLiteral().Text); * * if (node.GetBinop() is APlusBinop) * { * a += b; * } * else if (node.GetBinop() is AMinusBinop) * { * a -= b; * } * else if (node.GetBinop() is ATimesBinop) * { * a *= b; * } * else if (node.GetBinop() is ADivideBinop) * { * if (b == 0) * { * base.CaseABinopExp(node); * return; * } * a /= b; * } * else * { * base.CaseABinopExp(node); * return; * } * * left.GetIntegerLiteral().Text = a.ToString(); * node.ReplaceBy(left); * left.Apply(this); * return; * } * //Case (<exp> + int) + int * if (node.GetLeft() is ABinopExp && node.GetRight() is AIntConstExp && (node.GetBinop() is APlusBinop || node.GetBinop() is AMinusBinop)) * { * ABinopExp leftBinop = (ABinopExp) node.GetLeft(); * PType leftType = data.ExpTypes[leftBinop]; * if (leftBinop.GetRight() is AIntConstExp && leftType is ANamedType && ((ANamedType) leftType).GetName().Text == "int" && * (leftBinop.GetBinop() is APlusBinop || leftBinop.GetBinop() is AMinusBinop)) * { * AIntConstExp left = (AIntConstExp)leftBinop.GetRight(); * AIntConstExp right = (AIntConstExp)node.GetRight(); * int a = int.Parse(left.GetIntegerLiteral().Text); * int b = int.Parse(right.GetIntegerLiteral().Text); * * if (node.GetBinop() is APlusBinop) * { * if (leftBinop.GetBinop() is APlusBinop) * { * //(<exp> + int) + int * int c = a + b; * //Test for overflow * if (a > 0 && b > 0 && (c < a || c < b) || * a < 0 && b < 0 && (c > a || c > b)) * { * //Don't add them * base.CaseABinopExp(node); * return; * } * if (c < 0) * { * //Change binop to <exp> - c * if (c != int.MinValue) * { * c = -c; * leftBinop.SetBinop(new AMinusBinop(new TMinus("-"))); * } * } * //Replace node with leftbinop * left.GetIntegerLiteral().Text = c.ToString(); * node.ReplaceBy(leftBinop); * leftBinop.Apply(this); * return; * } * else * { * //(<exp> - int) + int * int c = b - a; * //Test for overflow * if (a < 0 && b > 0 && (c < a || c < b) || * a > 0 && b < 0 && (c > a || c > b)) * { * //Don't add them * base.CaseABinopExp(node); * return; * } * if (c > 0 || c == int.MinValue) * { * //Change binop to <exp> + c * leftBinop.SetBinop(new APlusBinop(new TPlus("+"))); * * } * else * c = -c; * //Replace node with leftbinop * left.GetIntegerLiteral().Text = c.ToString(); * node.ReplaceBy(leftBinop); * leftBinop.Apply(this); * return; * } * } * else * { * if (leftBinop.GetBinop() is APlusBinop) * { * //(<exp> + int) - int * //ALso need to consider <exp> in the other position, and int on the other side of the binop * //Make a more general algorithm * } * else * { * * } * } * } * }*/ base.CaseABinopExp(node); }
public override void CaseADelegateInvokeExp(ADelegateInvokeExp node) { //Build a list of the possible methods AASourceFile currentFile = Util.GetAncestor <AASourceFile>(node); List <AMethodDecl> methods = new List <AMethodDecl>(); ANamedType type = (ANamedType)finalTrans.data.ExpTypes[node.GetReceiver()]; AMethodDecl delegateMethod = finalTrans.data.DelegateTypeLinks[type]; foreach (KeyValuePair <ADelegateExp, AMethodDecl> delegateCreationPair in finalTrans.data.DelegateCreationMethod) { if (TypeChecking.Assignable(delegateCreationPair.Key.GetType(), type)) { if (!methods.Contains(delegateCreationPair.Value)) { methods.Add(delegateCreationPair.Value); } } } MoveMethodDeclsOut mover; if (methods.Count == 0) { //Can only remove it if the return value is unused if (!(node.Parent() is AExpStm)) { finalTrans.errors.Add(new ErrorCollection.Error(node.GetToken(), currentFile, "No possible methods found for delegate invoke.")); throw new ParserException(node.GetToken(), "Delegates.OutADelegateInvokeExp"); } mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data); foreach (Node arg in node.GetArgs()) { arg.Apply(mover); } node.Parent().Parent().RemoveChild(node.Parent()); foreach (PStm stm in mover.NewStatements) { stm.Apply(this); } return; } if (methods.Count == 1) { ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList()); while (node.GetArgs().Count > 0) { invoke.GetArgs().Add(node.GetArgs()[0]); } //If we have a struct method, add the pointer from the delegate if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(methods[0]))) { AStructDecl targetStr = finalTrans.data.StructMethods.First(str => str.Value.Contains(methods[0])).Key; AMethodDecl getPointerDecl = GetPointerMethod(targetStr.GetDimention() != null); ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList() { node.GetReceiver() }); invoke.GetArgs().Add(getPointerInvoke); finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl; finalTrans.data.ExpTypes[getPointerInvoke] = getPointerDecl.GetReturnType(); } finalTrans.data.SimpleMethodLinks[invoke] = methods[0]; finalTrans.data.ExpTypes[invoke] = methods[0].GetReturnType(); node.ReplaceBy(invoke); return; } //Multiple methods. Make /* * <Methods moved out from reciever> * string delegate = GetMethodPart(<reciever>); * if (delegate == "...") * { * Foo(...); * } * else if (delegate == "...") * { * Bar(..., GetPointerPart(<reciever>); * } * else if(...) * ... * else * { * UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("[<file>:<line>]: No methods matched delegate.")); * int i = 1/0; * return; * } * */ AABlock block = new AABlock(new ArrayList(), new TRBrace("}")); mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data); node.GetReceiver().Apply(mover); AMethodDecl methodPartMethod = GetMethodMethod(); ASimpleInvokeExp methodPartInvoke = new ASimpleInvokeExp(new TIdentifier("GetMethodPart"), new ArrayList() { Util.MakeClone(node.GetReceiver(), finalTrans.data) }); finalTrans.data.SimpleMethodLinks[methodPartInvoke] = methodPartMethod; finalTrans.data.ExpTypes[methodPartInvoke] = methodPartMethod.GetReturnType(); AALocalDecl methodPartDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("methodPart"), methodPartInvoke); block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), methodPartDecl)); //If the invoke's return value is used, get the lvalue PLvalue leftSide; if (node.Parent() is AALocalDecl) { leftSide = new ALocalLvalue(new TIdentifier("renameMe")); finalTrans.data.LocalLinks[(ALocalLvalue)leftSide] = (AALocalDecl)node.Parent(); finalTrans.data.LvalueTypes[leftSide] = new ANamedType(new TIdentifier("string"), null); PStm pStm = Util.GetAncestor <PStm>(node); AABlock pBlock = (AABlock)pStm.Parent(); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm) + 1, new ABlockStm(new TLBrace("{"), block)); node.Parent().RemoveChild(node); } else if (node.Parent() is AAssignmentExp) { AAssignmentExp assignExp = (AAssignmentExp)node.Parent(); leftSide = assignExp.GetLvalue(); leftSide.Apply(mover); PStm pStm = Util.GetAncestor <PStm>(node); pStm.ReplaceBy(new ABlockStm(new TLBrace("{"), block)); } else if (node.Parent() is AExpStm) { //No assignments needed leftSide = null; node.Parent().ReplaceBy(new ABlockStm(new TLBrace("{"), block)); } else { //Create a new local AALocalDecl leftSideDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(delegateMethod.GetReturnType(), finalTrans.data), new TIdentifier("delegateVar"), null); ALocalLvalue leftSideLink = new ALocalLvalue(new TIdentifier("delegateVar")); ALvalueExp leftSideLinkExp = new ALvalueExp(leftSideLink); PStm pStm = Util.GetAncestor <PStm>(node); AABlock pBlock = (AABlock)pStm.Parent(); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ABlockStm(new TLBrace("{"), block)); node.ReplaceBy(leftSideLinkExp); finalTrans.data.LocalLinks[leftSideLink] = leftSideDecl; finalTrans.data.LvalueTypes[leftSideLink] = finalTrans.data.ExpTypes[leftSideLinkExp] = leftSideDecl.GetType(); leftSide = leftSideLink; block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), leftSideDecl)); } ABlockStm elseBranch; //Make final else branch /* { * UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("<file>[<line>, <pos>]: No methods matched delegate.")); * IntToString(1/0); * return; * } */ { AABlock innerBlock = new AABlock(new ArrayList(), new TRBrace("}")); ASimpleInvokeExp playerGroupInvoke = new ASimpleInvokeExp(new TIdentifier("PlayerGroupAll"), new ArrayList()); AFieldLvalue messageAreaLink = new AFieldLvalue(new TIdentifier("c_messageAreaDebug")); ALvalueExp messageAreaLinkExp = new ALvalueExp(messageAreaLink); AStringConstExp stringConst = new AStringConstExp( new TStringLiteral("\"" + currentFile.GetName().Text.Replace('\\', '/') + "[" + node.GetToken().Line + ", " + node.GetToken().Pos + "]: Got a null delegate.\"")); ASimpleInvokeExp stringToTextInvoke = new ASimpleInvokeExp(new TIdentifier("StringToText"), new ArrayList() { stringConst }); ASimpleInvokeExp displayMessageInvoke = new ASimpleInvokeExp(new TIdentifier("UIDisplayMessage"), new ArrayList() { playerGroupInvoke, messageAreaLinkExp, stringToTextInvoke }); AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral("0")); ABinopExp binop = new ABinopExp(intConst1, new ADivideBinop(new TDiv("/")), intConst2); ASimpleInvokeExp intToStringInvoke = new ASimpleInvokeExp(new TIdentifier("IntToString"), new ArrayList() { binop }); innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), displayMessageInvoke)); innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), intToStringInvoke)); //innerBlock.GetStatements().Add(new AVoidReturnStm(new TReturn("return"))); elseBranch = new ABlockStm(new TLBrace("{"), innerBlock); finalTrans.data.SimpleMethodLinks[playerGroupInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == playerGroupInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[stringToTextInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringToTextInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[displayMessageInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == displayMessageInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[intToStringInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == intToStringInvoke.GetName().Text); finalTrans.data.FieldLinks[messageAreaLink] = finalTrans.data.Libraries.Fields.First(m => m.GetName().Text == messageAreaLink.GetName().Text); finalTrans.data.ExpTypes[playerGroupInvoke] = finalTrans.data.SimpleMethodLinks[playerGroupInvoke].GetReturnType(); finalTrans.data.LvalueTypes[messageAreaLink] = finalTrans.data.ExpTypes[messageAreaLinkExp] = finalTrans.data.FieldLinks[messageAreaLink].GetType(); finalTrans.data.ExpTypes[stringToTextInvoke] = finalTrans.data.SimpleMethodLinks[stringToTextInvoke].GetReturnType(); finalTrans.data.ExpTypes[stringConst] = finalTrans.data.ExpTypes[intToStringInvoke] = new ANamedType(new TIdentifier("string"), null); finalTrans.data.ExpTypes[displayMessageInvoke] = new AVoidType(); finalTrans.data.ExpTypes[intConst1] = finalTrans.data.ExpTypes[intConst2] = finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("int"), null); } foreach (AMethodDecl method in methods) { /* * if (delegate == "...") * { * Foo(...); * } * else if (delegate == "...") * { * Bar(..., GetPointerPart(<reciever>); * } * else if(...) * ... */ AABlock innerBlock = new AABlock(new ArrayList(), new TRBrace("}")); ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier(method.GetName().Text), new ArrayList()); for (int i = 0; i < node.GetArgs().Count; i++) { PExp arg = (PExp)node.GetArgs()[i]; invoke.GetArgs().Add(Util.MakeClone(arg, finalTrans.data)); } //If we have a struct method, add the pointer from the delegate if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(method))) { AStructDecl targetStr = finalTrans.data.StructMethods.First(str => str.Value.Contains(method)).Key; AMethodDecl getPointerDecl = GetPointerMethod(targetStr.GetDimention() != null); ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList() { Util.MakeClone(node.GetReceiver(), data) }); invoke.GetArgs().Add(getPointerInvoke); finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl; finalTrans.data.ExpTypes[getPointerInvoke] = getPointerDecl.GetReturnType(); } finalTrans.data.SimpleMethodLinks[invoke] = method; finalTrans.data.ExpTypes[invoke] = method.GetReturnType(); if (leftSide == null) { innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), invoke)); } else { AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(leftSide, finalTrans.data), invoke); finalTrans.data.ExpTypes[assignment] = finalTrans.data.ExpTypes[invoke]; innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment)); } ALocalLvalue methodPartLink = new ALocalLvalue(new TIdentifier("methodPart")); ALvalueExp methodPartLinkExp = new ALvalueExp(methodPartLink); AStringConstExp stringConst = new AStringConstExp(new TStringLiteral("\"" + GetName(method) + "\"")); finalTrans.data.LocalLinks[methodPartLink] = methodPartDecl; finalTrans.data.LvalueTypes[methodPartLink] = finalTrans.data.ExpTypes[methodPartLinkExp] = finalTrans.data.ExpTypes[stringConst] = new ANamedType(new TIdentifier("string"), null); ABinopExp binop = new ABinopExp(methodPartLinkExp, new AEqBinop(new TEq("==")), stringConst); finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("bool"), null); AIfThenElseStm ifThenElse = new AIfThenElseStm(new TLParen("("), binop, new ABlockStm(new TLBrace("{"), innerBlock), elseBranch); elseBranch = new ABlockStm(new TLBrace("{"), new AABlock(new ArrayList() { ifThenElse }, new TRBrace("}"))); } block.GetStatements().Add(elseBranch); }
private AMethodDecl GetMethodMethod() { if (GetMethodPartMethod != null) { return(GetMethodPartMethod); } /* * string GetMethodPart(string delegate) * { * int i = StringFind(delegate, ":", false); * if (i == -1) * { * return delegate; * } * return StringSub(delegate, 1, i - 1); * } */ AASourceFile sourceFile = Util.GetAncestor <AASourceFile>(finalTrans.mainEntry); AALocalDecl delegateFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("delegate"), null); ALocalLvalue delegateRef1 = new ALocalLvalue(new TIdentifier("delegate")); ALocalLvalue delegateRef2 = new ALocalLvalue(new TIdentifier("delegate")); ALocalLvalue delegateRef3 = new ALocalLvalue(new TIdentifier("delegate")); ALvalueExp delegateRef1Exp = new ALvalueExp(delegateRef1); ALvalueExp delegateRef2Exp = new ALvalueExp(delegateRef2); ALvalueExp delegateRef3Exp = new ALvalueExp(delegateRef3); AStringConstExp stringConst = new AStringConstExp(new TStringLiteral("\":\"")); ABooleanConstExp booleanConst = new ABooleanConstExp(new AFalseBool()); AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("-1")); AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst3 = new AIntConstExp(new TIntegerLiteral("1")); ASimpleInvokeExp stringFindInvoke = new ASimpleInvokeExp(new TIdentifier("StringFind"), new ArrayList() { delegateRef1Exp, stringConst, booleanConst }); AALocalDecl iDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("int"), null), new TIdentifier("i"), stringFindInvoke); ALocalLvalue iRef1 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue iRef2 = new ALocalLvalue(new TIdentifier("i")); ALvalueExp iRef1Exp = new ALvalueExp(iRef1); ALvalueExp iRef2Exp = new ALvalueExp(iRef2); ABinopExp binop1 = new ABinopExp(iRef1Exp, new AEqBinop(new TEq("==")), intConst1); AIfThenStm ifThen = new AIfThenStm(new TLParen("("), binop1, new ABlockStm(new TLBrace("{"), new AABlock( new ArrayList() { new AValueReturnStm(new TReturn("return"), delegateRef2Exp) }, new TRBrace("}")))); ABinopExp binop2 = new ABinopExp(iRef2Exp, new AMinusBinop(new TMinus("-")), intConst3); ASimpleInvokeExp stringSubInvoke = new ASimpleInvokeExp(new TIdentifier("StringSub"), new ArrayList() { delegateRef3Exp, intConst2, binop2 }); GetMethodPartMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("GetMethodPart", finalTrans.data.LineCounts[sourceFile] + 1, 1), new ArrayList() { delegateFormal }, new AABlock( new ArrayList() { new ALocalDeclStm(new TSemicolon(";"), iDecl), ifThen, new AValueReturnStm(new TReturn("return"), stringSubInvoke) }, new TRBrace("}"))); sourceFile.GetDecl().Add(GetMethodPartMethod); finalTrans.data.LocalLinks[delegateRef1] = finalTrans.data.LocalLinks[delegateRef2] = finalTrans.data.LocalLinks[delegateRef3] = delegateFormal; finalTrans.data.LocalLinks[iRef1] = finalTrans.data.LocalLinks[iRef2] = iDecl; finalTrans.data.LvalueTypes[delegateRef1] = finalTrans.data.LvalueTypes[delegateRef2] = finalTrans.data.LvalueTypes[delegateRef3] = finalTrans.data.ExpTypes[delegateRef1Exp] = finalTrans.data.ExpTypes[delegateRef2Exp] = finalTrans.data.ExpTypes[delegateRef3Exp] = finalTrans.data.ExpTypes[stringConst] = finalTrans.data.ExpTypes[stringSubInvoke] = new ANamedType(new TIdentifier("string"), null); finalTrans.data.LvalueTypes[iRef1] = finalTrans.data.LvalueTypes[iRef2] = finalTrans.data.ExpTypes[iRef1Exp] = finalTrans.data.ExpTypes[iRef2Exp] = finalTrans.data.ExpTypes[stringFindInvoke] = finalTrans.data.ExpTypes[intConst1] = finalTrans.data.ExpTypes[intConst2] = finalTrans.data.ExpTypes[intConst3] = finalTrans.data.ExpTypes[binop2] = new ANamedType(new TIdentifier("int"), null); finalTrans.data.ExpTypes[booleanConst] = finalTrans.data.ExpTypes[binop1] = new ANamedType(new TIdentifier("bool"), null); finalTrans.data.SimpleMethodLinks[stringFindInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringFindInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[stringSubInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringSubInvoke.GetName().Text); return(GetMethodPartMethod); }
private AMethodDecl GetStringPointerMethod() { if (GetStringPointerPartMethod != null) { return(GetStringPointerPartMethod); } /* * string GetPointerPart(string delegate) * { * return StringSub(delegate, StringFind(delegate, ":", false) + 1, StringLength(delegate)); * } */ AASourceFile sourceFile = Util.GetAncestor <AASourceFile>(finalTrans.mainEntry); AALocalDecl delegateFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("delegate"), null); ALocalLvalue delegateRef1 = new ALocalLvalue(new TIdentifier("delegate")); ALocalLvalue delegateRef2 = new ALocalLvalue(new TIdentifier("delegate")); ALocalLvalue delegateRef3 = new ALocalLvalue(new TIdentifier("delegate")); ALvalueExp delegateRef1Exp = new ALvalueExp(delegateRef1); ALvalueExp delegateRef2Exp = new ALvalueExp(delegateRef2); ALvalueExp delegateRef3Exp = new ALvalueExp(delegateRef3); AStringConstExp stringConst = new AStringConstExp(new TStringLiteral("\":\"")); ABooleanConstExp booleanConst = new ABooleanConstExp(new AFalseBool()); ASimpleInvokeExp stringFindInvoke = new ASimpleInvokeExp(new TIdentifier("StringFind"), new ArrayList() { delegateRef2Exp, stringConst, booleanConst }); AIntConstExp intConst = new AIntConstExp(new TIntegerLiteral("1")); ABinopExp binop = new ABinopExp(stringFindInvoke, new APlusBinop(new TPlus("+")), intConst); ASimpleInvokeExp stringLengthInvoke = new ASimpleInvokeExp(new TIdentifier("StringLength"), new ArrayList() { delegateRef3Exp }); ASimpleInvokeExp stringSubInvoke = new ASimpleInvokeExp(new TIdentifier("StringSub"), new ArrayList() { delegateRef1Exp, binop, stringLengthInvoke }); GetStringPointerPartMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("GetPointerPart", finalTrans.data.LineCounts[sourceFile] + 1, 1), new ArrayList() { delegateFormal }, new AABlock( new ArrayList() { new AValueReturnStm(new TReturn("return"), stringSubInvoke) }, new TRBrace("}"))); sourceFile.GetDecl().Add(GetStringPointerPartMethod); data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(sourceFile, GetStringPointerPartMethod)); finalTrans.data.LocalLinks[delegateRef1] = finalTrans.data.LocalLinks[delegateRef2] = finalTrans.data.LocalLinks[delegateRef3] = delegateFormal; finalTrans.data.LvalueTypes[delegateRef1] = finalTrans.data.LvalueTypes[delegateRef2] = finalTrans.data.LvalueTypes[delegateRef3] = finalTrans.data.ExpTypes[delegateRef1Exp] = finalTrans.data.ExpTypes[delegateRef2Exp] = finalTrans.data.ExpTypes[delegateRef3Exp] = finalTrans.data.ExpTypes[stringConst] = finalTrans.data.ExpTypes[stringSubInvoke] = new ANamedType(new TIdentifier("string"), null); finalTrans.data.ExpTypes[booleanConst] = new ANamedType(new TIdentifier("bool"), null); finalTrans.data.ExpTypes[intConst] = finalTrans.data.ExpTypes[binop] = finalTrans.data.ExpTypes[stringFindInvoke] = finalTrans.data.ExpTypes[stringLengthInvoke] = new ANamedType(new TIdentifier("int"), null); finalTrans.data.SimpleMethodLinks[stringFindInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringFindInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[stringLengthInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringLengthInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[stringSubInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringSubInvoke.GetName().Text); return(GetStringPointerPartMethod); }
public override void CaseAStringConstExp(AStringConstExp node) { Value += node.GetStringLiteral().Text; }
public override void CaseABinopExp(ABinopExp node) { bool pushed = false; if (!(node.Parent() is ABinopExp)) { PushStack(); pushed = true; } try { bool isIntegerType = data.ExpTypes[node] is ANamedType && (((ANamedType)data.ExpTypes[node]).IsPrimitive("int") || ((ANamedType)data.ExpTypes[node]).IsPrimitive("byte")); if (isIntegerType) { if (node.GetBinop() is APlusBinop || node.GetBinop() is AMinusBinop) { node.GetLeft().Apply(this); if (!Util.HasAncestor <AAProgram>(node)) { return; } if (node.GetBinop() is AMinusBinop) { isNegativeRightSide = !isNegativeRightSide; } node.GetRight().Apply(this); if (node.GetBinop() is AMinusBinop) { isNegativeRightSide = !isNegativeRightSide; } if (!Util.HasAncestor <AAProgram>(node)) { return; } for (int i = 0; i < intConsts.Count; i++) { for (int j = i + 1; j < intConsts.Count; j++) { Pair <AIntConstExp, bool> const1 = intConsts[i]; Pair <AIntConstExp, bool> const2 = intConsts[j]; ABinopExp pBinop1 = (ABinopExp)const1.Car.Parent(); ABinopExp pBinop2 = (ABinopExp)const2.Car.Parent(); int a = int.Parse(const1.Car.GetIntegerLiteral().Text); int b = int.Parse(const2.Car.GetIntegerLiteral().Text); int c; if (const1.Cdr != const2.Cdr) { c = a - b; } else { c = a + b; } //Eliminate stuff like <exp> + -1 if (c < 0 && pBinop1.GetRight() == const1.Car) { c = -c; if (pBinop1.GetBinop() is AMinusBinop) { pBinop1.SetBinop(new APlusBinop(new TPlus("+"))); } else { pBinop1.SetBinop(new AMinusBinop(new TMinus("-"))); } const1.Cdr = !const1.Cdr; } const1.Car.GetIntegerLiteral().Text = c.ToString(); //Remove binop2 if (pBinop2.GetLeft() == const2.Car) { if (pBinop2.GetBinop() is AMinusBinop) { if (pBinop2.GetRight() is AIntConstExp) { AIntConstExp const3 = (AIntConstExp)pBinop2.GetRight(); const3.GetIntegerLiteral().Text = (-int.Parse(const3.GetIntegerLiteral().Text)).ToString(); pBinop2.ReplaceBy(const3); intConsts.Add(new Pair <AIntConstExp, bool>(const3, isNegativeRightSide)); } else { AUnopExp unop = new AUnopExp(new ANegateUnop(new TMinus("-")), pBinop2.GetRight()); data.ExpTypes[unop] = new ANamedType(new TIdentifier("int"), null); pBinop2.ReplaceBy(unop); } } else { pBinop2.ReplaceBy(pBinop2.GetRight()); } } else { pBinop2.ReplaceBy(pBinop2.GetLeft()); } intConsts.RemoveAt(j); j--; } } return; } } { PushStack(); node.GetLeft().Apply(this); PopStack(); PushStack(); node.GetRight().Apply(this); PopStack(); } if (isIntegerType && (node.GetBinop() is ATimesBinop || node.GetBinop() is ADivideBinop) && node.GetLeft() is AIntConstExp && node.GetRight() is AIntConstExp) { AIntConstExp const1 = (AIntConstExp)node.GetLeft(); AIntConstExp const2 = (AIntConstExp)node.GetRight(); int a = int.Parse(const1.GetIntegerLiteral().Text); int b = int.Parse(const2.GetIntegerLiteral().Text); int c; if (node.GetBinop() is ATimesBinop || b != 0) { if (node.GetBinop() is ATimesBinop) { c = a * b; } else { c = a / b; } const1.GetIntegerLiteral().Text = c.ToString(); node.ReplaceBy(const1); const1.Apply(this); return; } } if (node.GetBinop() is AEqBinop || node.GetBinop() is ANeBinop) { if (node.GetLeft() is ABooleanConstExp && node.GetRight() is ABooleanConstExp) { bool b1 = ((ABooleanConstExp)node.GetLeft()).GetBool() is ATrueBool; bool b2 = ((ABooleanConstExp)node.GetRight()).GetBool() is ATrueBool; bool b3 = false; if (node.GetBinop() is AEqBinop) { b3 = b1 == b2; } else if (node.GetBinop() is ANeBinop) { b3 = b1 != b2; } ((ABooleanConstExp)node.GetLeft()).SetBool(b3 ? (PBool) new ATrueBool() : new AFalseBool()); node.ReplaceBy(node.GetLeft()); return; } else if (node.GetLeft() is AIntConstExp && node.GetRight() is AIntConstExp) { AIntConstExp const1 = (AIntConstExp)node.GetLeft(); AIntConstExp const2 = (AIntConstExp)node.GetRight(); int a = int.Parse(const1.GetIntegerLiteral().Text); int b = int.Parse(const2.GetIntegerLiteral().Text); bool c = false; if (node.GetBinop() is AEqBinop) { c = a == b; } else if (node.GetBinop() is ANeBinop) { c = a != b; } ABooleanConstExp booleanExp = new ABooleanConstExp(c ? (PBool) new ATrueBool() : new AFalseBool()); data.ExpTypes[booleanExp] = new ANamedType(new TIdentifier("bool"), null); node.ReplaceBy(booleanExp); return; } else if (node.GetLeft() is ANullExp && node.GetRight() is ANullExp) { ABooleanConstExp booleanExp = new ABooleanConstExp(node.GetBinop() is AEqBinop ? (PBool) new ATrueBool() : new AFalseBool()); data.ExpTypes[booleanExp] = new ANamedType(new TIdentifier("bool"), null); node.ReplaceBy(booleanExp); return; } else if (node.GetLeft() is AStringConstExp && node.GetRight() is AStringConstExp) { AStringConstExp const1 = (AStringConstExp)node.GetLeft(); AStringConstExp const2 = (AStringConstExp)node.GetRight(); string a = const1.GetStringLiteral().Text; string b = const2.GetStringLiteral().Text; bool c = false; if (node.GetBinop() is AEqBinop) { c = a == b; } else if (node.GetBinop() is ANeBinop) { c = a != b; } ABooleanConstExp booleanExp = new ABooleanConstExp(c ? (PBool) new ATrueBool() : new AFalseBool()); data.ExpTypes[booleanExp] = new ANamedType(new TIdentifier("bool"), null); node.ReplaceBy(booleanExp); return; } } if ((node.GetLeft() is ABooleanConstExp || node.GetRight() is ABooleanConstExp) && (node.GetBinop() is ALazyAndBinop || node.GetBinop() is ALazyOrBinop)) { ABooleanConstExp boolExp; PExp other; if (node.GetLeft() is ABooleanConstExp) { boolExp = (ABooleanConstExp)node.GetLeft(); other = node.GetRight(); } else { boolExp = (ABooleanConstExp)node.GetRight(); other = node.GetLeft(); } if (node.GetBinop() is ALazyAndBinop) { if (boolExp.GetBool() is ATrueBool) { //true && <exp> node.ReplaceBy(other); } else { //false && <exp> node.ReplaceBy(boolExp); } } else { if (boolExp.GetBool() is ATrueBool) { //true || <exp> node.ReplaceBy(boolExp); } else { //false || <exp> node.ReplaceBy(other); } } return; } } finally { if (pushed) { PopStack(); } } }
public override void DefaultIn(Node node) { if (!canMerge) { return; } if (node is AMethodDecl) { //First node - no need to fetch if (((AMethodDecl)node).GetFormals().Count != ((AMethodDecl)otherNode).GetFormals().Count) { canMerge = false; } return; } //Fetch corrosponding other node int index = 0; GetChildTypeIndex getChildTypeIndex = new GetChildTypeIndex() { Parent = node.Parent(), Child = node }; node.Parent().Apply(getChildTypeIndex); index = getChildTypeIndex.Index; GetChildTypeByIndex getChildTypeByIndex = new GetChildTypeByIndex() { Child = node, Index = index, Parent = otherNode }; otherNode.Apply(getChildTypeByIndex); otherNode = getChildTypeByIndex.Child; if (otherNode.GetType() != node.GetType()) { canMerge = false; return; } if (node is AALocalDecl) { locals.Add((AALocalDecl)node); otherLocals.Add((AALocalDecl)otherNode); return; } if (node is ANamedType) { ANamedType aNode = (ANamedType)node; ANamedType aOther = (ANamedType)otherNode; if (data.StructTypeLinks.ContainsKey(aNode) != data.StructTypeLinks.ContainsKey(aOther)) { canMerge = false; return; } if (data.StructTypeLinks.ContainsKey(aNode) && data.StructTypeLinks[aNode] != data.StructTypeLinks[aOther]) { canMerge = false; } if (!data.StructTypeLinks.ContainsKey(aNode) && aNode.IsSame(aOther, true))//aNode.GetName().Text != aOther.GetName().Text) { canMerge = false; } if (aNode.IsPrimitive() && !aOther.IsPrimitive(aNode.AsIdentifierString())) { canMerge = false; } return; } if (node is AABlock) { AABlock aNode = (AABlock)node; AABlock aOther = (AABlock)otherNode; if (aNode.GetStatements().Count != aOther.GetStatements().Count) { canMerge = false; } return; } if (node is AIntConstExp) { AIntConstExp aNode = (AIntConstExp)node; AIntConstExp aOther = (AIntConstExp)otherNode; if (aNode.GetIntegerLiteral().Text != aOther.GetIntegerLiteral().Text) { canMerge = false; } return; } if (node is AFixedConstExp) { AFixedConstExp aNode = (AFixedConstExp)node; AFixedConstExp aOther = (AFixedConstExp)otherNode; if (aNode.GetFixedLiteral().Text != aOther.GetFixedLiteral().Text) { canMerge = false; } return; } if (node is AStringConstExp) { AStringConstExp aNode = (AStringConstExp)node; AStringConstExp aOther = (AStringConstExp)otherNode; if (aNode.GetStringLiteral().Text != aOther.GetStringLiteral().Text) { canMerge = false; } return; } if (node is ACharConstExp) { ACharConstExp aNode = (ACharConstExp)node; ACharConstExp aOther = (ACharConstExp)otherNode; if (aNode.GetCharLiteral().Text != aOther.GetCharLiteral().Text) { canMerge = false; } return; } if (node is ASimpleInvokeExp) { ASimpleInvokeExp aNode = (ASimpleInvokeExp)node; ASimpleInvokeExp aOther = (ASimpleInvokeExp)otherNode; if (data.SimpleMethodLinks[aNode] != data.SimpleMethodLinks[aOther] && !(data.SimpleMethodLinks[aNode] == Util.GetAncestor <AMethodDecl>(aNode) && data.SimpleMethodLinks[aOther] == Util.GetAncestor <AMethodDecl>(aOther))) { canMerge = false; } return; } if (node is ALocalLvalue) { ALocalLvalue aNode = (ALocalLvalue)node; ALocalLvalue aOther = (ALocalLvalue)otherNode; if (locals.IndexOf(data.LocalLinks[aNode]) != otherLocals.IndexOf(data.LocalLinks[aOther])) { canMerge = false; } return; } if (node is AFieldLvalue) { AFieldLvalue aNode = (AFieldLvalue)node; AFieldLvalue aOther = (AFieldLvalue)otherNode; if (data.FieldLinks[aNode] != data.FieldLinks[aOther]) { canMerge = false; } return; } if (node is AStructLvalue) { AStructLvalue aNode = (AStructLvalue)node; AStructLvalue aOther = (AStructLvalue)otherNode; if (data.StructFieldLinks[aNode] != data.StructFieldLinks[aOther]) { canMerge = false; } return; } }
private List <PStm> AssignDefault(PLvalue lvalue) { List <PStm> returner = new List <PStm>(); PType type = data.LvalueTypes[lvalue]; PExp rightSide = null; if (type is ANamedType) { ANamedType aType = (ANamedType)type; if (aType.IsPrimitive("string"))//aType.GetName().Text == "string") { rightSide = new AStringConstExp(new TStringLiteral("\"\"")); data.ExpTypes[rightSide] = new ANamedType(new TIdentifier("string"), null); } else if (aType.IsPrimitive(GalaxyKeywords.NullablePrimitives.words)) //GalaxyKeywords.NullablePrimitives.words.Contains(aType.GetName().Text)) { rightSide = new ANullExp(); data.ExpTypes[rightSide] = new ANamedType(new TIdentifier("null"), null); } else if (aType.IsPrimitive(new [] { "int", "byte", "fixed" })) /*aType.GetName().Text == "int" || * aType.GetName().Text == "byte" || * aType.GetName().Text == "fixed")*/ { rightSide = new AIntConstExp(new TIntegerLiteral("0")); data.ExpTypes[rightSide] = type; } else if (aType.IsPrimitive("bool"))//aType.GetName().Text == "bool") { rightSide = new ABooleanConstExp(new AFalseBool()); data.ExpTypes[rightSide] = type; } else if (aType.IsPrimitive("color"))//aType.GetName().Text == "color") { PExp arg1 = new AIntConstExp(new TIntegerLiteral("0")); PExp arg2 = new AIntConstExp(new TIntegerLiteral("0")); PExp arg3 = new AIntConstExp(new TIntegerLiteral("0")); ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("Color"), new ArrayList() { arg1, arg2, arg3 }); rightSide = invoke; data.ExpTypes[rightSide] = type; data.ExpTypes[arg1] = data.ExpTypes[arg2] = data.ExpTypes[arg3] = new ANamedType(new TIdentifier("int"), null); data.SimpleMethodLinks[invoke] = data.Libraries.Methods.First(func => func.GetName().Text == invoke.GetName().Text); } else if (aType.IsPrimitive("char"))//aType.GetName().Text == "char") { //Dunno?! rightSide = new ACharConstExp(new TCharLiteral("'\0'")); data.ExpTypes[rightSide] = type; } else //Struct { AStructDecl str = data.StructTypeLinks[aType]; foreach (AALocalDecl localDecl in str.GetLocals()) { ALvalueExp reciever = new ALvalueExp(Util.MakeClone(lvalue, data)); AStructLvalue newLvalue = new AStructLvalue(reciever, new ADotDotType(new TDot(".")), new TIdentifier(localDecl.GetName().Text)); data.StructFieldLinks[newLvalue] = localDecl; data.ExpTypes[reciever] = type; data.LvalueTypes[newLvalue] = localDecl.GetType(); returner.AddRange(AssignDefault(newLvalue)); } return(returner); } AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(lvalue, data), rightSide); data.ExpTypes[assignment] = type; return(new List <PStm>() { new AExpStm(new TSemicolon(";"), assignment) }); } if (type is AArrayTempType) { AArrayTempType aType = (AArrayTempType)type; for (int i = 0; i < int.Parse(aType.GetIntDim().Text); i++) { ALvalueExp reciever = new ALvalueExp(Util.MakeClone(lvalue, data)); AArrayLvalue newLvalue = new AArrayLvalue(new TLBracket("["), reciever, new AIntConstExp(new TIntegerLiteral(i.ToString()))); data.ExpTypes[reciever] = type; data.LvalueTypes[newLvalue] = aType.GetType(); data.ExpTypes[newLvalue.GetIndex()] = new ANamedType(new TIdentifier("int"), null); returner.AddRange(AssignDefault(newLvalue)); } return(returner); } throw new Exception("Unexpected type. (LivenessAnalasys.AssignDefault), got " + type); }
private void MakeInitializerInvokes() { AABlock block = (AABlock)mainEntry.GetBlock(); AASourceFile file = Util.GetAncestor <AASourceFile>(mainEntry); AMethodDecl invokeMethod; /* Add * void Invoke(string methodName) * { * trigger initTrigger = TriggerCreate(methodName); * TriggerExecute(initTrigger, false, true); * TriggerDestroy(initTrigger); * } */ { //void Invoke(string methodName) AALocalDecl parameter = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("methodName"), null); AABlock methodBody = new AABlock(); invokeMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new AVoidType(new TVoid("void")), new TIdentifier("Invoke"), new ArrayList() { parameter }, methodBody); //trigger initTrigger = TriggerCreate(methodName); ALocalLvalue parameterLvalue = new ALocalLvalue(new TIdentifier("methodName")); data.LocalLinks[parameterLvalue] = parameter; ALvalueExp parameterLvalueExp = new ALvalueExp(parameterLvalue); data.LvalueTypes[parameterLvalue] = data.ExpTypes[parameterLvalueExp] = parameter.GetType(); ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("TriggerCreate"), new ArrayList() { parameterLvalueExp }); data.ExpTypes[invoke] = new ANamedType(new TIdentifier("trigger"), null); foreach (AMethodDecl methodDecl in data.Libraries.Methods) { if (methodDecl.GetName().Text == "TriggerCreate") { data.SimpleMethodLinks[invoke] = methodDecl; break; } } AALocalDecl initTriggerDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("trigger"), null), new TIdentifier("initTrigger"), invoke); methodBody.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), initTriggerDecl)); //TriggerExecute(initTrigger, false, true); ALocalLvalue initTriggerLvalue = new ALocalLvalue(new TIdentifier("initTrigger")); data.LocalLinks[initTriggerLvalue] = initTriggerDecl; ALvalueExp initTriggerLvalueExp = new ALvalueExp(initTriggerLvalue); data.LvalueTypes[initTriggerLvalue] = data.ExpTypes[initTriggerLvalueExp] = initTriggerDecl.GetType(); ABooleanConstExp falseBool = new ABooleanConstExp(new AFalseBool()); ABooleanConstExp trueBool = new ABooleanConstExp(new ATrueBool()); data.ExpTypes[falseBool] = data.ExpTypes[trueBool] = new ANamedType(new TIdentifier("bool"), null); invoke = new ASimpleInvokeExp(new TIdentifier("TriggerExecute"), new ArrayList() { initTriggerLvalueExp, falseBool, trueBool }); data.ExpTypes[invoke] = new AVoidType(new TVoid("void")); foreach (AMethodDecl methodDecl in data.Libraries.Methods) { if (methodDecl.GetName().Text == "TriggerExecute") { data.SimpleMethodLinks[invoke] = methodDecl; break; } } methodBody.GetStatements().Add(new AExpStm(new TSemicolon(";"), invoke)); //TriggerDestroy(initTrigger); initTriggerLvalue = new ALocalLvalue(new TIdentifier("initTrigger")); data.LocalLinks[initTriggerLvalue] = initTriggerDecl; initTriggerLvalueExp = new ALvalueExp(initTriggerLvalue); data.LvalueTypes[initTriggerLvalue] = data.ExpTypes[initTriggerLvalueExp] = initTriggerDecl.GetType(); invoke = new ASimpleInvokeExp(new TIdentifier("TriggerDestroy"), new ArrayList() { initTriggerLvalueExp }); data.ExpTypes[invoke] = new AVoidType(new TVoid("void")); foreach (AMethodDecl methodDecl in data.Libraries.Methods) { if (methodDecl.GetName().Text == "TriggerDestroy") { data.SimpleMethodLinks[invoke] = methodDecl; break; } } methodBody.GetStatements().Add(new AExpStm(new TSemicolon(";"), invoke)); file.GetDecl().Add(invokeMethod); } for (int i = data.InitializerMethods.Count - 1; i >= 0; i--) { AMethodDecl method = data.InitializerMethods[i]; //Turn method into a trigger method.SetReturnType(new ANamedType(new TIdentifier("bool"), null)); method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("bool"), null), new TIdentifier("testConds"), null)); method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("bool"), null), new TIdentifier("runActions"), null)); method.SetTrigger(new TTrigger("trigger")); ((AABlock)method.GetBlock()).GetStatements().Add(new AVoidReturnStm(new TReturn("return"))); TriggerConvertReturn returnConverter = new TriggerConvertReturn(data); method.Apply(returnConverter); data.TriggerDeclarations[method] = new List <TStringLiteral>(); //Add Invoke(<name>); to main entry TStringLiteral literal = new TStringLiteral(method.GetName().Text); data.TriggerDeclarations[method].Add(literal); AStringConstExp stringConst = new AStringConstExp(literal); data.ExpTypes[stringConst] = new ANamedType(new TIdentifier("string"), null); ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("Invoke"), new ArrayList() { stringConst }); data.SimpleMethodLinks[invoke] = invokeMethod; data.ExpTypes[invoke] = invokeMethod.GetReturnType(); block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), invoke)); //ASyncInvokeExp syncInvokeExp = new ASyncInvokeExp(new TSyncInvoke("Invoke"), new AAmbiguousNameLvalue(new ASimpleName(new TIdentifier(method.GetName().Text))), new ArrayList()); //data.Invokes.Add(method, new List<InvokeStm>(){new InvokeStm(syncInvokeExp)}); //data.ExpTypes[syncInvokeExp] = new AVoidType(new TVoid("void")); //block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), syncInvokeExp)); } for (int i = data.InvokeOnIniti.Count - 1; i >= 0; i--) { AMethodDecl method = data.InvokeOnIniti[i]; //Turn method into a trigger method.SetReturnType(new ANamedType(new TIdentifier("bool"), null)); method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("bool"), null), new TIdentifier("testConds"), null)); method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("bool"), null), new TIdentifier("runActions"), null)); method.SetTrigger(new TTrigger("trigger")); ((AABlock)method.GetBlock()).GetStatements().Add(new AVoidReturnStm(new TReturn("return"))); TriggerConvertReturn returnConverter = new TriggerConvertReturn(data); method.Apply(returnConverter); data.TriggerDeclarations[method] = new List <TStringLiteral>(); //Add Invoke(<name>); to main entry TStringLiteral literal = new TStringLiteral(method.GetName().Text); data.TriggerDeclarations[method].Add(literal); AStringConstExp stringConst = new AStringConstExp(literal); data.ExpTypes[stringConst] = new ANamedType(new TIdentifier("string"), null); ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("Invoke"), new ArrayList() { stringConst }); data.SimpleMethodLinks[invoke] = invokeMethod; data.ExpTypes[invoke] = invokeMethod.GetReturnType(); block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), invoke)); /* * ASyncInvokeExp syncInvokeExp = new ASyncInvokeExp(new TSyncInvoke("Invoke"), new AAmbiguousNameLvalue(new ASimpleName(new TIdentifier(method.GetName().Text))), new ArrayList()); * data.Invokes.Add(method, new List<InvokeStm>() { new InvokeStm(syncInvokeExp) }); * data.ExpTypes[syncInvokeExp] = new AVoidType(new TVoid("void")); * * block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), syncInvokeExp));*/ } for (int i = data.FieldsToInitInMapInit.Count - 1; i >= 0; i--) { AFieldDecl field = data.FieldsToInitInMapInit[i]; if (field.GetInit() == null) { continue; } AFieldLvalue lvalue = new AFieldLvalue(new TIdentifier(field.GetName().Text)); AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), lvalue, field.GetInit()); data.ExpTypes[assignment] = data.LvalueTypes[lvalue] = field.GetType(); data.FieldLinks[lvalue] = field; block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), assignment)); } block.RemoveChild(mainEntryFieldInitBlock); block.GetStatements().Insert(0, mainEntryFieldInitBlock); }
public override void CaseAStringConstExp(AStringConstExp node) { Write(node.GetStringLiteral().Text); }
public override void CaseAStringConstExp(AStringConstExp node) { List.Add(node); HasStringConst = true; }
private static void MakeCloneRefferences(PExp clone, PExp exp, SharedData data) { data.ExpTypes[clone] = data.ExpTypes[exp]; if (exp is AIntConstExp || exp is AHexConstExp || exp is AOctalConstExp || exp is AFixedConstExp || exp is AStringConstExp || exp is ACharConstExp || exp is ABooleanConstExp || exp is ANullExp) { //No more required } else if (exp is AIncDecExp) { AIncDecExp aExp = (AIncDecExp)exp; AIncDecExp aClone = (AIncDecExp)clone; MakeCloneRefferences(aClone.GetLvalue(), aExp.GetLvalue(), data); } else if (exp is ABinopExp) { ABinopExp aExp = (ABinopExp)exp; ABinopExp aClone = (ABinopExp)clone; MakeCloneRefferences(aClone.GetLeft(), aExp.GetLeft(), data); MakeCloneRefferences(aClone.GetRight(), aExp.GetRight(), data); } else if (exp is AUnopExp) { AUnopExp aExp = (AUnopExp)exp; AUnopExp aClone = (AUnopExp)clone; MakeCloneRefferences(aClone.GetExp(), aExp.GetExp(), data); } else if (exp is ASimpleInvokeExp) { ASimpleInvokeExp aExp = (ASimpleInvokeExp)exp; ASimpleInvokeExp aClone = (ASimpleInvokeExp)clone; data.SimpleMethodLinks[aClone] = data.SimpleMethodLinks[aExp]; for (int i = 0; i < aExp.GetArgs().Count; i++) { MakeCloneRefferences((PExp)aClone.GetArgs()[i], (PExp)aExp.GetArgs()[i], data); } } else if (exp is ANonstaticInvokeExp) { ANonstaticInvokeExp aExp = (ANonstaticInvokeExp)exp; ANonstaticInvokeExp aClone = (ANonstaticInvokeExp)clone; data.StructMethodLinks[aClone] = data.StructMethodLinks[aExp]; for (int i = 0; i < aExp.GetArgs().Count; i++) { MakeCloneRefferences((PExp)aClone.GetArgs()[i], (PExp)aExp.GetArgs()[i], data); } MakeCloneRefferences(aClone.GetReceiver(), aExp.GetReceiver(), data); } else if (exp is ALvalueExp) { ALvalueExp aExp = (ALvalueExp)exp; ALvalueExp aClone = (ALvalueExp)clone; MakeCloneRefferences(aClone.GetLvalue(), aExp.GetLvalue(), data); } else if (exp is AAssignmentExp) { AAssignmentExp aExp = (AAssignmentExp)exp; AAssignmentExp aClone = (AAssignmentExp)clone; MakeCloneRefferences(aClone.GetLvalue(), aExp.GetLvalue(), data); MakeCloneRefferences(aClone.GetExp(), aExp.GetExp(), data); } else if (exp is AParenExp) { AParenExp aExp = (AParenExp)exp; AParenExp aClone = (AParenExp)clone; MakeCloneRefferences(aClone.GetExp(), aExp.GetExp(), data); } else if (exp is AStringConstExp) { AStringConstExp aExp = (AStringConstExp)exp; AStringConstExp aClone = (AStringConstExp)clone; if (data.ObfuscatedStrings.ContainsKey(aExp)) { data.ObfuscatedStrings[aClone] = data.ObfuscatedStrings[aExp]; } if (data.StringsDontJoinRight.Contains(aExp)) { data.StringsDontJoinRight.Add(aClone); } } else if (exp is ANewExp) { ANewExp aExp = (ANewExp)exp; ANewExp aClone = (ANewExp)clone; if (data.ConstructorLinks.ContainsKey(aExp)) { data.ConstructorLinks[aClone] = data.ConstructorLinks[aExp]; } MakeCloneRefferences(aClone.GetType(), aExp.GetType(), data); for (int i = 0; i < aExp.GetArgs().Count; i++) { MakeCloneRefferences((PExp)aClone.GetArgs()[i], (PExp)aExp.GetArgs()[i], data); } } else if (exp is ACastExp) { ACastExp aExp = (ACastExp)exp; ACastExp aClone = (ACastExp)clone; MakeCloneRefferences(aClone.GetType(), aExp.GetType(), data); MakeCloneRefferences(aClone.GetExp(), aExp.GetExp(), data); } else if (exp is ADelegateExp) { ADelegateExp aExp = (ADelegateExp)exp; ADelegateExp aClone = (ADelegateExp)clone; if (data.DelegateCreationMethod.ContainsKey(aExp)) { data.DelegateCreationMethod[aClone] = data.DelegateCreationMethod[aExp]; } MakeCloneRefferences(aClone.GetType(), aExp.GetType(), data); MakeCloneRefferences(aClone.GetLvalue(), aExp.GetLvalue(), data); } else if (exp is ADelegateInvokeExp) { ADelegateInvokeExp aExp = (ADelegateInvokeExp)exp; ADelegateInvokeExp aClone = (ADelegateInvokeExp)clone; MakeCloneRefferences(aClone.GetReceiver(), aExp.GetReceiver(), data); for (int i = 0; i < aExp.GetArgs().Count; i++) { MakeCloneRefferences((PExp)aClone.GetArgs()[i], (PExp)aExp.GetArgs()[i], data); } } else if (exp is AIfExp) { AIfExp aExp = (AIfExp)exp; AIfExp aClone = (AIfExp)clone; MakeCloneRefferences(aClone.GetCond(), aExp.GetCond(), data); MakeCloneRefferences(aClone.GetThen(), aExp.GetThen(), data); MakeCloneRefferences(aClone.GetElse(), aExp.GetElse(), data); } else { throw new Exception("Unexpect exp. Got " + exp.GetType()); } }
public static bool ReturnsTheSame(PExp left, PExp right, SharedData data) { if (left.GetType() != right.GetType()) { return(false); } if (left is ABinopExp) { ABinopExp aLeft = (ABinopExp)left; ABinopExp aRight = (ABinopExp)right; if (aLeft.GetBinop().GetType() != aRight.GetBinop().GetType()) { return(false); } return(ReturnsTheSame(aLeft.GetLeft(), aRight.GetLeft(), data) && ReturnsTheSame(aLeft.GetRight(), aRight.GetRight(), data)); } if (left is AUnopExp) { AUnopExp aLeft = (AUnopExp)left; AUnopExp aRight = (AUnopExp)right; if (aLeft.GetUnop().GetType() != aRight.GetUnop().GetType()) { return(false); } return(ReturnsTheSame(aLeft.GetExp(), aRight.GetExp(), data)); } if (left is AIntConstExp) { AIntConstExp aLeft = (AIntConstExp)left; AIntConstExp aRight = (AIntConstExp)right; return(int.Parse(aLeft.GetIntegerLiteral().Text) == int.Parse(aRight.GetIntegerLiteral().Text)); } if (left is AFixedConstExp) { AFixedConstExp aLeft = (AFixedConstExp)left; AFixedConstExp aRight = (AFixedConstExp)right; return(aLeft.GetFixedLiteral().Text == aRight.GetFixedLiteral().Text); } if (left is AStringConstExp) { AStringConstExp aLeft = (AStringConstExp)left; AStringConstExp aRight = (AStringConstExp)right; return(aLeft.GetStringLiteral().Text == aRight.GetStringLiteral().Text); } if (left is ACharConstExp) { ACharConstExp aLeft = (ACharConstExp)left; ACharConstExp aRight = (ACharConstExp)right; return(aLeft.GetCharLiteral().Text == aRight.GetCharLiteral().Text); } if (left is ABooleanConstExp) { ABooleanConstExp aLeft = (ABooleanConstExp)left; ABooleanConstExp aRight = (ABooleanConstExp)right; return(aLeft.GetBool().GetType() == aRight.GetBool().GetType()); } if (left is ASimpleInvokeExp) { //A method might not return the same thing each time it is called return(false); } if (left is ALvalueExp) { ALvalueExp aLeft = (ALvalueExp)left; ALvalueExp aRight = (ALvalueExp)right; return(ReturnsTheSame(aLeft.GetLvalue(), aRight.GetLvalue(), data)); } if (left is AParenExp) { AParenExp aLeft = (AParenExp)left; AParenExp aRight = (AParenExp)right; return(ReturnsTheSame(aLeft.GetExp(), aRight.GetExp(), data)); } throw new Exception("Util.ReturnsTheSame. Unexpected type, got " + left.GetType()); }