Generating Smart Pascal code that outputs XML code for a block

This example makes use of the code on the preceding page for generating a random ID for each block. In order to obtain code that you can copy you can (1) change Console.writeln; to writeln at the end of the Smart Pascal code, then (2) copy the console output between <p></p> tags in an HTML document (opened in Notepad++) and (3) open the HTML document in Chrome. Refresh BlocklyToSmartPascal, export the blank workspace and paste the generated XML code between the opening and closing xml tags in the text area. Import the XML to see the blocks and then export to see your XML code nicely formatted.

The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="procedures_defreturn" id="qd?L72F59OXDh=b6Xqdv" x="-62" y="-337">
    <field name="NAME">CreateID</field>
    <comment pinned="false" h="80" w="160">Creates random 20 char ID</comment>
    <statement name="STACK">
      <block type="variables_set" id="$)d#Bn,O#$;2,9W7GL3s">
        <field name="VAR">strID</field>
        <value name="VALUE">
          <block type="text" id=";JB@v4jy2`8kFAS2.gv#">
            <field name="TEXT"></field>
          </block>
        </value>
        <next>
          <block type="variables_set" id="c`K}l5taJE9.!50ArwY,">
            <field name="VAR">strSoup</field>
            <value name="VALUE">
              <block type="text" id="m7L`WAElNSVr=g^Vp8w%">
                <field name="TEXT">!#$%()*+,-./:;=?@[]^_`{|}~ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789</field>
              </block>
            </value>
            <next>
              <block type="variables_set" id="QmX~1~Y.:_w;U97tI7^n">
                <field name="VAR">intSoupLength</field>
                <value name="VALUE">
                  <block type="text_length" id="-x%,ou@jC1-PU8E1V|eu">
                    <value name="VALUE">
                      <shadow id="f4Q;E/-1?|zt#4CS5pUn" type="text">
                        <field name="TEXT">abc</field>
                      </shadow>
                      <block type="variables_get" id="WdE;`dX:c~~F_DxXC$v+">
                        <field name="VAR">strSoup</field>
                      </block>
                    </value>
                  </block>
                </value>
                <next>
                  <block type="controls_for" id="ogS2#0Op:I)w_Hkh8,p[">
                    <field name="VAR">intCount</field>
                    <value name="FROM">
                      <shadow type="math_number" id="p}5Nq4beL`h6qhSLX)q6">
                        <field name="NUM">1</field>
                      </shadow>
                    </value>
                    <value name="TO">
                      <shadow type="math_number" id="*So)bS%;g#E.O}#uhU;?">
                        <field name="NUM">20</field>
                      </shadow>
                    </value>
                    <value name="BY">
                      <shadow type="math_number" id="(jXcKJ:3[)Z?lg2ky]]d">
                        <field name="NUM">1</field>
                      </shadow>
                    </value>
                    <statement name="DO">
                      <block type="text_append" id="hq}p/}HT$IO+GB[/6.[_">
                        <field name="VAR">strID</field>
                        <value name="TEXT">
                          <shadow id="g@S#k!?=7X2rr!{?]3Al" type="text">
                            <field name="TEXT"></field>
                          </shadow>
                          <block type="text_charAt" id="N=bX~QdKCZPC5]+gZ-$@">
                            <mutation at="true"></mutation>
                            <field name="WHERE">FROM_START</field>
                            <value name="VALUE">
                              <block type="variables_get" id="xJJIBAS!H3A1eq)Yy.c1">
                                <field name="VAR">strSoup</field>
                              </block>
                            </value>
                            <value name="AT">
                              <block type="math_random_int" id="M9Y-VZ*k,}c1P!lSxa*,">
                                <value name="FROM">
                                  <shadow type="math_number" id="gNcEH{F[#u%[jdS_`X{z">
                                    <field name="NUM">1</field>
                                  </shadow>
                                </value>
                                <value name="TO">
                                  <shadow id="!$@-qA_Zx.Tmuvsq(jLj" type="math_number">
                                    <field name="NUM">100</field>
                                  </shadow>
                                  <block type="variables_get" id="3{N!]yqX(%;T%B=`n7aA">
                                    <field name="VAR">intSoupLength</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                          </block>
                        </value>
                      </block>
                    </statement>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </statement>
    <value name="RETURN">
      <block type="variables_get" id="}:xI#@|v9gY9)z^V1D1T">
        <field name="VAR">strID</field>
      </block>
    </value>
  </block>
  <block type="variables_set" id="G{+6DU_4:w_LFx8Hofg6" x="-62" y="-87">
    <field name="VAR">strNums</field>
    <value name="VALUE">
      <block type="text_prompt_ext" id="1y}j^VKbzJ#!m_=)^$+@">
        <mutation type="TEXT"></mutation>
        <field name="TYPE">TEXT</field>
        <value name="TEXT">
          <shadow type="text" id="A:{lqM7oB~C9UL*1?-o6">
            <field name="TEXT">Please enter a comma-separated list of numbers</field>
          </shadow>
        </value>
      </block>
    </value>
    <next>
      <block type="variables_set" id="xJ)L[s)+;e;cZ5v50HUu">
        <field name="VAR">strNumArray</field>
        <value name="VALUE">
          <block type="lists_split" id="f.=sJlWo+*0E@ON?{Qf2">
            <mutation mode="SPLIT"></mutation>
            <field name="MODE">SPLIT</field>
            <value name="INPUT">
              <block type="variables_get" id="`H7b,3%}j`DO-#7c_Dbo">
                <field name="VAR">strNums</field>
              </block>
            </value>
            <value name="DELIM">
              <shadow type="text" id="$A*ZtoX,.`_KkyeVfH}I">
                <field name="TEXT">,</field>
              </shadow>
            </value>
          </block>
        </value>
        <next>
          <block type="variables_set" id=",.IF-X:wb^HL`yxqPKb2">
            <field name="VAR">strXML</field>
            <value name="VALUE">
              <block type="text" id="lJfD{hAObVc43~6|?ElJ">
                <field name="TEXT">&amp;lt;block type=&amp;quot;lists_create_with&amp;quot; id=&amp;quot;</field>
              </block>
            </value>
            <next>
              <block type="text_append" id="!i@0+f^H%/DO{E/C;(D]">
                <field name="VAR">strXML</field>
                <value name="TEXT">
                  <shadow type="text" id="sTi:2{$oZ[f+gBV|uz^(">
                    <field name="TEXT"></field>
                  </shadow>
                  <block type="procedures_callreturn" id="3.x+pM*sx~FQr^_.hRHE">
                    <mutation name="CreateID"></mutation>
                  </block>
                </value>
                <next>
                  <block type="text_append" id="[QSB7Ncc,bfRZS|rlQyL">
                    <field name="VAR">strXML</field>
                    <value name="TEXT">
                      <shadow id="sTi:2{$oZ[f+gBV|uz^(" type="text">
                        <field name="TEXT"></field>
                      </shadow>
                      <block type="text" id="!Im:g=02J2!75etE+qB(">
                        <field name="TEXT">" x="50" y="50"&gt;&amp;lt;mutation items=&amp;quot;</field>
                      </block>
                    </value>
                    <next>
                      <block type="text_append" id="2/RgfPIg%i,OQ7hTY%Z*">
                        <field name="VAR">strXML</field>
                        <value name="TEXT">
                          <shadow type="text" id="q?eOxc$6{+ztW/lFQ5j2">
                            <field name="TEXT"></field>
                          </shadow>
                          <block type="text_join" id="L?L6901DE;gKz;|wOIDV">
                            <mutation items="2"></mutation>
                            <value name="ADD0">
                              <block type="lists_length" id=":!jl#n~ky4[zM^s#exPE">
                                <value name="VALUE">
                                  <block type="variables_get" id="kOzv+1x,oBM5]O^S.7,W">
                                    <field name="VAR">strNumArray</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <value name="ADD1">
                              <block type="text" id="f~I!P+I}MRsaU!g^c1Xz">
                                <field name="TEXT">&amp;quot;&amp;gt;&amp;lt;/mutation&amp;gt;</field>
                              </block>
                            </value>
                          </block>
                        </value>
                        <next>
                          <block type="controls_for" id="s]G$g8i.l49!|91mk_a*">
                            <field name="VAR">i</field>
                            <value name="FROM">
                              <shadow type="math_number" id="gef=r3~zXQfj)1ddF?WL">
                                <field name="NUM">0</field>
                              </shadow>
                            </value>
                            <value name="TO">
                              <shadow type="math_number" id="^9uX?of6Y~]T?I!2^r/1">
                                <field name="NUM">10</field>
                              </shadow>
                              <block type="math_arithmetic" id="SQ}Sc6s}mrnQ+{UU_2wB">
                                <field name="OP">MINUS</field>
                                <value name="A">
                                  <shadow type="math_number" id="P4dcE$]O@/ogD;YJ|JbI">
                                    <field name="NUM">1</field>
                                  </shadow>
                                  <block type="lists_length" id="jOqiqP3tvXyOKp=qG7U6">
                                    <value name="VALUE">
                                      <block type="variables_get" id="J$1C*YK)oDVhfHf.1}xy">
                                        <field name="VAR">strNumArray</field>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                                <value name="B">
                                  <shadow type="math_number" id="G.IUKZigS5edrMZ,QYH1">
                                    <field name="NUM">1</field>
                                  </shadow>
                                </value>
                              </block>
                            </value>
                            <value name="BY">
                              <shadow type="math_number" id="Mxq~urt(:6rm]9R`VQ.f">
                                <field name="NUM">1</field>
                              </shadow>
                            </value>
                            <statement name="DO">
                              <block type="text_append" id="-yBIIU0i.`TbL4E?ND1[">
                                <field name="VAR">strXML</field>
                                <value name="TEXT">
                                  <shadow id="sTi:2{$oZ[f+gBV|uz^(" type="text">
                                    <field name="TEXT"></field>
                                  </shadow>
                                  <block type="text" id="`eOS-MvA+XZR3cqicb{Z">
                                    <field name="TEXT">&amp;lt;value name= "ADD</field>
                                  </block>
                                </value>
                                <next>
                                  <block type="text_append" id="]jrc6KJ,DE60ySuR5mN!">
                                    <field name="VAR">strXML</field>
                                    <value name="TEXT">
                                      <shadow type="text" id="sTi:2{$oZ[f+gBV|uz^(">
                                        <field name="TEXT"></field>
                                      </shadow>
                                      <block type="text_join" id="6!AFEZfZ=pqLwEL16*^d">
                                        <mutation items="2"></mutation>
                                        <value name="ADD0">
                                          <block type="variables_get" id="E^P:c$E=hNixINQYYV$1">
                                            <field name="VAR">i</field>
                                          </block>
                                        </value>
                                        <value name="ADD1">
                                          <block type="text" id="0w_JK--7hA,wfU8cMEu|">
                                            <field name="TEXT">"&amp;gt;&amp;lt;block type= "math_number" id= "</field>
                                          </block>
                                        </value>
                                      </block>
                                    </value>
                                    <next>
                                      <block type="text_append" id="Cy$GZY^[wO..5W0gdr/W">
                                        <field name="VAR">strXML</field>
                                        <value name="TEXT">
                                          <shadow type="text" id="^bCsIoor31[U5qQu``kO">
                                            <field name="TEXT"></field>
                                          </shadow>
                                          <block type="procedures_callreturn" id="~(HG:sTZ_k[T)zknkQag">
                                            <mutation name="CreateID"></mutation>
                                          </block>
                                        </value>
                                        <next>
                                          <block type="text_append" id="jCqr#oFmq9RbfgB#bR9l">
                                            <field name="VAR">strXML</field>
                                            <value name="TEXT">
                                              <shadow id="sTi:2{$oZ[f+gBV|uz^(" type="text">
                                                <field name="TEXT"></field>
                                              </shadow>
                                              <block type="text" id="@_Y092n#zdk++`zSj9Pr">
                                                <field name="TEXT">"&amp;gt;&amp;lt;field name= "NUM"&amp;gt;</field>
                                              </block>
                                            </value>
                                            <next>
                                              <block type="text_append" id="XYMUEv{[H(M*=hsTlLRS">
                                                <field name="VAR">strXML</field>
                                                <value name="TEXT">
                                                  <shadow type="text" id="sTi:2{$oZ[f+gBV|uz^(">
                                                    <field name="TEXT"></field>
                                                  </shadow>
                                                  <block type="lists_getIndex" id="FHC$US;l$^=oqslB$sW|">
                                                    <mutation statement="false" at="true"></mutation>
                                                    <field name="MODE">GET</field>
                                                    <field name="WHERE">FROM_START</field>
                                                    <value name="VALUE">
                                                      <block type="variables_get" id="o1$7+(w,x+dE$^,pQSxB">
                                                        <field name="VAR">strNumArray</field>
                                                      </block>
                                                    </value>
                                                    <value name="AT">
                                                      <block type="math_arithmetic" id="TqeX,?#c*6):Mt{vDL9Y">
                                                        <field name="OP">ADD</field>
                                                        <value name="A">
                                                          <shadow type="math_number" id="_2zmVkvnwuP@~bh*nxP2">
                                                            <field name="NUM">1</field>
                                                          </shadow>
                                                          <block type="variables_get" id="C_e1.[}6pL%f~bZ%aa!$">
                                                            <field name="VAR">i</field>
                                                          </block>
                                                        </value>
                                                        <value name="B">
                                                          <shadow type="math_number" id="kEsKTy3[{j6G6N%xTD|/">
                                                            <field name="NUM">1</field>
                                                          </shadow>
                                                        </value>
                                                      </block>
                                                    </value>
                                                  </block>
                                                </value>
                                                <next>
                                                  <block type="text_append" id="2^#Z?M-X?x@LT^hI=ws]">
                                                    <field name="VAR">strXML</field>
                                                    <value name="TEXT">
                                                      <shadow type="text" id="sTi:2{$oZ[f+gBV|uz^(">
                                                        <field name="TEXT"></field>
                                                      </shadow>
                                                      <block type="text" id="Cm.a4v8i{C7h/-oCi!g6">
                                                        <field name="TEXT">&amp;lt;/field&amp;gt;&amp;lt;/block&amp;gt;&amp;lt;/value&amp;gt;</field>
                                                      </block>
                                                    </value>
                                                  </block>
                                                </next>
                                              </block>
                                            </next>
                                          </block>
                                        </next>
                                      </block>
                                    </next>
                                  </block>
                                </next>
                              </block>
                            </statement>
                            <next>
                              <block type="text_append" id="OIy[.TqX^~}5dJAJ1:=T">
                                <field name="VAR">strXML</field>
                                <value name="TEXT">
                                  <shadow type="text" id="afKE|ujqPqeEbQKGrMyv">
                                    <field name="TEXT">&amp;lt;/block&amp;gt;</field>
                                  </shadow>
                                </value>
                                <next>
                                  <block type="text_print" id="3T*P#6|VtCho]yTw6}M`">
                                    <value name="TEXT">
                                      <shadow type="text" id="tAVLjy|`]sS?*vs2:-uc">
                                        <field name="TEXT">abc</field>
                                      </shadow>
                                      <block type="variables_get" id="Slhl$3wiftpMO[)nUYF?">
                                        <field name="VAR">strXML</field>
                                      </block>
                                    </value>
                                  </block>
                                </next>
                              </block>
                            </next>
                          </block>
                        </next>
                      </block>
                    </next>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var strID: String;
var intSoupLength: Integer;
var strSoup: String;
var intCount: Integer;
var strXML: String;
var strNumArray: TStrArray;
var strNums: String;


function RandomIntBetween(a, b: integer): integer;
begin
  if (a > b) then
    begin
      // Swap a and b to ensure a is smaller.
      var c := a;
      a := b;
      b := c;
    end;
  result := Floor(Random() * (b - a + 1) + a)
end;
{
  Creates random 20 char ID
}
function CreateID(): String;
begin
  strID := '';
  strSoup := '!#$%()*+,-./:;=?@[]^_`{|}~ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  intSoupLength := length(strSoup);
  for var intCount := 1 to 20 do
  begin
    strID += CharAt(strSoup, (RandomIntBetween(1, intSoupLength) - 1) + 1);
  end;
  Result := strID;
end;


function Str(v: Variant) : string;
begin
  asm
    if (@v === 0) {
      @Result = '0';
    }
    else if (@v === '') {
      @Result = '';
    }
    else if (isNaN(@v)) {
      @Result = @v;
    }
    else {
      @Result = (@v).toString();
    }
  end;
end;



strNums := prompt('Please enter a comma-separated list of numbers');
strNumArray := StrSplit(strNums, ',');
strXML := '&lt;block type=&quot;lists_create_with&quot; id=&quot;';
strXML += CreateID();
strXML += '" x="50" y="50">&lt;mutation items=&quot;';
strXML += Str(strNumArray.length) + Str('&quot;&gt;&lt;/mutation&gt;');
for var i := 0 to strNumArray.length - 1 step 1 do
begin
  strXML += '&lt;value name= "ADD';
  strXML += Str(i) + Str('"&gt;&lt;block type= "math_number" id= "');
  strXML += CreateID();
  strXML += '"&gt;&lt;field name= "NUM"&gt;';
  strXML += strNumArray[((i + 1) - 1)];
  strXML += '&lt;/field&gt;&lt;/block&gt;&lt;/value&gt;';
end;
strXML += '&lt;/block&gt;';
Console.writeln(strXML);
    

Copy of Sample Output

<block type="lists_create_with" id="3EV.8G[5(py]D,lm$pdT" x="50" y="50"><mutation
 items="7"></mutation><value name= "ADD0"><block type= "math_number" 
id= "J(kaklA%B=ULa1]7;mG="><field name= "NUM">41</field></block></value>
<value name= "ADD1"><block type= "math_number" id= "33hQh=O?D9xwNp9,7^dw">
<field name= "NUM">42</field></block></value><value name= "ADD2">
<block type= "math_number" id= "axxP:Kl8B?/R3=cLDgDl"><field name=
 "NUM">43</field></block></value><value name= "ADD3">
<block type= "math_number" id= "7~ha)0~Q-n0ik(%(OsXp"><field name= "NUM">44</field>
</block></value><value name= "ADD4"><block type= "math_number"
 id= "x*4@^2767CXU!Uh]Q7%!"><field name= "NUM">45</field></block></value>
<value name= "ADD5"><block type= "math_number" id= "=Eoa~T7H.FTNtX4}G|u,">
<field name= "NUM">46</field></block></value><value name= "ADD6">
<block type= "math_number" id= "fBhOow7g@$gnF,FU(W+s"><field name=
 "NUM">47</field></block></value></block>

Generated block

Generated Block

Generated Block

Programming - a skill for life!

XML, images and generated Smart Pascal code of examples including loops, arrays, validation and recursion