Syntax question...

Jarrett Billingsley kb3ctd2 at yahoo.com
Mon Oct 30 17:02:17 PST 2006


"RA" <ra at wolven.net> wrote in message 
news:mailman.2.1162241354.24029.digitalmars-d at puremagic.com...

---------------------------
I was curious about how hard it would be to create a different "language 
front end" for D?
Since you (Walter) have made the compiler front end (lexer, parser, etc) 
source code available,
would it be possible for someone to modify it and create a different (for 
example, something
more like VB) language syntax that would still work with the back end 
compiler?

I realize most of you long time C\C++ programmers probably couldn't see any 
need for
such a thing, but others of us (at least me) would prefer something not 
quite as cryptic
as C like syntax.

I've never written a compiler\lexer\parser\etc. so I don't know what all 
would be involved
in creating a different syntax.  In theory, it doesn't seem like it should 
be "all" that hard.
But then, naiveté is great isn't it.  J
---------------------------

(as a side note, it's kind of .. etiquette here to use plain text messages. 
some newsreaders don't take kindly to the HTML messages.)

It shouldn't be too incredibly difficult.  Probably the main problem would 
be coming up with some new syntaxes for things which commonly don't exist in 
Basic languages, or for which there's no clear standard.  For example, in a 
lot of Basic languages I've used, arrays are indexed with parentheses, but 
that would mean that you wouldn't be able to have opCall and 
opIndex/IndexAssign in the same class, so we'd have to use the C-style 
square brackets for that.

Another big problem is that some things that look cryptic in C-style syntax 
would look just as cryptic in Basic-style syntax, but with words instead of 
symbols.  So you wouldn't really be gaining that much.

Although, when it really comes down to it, Basic and C syntaxes aren't too 
different.  You've got basically all the same control structures, just with 
"control .. endcontrol" rather than "control { .. }".

Lastly there's the issue of case sensitivity.  I know a lot of Basic 
languages are not case sensitive, and so foo, Foo, and FOO are all the same 
thing.  But they're all different in D.  The same goes for the control 
structures -- if we _did_ make this DBasic dialect case sensitive, would we 
make the keywords and control structures all lowercase, camelcase, or all 
caps (AAAAAAGH!)?

So.. let's see what we've got.  Assuming that we decide that the DBasic 
dialect would be case-sensitive, with camelcase keywords..

Import std.stdio

Function main(args As Char[][]) As Void
    writefln("Hello World, Reloaded")

    'auto type inference and built-in foreach
    For Each(argc, argv; args)
        'Object Oriented Programming
        Dim cl As CmdLin = New CmdLin(argc, argv)

        'Improved typesafe printf
        writefln(cl.argnum, cl.suffix, " arg: %s", cl.argv)

        'Automatic or explicit memory management
        Delete cl
    Next Each

    'Nested structs and classes
    Struct specs
        'all members automatically initialized
        Field count As Int
        Field allocated As Int
    End Struct

    'Nested functions can refer to outer
    'variables like args
    Function argspecs() As Specs
        Local s as Specs* = New specs

        'no need for '->'
        s.count = args.length              'get length of array with .length
        s.allocated = TypeOf(args).sizeof  'built-in native type properties

        For Each(argv; args)
            s.allocated += argv.length * TypeOf(argv[0]).sizeof
        Next Each

        Return *s
    End Function

    'built-in string and common string operations
    writefln("argc = %d, " ~ "allocated = %d", argspecs().count, 
argspecs().allocated);
End Function

Class CmdLin
    Private Field _argc As Int
    Private Field _argv As Char[]

Public:
    'constructor
    Me(argc As Int, argv As Char[])
        _argc = argc
        _argv = argv
    End Me

    Function argnum() As Int
        return _argc + 1
    End Function

    Function argv() As Char[]
        return _argv
    End Function

    Function suffix() As Char[]
        Local suffix As Char[] = "th"

        Select(_argc)
            Case 0:
                suffix = "st"
                Break

            Case 1:
                suffix = "nd"
                Break

            Case 2:
                suffix = "rd"
                Break

            Default:
                Break
        End Select

        Return suffix
    End Function
End Class


Though when you're dealing with something like

Foo!(delegate int(int[char[]] arr) { return arr["hi"]; });

That would end up as..

'For lack of a better template instantiation syntax..
Foo!(Delegate(arr As Int[Char[]]) As Int Return arr["hi"] End Delegate)

That's a little cryptic too.. 





More information about the Digitalmars-d mailing list