예제 #1
0
 public static CodeResult GetCodeResult(string filepath, string entry = "main")
 {
     filepath = Program.GetFilePath(filepath);
     if (!codeCache.ContainsKey(filepath))
     {
         string code = System.IO.File.ReadAllText(filepath);
         CellMachineAssembler assembler = new CellMachineAssembler(code, entry);
         CodeResult           cr        = CellMachineAssembler.Assemble(code, entry);
         codeCache.Add(filepath, cr);
     }
     return(codeCache[filepath]);
 }
예제 #2
0
            public static void TestCompileFac()
            {
                string     eval  = System.IO.File.ReadAllText(Program.GetFilePath("Eval.asm"));
                string     entry = "main";
                CodeResult result;

                try {
#if DEBUG
                    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                    sw.Start();
#endif
                    result = CellMachineAssembler.Assemble(eval, entry);
#if DEBUG
                    sw.Stop();
                    Console.Error.WriteLine("!Code assembled in {0}", sw.Elapsed);
#endif
                } catch (Exception e) {
                    Console.WriteLine("Failed to assemble: {0}", e.Message);
#if DEBUG
                    Console.WriteLine("Stack trace:");
                    Console.WriteLine(e.StackTrace);
#endif
                    return;
                }

                List <Cell> args = new List <Cell>();
                args.Add(new Cell(10));

                Machine machine = new Machine(result, args);

                while (machine.Finished == false)
                {
                    machine.Step();
                    if (false)
                    {
                        machine.PrintState();
                    }
                }
            }
예제 #3
0
            public static CodeResult Assemble(string code, string entry)
            {
                CellMachineAssembler assembler = new CellMachineAssembler(code, entry);

                return(assembler.Generate());
            }
예제 #4
0
            public static void TestCompileEval()
            {
                string eval  = System.IO.File.ReadAllText(Program.GetFilePath("Eval.asm"));
                string entry = "main";
                CellMachineAssembler assembler = new CellMachineAssembler(eval, entry);
                CodeResult           cr;

                try {
                    cr = CellMachineAssembler.Assemble(eval, entry);
                } catch (Exception e) {
                    Console.WriteLine("Failed to assemble: {0}", e.Message);
#if DEBUG
                    Console.WriteLine("Stack trace:");
                    Console.WriteLine(e.StackTrace);
#endif
                    return;
                }

                Console.WriteLine("Compiled Core/Eval.asm, {0} bytes into:", eval.Length);
                Console.WriteLine("  Code: {0} instructions ({1} bytes)", cr.Code.Count, cr.Code.Count * sizeof(int));
                int wordlength = 0;
                foreach (Cell c in cr.Data)
                {
                    wordlength += c.Value.Length;
                }
                Console.WriteLine("  Data: {0} elements ({1} bytes)", cr.Data.Count, wordlength);

                SchemeEnvironment env = new SchemeEnvironment();
                StandardRuntime.AddGlobals(env);

                string code = ""
                              + "(begin "
                              + "   (define fac (lambda (n) (if (<= n 1) 1 (* n (fac (- n 1))))))"
                              + "   (fac 10))";
                if (1 == 1)
                {
                    AssertEqual(Eval(StandardRuntime.True.Value, cr, env), StandardRuntime.True);
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("1", cr, env), new Cell(1));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval(new Cell(new Cell[] { }), cr, env), StandardRuntime.Nil);
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("()", cr, env), StandardRuntime.Nil);
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("(quote 1 2 3)", cr, env), new Cell(1));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("(define x 123)", cr, env), new Cell(123));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("x", cr, env), new Cell(123));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("(set! x 456)", cr, env), new Cell(456));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("x", cr, env), new Cell(456));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("(lambda (x y) (+ x y))", cr, env), new Cell("#Lambda((x y) (+ x y))"));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("(begin (define y 789) y)", cr, env), new Cell(789));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("(+ 1 2)", cr, env), new Cell(3));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("(begin (define add (lambda (x y) (+ x y))) (add 1 2))", cr, env), new Cell(3));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("(if (= 1 1) 1 0)", cr, env), new Cell("1"));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("(if (= 0 1) 0 1)", cr, env), new Cell("1"));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("(if (= 1 1) 1)", cr, env), new Cell("1"));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("(if (= 1 0) 1)", cr, env), StandardRuntime.Nil);
                }
                if (1 == 1)
                {
                    AssertEqual(Eval(code, cr, env), new Cell(3628800));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("(if (= 1 1) (+ 1 1) (- 1 1))", cr, env), new Cell(2));
                }
                if (1 == 1)
                {
                    AssertEqual(Eval("(if (= 1 1) (begin 1) (- 1 1))", cr, env), new Cell(1));
                }
            }