public IF(ExprTree Condition, ExprTree Then, ExprTree Else) { Type = ExprType.Branching; this.Condition = Condition; this.Then = Then; this.Else = Else; this.Add(Condition); }
//public abstract void Run(); public abstract void Run(ExprTree program);
public abstract string Compile(ExprTree program);
public override void Run(ExprTree program) { }
public override string Compile(ExprTree program) { string s = ""; program.TraverseDF((n) => { ExprTree e = (ExprTree)n; Type t = e.GetType(); if (t == typeof(IF)) { s += "IF("; } else if (t == typeof(LogicalAND)) { s += "AND("; LogicalAND la = (LogicalAND)e; } else if (t == typeof(LogicalNOT)) { s += "NOT("; LogicalNOT ln = (LogicalNOT)e; s += " " + ln.VariableOperand.name +" "; } }, (n) => { ExprTree e = (ExprTree)n; Type t = e.GetType(); if (t == typeof(IF)) { s += ")"; } else if (t == typeof(LogicalAND)) { s += ")"; } else if (t == typeof(LogicalNOT)) { s += ")"; } }); return s; }
public static void Main(string[] args) { info(); var lisp = new LISPCompiler(); lisp.symbols.Add(new Variable("n", true)); lisp.symbols.Add(new Variable("s", false)); lisp.symbols.Add(new Variable("e", false)); lisp.symbols.Add(new Variable("w", false)); lisp.symbols.Add(new Variable("ne", false)); lisp.symbols.Add(new Variable("nw", false)); lisp.symbols.Add(new Variable("se", false)); lisp.symbols.Add(new Variable("sw", false)); lisp.functions.Add(new Predicate((a) => Math.Sin(a)) { name = "SIN" }); lisp.functions.Add(new Predicate((a) => Math.Cos(a)) { name = "COS" }); lisp.functions.Add(new Predicate((a, b) => a && b) { name = "AND" }); lisp.functions.Add(new Predicate((a, b) => a || b) { name = "OR" }); lisp.functions.Add(new Predicate((a) => !a ) { name = "NOT" }); lisp.functions.Add(new Predicate(() => Console.WriteLine("east")) { name = "east" }); lisp.functions.Add(new Predicate(() => Console.WriteLine("west")) { name = "west" }); lisp.functions.Add(new Predicate(() => Console.WriteLine("north")) { name = "north" }); lisp.functions.Add(new Predicate(() => Console.WriteLine("south")) { name = "south" }); var program = new ExprTree(); program.Add(new IF ( Condition: new LogicalAND() { Left = new LogicalOR() { Left = new VariableOperand("n"), Right = new VariableOperand("ne") }, Right = new LogicalNOT(new VariableOperand("e")) }, Then: lisp.functions.Get("east"), Else: new IF ( Condition : new LogicalAND() { Left = new LogicalOR() { Left = new VariableOperand("e"), Right = new VariableOperand("se") }, Right = new LogicalNOT(new VariableOperand("s")) }, Then : lisp.functions.Get("south"), Else : new IF ( Condition : new LogicalAND() { Left = new LogicalOR() { Left = new VariableOperand("s"), Right = new VariableOperand("sw") }, Right = new LogicalNOT(new VariableOperand("w")) }, Then : lisp.functions.Get("west"), Else : lisp.functions.Get("north") ) ) ) ); string output = lisp.Compile(program); //lisp.Run(program); //Console.WriteLine(output); Console.ReadLine(); }