Пример #1
0
    //TODO: args processing
    public static Object Closure(Cons symbols, LSharp.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();\n", environment.GetValue(arg));

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

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

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

        }
        return v;
    }
Пример #2
0
        public static string GenerateAssign(Symbol s, object value, LSharp.Environment environment)
    {
        if (environment.Contains(s))
        {
            object sn = environment.GetValue(s);
            return string.Format(@"{1}
{0} = retval;",
                                 sn, value) + NewLine;
        }
        else
        {
            environment.Assign(s, s.Name);
            return string.Format(@"{1}
object {0} = retval;",
                                 s.Name, value) + NewLine;
        }
    }
Пример #3
0
        public static string GenerateCons(Cons args, LSharp.Environment environment)
    {
        // ananlysi cons
        if (args == null)
        {
            return @"//retval = null; // dont null mite need retval
";
        }
        else
        {
            Symbol sym = args.Car() as Symbol;

            object e = Runtime.Eval(sym, environment);

            if (e is Function)
            {
                Function f = e as Function;
                
                string v = "{" + NewLine;
                Cons rest = args.Rest() as Cons;
                v += GenerateFuncCall(f.Method.DeclaringType.ToString(), f.Method.Name, rest, environment);
                return v + "}" + NewLine;
            }

            else if (e is SpecialForm)
            {
                SpecialForm f = e as SpecialForm;

                Cons rest = args.Rest() as Cons;

                string r = Printer.ConsToString(rest);
                string lFName = f.Method.Name.ToLower();
                if (lFName == "while")
                    return While(rest, environment) as string;
                if (lFName == "for")
                    return For(rest, environment) as string;
                if (lFName == "and")
                    return And(rest, environment) as string;
                if (lFName == "call")
                    return Call(rest, environment) as string;
                if (lFName == "cond")
                    return Cond(rest, environment) as string;
                if (lFName == "do")
                    return Do(rest, environment) as string;
                if (lFName == "foreach")
                    return ForEach(rest, environment) as string;
                if (lFName == "if")
                    return If(rest, environment) as string;
                if (lFName == "let")
                    return Let(rest, environment) as string;
                if (lFName == "or")
                    return Or(rest, environment) as string;
                if (lFName == "quote")
                    return Quote(rest, environment) as string;
                if (lFName == "setq")
                    return Setq(rest, environment) as string;
                if (lFName == "the")
                    return The(rest, environment) as string;
                if (lFName == "to")
                    return To(rest, environment) as string;
                if (lFName == "try")
                    return Try(rest, environment) as string;
                if (lFName == "when")
                    return When(rest, environment) as string;
                if (lFName == "with")
                    return With(rest, environment) as string;
                
                return Runtime.EvalString("(" + f.Method.Name + " " + r + ")", environment) as string;
            }

            else if (e is Macro)
            {
                Macro m = e as Macro;
                Cons rest = args.Rest() as Cons;
                Cons em = m.Expand(rest) as Cons;

                return Generate(em, environment);
            }
            else if (e is Closure)
            {
                extracode += Closure(new Cons(sym), environment) as string;
                
                string v = "{" + NewLine;
                Cons rest = args.Rest() as Cons;
                v += GenerateFuncCall(null, environment.GetValue(sym) as string, rest, environment);
                return v + "}" + NewLine;
                
            }
            else if (currsymbols.ContainsKey(sym))
            {
                string v = "{" + NewLine;
                Cons rest = args.Rest() as Cons;
                v += GenerateFuncCall(null, environment.GetValue(sym) as string, rest, environment);
                return v + "}" + NewLine;
            }
            else
            {
                // not good, lets not support this for now: .NET method call
                try
                {
                    // try: LSharp.Runtime.Appy(<args>);
                    Cons rest = args.Rest() as Cons;
                    string ret = GenerateList(rest, environment, "temporarytableforspecialfunctioncall");
                    ret += "\nLSharp.Runtime.Apply(retval, LSharp.Cons.FromList(temporarytableforspecialfunctioncall), environment);";
                    //return ret; //"// call not supported";// Call(args, environment).ToString();
                    return Call(args, environment) as string;
                }
                catch
                {
                    string margs = GetArgs();
                    string v = GenerateList(args.Rest() as Cons, environment, margs);
                    return string.Format("retval = LSharp.Cons.FromList({0});{1}",
                                         margs, NewLine);
                }
            }
        }
    }
Пример #4
0
        public static string GenerateFuncCall(string type, string method, Cons rest, LSharp.Environment environment)
    {
        string v = string.Empty;
        string typemeth = method;
        if (type != null)
        {
            typemeth = type + "." + method;
        }

        if (rest == null)
        {
            return v + "retval = " + typemeth
                + string.Format(@"(null, environment);
");
        }
        else
        {
            if (rest.Length() == 1)
            {
                object argo = rest.First();
                string argn = "retval";
                if (argo is Symbol)
                {
                    Symbol sarg = (Symbol)argo;

                    if (environment.Contains(sarg))
                    {
                        argn = environment.GetValue(sarg) as string;
                    }
                    else
                    {
                        v += Generate(argo, environment);
                    }
                }
                else if (argo is Cons)
                {
                    v += Generate(argo, environment);
                }
                else
                {
                    // load primitive
                    argn = Printer.WriteToString(argo);
                }

                return v + "retval = " + typemeth
                    + string.Format(@"(new LSharp.Cons({0}), environment);
", argn);
                
            }
            else
            {
                string margs = GetArgs();
                v += GenerateList(rest, environment, margs);

                return v + "retval = " + typemeth
                    + string.Format(@"(LSharp.Cons.FromList({0}), environment);
", margs);
            }
        }
    }
Пример #5
0
        public static string GenerateList(Cons rest, LSharp.Environment environment, string margs)
    {
        string v = string.Format(@"System.Collections.ArrayList {0} = new System.Collections.ArrayList({1});
", margs, rest.Length());
        // load args
        foreach (object argo in rest)
        {
            if (argo is Symbol)
            {
                Symbol sarg = (Symbol)argo;

                if (environment.Contains(sarg))
                {
                    v += string.Format(@"{1}.Add({0});
", environment.GetValue(sarg), margs);
                }
                else
                {
                    v += Generate(argo, environment);
                    v += margs + @".Add(retval);
";
                }
            }
            else if (argo is Cons)
            {
                v += Generate(argo, environment);
                v += margs + @".Add(retval);
";
            }
            else
            {
                // load primitive
                v += string.Format(@"{1}.Add({0});
", Printer.WriteToString(argo), margs);
            }
        }
        return v;
    }
Пример #6
0
     public static string Generate(object obj, LSharp.Environment environment)
 {
     if (obj is Cons)
     {
         return GenerateCons(obj as Cons, environment);
     }
     string s = "// " + Printer.WriteToString(obj) + NewLine;
     if (obj is Symbol)
     {
         if (environment.Contains(obj as Symbol))
         {
             string symn = environment.GetValue(obj as Symbol) as string;
             return s + "retval = " + symn + ";" +  NewLine;
         }
         else
         {
             return s + "retval = " + obj + ";" +  NewLine;
         }
     }
     else
     {
         return s + "retval = "  + Printer.WriteToString(obj) + ";" + NewLine;
     }
 }