home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples1.exe / CLisp / Exp.cs < prev    next >
Encoding:
Text File  |  2000-06-23  |  9.3 KB  |  592 lines

  1.  
  2. using System;
  3. using System.Collections;
  4. using LexToken;
  5.  
  6.  
  7. namespace Absyn {
  8.     
  9.     enum Operator {
  10.     ADD = 2,
  11.     SUB = 3,
  12.     MUL = 4,
  13.     DIVIDE = 5,
  14.     LT = 6,
  15.     GT = 7,
  16.     EQ = 8,
  17.     LE = 9,
  18.     GE = 10
  19.     }
  20.  
  21.     class ExpList
  22.     {
  23.     public Exp Head;
  24.     public ExpList Tail;
  25.     
  26.     public override String ToString()
  27.     {
  28.         return (Head.ToString() + ((Tail != null) ? Tail.ToString() : null));
  29.     }
  30.     
  31.     }
  32.  
  33.     abstract class Exp 
  34.     {
  35.     public Type ExpType;
  36.     
  37.     public abstract void Visit(IExpVisitor v);
  38.     
  39.     }
  40.  
  41.         
  42.  
  43.     abstract class NumericExp : Exp
  44.     {
  45.     }
  46.  
  47.     abstract class BoolExp : Exp
  48.     {
  49.     }
  50.  
  51.     abstract class ListExp : Exp
  52.     {
  53.     }
  54.     
  55.     class BinopExp : NumericExp
  56.     {
  57.     public Exp Left, Right;
  58.     public Operator Oper;
  59.     
  60.     public BinopExp(Exp l, Token op, Exp r)
  61.     {
  62.         Left = l;
  63.         Right = r;
  64.         ExpType = typeof(int);
  65.         
  66.         switch(op.Type){
  67.         case TokenType.PLUS:
  68.         Oper = Operator.ADD;
  69.         break;
  70.         case TokenType.MINUS:
  71.         Oper = Operator.SUB;
  72.         break;
  73.         case TokenType.MUL:
  74.         Oper = Operator.MUL;
  75.         break;
  76.         case TokenType.DIVIDE:
  77.         Oper = Operator.DIVIDE;
  78.         break;
  79.         }
  80.     }
  81.  
  82.     public override String ToString()
  83.     {
  84.         return ("BinopExp(\n" + Left.ToString() + Oper.ToString() + "\n" + Right.ToString() + "\n)" );
  85.     }
  86.     
  87.     public override void Visit(IExpVisitor v) 
  88.     {
  89.         v.BinopExp(this);
  90.     }
  91.     }
  92.  
  93.     class CompareExp : BoolExp
  94.     {
  95.     public Exp Left, Right;
  96.     public Operator Oper;
  97.     
  98.     public CompareExp(Exp l, Token op, Exp r)
  99.     {
  100.         Left = l;
  101.         Right = r;
  102.         
  103.         switch(op.Type){
  104.         case TokenType.LT:
  105.         Oper = Operator.LT;
  106.         break;
  107.         case TokenType.GT:
  108.         Oper = Operator.GT;
  109.         break;
  110.         case TokenType.EQ:
  111.         Oper = Operator.EQ;
  112.         break;
  113.         case TokenType.LE:
  114.         Oper = Operator.LE;
  115.         break;
  116.         case TokenType.GE:
  117.         Oper = Operator.GE;
  118.         break;
  119.         }
  120.     }
  121.     
  122.     public override String ToString()
  123.     {
  124.         return ("CompareExp(\n" + Left.ToString() + Oper.ToString() + "\n" + Right.ToString() + "\n)" );
  125.     }
  126.  
  127.     public override void Visit(IExpVisitor v) 
  128.     {
  129.         v.CompareExp(this);
  130.     }
  131.     }
  132.     
  133.  
  134.     class IntExp : NumericExp
  135.     {
  136.     public long Value;
  137.     
  138.     public IntExp(long v)
  139.     {
  140.         Value = v;
  141.         ExpType = typeof(int);
  142.     }
  143.  
  144.     public override String ToString()
  145.     {
  146.         return ("IntExp(" + Value + ")\n");
  147.     }
  148.  
  149.     public override void Visit(IExpVisitor v) 
  150.     {
  151.         v.IntExp(this);
  152.     }
  153.     }
  154.  
  155.     class IsNullExp : BoolExp
  156.     {
  157.     public Exp Expr;
  158.     
  159.     public IsNullExp(Exp e)
  160.     {
  161.         Expr = e;
  162.     }
  163.     
  164.     public override String ToString()
  165.     {
  166.         return ("IsNullExp(\n" + Expr.ToString() + ")\n");
  167.     }
  168.  
  169.     public override void Visit(IExpVisitor v) 
  170.     {
  171.  
  172.     }
  173.     }
  174.     
  175.         
  176.     /*
  177.     class NilExp : ListExp
  178.     {
  179.     public override String ToString()
  180.     {
  181.         return "NilExp()\n";
  182.     }
  183.     
  184.     }
  185.     */
  186.  
  187.     class CdrExp : ListExp
  188.     {
  189.     public Exp Left;
  190.     
  191.     public CdrExp(Exp l)
  192.     {
  193.         Left = l;
  194.     }
  195.     
  196.     public override String ToString()
  197.     {
  198.         return ("CdrExp(\n" + Left.ToString() + ")\n");
  199.     }
  200.     
  201.     public override void Visit(IExpVisitor v)
  202.     {
  203.     }
  204.     
  205.     
  206.     }
  207.  
  208.     class CarExp : ListExp
  209.     {
  210.     public Exp Left;
  211.     
  212.     public CarExp(Exp l)
  213.     {
  214.         Left = l;
  215.     }
  216.     
  217.     public override String ToString()
  218.     {
  219.         return ("CarExp(\n" + Left.ToString() + ")\n");
  220.     }
  221.  
  222.     public override void Visit(IExpVisitor v) 
  223.     {
  224.         v.CarExp(this);
  225.     }
  226.     
  227.     }
  228.  
  229.  
  230.     class ConsExp : ListExp
  231.     {
  232.     public Exp Left, Right;
  233.     
  234.     public ConsExp(Exp l, Exp r)
  235.     {
  236.         Left = l;
  237.         Right = r;
  238.     }
  239.  
  240.     public override String ToString()
  241.     {
  242.         return ("ConsExp(\n" + Left.ToString() + ",\n " + Right.ToString() + ")\n");
  243.     }
  244.     public override void Visit(IExpVisitor v)
  245.     {
  246.     }
  247.     }
  248.  
  249.     class SubstExp : ListExp
  250.     {
  251.     public Exp Replace, Find, Expr;
  252.     
  253.     public SubstExp(Exp r, Exp f, Exp e)
  254.     {
  255.         Replace = r;
  256.         Find = f;
  257.         Expr = e;
  258.     }
  259.     
  260.     public override String ToString()
  261.     {
  262.         return ("SubsExp(\n" + Replace.ToString() + ",\n" + Find.ToString() + ",\n" + Expr.ToString() + ")\n");
  263.     }
  264.     public override void Visit(IExpVisitor v)
  265.     {
  266.     }
  267.     }
  268.  
  269.  
  270.     class StringExp : ListExp
  271.     {
  272.     public String Value;
  273.     
  274.     public StringExp(String v)
  275.     {
  276.         Value = v;
  277.     }
  278.  
  279.     public override String ToString()
  280.     {
  281.         return ("StringExp(" + Value + ")\n");
  282.     }
  283.     
  284.     public override void Visit(IExpVisitor v) 
  285.     {
  286.         v.StringExp(this);
  287.     }
  288.     }
  289.  
  290.     class VarExp : Exp
  291.     {
  292.     public String Name;
  293.     public int Pos;
  294.     
  295.     public VarExp(String n, int p)
  296.     {
  297.         Name = n;
  298.         Pos = p;
  299.     }
  300.     
  301.     public override String ToString()
  302.     {
  303.         return ("VarExp(" + Name + ", " + Pos + ")");
  304.     }
  305.  
  306.     public override void Visit(IExpVisitor v) 
  307.     {
  308.         v.VarExp(this);
  309.     }
  310.     
  311.     }
  312.  
  313.     class GlobalVarExp : Exp
  314.     {
  315.     public String Name;
  316.     
  317.     public GlobalVarExp(String n)
  318.     {
  319.         Name = n;
  320.         ExpType = typeof(int);
  321.     }
  322.     
  323.     public override String ToString()
  324.     {
  325.         return ("GlobalVarExp(" + Name + ")");
  326.     }
  327.  
  328.     public override void Visit(IExpVisitor v) 
  329.     {
  330.         v.GlobalVarExp(this);
  331.     }
  332.  
  333.     
  334.     }
  335.  
  336.  
  337.     class CallExp : Exp
  338.     {
  339.     public String ClassName;
  340.     public String FunctionName;
  341.     public Exp[] Params;
  342.     public bool System;
  343.     
  344.     public CallExp(String c, String f, Exp[] p)
  345.     {
  346.         ClassName = c;
  347.         FunctionName = f;
  348.         Params = p;
  349.         System = false;
  350.     }
  351.     
  352.     public CallExp(String c, String f, Exp[] p, Type t)
  353.     {
  354.         ClassName = c;
  355.         FunctionName = f;
  356.         Params = p;
  357.         System = true;
  358.         ExpType = t;
  359.     }
  360.     
  361.     public override String ToString()
  362.     {
  363.         return ("CallExp(" + FunctionName + ", " + Params.Length + ")");
  364.     }
  365.  
  366.     public override void Visit(IExpVisitor v) 
  367.     {
  368.         v.CallExp(this);
  369.     }
  370.     
  371.  
  372.     }
  373.  
  374.     class IfExp : Exp
  375.     {
  376.     public Exp EvalExp, ThenExp, ElseExp;
  377.     
  378.     public IfExp(Exp e, Exp thene, Exp elsee)
  379.     {
  380.         EvalExp = e;
  381.         ThenExp = thene;
  382.         ElseExp = elsee;
  383.     }
  384.     
  385.     public override String ToString()
  386.     {
  387.         return ("IfExp(\n" + EvalExp.ToString() + " ," + ThenExp.ToString() + " ," + ElseExp.ToString() + ")\n");
  388.     }
  389.  
  390.     public override void Visit(IExpVisitor v) 
  391.     {
  392.         v.IfExp(this);
  393.     }
  394.     }
  395.     
  396.     
  397.     class GlobalVarDef : Exp
  398.     {
  399.     public String Name;
  400.     public Exp Value;
  401.     
  402.     public GlobalVarDef(String n, Exp v)
  403.     {
  404.         Name = n;
  405.         Value = v;
  406.         ExpType = Value.ExpType;
  407.     }
  408.  
  409.     public override String ToString()
  410.     {
  411.         return ("GlobalVarDef(" + Name + ",\n" + Value.ToString() + ")");
  412.     }
  413.  
  414.     public override void Visit(IExpVisitor v)
  415.     {
  416.         v.GlobalVarDef(this);
  417.     }
  418.     
  419.     }
  420.  
  421.  
  422.     class FunctionDef : Exp
  423.     {
  424.     public String Name;
  425.     public ArrayList Params;
  426.     public Exp Body;
  427.     public int Count;
  428.     
  429.  
  430.     public FunctionDef(String n, ArrayList p, Exp b)
  431.     {
  432.         Name = n;
  433.         Params = p;
  434.         Body = b;
  435.         Count = Params.Count;
  436.         //ExpType = Body.ExpType;
  437.     }
  438.     
  439.     public override String ToString()
  440.     {
  441.         return ("FunctionDef( " + Name + " " + Params.Count + "\n" + Body.ToString() + ")");
  442.     }
  443.  
  444.     public override void Visit(IExpVisitor v)
  445.     {
  446.         v.FunctionDef(this);
  447.     }
  448.     }
  449.  
  450.  
  451.     class ToIntExp : NumericExp
  452.     {
  453.     public Exp Value;
  454.     
  455.     public ToIntExp(Exp v)
  456.     {
  457.         Value = v;
  458.     }
  459.     
  460.     public override String ToString()
  461.     {
  462.         return ("ToIntExp(\n" + Value.ToString() + ")");
  463.     }
  464.     public override void Visit(IExpVisitor v)
  465.     {
  466.         v.ToIntExp(this);
  467.     }
  468.     }
  469.  
  470.     class ToListExp : ListExp
  471.     {
  472.     public Exp Value;
  473.     
  474.     public ToListExp(Exp v)
  475.     {
  476.         Value = v;
  477.     }
  478.     
  479.     public override String ToString()
  480.     {
  481.         return ("ToListExp(\n" + Value.ToString() + ")");
  482.     }
  483.     
  484.     public override void Visit(IExpVisitor v)
  485.     {
  486.         v.ToListExp(this);
  487.     }
  488.     }
  489.     
  490.  
  491.     
  492.     class DoExp : Exp
  493.     {
  494.     public ExpList Vars, Conds;
  495.      
  496.     public DoExp(ExpList v, ExpList c)
  497.     {
  498.         Vars = v;
  499.         Conds = c;
  500.     }
  501.     
  502.     public override String ToString()
  503.     {
  504.         return ("DoExp(\n" + Vars.ToString() + ",\n" + Conds.ToString() + ")\n")  ;
  505.     }
  506.  
  507.     public override void Visit(IExpVisitor v)
  508.     {
  509.         v.DoExp(this);
  510.         
  511.     }
  512.     
  513.     }
  514.     
  515.     class DoVarExp : Exp
  516.     {
  517.     public String Name;
  518.     public int Pos;
  519.     
  520.     public DoVarExp(String n, int p)
  521.     {
  522.         Name = n;
  523.         Pos = p;
  524.     }
  525.     
  526.     public override String ToString()
  527.     {
  528.         return ("DoVarExp(\n" + Name + ", " + Pos.ToString() + ")");
  529.     }
  530.  
  531.     public override void Visit(IExpVisitor v)
  532.     {
  533.         v.DoVarExp(this);
  534.         
  535.     }
  536.     
  537.     }
  538.     
  539.         
  540.     class DoVarDef : Exp
  541.     {
  542.     public String Name;
  543.     public Exp Init, Iter;
  544.     public int Pos;
  545.     
  546.     public DoVarDef(String n, Exp init, Exp it, int p)
  547.     {
  548.         Name = n;
  549.         Init = init;
  550.         Iter = it;
  551.         Pos = p;
  552.     }
  553.     
  554.  
  555.     public override String ToString()
  556.     {
  557.         return ("DoVarDef(\n" + Name + ",\n" + Init.ToString() + ",\n" + Iter.ToString() + ")\n");
  558.     }
  559.  
  560.     public override void Visit(IExpVisitor v)
  561.     {
  562.         v.DoVarDef(this);
  563.     }
  564.     }
  565.     
  566.     class DoCondExp : Exp
  567.     {
  568.     public Exp Cond, Ret;
  569.     public System.Reflection.Emit.Label EndLabel;
  570.     
  571.     public DoCondExp(Exp c, Exp r)
  572.     {
  573.         Cond = c;
  574.         Ret = r;
  575.     }
  576.  
  577.     public override String ToString()
  578.     {
  579.         return ("DoCondExp(\n" + Cond.ToString() + ",\n" + Ret.ToString() + ")\n");
  580.     }
  581.  
  582.     public override void Visit(IExpVisitor v)
  583.     {
  584.         v.DoCondExp(this);
  585.     }
  586.     
  587.     }
  588.     
  589.  
  590. }
  591.  
  592.