private static Lexeme EvalLessThan(Lexeme t, Lexeme env) { var left = Eval(t.Left, env); var right = Eval(t.Right, env); if (left.type == "INTEGER" && right.type == "INTEGER") { if (left.ival < right.ival) { return(new Lexeme("INTEGER", 1)); } else { return(new Lexeme("INTEGER", 0)); } } throw new Exception("Invalid LESSTHAN!" + ExceptionTypesLeftRight(left, right)); }
Lexeme op7() { Lexeme tree = null; if (check("TIMES")) { tree = match("TIMES"); } else if (check("DIVIDES")) { tree = match("DIVIDES"); } else if (check("MOD")) { tree = match("MOD"); } return(tree); }
private static Lexeme EvalGreaterThanEqualTo(Lexeme t, Lexeme env) { var left = Eval(t.Left, env); var right = Eval(t.Right, env); if (left.type == "INTEGER" && right.type == "INTEGER") { if (left.ival >= right.ival) { return(new Lexeme("INTEGER", 1)); } else { return(new Lexeme("INTEGER", 0)); } } throw new Exception("Invalid GREATERTHAN_EQUALTO!" + ExceptionTypesLeftRight(left, right)); }
private static Lexeme EvalIf(Lexeme tree, Lexeme env) { var conditional = tree.Left.Left; var ifBlock = tree.Left.Right.Left; var origCond = conditional; //only used for error message conditional = Eval(conditional, env); if (conditional.type != "INTEGER") { throw new Exception("Invalid conditional in IF statement! Conditional is type: " + origCond.type); } if (conditional.ival == 1) //true { var ifEnv = Environment.Extend(null, null, env); return(Eval(ifBlock, ifEnv)); } else //false { if (tree.Right != null) { if (tree.Right.type == "ELIF") { return(EvalIf(tree.Right, env)); //shares same logic as IF so no need to make a new function } else if (tree.Right.type == "ELSE") { return(EvalElse(tree.Right, env)); } else { throw new Exception("Invalid link in if statement: " + tree.Right.type); } } else { return(conditional); //if is false and no chain } } }
/// <summary> /// Prints arguments to console followed by a newline /// </summary> private static Lexeme EvalPrintLn(Lexeme tree, Lexeme env) { var args = tree.Left.Left; if (args == null) { Console.WriteLine(); } else { string message = Eval(args.Left, env).GetValue(); while (args.Right != null) { message += Eval(args.Right.Left, env).GetValue(); args = args.Right; } Console.WriteLine(message); } return(tree); }
private static Lexeme EvalPlusTo(Lexeme t, Lexeme env) { var left = t.Left; var right = Eval(t.Right, env); var leftVal = Environment.Lookup(left.sval, env); if (leftVal.type == "INTEGER" && right.type == "INTEGER") { var newVal = (leftVal.ival + right.ival).ToString(); Environment.Update(t.Left.sval, env, newVal); } else { var newVal = (leftVal.GetValue() + right.GetValue()).ToString(); Environment.Update(t.Left.sval, env, newVal); } t = t.Left; return(t); }
private static Lexeme EvalTimesTo(Lexeme t, Lexeme env) { var left = t.Left; var right = Eval(t.Right, env); var leftVal = Environment.Lookup(left.sval, env); String newVal = null; if (leftVal.type == "INTEGER" && right.type == "INTEGER") { newVal = (leftVal.ival * right.ival).ToString(); Environment.Update(t.Left.sval, env, newVal); } else { throw new Exception("Unexpected types in EvalTimesTo!" + ExceptionTypesLeftRight(leftVal, right)); } t = t.Left; return(t); }
private static Lexeme EvalWhile(Lexeme tree, Lexeme env) { var conditional = Eval(tree.Left, env); var whileBlock = tree.Right.Left; if (conditional.type != "INTEGER") { throw new Exception("Invalid conditional in WHILE statement"); } if (conditional.ival == 1) //execute loop { var whileEnv = Environment.Extend(null, null, env); Eval(whileBlock, whileEnv); return(EvalWhile(tree, env)); } else { return(tree); } }
static public int Main() { Lexeme env = null; Console.WriteLine("Creating a new environment"); env = Environment.Create(); Environment.Print(env); Console.WriteLine("Adding variable x with value 3"); Lexeme vari1 = new Lexeme("ID", "x"); Lexeme val1 = new Lexeme("INTEGER", 3); Environment.Insert(vari1, val1, env); Environment.Print(env); Console.WriteLine("Extending the environment with y:4 and z:\"hello\""); var childEnv = Environment.Create(); Lexeme vari2 = new Lexeme("ID", "y"); Lexeme val2 = new Lexeme("INTEGER", 4); Environment.Insert(vari2, val2, childEnv); Lexeme vari3 = new Lexeme("ID", "z"); Lexeme val3 = new Lexeme("STRING", "hello"); vari2.Right = vari3; val2.Right = val3; Environment.Insert(vari3, val3, childEnv); env = Environment.Extend(childEnv.Left.Left, childEnv.Left.Right, env); Console.WriteLine("The local environment is:"); Environment.Print(env); Console.WriteLine("The environment is:"); Environment.Print(Environment.GetParent(env)); return(0); }
private static Lexeme EvalMinusTo(Lexeme t, Lexeme env) { var left = t.Left; var right = Eval(t.Right, env); var leftVal = Environment.Lookup(left.sval, env); String newVal = null; if (leftVal.type == "INTEGER" && right.type == "INTEGER") { newVal = (leftVal.ival - right.ival).ToString(); Environment.Update(t.Left.sval, env, newVal); } else { var baseStr = Eval(left, env).GetValue(); var removeStr = Eval(right, env).GetValue(); newVal = baseStr.Replace(removeStr, ""); Environment.Update(t.Left.sval, env, newVal); } t = t.Left; return(t); }
Lexeme op5() { Lexeme tree = null; if (check("LESSTHAN")) { tree = match("LESSTHAN"); } else if (check("LESSTHAN_EQUALTO")) { tree = match("LESSTHAN_EQUALTO"); } else if (check("GREATERTHAN")) { tree = match("GREATERTHAN"); } else if (check("GREATERTHAN_EQUALTO")) { tree = match("GREATERTHAN_EQUALTO"); } return(tree); }
/// <summary> /// no args returns random int from 1-100 inclusive /// one arg returns random int from 1-arg1 inclusive /// two args returns random int from arg1-arg2 inclusive /// </summary> private static Lexeme EvalRandom(Lexeme tree, Lexeme env) { var args = tree.Left.Left; int lowerBound = 1; int upperBound = 100; if (tree.Left != null) { upperBound = Convert.ToInt32(Eval(args.Left, env).GetValue()); if (tree.Left.Right != null) { lowerBound = upperBound; upperBound = Convert.ToInt32(Eval(args.Right, env).GetValue()); } } var result = new Random().Next(lowerBound, upperBound + 1); System.Threading.Thread.Sleep(new Random().Next(15, 20)); //Without sleeping for at least 11-15 milliseconds repeats will be generated. Sleeping a random time between 15-20 for more random? :D return(new Lexeme("INTEGER", result)); }
public static Lexeme Lookup(string id, Lexeme env) { if (id == "this") { return(env); } if (env.type == "CLOSURE") { env = env.Left; } while (env != null) { var table = Car(env); var vars = Car(table); var vals = Cdr(table); while (vars != null) { if (vars.type == "JOIN" && id == Car(vars).sval) { return(Car(vals)); } else if (id == vars.sval) { return(vals); } vars = Cdr(vars); vals = Cdr(vals); } env = Cdr(env); } throw new Exception(String.Format("variable '{0}' is undefined!", id)); }
public static void Print(Lexeme env) { if (env != null) { var table = Car(env); var vars = Car(table); var vals = Cdr(table); while (vars != null) { if (Car(vals).type == "STRING") { Console.WriteLine(String.Format("\t{0} : '{1}'", Car(vars).sval, Car(vals).GetValue())); } else { Console.WriteLine(String.Format("\t{0} : {1}", Car(vars).sval, Car(vals).GetValue())); } vars = Cdr(vars); vals = Cdr(vals); } } }
public static Lexeme GetParent(Lexeme env) { return(Cdr(env)); }
public static Lexeme Extend(Lexeme vars, Lexeme vals, Lexeme env) { return(Cons(new Lexeme("ENV"), Cons(new Lexeme("VALUES"), vars, vals), env)); }
public void SetEnvValue(Lexeme val) { type = "ENV"; envVal = val; }
//operators private static Lexeme EvalSimpleOp(Lexeme t, Lexeme env) { switch (t.type) { case "PLUS": return(EvalPlus(t, env)); case "PLUS_TO": return(EvalPlusTo(t, env)); case "INCREMENT": return(EvalIncrement(t, env)); case "MINUS": return(EvalMinus(t, env)); case "MINUS_TO": return(EvalMinusTo(t, env)); case "DECREMENT": return(EvalDecrement(t, env)); case "TIMES": return(EvalTimes(t, env)); case "TIMES_TO": return(EvalTimesTo(t, env)); case "DIVIDES": return(EvalDivides(t, env)); case "DIVIDES_TO": return(EvalDivideTo(t, env)); case "MOD": return(EvalMod(t, env)); case "MOD_TO": return(EvalModTo(t, env)); case "EXPN": return(EvalExpn(t, env)); case "EXPN_TO": return(EvalExpnTo(t, env)); case "LESSTHAN": return(EvalLessThan(t, env)); case "LESSTHAN_EQUALTO": return(EvalLessThanEqualTo(t, env)); case "GREATERTHAN": return(EvalGreaterThan(t, env)); case "GREATERTHAN_EQUALTO": return(EvalGreaterThanEqualTo(t, env)); case "EQUAL": return(EvalEqual(t, env)); case "NOT_EQUAL": return(EvalNotEqual(t, env)); } throw new Exception("Unrecognized operator!"); }
private static Lexeme GetFuncCallArgs(Lexeme t) { return(t.Right.Left); }
private static Lexeme GetFuncCallClosure(Lexeme t, Lexeme env) { return(Environment.Lookup(t.sval, env)); }
private static Lexeme GetClosureParams(Lexeme closure) { return(closure.Right.Right.Left); }
public static void SetCdr(Lexeme cons, Lexeme val) { cons.Right = val; }
public static void SetCar(Lexeme cons, Lexeme val) { cons.Left = val; }
//<<<<<<<<<<<<<<<<<<<<<<<< Builtins private static Lexeme EvalLambdaDef(Lexeme t, Lexeme env) { var body = t.Right; return(Eval(body, env)); }
public static Lexeme Car(Lexeme cons) { return(cons.Left); }
private static Lexeme EvalReturn(Lexeme tree, Lexeme env) { return(Eval(tree.Left, env)); }
public static Lexeme Cdr(Lexeme cons) { return(cons.Right); }
private static Lexeme GetFuncDefName(Lexeme t) { return(new Lexeme("ID", t.sval)); }
public static Lexeme Caddr(Lexeme cons) { return(cons.Right.Right.Left); }
private static Lexeme GetClosureEnvironment(Lexeme closure) { return(closure.Left); }