private CodePiece GenerateCode_Display(CodePiece val) { MathExt.Point s = new MathExt.Point(DisplayWidth, DisplayHeight); int b = CGO.DisplayBorderThickness; CodePiece p = new CodePiece(); if (s.Size == 0) { return(p); } p.SetAt(b, b, val); // 44111111 // 44111111 // 44 22 // 44 22 // 44 22 // 44 22 // 33333322 // 33333322 p.Fill(b, 0, s.X + 2 * b, b, BCHelper.Chr(CGO.DisplayBorder)); // 1 p.Fill(s.X + b, b, s.X + 2 * b, s.Y + 2 * b, BCHelper.Chr(CGO.DisplayBorder)); // 2 p.Fill(0, s.Y + b, s.X + b, s.Y + 2 * b, BCHelper.Chr(CGO.DisplayBorder)); // 3 p.Fill(0, 0, b, s.Y + b, BCHelper.Chr(CGO.DisplayBorder)); // 4 p.SetTag(0, 0, new DisplayTopLeftTag(this, DisplayWidth + 2 * b, DisplayHeight + 2 * b)); return(p); }
private static void GetFactors(CodePiece p, long a) // Wenn nicht möglich so gut wie mögl und am ende add { List <int> result = new List <int>(); if (a < 10) { p.AppendRight(BCHelper.Dig((byte)a)); return; } for (byte i = 9; i > 1; i--) { if (a % i == 0) { GetFactors(p, a / i); p.AppendRight(BCHelper.Dig(i)); p.AppendRight(BCHelper.Mult); return; } } for (byte i = 1; i < 10; i++) { if ((a - i) % 9 == 0) { GetFactors(p, a - i); p.AppendRight(BCHelper.Dig(i)); p.AppendRight(BCHelper.Add); return; } } throw new WTFException(); }
public static CodePiece GenerateCode(bool value) { CodePiece p = new CodePiece(); p[0, 0] = BCHelper.Dig(value ? (byte)1 : (byte)0); return(p); }
public static CodePiece GenerateCode(long d) { if (d < 0 || d > 9) { return(null); } CodePiece p = new CodePiece(); p[0, 0] = BCHelper.Dig((byte)d); return(p); }
public void CodePieceTest_Replace() { CodePiece p = CodePiece.ParseFromLine(" ", true); ; p.SetTag(1, 0, new TemporaryCodeFieldTag()); p.ReplaceWalkway(0, 0, BCHelper.Sub_tagged(new TemporaryCodeFieldTag()), false); p.ReplaceWalkway(1, 0, BCHelper.Add, true); }
public override CodePiece GenerateCodeReturnFromMethodCall(CodeGenEnvironment env, SourceCodePosition pos, Expression value, bool reversed) { CodePiece p = CodePiece.ParseFromLine(@"0\0"); p.AppendRight(BCHelper.PC_Up_tagged(new MethodCallVerticalExitTag())); if (reversed) { p.ReverseX(false); } return(p); }
public override CodePiece GenerateCodeReturnFromMethodCall(CodeGenEnvironment env, SourceCodePosition pos, Expression value, bool reversed) { CodePiece p = new CodePiece(); if (reversed) { #region Reversed p.AppendLeft(value.GenerateCode(env, reversed)); // Switch ReturnValue (Array) and BackJumpAddr p.AppendLeft(CodePieceStore.WriteArrayFromStack(env, env.TMP_ARRFIELD_RETURNVAL, reversed)); p.AppendLeft(CodePieceStore.WriteValueToField(env.TMP_FIELD_JMP_ADDR, reversed)); p.AppendLeft(CodePieceStore.ReadArrayToStack(env, env.TMP_ARRFIELD_RETURNVAL, reversed)); p.AppendLeft(CodePieceStore.ReadValueFromField(env.TMP_FIELD_JMP_ADDR, reversed)); p.AppendLeft(BCHelper.Digit0); // Right Lane p.AppendLeft(BCHelper.PC_Up_tagged(new MethodCallVerticalExitTag())); #endregion } else { #region Normal p.AppendRight(value.GenerateCode(env, reversed)); // Switch ReturnValue (Array) and BackJumpAddr p.AppendRight(CodePieceStore.WriteArrayFromStack(env, env.TMP_ARRFIELD_RETURNVAL, reversed)); p.AppendRight(CodePieceStore.WriteValueToField(env.TMP_FIELD_JMP_ADDR, reversed)); p.AppendRight(CodePieceStore.ReadArrayToStack(env, env.TMP_ARRFIELD_RETURNVAL, reversed)); p.AppendRight(CodePieceStore.ReadValueFromField(env.TMP_FIELD_JMP_ADDR, reversed)); p.AppendRight(BCHelper.Digit0); // Right Lane p.AppendRight(BCHelper.PC_Up_tagged(new MethodCallVerticalExitTag())); #endregion } p.NormalizeX(); return(p); }
public static CodePiece GenerateCode(long value) { CodePiece p = new CodePiece(); if (value == -(int)'"') { p[0, 0] = BCHelper.Digit1; p[1, 0] = BCHelper.Stringmode; p[2, 0] = BCHelper.Chr(-value + 1); p[3, 0] = BCHelper.Stringmode; p[4, 0] = BCHelper.Sub; return(p); } else if (value == (int)'"') { p[0, 0] = BCHelper.Digit1; p[1, 0] = BCHelper.Stringmode; p[2, 0] = BCHelper.Chr(value - 1); p[3, 0] = BCHelper.Stringmode; p[4, 0] = BCHelper.Add; return(p); } else if (value <= -(int)' ' && value >= -(int)'~') { p[0, 0] = BCHelper.Digit0; p[1, 0] = BCHelper.Stringmode; p[2, 0] = BCHelper.Chr(-value); p[3, 0] = BCHelper.Stringmode; p[4, 0] = BCHelper.Sub; return(p); } else if (value >= (int)' ' && value <= (int)'~') { p[0, 0] = BCHelper.Stringmode; p[1, 0] = BCHelper.Chr(value); p[2, 0] = BCHelper.Stringmode; return(p); } else { return(null); } }
public static CodePiece GenerateCode(long lit) { CodePiece p = new CodePiece(); bool isneg; if (isneg = lit < 0) { lit *= -1; } string rep = ConvertToBase(lit, 9); int pos = 0; if (isneg) { p[pos++, 0] = BCHelper.Digit0; } for (int i = 0; i < rep.Length; i++) { p[pos++, 0] = BCHelper.Dig((byte)(rep[rep.Length - i - 1] - '0')); if (i + 1 != rep.Length) { p[pos++, 0] = BCHelper.Dig(9); } } int count = rep.Length - 1; for (int i = 0; i < count; i++) { p[pos++, 0] = BCHelper.Mult; p[pos++, 0] = BCHelper.Add; } if (isneg) { p[pos++, 0] = BCHelper.Sub; } return(p); }
private CodePiece GenerateCode_DisplayValue(string dv) { CodePiece r = new CodePiece(); int w = dv.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None).Max(s => s.Length); int h = dv.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None).Length; if (dv == "") { w = 0; h = 0; } if (w > DisplayWidth || h > DisplayHeight) { throw new InitialDisplayValueTooBigException(DisplayWidth, DisplayHeight, w, h); } string[] split = dv.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); BefungeCommand def = BCHelper.Chr(CGO.DefaultDisplayValue); if (def.Type == BefungeCommandType.NOP) { def = BCHelper.Walkway; } for (int y = 0; y < DisplayHeight; y++) { for (int x = 0; x < DisplayWidth; x++) { r[x, y] = (y < split.Length && x < split[y].Length) ? BCHelper.Chr(split[y][x]) : def; } } return(r); }
public override CodePiece GenerateCodeReturnFromMethodCall(CodeGenEnvironment env, SourceCodePosition pos, Expression value, bool reversed) { CodePiece p = new CodePiece(); if (reversed) { #region Reversed p.AppendRight(BCHelper.PC_Up_tagged(new MethodCallVerticalExitTag())); p.AppendRight(BCHelper.Digit0); // Right Lane p.AppendRight(BCHelper.StackSwap); // Swap BackjumpAddr back to Stack-Front p.AppendRight(value.GenerateCode(env, reversed)); #endregion } else { #region Normal p.AppendRight(value.GenerateCode(env, reversed)); p.AppendRight(BCHelper.StackSwap); // Swap BackjumpAddr back to Stack-Front p.AppendRight(BCHelper.Digit0); // Right Lane p.AppendRight(BCHelper.PC_Up_tagged(new MethodCallVerticalExitTag())); #endregion } p.NormalizeX(); return(p); }
public override CodePiece GenerateCode(CodeGenEnvironment env, bool reversed) { CodePiece p = new CodePiece(); int i = 0; if (reversed) { foreach (byte val in Value) { p[i--, 0] = BCHelper.Dig(val); } } else { foreach (byte val in Value) { p[i++, 0] = BCHelper.Dig(val); } } p.NormalizeX(); return(p); }
private CodePiece GenerateCode(int estimatedWidth, string initialDisp) { // v {TEMP..} // 0 v{STACKFLOODER} < // {++++++++++++} | // v < // ############### // ############### // ## ## // ## {DISPLAY} ## // ## ## // ############### // ############### | // v < // :# $ {GLOBALVAR} # // !# $ {GLOBALVAR} ! // ## $ // ># $ {METHOD} // |# $ {++++++} // # $ {++++++} // ##$ // #>$ {METHOD} // #|$ {++++++} // # $ {++++++} // # $ {METHOD} // # $ {++++++} ResetBeforeCodeGen(); List <Tuple <MathExt.Point, CodePiece> > methPieces = new List <Tuple <MathExt.Point, CodePiece> >(); CodeGenEnvironment env = new CodeGenEnvironment(); env.MaxVarDeclarationWidth = MathExt.Max(estimatedWidth - 4 - CodeGenConstants.LANE_VERTICAL_MARGIN - 2, CodeGenConstants.MinVarDeclarationWidth, DisplayWidth, CGO.DefaultVarDeclarationWidth); CodePiece p = new CodePiece(); int maxReturnValWidth = GetMaxReturnValueWidth(); int methOffsetX = 4 + CodeGenConstants.LANE_VERTICAL_MARGIN; #region Generate Top Lane CodePiece pTopLane = new CodePiece(); pTopLane[0, 0] = BCHelper.PCDown; pTopLane[CodeGenConstants.TMP_FIELDPOS_IO_ARR.X, CodeGenConstants.TMP_FIELDPOS_IO_ARR.Y] = BCHelper.Chr(CGO.DefaultTempSymbol, new TemporaryCodeFieldTag()); env.TMP_FIELD_IO_ARR = CodeGenConstants.TMP_FIELDPOS_IO_ARR; pTopLane[CodeGenConstants.TMP_FIELDPOS_OUT_ARR.X, CodeGenConstants.TMP_FIELDPOS_OUT_ARR.Y] = BCHelper.Chr(CGO.DefaultTempSymbol, new TemporaryCodeFieldTag()); env.TMP_FIELD_OUT_ARR = CodeGenConstants.TMP_FIELDPOS_OUT_ARR; pTopLane[CodeGenConstants.TMP_FIELDPOS_JMP_ADDR.X, CodeGenConstants.TMP_FIELDPOS_JMP_ADDR.Y] = BCHelper.Chr(CGO.DefaultTempSymbol, new TemporaryCodeFieldTag()); env.TMP_FIELD_JMP_ADDR = CodeGenConstants.TMP_FIELDPOS_JMP_ADDR; pTopLane[CodeGenConstants.TMP_FIELDPOS_GENERAL.X, CodeGenConstants.TMP_FIELDPOS_GENERAL.Y] = BCHelper.Chr(CGO.DefaultTempSymbol, new TemporaryCodeFieldTag()); env.TMP_FIELD_GENERAL = CodeGenConstants.TMP_FIELDPOS_GENERAL; int tempDeclHeight = 0; if (maxReturnValWidth < (CodeGenConstants.TOP_COMMENT_X - CodeGenConstants.TMP_ARRFIELDPOS_RETURNVAL_TL.X - 3)) { // Single line env.TMP_ARRFIELD_RETURNVAL = new VarDeclarationPosition(CodeGenConstants.TMP_ARRFIELDPOS_RETURNVAL_TL, maxReturnValWidth, 1, maxReturnValWidth); pTopLane.Fill( env.TMP_ARRFIELD_RETURNVAL.X, env.TMP_ARRFIELD_RETURNVAL.Y, env.TMP_ARRFIELD_RETURNVAL.X + maxReturnValWidth, env.TMP_ARRFIELD_RETURNVAL.Y + 1, BCHelper.Chr(CGO.DefaultResultTempSymbol), new TemporaryResultCodeFieldTag(maxReturnValWidth)); tempDeclHeight = 1; } else { // Multiline (or at least in its own seperate row) var space = CodePieceStore.CreateVariableSpace( maxReturnValWidth, CGO, env.MaxVarDeclarationWidth, BCHelper.Chr(CGO.DefaultResultTempSymbol), new TemporaryResultCodeFieldTag(maxReturnValWidth)); env.TMP_ARRFIELD_RETURNVAL = space.Item2 + new MathExt.Point(1, 1); pTopLane.SetAt(1, 1, space.Item1); tempDeclHeight = 1 + space.Item1.Height; } pTopLane.SetText(CodeGenConstants.TOP_COMMENT_X, 0, "// generated by BefunGen v" + CodeGenConstants.BEFUNGEN_VERSION); pTopLane.CreateColWw(0, 1, tempDeclHeight); pTopLane[0, tempDeclHeight] = BCHelper.Digit0; pTopLane[2, tempDeclHeight] = BCHelper.PCDown; CodePiece pFlooder = CodePieceStore.BooleanStackFlooder(); pTopLane.SetAt(3, tempDeclHeight, pFlooder); CodePiece displayValue = GenerateCode_DisplayValue(initialDisp); CodePiece pDisplay = GenerateCode_Display(displayValue); DisplayOffsetX = 3; DisplayOffsetY = 2 + tempDeclHeight; pTopLane.SetAt(DisplayOffsetX, DisplayOffsetY, pDisplay); int topLaneBottomRow = 2 + tempDeclHeight + pDisplay.Height; DisplayOffsetX += CGO.DisplayBorderThickness; DisplayOffsetY += CGO.DisplayBorderThickness; pTopLane[0, topLaneBottomRow] = BCHelper.PCDown; pTopLane[1, topLaneBottomRow] = BCHelper.Walkway; pTopLane.FillColWw(0, tempDeclHeight + 1, topLaneBottomRow); pTopLane.FillColWw(2, tempDeclHeight + 1, topLaneBottomRow + 1); p.SetAt(0, 0, pTopLane); #endregion int laneStartY = p.MaxY; int methOffsetY = p.MaxY; // +3 For the MinY=3 of VerticalLaneTurnout_Dec #region Insert VariableSpace CodePiece pVars = CodePieceStore.CreateVariableSpace(Variables, methOffsetX, methOffsetY, CGO, env.MaxVarDeclarationWidth); p.SetAt(methOffsetX, methOffsetY, pVars); #endregion methOffsetY += Math.Max(0, pVars.Height - 3); // -3 For the MinY=3 of VerticalLaneTurnout_Dec methOffsetY += 3; // +3 For the MinY=3 of VerticalLaneTurnout_Dec #region Insert Methods for (int i = 0; i < MethodList.Count; i++) { Method m = MethodList[i]; CodePiece pMethod = m.GenerateCode(env, methOffsetX, methOffsetY); if (p.HasActiveTag(typeof(MethodEntryFullInitializationTag))) // Force MethodEntry_FullIntialization Distance (at least so that lanes can be generated) { int pLast = p.FindAllActiveCodeTags(typeof(MethodEntryFullInitializationTag)).Last().Y; int pNext = pMethod.FindAllActiveCodeTags(typeof(MethodEntryFullInitializationTag)).First().Y + (methOffsetY - pMethod.MinY); int overflow = (pNext - pLast) - CodePieceStore.VerticalLaneTurnout_Dec(false).Height; if (overflow < 0) { methOffsetY -= overflow; } } int mx = methOffsetX - pMethod.MinX; int my = methOffsetY - pMethod.MinY; methPieces.Add(Tuple.Create(new MathExt.Point(mx, my), pMethod)); p.SetAt(mx, my, pMethod); methOffsetY += pMethod.Height + CodeGenConstants.VERTICAL_METHOD_DISTANCE; } #endregion int highwayX = p.MaxX; #region Generate Lane Chooser p.FillRowWw(tempDeclHeight, 3 + pFlooder.Width, highwayX); p.FillRowWw(topLaneBottomRow, 3, highwayX); p[highwayX, tempDeclHeight] = BCHelper.PCLeft; p[highwayX, topLaneBottomRow - 1] = BCHelper.IfVertical; p[highwayX, topLaneBottomRow + 0] = BCHelper.PCLeft; p[highwayX, topLaneBottomRow + 1] = BCHelper.PCJump; p[highwayX, topLaneBottomRow + 2] = BCHelper.Not; p.FillColWw(highwayX, tempDeclHeight + 1, topLaneBottomRow - 1); #endregion #region Generate Lanes (Left Lane && Right Lane) List <TagLocation> methodEntries = p.FindAllActiveCodeTags(typeof(MethodEntryFullInitializationTag)) // Left Lane .OrderBy(tp => tp.Y) .ToList(); List <TagLocation> codeEntries = p.FindAllActiveCodeTags(typeof(MethodCallHorizontalReEntryTag)) // Right Lane .OrderBy(tp => tp.Y) .ToList(); int last; bool first; //######### LEFT LANE ######### first = true; last = laneStartY; foreach (TagLocation methodEntry in methodEntries) { CodePiece pTurnout = CodePieceStore.VerticalLaneTurnout_Dec(first); p.FillColWw(0, last, methodEntry.Y + pTurnout.MinY); p.SetAt(0, methodEntry.Y, pTurnout); p.FillRowWw(methodEntry.Y, 4, methodEntry.X); last = methodEntry.Y + pTurnout.MaxY; first = false; } //p.FillColWW(0, last, p.MaxY); //######### RIGHT LANE ######### first = true; last = laneStartY; foreach (TagLocation codeEntry in codeEntries) { CodePiece pTurnout = CodePieceStore.VerticalLaneTurnout_Test(); p.FillColWw(2, last, codeEntry.Y + pTurnout.MinY); p.SetAt(2, codeEntry.Y, pTurnout); p.CreateRowWw(codeEntry.Y, 4, codeEntry.X); last = codeEntry.Y + pTurnout.MaxY; first = false; } //p.FillColWW(2, last, p.MaxY); //######### MIDDLE LANE ######### p.Fill(1, laneStartY, 2, p.MaxY, BCHelper.PCJump); //######### POP LANE ######### p.Fill(3, laneStartY, 4, p.MaxY, BCHelper.StackPop); #endregion #region Generate Highway (Path on right side of code) List <TagLocation> codeExits = p.FindAllActiveCodeTags(typeof(MethodCallHorizontalExitTag)) .OrderBy(tp => tp.Y) .ToList(); first = true; last = topLaneBottomRow + 3; foreach (TagLocation exit in codeExits) { p.FillColWw(highwayX, last, exit.Y); p[highwayX, exit.Y] = BCHelper.PCUp; p.CreateRowWw(exit.Y, exit.X + 1, highwayX); last = exit.Y + 1; exit.Tag.Deactivate(); first = false; } #endregion return(p); }
public static CodePiece GenerateCode(long lit) { if (lit < 0) { CodePiece p = GenerateCode(-lit); if (p == null) { return(null); } p.AppendLeft(BCHelper.Digit0); p.AppendRight(BCHelper.Sub); p.NormalizeX(); return(p); } if (lit >= 0 && lit <= 9) { return(new CodePiece(BCHelper.Dig((byte)lit))); } if (lit < MIN_ASCII && lit >= (MIN_ASCII - 9)) { if (lit + 9 == '"') { CodePiece p = GenerateCode(lit + 8); if (p == null) { return(null); } p.AppendRight(BCHelper.Digit8); p.AppendRight(BCHelper.Sub); return(p); } else { CodePiece p = GenerateCode(lit + 9); if (p == null) { return(null); } p.AppendRight(BCHelper.Digit9); p.AppendRight(BCHelper.Sub); return(p); } } if (lit < (MIN_ASCII - 9)) { return(null); } List <char> str; List <StripOp> ops; if (CalculateStringOps(out str, out ops, lit)) { CodePiece p = new CodePiece(); p.AppendRight(BCHelper.Stringmode); foreach (char c in str) { p.AppendRight(BCHelper.Chr(c)); } p.AppendRight(BCHelper.Stringmode); for (int i = 0; i < ops.Count; i++) { switch (ops[i]) { case StripOp.Add: p.AppendRight(BCHelper.Add); break; case StripOp.Mult: p.AppendRight(BCHelper.Mult); break; default: throw new WTFException(); } } return(p); } return(null); }