From: Joel Reymont
Subject: what would c# loop like in lisp?
Date: 
Message-ID: <86552a98-1e18-49d1-ad1a-1ba6a8a7bc0d@j8g2000yql.googlegroups.com>
I'm working on a translator that produces c# code (top) from a trading
language (bottom), e.g.

http://groups.google.com/group/topdog/browse_thread/thread/d5fd8feac8e68331

I'm toying with various ideas of how to represent c# classes as lisp
code and I'm looking for suggestions.

I can always represent a class as a struct and that's what I do know,
e.g.

(defstruct klass
  modifier
  name
  parent
  main
  fields
  methods
  props)

(defun new-klass (name parent main)
  (make-klass `public
              name
              parent
              main
              (make-hash-table)
              (make-hash-table)
              (make-hash-table)))

I also have structures and functions for methods, fields, etc., e.g.

(defstruct method
  modifier
  name
  return-type
  args
  vars
  code)

(defun new-call-method (code)
  (make-method :modifier `(protected override)
               :name "Call"
               :return-type `void
               :args '()
               :vars '()
               code))

I create this bit of C#

   protected double PointSize(int bip)
     {
         return BarsArray[bip].Instrument.MasterInstrument.PointValue;
     }

like this in the OCaml version of my translator

let make_point_size_meth = {
  meth_modifier = [Protected];
  meth_name = "PointSize";
  meth_return_type = TyFloat;
  meth_args = [TyInt, "bip"];
  meth_code = [
    Return (Some (Prop (Some (Var ("BarsArray", [Var ("bip", [])])),
                          "Instrument.MasterInstrument.PointValue")));
  ]
}

I'm wondering if there's a more elegant way to do this than just
simply translating the above into a function that creates a structure
and where the code looks the same, sans "Some", e.g. (return (prop
(var "BarsArray" (var "bip")....)

    Thanks, Joel

From: Joel Reymont
Subject: Re: what would c# loop like in lisp?
Date: 
Message-ID: <138750d1-2d4b-4f61-9ea9-e3d2a03a2b37@w35g2000yqm.googlegroups.com>
I forgot to mention that I mostly add methods to classes and filter
method code when appending or prepending more code. The code
modification happens when new variables need to be inserted, for
example.

When everything is said and done, I pretty-print the C# AST to arrive
at the code you see here

http://groups.google.com/group/topdog/browse_thread/thread/d5fd8feac8e68331

The reason I'm porting from OCaml is that Lisp sort of gives me
everything in a box to run on Solaris, Windows and Mac, including a
Windows GUI toolkit, web tools, etc.

    Thanks, Joel
From: Pascal J. Bourguignon
Subject: Re: what would c# loop like in lisp?
Date: 
Message-ID: <87zlf9e11v.fsf@galatea.local>
Joel Reymont <······@gmail.com> writes:

> I'm working on a translator that produces c# code (top) from a trading
> language (bottom), e.g.
>
> http://groups.google.com/group/topdog/browse_thread/thread/d5fd8feac8e68331
>
> I'm toying with various ideas of how to represent c# classes as lisp
> code and I'm looking for suggestions.
>
> I can always represent a class as a struct and that's what I do know,
> e.g.
>
> (defstruct klass
>   modifier
>   name
>   parent
>   main
>   fields
>   methods
>   props)

It's good.  You could also use CLOS objects.  That's what I do because
anyways I defined my own macros to define these classes since in the
language I was concerned with (C++), there are several categories of
these classes.  This also allows me to generate automatically some
methods on these classes.  Therefore there's no gain in using
defstruct over defclass, once it's wrapped over by your own macro.


> I create this bit of C#
>
>    protected double PointSize(int bip)
>      {
>          return BarsArray[bip].Instrument.MasterInstrument.PointValue;
>      }
>
> like this in the OCaml version of my translator
>
> let make_point_size_meth = {
>   meth_modifier = [Protected];
>   meth_name = "PointSize";
>   meth_return_type = TyFloat;
>   meth_args = [TyInt, "bip"];
>   meth_code = [
>     Return (Some (Prop (Some (Var ("BarsArray", [Var ("bip", [])])),
>                           "Instrument.MasterInstrument.PointValue")));
>   ]
> }
>
> I'm wondering if there's a more elegant way to do this than just
> simply translating the above into a function that creates a structure
> and where the code looks the same, sans "Some", e.g. (return (prop
> (var "BarsArray" (var "bip")....)

It would depend.  If you start from a source in your "trading
language", parse it, and produce the parse tree from it, then you
don't have to write functions such as this OCaml example.  Your parse
trees are built dynamically from the source language parsing.

On the other hand, if you want to be able to write easily C# routines
from Lisp, then I would advise to define a s-exp based simplified
syntax for C#.  You'd implement this syntax as a set of macros (and
possibly functions) that would expand into these structure (or object)
instanciations.

I would never write in lisp something like your OCaml function above,
but rather write a s-exp such as:

(define-method point-size ((bip int)) double :protected
  (return (dot (dot (dot (aref bars-array bit) instrument) 
                     master-instrument) point-value)))

and macros to expand that to the needed tree building code.


-- 
__Pascal Bourguignon__
From: Joel Reymont
Subject: What does the simplified sexp-based syntax for C# looks like?
Date: 
Message-ID: <d68d33c7-caee-4433-884a-120499c79845@r29g2000vbp.googlegroups.com>
On Mar 25, 9:37 pm, ····@informatimago.com (Pascal J. Bourguignon)
wrote:

> It's good.  You could also use CLOS objects.  That's what I do because
> anyways I defined my own macros to define these classes since in the
> language I was concerned with (C++), there are several categories of
> these classes.

It's more of a question of struct vs sexp but you have responded to
that below.

> It would depend.  If you start from a source in your "trading
> language", parse it, and produce the parse tree from it, then you
> don't have to write functions such as this OCaml example.  Your parse
> trees are built dynamically from the source language parsing.

I lex, parse, produce the trading language parse tree and transform it
into the C# AST. I do have to write functions like my OCaml example
both to pre-populate the target C# class with code that's always
present, and to set up the tests.

I have an extensive test harness that ensures my transformations are
correct by comparing the C# AST I produce against one that I expect.
That OCaml code is just an AST helper.

> On the other hand, if you want to be able to write easily C# routines
> from Lisp, then I would advise to define a s-exp based simplified
> syntax for C#.

My original post has the wrong title and has a typo. The real title
should be "What does the simplified sexp-based syntax for C# look
like?" :-).

> You'd implement this syntax as a set of macros (and
> possibly functions) that would expand into these structure (or object)
> instanciations.

Right.

> I would never write in lisp something like your OCaml function above,
> but rather write a s-exp such as:
>
> (define-method point-size ((bip int)) double :protected
>   (return (dot (dot (dot (aref bars-array bit) instrument)
>                      master-instrument) point-value)))

Bingo, thanks Pascal!

Now, what would a C# class sexp look like? It also needs fields and
properties. The latter are a bit of a pain:

 [Description("")]
     [Category("Parameters")]
     public int PointsRisked
     {
         get
         {
             return pointsRisked;
         };
         set(int value)
         {
             pointsRisked = value;
         };
     }

Thanks, Joel
From: Joel Reymont
Subject: Re: What does the simplified sexp-based syntax for C# look like?
Date: 
Message-ID: <86d82b3e-76ba-4e64-9d1b-3ce8fbd36b66@w34g2000yqm.googlegroups.com>
Argh... I haven't slept much tonight, thus the speech impediment :-(.
From: Pascal J. Bourguignon
Subject: Re: What does the simplified sexp-based syntax for C# looks like?
Date: 
Message-ID: <87ocvpdwqu.fsf@galatea.local>
Joel Reymont <······@gmail.com> writes:
> Now, what would a C# class sexp look like? It also needs fields and
> properties. The latter are a bit of a pain:
>
>  [Description("")]
>      [Category("Parameters")]
>      public int PointsRisked
>      {
>          get
>          {
>              return pointsRisked;
>          };
>          set(int value)
>          {
>              pointsRisked = value;
>          };
>      }

I'm a little puzzled by this syntax.  I don't know C#.  
Is it how you declare PointsRisked to be an attribute of a C# class?


To elaborate a sexp syntax of a language, you can start from the
ASTree, and flatten it a tad.

Take the label (or the types) of each node of the tree as the s-exp
operator, and choose an order for the children, or use lisp keywords.
You can do some flattening when lists of children are represented as
degenerate subtrees, or when the ASTree is not too abstract but
contains some irrelevant details. In some places you may have some
wrapper nodes used to qualify subtrees implemented with the same node
types when they could have duplicated the node types.  In the sexp,
you don't necessarily need to bother with that, since you can
determine the wrapper (or the actual subnode types) from the context,
from the operator of the sexp.  Some languages have really baroque
syntaxes, so you may want to simplify the sexp syntax if it is to be
used to write programs.  Unless you want to use the sexp syntax only
to generate the most faithfully all the possible target syntax.



Perhaps it could be something like:

(define-class some-class ...
   ( ; slots:
     (points-risked :visbility :public :type int
                    :getter (lambda () (return points-risked))
                    :setter (lambda ((value int)) (assign points-risked value))
                    :description "THe number of points risked"
                    :category "Parameters")
    ))

You can take some excursion from the syntax of the target language,
and get rather close to lisp, if you are willing to implement the
needed transformations in the macros.


On the other hand, if we have a syntax such as:

attribute ::= [description] [category] visibility type identifier '{'
                  method_definition*
              '}'

you could have a more direct translation to a sexp syntax such as:

(defmacro attribute (visibility type identifier (&key description category) 
                      &body method-definitions)
      ...)

(attribute :public int points-risked (:category "Parameters")
   (method get ()
      (return points-risked))
   (method set ((value int))
      (assign points-risked value)))

-- 
__Pascal Bourguignon__
From: George Neuner
Subject: Re: What does the simplified sexp-based syntax for C# looks like?
Date: 
Message-ID: <19ios45t44kjd7sifgn84v73h81n8itffo@4ax.com>
On Thu, 26 Mar 2009 00:10:49 +0100, ···@informatimago.com (Pascal J.
Bourguignon) wrote:

>Joel Reymont <······@gmail.com> writes:
>> Now, what would a C# class sexp look like? It also needs fields and
>> properties. The latter are a bit of a pain:
>>
>>  [Description("")]
>>      [Category("Parameters")]
>>      public int PointsRisked
>>      {
>>          get
>>          {
>>              return pointsRisked;
>>          };
>>          set(int value)
>>          {
>>              pointsRisked = value;
>>          };
>>      }
>
>I'm a little puzzled by this syntax.  I don't know C#.  
>Is it how you declare PointsRisked to be an attribute of a C# class?


This is a property implementation.  As in Java, a C# property is
syntactically accessed as if it is a public data member but is really
implemented with getter/setter functions.

"pointsRisked" would be a (not shown) private data member.
"PointsRisked" is the publicly available property.

George
From: Mark Wooding
Subject: Re: What does the simplified sexp-based syntax for C# looks like?
Date: 
Message-ID: <87hc1gikys.fsf.mdw@metalzone.distorted.org.uk>
Joel Reymont <······@gmail.com> writes:

> Now, what would a C# class sexp look like? It also needs fields and
> properties. The latter are a bit of a pain:
>
>  [Description("")]
>      [Category("Parameters")]
>      public int PointsRisked
>      {
>          get
>          {
>              return pointsRisked;
>          };
>          set(int value)
>          {
>              pointsRisked = value;
>          };
>      }

I'd probably write it as

        (property :public *points-risked int
          (declare (*description "")
                   (*category "Parameters"))
          (:get (return points-risked))
          (:set ((value int)) (setf points-risked value)))

(The `*' is part of a convention I use for representing studlycaps names
as Lisp symbols, and indicates that the first letter should be capital.)

The DECLARE form captures attributes which in C# are placed before the
actual declaration form, but it seems better to stash them inside in
S-expressions.  The contents of the attributes look like function calls
(I know that the `function' is really a type name, but that's a minor
lexical difference), so I'll go with that.

Property declarations have a grab bag of modifiers which can be attached
to them, so write those as keywords.  The name of the property follows,
and then its type.  The rest is straightforward.

A method might look like

        (method :protected :virtual *greet void ((name string))
          (declare (*pointless :true))
          (*console.*write-line "Hello, {0}!" name))

-- [mdw]