Ejemplo n.º 1
0
        public static Object When(Cons args, Environment environment)
        {
            string v = //"//(when " + Printer.ConsToString(args) + ")" + NewLine +
                       Generate(args.First(), environment) +
                       string.Format(@"
if (LSharp.Conversions.ObjectToBoolean(retval))
{{  // Evaluate the then part
  {0}
}}
", Generate(args.Second(), environment));

            return(v);
        }
Ejemplo n.º 2
0
        public static Object Quote(Cons args, Environment environment)
        {
            //string v = "//(quote " + Printer.ConsToString(args) + ")" + NewLine;
            string v = string.Empty;
            //reconstruct

            Cons c = args.First() as Cons;

            if (c != null)
            {
                string margs = GetArgs();
                v += GenerateList(c, environment, margs);
                v += string.Format("retval = LSharp.Cons.FromList({0});", margs);
            }
            Symbol s = args.First() as Symbol;

            if (s != null)
            {
                v += string.Format("retval = (LSharp.Symbol)\"{0}\";", s);
            }

            return(v + NewLine);
        }
Ejemplo n.º 3
0
        public static Object Let(Cons args, Environment environment)
        {
            string v = //"//(let " + Printer.ConsToString(args) + ")" + NewLine +
                       "{" + NewLine;
            Environment localEnvironment = new Environment(environment);

            v += GenerateAssignLocal((Symbol)args.First(), Generate(args.Second(), environment), localEnvironment);

            foreach (object item in (Cons)args.Cddr())
            {
                v += Generate(item, localEnvironment);
            }

            return(v + "}" + NewLine);
        }
Ejemplo n.º 4
0
        public static Object Cond(Cons args, Environment environment)
        {
            //string v = "//(cond " + Printer.ConsToString(args) + ")" + NewLine;
            string v       = string.Empty;
            Cons   clauses = args;
            string label   = GetLabel("cond");

            while (clauses.Length() > 0)
            {
                if (clauses.Length() == 1)
                {
                    // This is a default (else) clause, so just execute it
                    string sv = Generate(clauses.First(), environment) + NewLine;
                    v += sv;
                    break;
                }

                if (clauses.Length() >= 2)
                {
                    string sv = Generate(clauses.First(), environment) + NewLine +
                                string.Format(@"if (LSharp.Conversions.ObjectToBoolean(retval))
{{
  {0}
  goto {1};
}}
", Generate(clauses.Second(), environment), label);

                    v += sv;

                    clauses = (Cons)clauses.Cddr();
                }
            }
            v += string.Format(@"{0}:;
", label);
            return(v);
        }
Ejemplo n.º 5
0
        public static Object With(Cons args, Environment environment)
        {
            string v = //"//(with " + Printer.ConsToString(args) + ")" + NewLine +
                       "{" + NewLine;
            Cons        bindings         = (Cons)args.First();
            Environment localEnvironment = new Environment(environment);

            while ((bindings != null) && (bindings.Length() > 1))
            {
                v       += GenerateAssignLocal((Symbol)bindings.First(), Generate(bindings.Second(), environment), localEnvironment);
                bindings = (Cons)bindings.Cddr();
            }

            foreach (object item in (Cons)args.Cdr())
            {
                v += Generate(item, localEnvironment);
            }
            return(v + "}");
        }
Ejemplo n.º 6
0
        public static SimObjectType LoadConfigCons(Cons cons)
        {
            Object first = Cons.First(cons);

            cons  = (Cons)Cons.Rest(cons);
            first = ((Symbol)first).ToString();
            if (first.ToString().ToLower() == "createobjectuse")
            {
                Object second = Cons.First(cons);
                return(CreateObjectUse(second.ToString(), ConsParams((Cons)Cons.Rest(cons))));
            }
            else
            if (first.ToString().ToLower() == "createobjecttype")
            {
                Object second = Cons.First(cons);
                return(CreateObjectType(second.ToString(), ConsParams((Cons)Cons.Rest(cons))));
            }
            else
            {
                return(CreateObjectType(first.ToString(), ConsParams(cons)));
            }
        }
Ejemplo n.º 7
0
        public static Object ForEach(Cons args, Environment environment)
        {
            //string v = "//(each " + Printer.ConsToString(args) + ")" + NewLine;
            string      v = "";
            Environment localEnvironment = new Environment(environment);
            Symbol      variable         = (Symbol)args.First();

            string vn = localEnvironment.AssignLocal(variable, MakeUnique(variable.Name)) as string;

            v += Generate(args.Second(), environment) +
                 string.Format(@"
foreach (object {0} in (System.Collections.IEnumerable)retval) 
{{", vn);

            foreach (object item in (Cons)args.Cddr())
            {
                v += Generate(item, localEnvironment);
            }

            v += "}" + NewLine;

            return(v);
        }
Ejemplo n.º 8
0
        public static Object If(Cons args, Environment environment)
        {
            string v = //"//(if " + Printer.ConsToString(args) + ")" + NewLine +
                       Generate(args.First(), environment) +
                       string.Format(@"
if (LSharp.Conversions.ObjectToBoolean(retval))
{{  // Evaluate the then part
  {0}
}}
", Generate(args.Second(), environment));

            if (args.Length() > 2)
            {
                // Evaluate the optional else part
                v += string.Format(@"
else
{{
  {0}
}}
", Generate(args.Third(), environment));
            }

            return(v);
        }
Ejemplo n.º 9
0
        public static Object For(Cons args, Environment environment)
        {
            //string v = "//(for " + Printer.ConsToString(args) + ")" + NewLine + "{" + NewLine;
            string      v = "{" + NewLine;
            Environment localEnvironment = new Environment(environment);

            v += Generate(args.First(), localEnvironment);
            v += Generate(args.Second(), localEnvironment);
            v += @"while ((Conversions.ObjectToBoolean(retval)) 
{
";
            foreach (object item in (Cons)args.Cdddr())
            {
                v += Generate(item, localEnvironment);
            }

            v += Generate(args.Third(), localEnvironment);

            v += Generate(args.Second(), localEnvironment);

            v += @"}
";
            return(v + "}" + NewLine);
        }
Ejemplo n.º 10
0
        public static Object Closure(Cons symbols, Environment environment)
        {
            string v = string.Empty;

            Symbol s = symbols.First() as Symbol;

            if (s != null)
            {
                string name  = s.Name;
                string cname = MakeCompat(name);

                Closure c = Runtime.Eval(s, environment) as Closure;

                if (c == null)
                {
                    return(NewLine);
                }

                v += string.Format(@"
[LSharp.Symbol(""{0}"")]
public static object {1}(LSharp.Cons args, LSharp.Environment environment)
{{
object retval = null;
", name, cname);


                currsymbols.Add(s, c);
                //after eval
                environment.AssignLocal(s, cname);

                Cons argnames = c.GetArgumentNames();
                Cons body     = c.GetBody();

                if (argnames != null)
                {
                    int i  = 0;
                    int al = argnames.Length();

                    foreach (Symbol arg in argnames)
                    {
                        environment.AssignLocal(arg, MakeCompat(arg.Name));

                        v += string.Format(@"object {0} = args.Car();
", environment.GetValue(arg));

                        if (++i < al)
                        {
                            v += @"args = (LSharp.Cons)args.Cdr();
";
                        }
                    }
                }

                // BODY
                if (body != null)
                {
                    foreach (object con in body)
                    {
                        v += Generate(con, environment);
                    }
                }

                // RETURN
                v += @"
return retval;
}
";
            }
            return(v);
        }