2
$\begingroup$

How might I display a simplified SymbolicC below.

For example in the following example

Needs["SymbolicC`"]
CWhile[COperator[Less, {i, j}],
  CAssign[i, CCall[ fun, {i}]]
  ] // ToCCodeString[#1, "Indent" -> 1] &

Is it possible to display it like such?

Needs["SymbolicC`"]
fun@While[i < j,
  i = CCall[ fun, {i}]];
  ] // ToCCodeString[#1, "Indent" -> 1] &
$\endgroup$
1
  • $\begingroup$ So, you want fun to generate the first expression from the second? $\endgroup$ – Leonid Shifrin Feb 2 '15 at 20:06
3
$\begingroup$

This can be a start:

ClearAll[fun, $opsPatt];
    $opsPatt = Alternatives @@ {Less, Greater, LessEqual, GreaterEqual, Equal, Unequal};
SetAttributes[fun, HoldAll];
fun[code_] :=
  ReleaseHold[
    Hold[code] //. {
      (head : $opsPatt)[x_, y_] :> COperator[head, {x, y}],
      HoldPattern[Set[lhs_, rhs_]] :> CAssign[lhs, rhs],
      HoldPattern[While[cond_, c_]] :> CWhile[cond, c]
    }
  ];
$\endgroup$
3
$\begingroup$

Here is a more complete example.

Needs["SymbolicC`"]
ClearAll[fun, $opsPatt];
$opsPatt = 
  Alternatives @@ {Minus, PreDecrement, Decrement, PreIncrement, 
    Increment, Greater, Less, GreaterEqual, LessEqual, Equal, Unequal,
     Mod, Divide, Times, Subtract, Plus, BitShiftLeft, BitShiftRight, 
    BitAnd, BitXor, BitOr, And, Or, Not};
$head = Alternatives @@ {char, int, float, double, void};
SetAttributes[fun, HoldAll];
fun[code_] := Hold[code] //. {
    (head : $opsPatt)[x_, y_] :> COperator[head, {x, y}],
    HoldPattern[Set[x_, (head_: $head)[y_]]] :> 
     CDeclare[head, CAssign[x, y]], 
    HoldPattern[Set[lhs_, rhs_]] :> CAssign[lhs, rhs],
    (*standardmathoperator[x___]\[RuleDelayed]CStandardMathOperator[
    x],HoldPattern[RuleDelayed][test,a]*)
    conditional[x___] :> CConditional[x], 
    expression[x___] :> CExpression[x], block[x___] :> CBlock[x], 
    program[x___] :> CProgram[x], statement[x___] :> CStatement[x], 
    do[x___] :> CDo[x], for[x___] :> CFor[x], if[x___] :> CIf[x], 
    switch[x___] :> CSwitch[x], default[x___] :> CDefault[x], 
    while[x___] :> CWhile[x], cast[x___] :> CCast[x], 
    HoldPattern[RuleDelayed][x_, c___] :> CDeclare[x, c],
    HoldPattern[StringExpression][x___] :> CDeclare[x], 
    enum[x___] :> CEnum[x], pointertype[x___] :> CPointerType[x], 
    struct[x___] :> CStruct[x], union[x___] :> CUnion[x], 
    typedef[x___] :> CTypedef[x], sizeof[x___] :> CSizeOf[x], 
    return[x___] :> CReturn[x], break[x___] :> CBreak[x], 
    continue[x___] :> CContinue[x], goto[x___] :> CGoto[x], 
    label[x___] :> CLabel[x], member[x___] :> CMember[x], 
    HoldPattern[Rule][x___] :> CPointerMember[x], 
    Function[x___] :> CAddress[x], $[x___] :> CDereference[x], 
    array[x___] :> CArray[x], 
    constant[x___] :> CConstant[x],(*comment[
    x___]\[RuleDelayed]CComment[x] parentheses[
    x___]\[RuleDelayed]CParentheses[x],*)str[x___] :> CString[x], 
    include[x___] :> CInclude[x], define[x___] :> CDefine[x], 
    error[x___] :> CError[x], line[x___] :> CLine[x], 
    pragma[x___] :> CPragma[x], undef[x___] :> CUndef[x], 
    fn[t_, n_, args_, x___] :> CFunction[t, n, args, {x}]};
t = fun@program[
    include["stdlib.h"],
    include["constants.h"],
    fn[int, yGreaterX, {{int, x}, {int, y}},
     i = int[3],
     if[x < y,
      block[{
        i = int[3]
        }], block[{
        x = int[4]
        }]
      ],
     return[i]
     ]
    ];
StringReplace[
 ToCCodeString[ReleaseHold@t, "Indent" -> 1], {"\n\n" -> "\n", 
  RegularExpression["\n[\\s]*{"] -> "{"}]
$\endgroup$
0
$\begingroup$

This is a complete example.

ClearAll[fun, $opsPatt];
    $opsPatt = 
  Alternatives @@ {Less, Greater, LessEqual, GreaterEqual, Equal, 
    Unequal};
SetAttributes[fun, HoldAll];
fun[code_] := ReleaseHold[
   Hold[code] //. {
     (head : $opsPatt)[x_, y_] :> COperator[head, {x, y}], 
     HoldPattern[Set[lhs_, rhs_]] :> CAssign[lhs, rhs], 
     HoldPattern[while[cond_, c_]] :> CWhile[cond, c]
     }];

ReleaseHold@fun@while[i < j,
   i = CCall[fun, {i}]
   ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.