
CyberLisp: A Lisp to JavaScript compiler - MaysonL
http://github.com/manuel/cyberlisp
======
jstevens85
Could someone explain to a non-CS major (Maths) what would be the simplest,
quickest way to implement a BASIC to JavaScript compiler?

~~~
brehaut
In overview:

1\. Write javascript prototypes to mimic the primatives of BASIC where there
is no JS equivalent - Arrays spring to mind, JS Arrays are dynamical growing
sparse arrays. To support BASIC arrays you will need support redim and redim
preserve to resize the array. You may also want to consider a string wrapper
type as some implementations of BASIC have mutable strings, JS does not.

2.Write a parser for BASIC that generates an AST (abstract syntax tree).

3\. Walk the AST recursively emitting Javascript equivalents (ie fragments of
code) of the BASIC constructs.

4\. Pass that javascript to eval or load it as an external JS file.

The two languages are fairly similar in broad details. subs and functions both
become functions, depending on the dialect of BASIC you are implementing you
wont need to implement classes, maybe only types. Probably the most finickity
detail will be mapping ON ERROR nonsense across to something more sensible,
and perhaps handlign by val and by ref arguments.

~~~
jstevens85
Am I right in saying that a tool like ANTLR would take care of most of these
steps? Although I'd love to learn compiler theory someday and do everything
myself, I'm constrained by time right now. What tools, frameworks could help
speed this thing up for a beginner?

~~~
scott_s
In order to use the tools, I think you'll need to understand some basic
compiler theory. I can't say for certain, since I only looked at various
compiler-related tools after taking a compilers course. But any tool is going
to introduce some incidental complexity, and it's having a grasp of the basics
that will let you realize "Oh, this is just how the tool does ___."

The good news is that the basics of compilers theory and implementation isn't
that difficult. You should be able to learn a lot just through some Googling
and some time spent on Wikipedia pages.

With that said, a tool such as ANTLR should handle all of step 2, and part of
step 3. The biggest step, though, is step 1, and that's all you.

Since you're a math person, think of this in terms of functions. You need to
write a function that takes Basic code and produces semantically equivalent
JavaScript code. The bulk of the work is in determining the semantic
JavaScript equivalents to all Basic constructs. This also implies that you
need to have a deep understanding of the semantics of both languages.

edit: It just occurred to me that I don't know if you want to do this for your
own edification, or for a practical purpose. If you have a practical reason,
there's probably an easier solution.

