www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Trying to build a Scheme interpreter in D

reply John <john.fourkiotis gmail.com> writes:
Hi,

I'm currently reading "Programming in D" and in order to get 
accustomed to D and it's syntax, I've decided to implement 
(actually port) a simple (and naive) Scheme interpreter from C to 
D. The original interpreter (in C) is described in a series of 
posts here:

http://peter.michaux.ca/articles/scheme-from-scratch-introduction

I've followed the tutorials and implemented various versions of 
the interpreter in different languages, and now I've decided to 
make a port to D. My source code resides in:

https://bitbucket.org/jfourkiotis/deimos

The basic object in my (naive, I repeat again) implementation, is 
the following:

alias Object = Algebraic!(
    long      , /* numbers   */
    bool      , /* #t or #f  */
    char      , /* characters*/
    string    , /* symbols   */
    char[]    , /* strings   */
    EmptyList , /* Nil       */
    ConsCell  ,
    void function(This*, This*), /* primitive procedures */
    CompoundProc);               /* compound procedures  */

where the following type definitions hold:

struct EmptyList {}

class ConsCell {
     Object car;
     Object cdr;

     this(Object car, Object cdr)
     {
         this.car = car;
         this.cdr = cdr;
     }
}

So, I have the following questions:

* For this kind of implementation, is the Algebraic type a good 
choice ? Is a simple union perhaps better ?
* I've defined the (recursive) Fibonacci function, for which DMD 
takes 30sec to calculate Fibonacci(30) and LDC takes 10sec. Is 
this a reasonable difference between the two compilers?
* I find it very difficult (actually impossible) to profile code 
in Mac OS X. There is no output for options -profile. Are there 
any other profiling/debugging tools for the Mac OS X ? My other 
ports (C++, Scala) run interpret the same example in under 2sec, 
so I would like to detect where my bottlenecks are.

Thanks.

ps: Any advice to make my code "better" (and more D-compliant) 
are appreciated.
Mar 08 2016
parent reply Guillaume Piolat <name.lastname gmail.com> writes:
On Tuesday, 8 March 2016 at 18:11:24 UTC, John wrote:
 * For this kind of implementation, is the Algebraic type a good 
 choice ? Is a simple union perhaps better ?
You can go with Algebraic. I used to do that in scheme-d. Then I switched to a tagged union by hand to avoid a compiler regression. Algebraic was OK.
 * I've defined the (recursive) Fibonacci function, for which 
 DMD takes 30sec to calculate Fibonacci(30) and LDC takes 10sec. 
 Is this a reasonable difference between the two compilers?
2x speed difference between DMD and LDC is common.
 * I find it very difficult (actually impossible) to profile 
 code in Mac OS X. There is no output for options -profile. Are 
 there any other profiling/debugging tools for the Mac OS X ? My 
 other ports (C++, Scala) run interpret the same example in 
 under 2sec, so I would like to detect where my bottlenecks are.

 Thanks.
You can build with -g and use Instruments.
Mar 09 2016
parent John <john.fourkiotis gmail.com> writes:
On Wednesday, 9 March 2016 at 20:30:44 UTC, Guillaume Piolat 
wrote:
 On Tuesday, 8 March 2016 at 18:11:24 UTC, John wrote:

 You can go with Algebraic. I used to do that in scheme-d. Then 
 I switched to a tagged union by hand to avoid a compiler 
 regression. Algebraic was OK.


 2x speed difference between DMD and LDC is common.


 You can build with -g and use Instruments.
Thank you for your advice.
Mar 10 2016