Like you said, the go std lib has a lexer and tools to build a AST of
you go file.
After that is just a matter of translating the concepts from Go to
your other language.
--
André Moraes
http://andredevchannel.blogspot.com/
> After that is just a matter of translating the concepts from Go to
> your other language.
"Just", as in that's the hardest part of the whole enterprise (depending
a little\\\\\ lot on which target language you choose to use and how much
performance you're prepared to trade off and how much of the
original Go structure you want to preserve in the compiled code).
(I've vaguely wondered about a Java-to-Go translator to take first
steps in converting existing Java code to Go, but it's probably more
sensible, if less meta-satisfying, to just rewrite from scratch guilded
by the available code.)
Chris
--
Chris "allusive" Dollin
No, as people have already said, the difficulty is not in parsing and
determining what a Go program is doing, but it is translating those
concepts into another language. That's not done with a parser, that's
done with a compiler that is aware of the source and target languages,
in this case Go and the other language.
I'm not sure what you're trying to accomplish, but the tools for
lexing and parsing Go code are already well established, they're in
the standard libraries. If you want to convert your Go code to another
language, you need to define a way to translate each of the different
concepts.
- Jim
Um, no. Bison and its ilk are for parsing token streams.
If you're compiling to some kind of machine code, then there are
tools to help with that, but they're probably written in C or C++.
If you're compiling into something like C, then you assemble
structures that represent C (etc) code and write those out.
As it happens, writing out C isn't particularly hard, either. What's
hard is deciding what C to write and what information is needed
to write it, and that's the hard part, and the part you have to do
with your brain and not a tool.
So if you really, really want to compile Go into some other
language, pick a language -- just one, because this job is hard
enough with one and trying to do another will just burn effort -- and
work out how to represent each of Go's constructs in that language
and what runtime support they will need.
(I have a compiler for a dynamically-typed open-stack programming
language which translates into C, so I have played this game
before. I wimped out and didn't write the garbage collector. The
generated C isn't fit for consumption by humans.)
I should have placed "just a matter of translating" under quotes.
I don't intended to mean that this is easy but after loading the source code
in the AST what is left is translate that into the target language.
So he don't need to work with tools like bison,lex,yacc, etc...
all that is already done.
Sorry for the misunderstood
> Yes, it's to translate from Go to JS because I'm tired of "fighting"
> with JS at creating the user interface for some server.
You'll probably spend just as much time fighting your go-to-js
compiler and non-idiomatic generated js code. This is not a
trivial conversion to do.
Watch out for longs!
This would be quite a worthy project. And a big one!
But after thinking and rethinking the value of such solution (and the
ammount of problemns that I would need to solve).
I decided to follow two paths:
For the "single page application" use something like GWT or a very
good js library (Ext-Js maybe). There is this project Jangaroo
(http://www.jangaroo.net/home/) which compiles from AS3 to JavaScript
code and I currently studying it.
For the "good and old multi page site" I just wrote a view engine on
top of the std library, very easy to use and I am really loving the
new template package.
That was just my personal opinion, also the Dart Language from google
tries to make some concepts like chanels to the browser, take a look
at that project too.
> No problem. It's just that I've no experience with lexers and parsers.
> And I was watching how has been created CoffeScript so I was confused
> with all that.
>
> Yes, it's to translate from Go to JS because I'm tired of "fighting"
> with JS at creating the user interface for some server.
I think the hardest part of translating Go to JavaScript would be the
runtime library support for go, panic, defer, recover, channels, and
select. There are other hard parts, but I would start by thinking
seriously about how those could be implemented--i.e., how to write a
JavaScript program which uses those concepts. If you can get all those
working, I think the rest can be done.
Ian
I think the hardest part of translating Go to JavaScript would be the
runtime library support for go, panic, defer, recover, channels, and
select. There are other hard parts, but I would start by thinking
seriously about how those could be implemented--i.e., how to write a
JavaScript program which uses those concepts. If you can get all those
working, I think the rest can be done.
Here's an excellent post describing language transformation:
http://www.moserware.com/2008/06/ometa-who-what-when-where-why.html
It uses OMeta, an intriguing "meta-language" to simplify the process,
as an alternative to repurposing an existing scanner/parser.
The real challenge will be mimicking the features that do not map
one-to-one to the target language. Any advanced features requiring
runtime support will require you to create the equivalent runtime
library in the target language.
But if you want to play and learn, you could start small with Ometa and just
arithmetic expressions, then add 'var' and '=', and build from there.
--Glenn
And thanks! I just to add it to the ToDo list for don't forget it
== Status
In a little time, maybe today, I'll have finished the translating of
all declarations included structs. So I hope that I can enjoy writing
Go -> JS scripts at the beginning of the new year :)
All code transformed is being checked through JavaScript Lint
== Advantages
+ Simple and clean sintaxis (due to Go).
+ Use one only language for all development. Also, I will don't need
to contract to some JS developer.
+ The translation will be line to line so if you get an error in the
JS file you'll can match this its Go code. This is not possible with
another translating compilers and it's very important. (It is in my
ToDo list).
If I read all that before of start with it, then I don't build it...
Just kidding! The project is being fun once that you get experience
with go/Parser.And thanks! I just to add it to the ToDo list for don't forget it
== Status
In a little time, maybe today, I'll have finished the translating of
all declarations included structs. So I hope that I can enjoy writing
Go -> JS scripts at the beginning of the new year :)
The mathematical expressions will be calculated during the translating
The idea is to use the sintaxis of Go to write JS.
On Nov 26, 2:54 pm, Jan Mercl <jan.me...@nic.cz> wrote:
On Nov 26, 2:54 pm, Jan Mercl <jan....@nic.cz> wrote:
> I'm not sure if such optimism is rational. E.g. what is/should be the JS
> version of the following Go declaration?
>
> var v int64 = 1<<63-1 // JS can't natively represent this number
> exactly
>
The mathematical expressions will be calculated during the translating
To stay it very clear. I'm implementing a subset of Go which could be
translated to JS. Things like channels, goroutines, numbers of 64
bits,etc., could not be used in JS so my program will show an error
about it.The idea is to use the sintaxis of Go to write JS.
Corresponding all integers to target's native types looks promising as
it allows to skip the step of type inference for polymorphic
functions, emiting "+" for "+" in Go code, etc. But doing so would
raise other problems due to lack of native complex numbers or
different semantic of bitwise shift (when the right param is bigger
than the bitwidth of the left param), etc.
Otherwise, converting all +,-,*,/,<<,>>,... to the non-polymorphic
functions (_plus_8, _plus_16, _plus_str, ...) allows to use non-native
int64 same way as non-native channel, interface, ...
I'd say, the most difficult part of the translator is the simplifier,
which will convert "var v int64 = 1<<63-1" to "var v int64; v =
CONSTDECL_I64(0x7FFFFFFFFFFFFFFF);".
Then such a simplified code can be converted easily.
int64 would be a type in target lang (struct in case of Javascript,
native in case of C), CONSTDECL_I64 is a kind of macro (which forms
struct constsnt in case of Javascript, or append LL suffix in case of
C). "1<<63-1" has to be calculated at the translation stage (it can be
also "(1<<100)>>37", which is correct for Go, but will not work in
most targets being just passed there).