org.dellroad.jc.cgen
Class CExpr

java.lang.Object
  extended byorg.dellroad.jc.cgen.CExpr

public class CExpr
extends Object

Class for assembling C language expressions and outputting them as C code.

The purpose of this class is to allow us to output arbitrarily complex expressions in C that contain sufficient, but not stupidly unnecessary, parentheses. Note that we do output unnecessary parentheses in those cases where it's deemed to make things clearer.


Field Summary
static int ADD
           
static int ADDRESS_OF
           
static int AND
           
static int ARRAYINDEX
           
static int ARROW
           
static int ATOMIC
           
static int CAST
           
static int COMPLEMENT
           
static int DEREFERENCE
           
static int DIVIDE
           
static int EQUAL
           
static int FUNCTION
           
static int GE
           
static int GT
           
static int LE
           
static int LOGICAL_AND
           
static int LOGICAL_NOT
           
static int LOGICAL_OR
           
static int LT
           
static int MODULO
           
static int MULTIPLY
           
static int NEGATE
           
static int NOT_EQUAL
           
static int NUM_TYPES
           
static int OR
           
static int SHIFT_LEFT
           
static int SHIFT_RIGHT
           
static int SUBTRACT
           
static int THREEWAY
           
static int XOR
           
 
Constructor Summary
CExpr(int type, CExpr op1)
          Create a function invocation or unary C expression.
CExpr(int type, CExpr op1, CExpr op2)
          Create a function invocation or binary C expression.
CExpr(int type, CExpr op1, CExpr op2, CExpr op3)
          Create a function invocation or ternary C expression.
CExpr(int type, Collection ops)
           
CExpr(int type, Object op1)
          Create a function invocation or unary C expression.
CExpr(int type, Object[] ops)
           
CExpr(int type, Object op1, Object op2)
          Create a function invocation or binary C expression.
CExpr(int type, Object op1, Object op2, Object op3)
          Create a function invocation or ternary C expression.
CExpr(int type, Object op1, Object op2, Object op3, Object op4)
          Create a function invocation.
CExpr(String s)
          Create an "atomic" C expression containing the given string.
 
Method Summary
 void dumpOps(int depth)
           
static boolean isBinary(int type)
           
static boolean isTernary(int type)
           
static boolean isUnary(int type)
           
static void main(String[] args)
          Test method.
static CExpr random(Random r)
          Generate a random C expression.
 String toString()
          Return valid C code that expresses this C expression.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ATOMIC

public static final int ATOMIC
See Also:
Constant Field Values

DEREFERENCE

public static final int DEREFERENCE
See Also:
Constant Field Values

ADDRESS_OF

public static final int ADDRESS_OF
See Also:
Constant Field Values

LOGICAL_NOT

public static final int LOGICAL_NOT
See Also:
Constant Field Values

COMPLEMENT

public static final int COMPLEMENT
See Also:
Constant Field Values

NEGATE

public static final int NEGATE
See Also:
Constant Field Values

ADD

public static final int ADD
See Also:
Constant Field Values

SUBTRACT

public static final int SUBTRACT
See Also:
Constant Field Values

LOGICAL_AND

public static final int LOGICAL_AND
See Also:
Constant Field Values

LOGICAL_OR

public static final int LOGICAL_OR
See Also:
Constant Field Values

AND

public static final int AND
See Also:
Constant Field Values

OR

public static final int OR
See Also:
Constant Field Values

XOR

public static final int XOR
See Also:
Constant Field Values

DIVIDE

public static final int DIVIDE
See Also:
Constant Field Values

MULTIPLY

public static final int MULTIPLY
See Also:
Constant Field Values

MODULO

public static final int MODULO
See Also:
Constant Field Values

SHIFT_LEFT

public static final int SHIFT_LEFT
See Also:
Constant Field Values

SHIFT_RIGHT

public static final int SHIFT_RIGHT
See Also:
Constant Field Values

ARROW

public static final int ARROW
See Also:
Constant Field Values

ARRAYINDEX

public static final int ARRAYINDEX
See Also:
Constant Field Values

LT

public static final int LT
See Also:
Constant Field Values

LE

public static final int LE
See Also:
Constant Field Values

GT

public static final int GT
See Also:
Constant Field Values

GE

public static final int GE
See Also:
Constant Field Values

EQUAL

public static final int EQUAL
See Also:
Constant Field Values

NOT_EQUAL

public static final int NOT_EQUAL
See Also:
Constant Field Values

CAST

public static final int CAST
See Also:
Constant Field Values

FUNCTION

public static final int FUNCTION
See Also:
Constant Field Values

THREEWAY

public static final int THREEWAY
See Also:
Constant Field Values

NUM_TYPES

public static final int NUM_TYPES
See Also:
Constant Field Values
Constructor Detail

CExpr

public CExpr(String s)
Create an "atomic" C expression containing the given string.


CExpr

public CExpr(int type,
             CExpr op1)
Create a function invocation or unary C expression.


CExpr

public CExpr(int type,
             CExpr op1,
             CExpr op2)
Create a function invocation or binary C expression.


CExpr

public CExpr(int type,
             CExpr op1,
             CExpr op2,
             CExpr op3)
Create a function invocation or ternary C expression.


CExpr

public CExpr(int type,
             Object op1)
Create a function invocation or unary C expression.


CExpr

public CExpr(int type,
             Object op1,
             Object op2)
Create a function invocation or binary C expression.


CExpr

public CExpr(int type,
             Object op1,
             Object op2,
             Object op3)
Create a function invocation or ternary C expression.


CExpr

public CExpr(int type,
             Object op1,
             Object op2,
             Object op3,
             Object op4)
Create a function invocation.


CExpr

public CExpr(int type,
             Collection ops)

CExpr

public CExpr(int type,
             Object[] ops)
Method Detail

isUnary

public static boolean isUnary(int type)

isBinary

public static boolean isBinary(int type)

isTernary

public static boolean isTernary(int type)

toString

public String toString()
Return valid C code that expresses this C expression.


random

public static CExpr random(Random r)
Generate a random C expression. Used for testing.


dumpOps

public void dumpOps(int depth)

main

public static void main(String[] args)
Test method.