2

I'm relatively new to programming, and I would like to write a simple scripting language as an exercise, and to learn a bit. I have experience with Python, C, and Ruby, and would like to learn to write a scripting language in Python. What should be my first step? How should I start?

Phrogz
  • 296,393
  • 112
  • 651
  • 745
tekknolagi
  • 10,663
  • 24
  • 75
  • 119
  • when i say very simple, i mean dead simple. think: print, some math, variables – tekknolagi Apr 27 '11 at 00:59
  • So If i really get you correct, you want to invent another programming language using Python? – ghostdog74 Apr 27 '11 at 01:00
  • 4
    yep! it's a learning exercise! – tekknolagi Apr 27 '11 at 01:02
  • @tekknolagi A simple language core isn't so complex in, he can reuse everything with Python language constructs and data structures. I.e. he doesn't need to implement dictionary, python has already one, etc. If his language doesn't differ from Python very heavily, I think he can be ready in some ten KBytes of code. – peterh Sep 13 '15 at 00:21
  • I think at first you should start to write a command interpreter, i.e. which reads in a command file and executes it. Because it is your first language (what extraordinarily meaning has it in this context ;-) ), try to implement the most primitive language as you can only imagine. – peterh Sep 13 '15 at 00:23

4 Answers4

3

I suppose the first step is to choose a language (or at least style of language). An easy one to start with is the style of Forth, where each "word" in the source code is handled in exactly the same way. This makes lexical analysis and parsing nearly trivial.

Another small language to start with is Scheme. The parsing is a bit more involved because it involves recursion, but it's still tractable. You can find various examples of Scheme-in-Python around the web, it's been done lots of times.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • i was thinking something in the style of a dumbed down python – tekknolagi Apr 27 '11 at 01:01
  • and i'm familiar with recursion – tekknolagi Apr 27 '11 at 01:02
  • 1
    Python (even dumbed-down Python) is still a reasonably complex language, as far as lexing, parsing, syntax trees, and so on. Both Forth and Scheme spare you from having to write an algebraic expression parser, which is helpful if you're learning the basics of language implementation. (You'll probably end up wanting to write an expression parser at some point anyway, but you don't have to do it first.) – Greg Hewgill Apr 27 '11 at 01:04
  • Okay scheme looks good - where could you point me in terms of a small tutorial? – tekknolagi Apr 27 '11 at 01:06
  • 2
    Here's a handful of links to get you started: http://norvig.com/lispy.html (<- Norvig has a good tutorial along with the code) https://hkn.eecs.berkeley.edu/~dyoo/python/pyscheme/ https://github.com/ghewgill/psil http://thinkpython.blogspot.com/2005/02/simple-scheme-interpreter.html – Greg Hewgill Apr 27 '11 at 01:09
  • 1
    hey i'm friends with peter! that's actually awesome... – tekknolagi Apr 27 '11 at 01:11
1

Writing a scripting language will probably exceed 'learn[ing] a bit'. I think it's too much. But if you are serious enough, you might want to start by playing around with racc or PLY, YACC bindings for ruby and python, respectively.

sawa
  • 165,429
  • 45
  • 277
  • 381
0

If it is just as a learning exercise, I would recommend reading through this tutorial on how to make a s*** javascript interpreter in python. Javascript can be very basic, and it explains things at a very beginner level.

NT3RP
  • 15,262
  • 9
  • 61
  • 97
0

draw out a finite state automata of how your language is going to work, write a syntax analyzer, draw some diagrams. Hack on!

Tom
  • 1,986
  • 2
  • 18
  • 29