tutorials or introductions to writing a simple scripting language?
Asked Answered
H

3

12

I know there are a few questions floating around here on the subject, but it was hard to find anything useful to what I'm after...

I also know it will probably end up being quite the task to complete, but I really want to make a simple scripting language for gaming engines... I want to use it in C++ and my Android Java game engines... but I don't know where to start... I've tried looking for tutorials online, but alot require converting things to byte code, virtual machines and such...

I really just want to create a simple scripting language which can be read from the engine, have some simple "if/else" logic... simple functions that can be called from other scripts and so on... Maybe even simpler for early versions... I really don't know where to start, but I do know this is something I need to start studying and understanding.

If anyone could point me in the right direction and point out some links to very simple "making a simple scripting language for games" kind of tutorial or even point out some key concepts that I should look into... I'd be really thankful.

I'd prefer a minimalist C based scripting language, but I guess the specifics will come into it once I've actually learnt more about it.

Thanks for any help anyone can give.

Hunger answered 15/1, 2012 at 5:58 Comment(5)
Do you really want to create a new scripting language? Cannot you provide a scripting interface to your game engine using an existing language?Synovitis
I know it's not "C-based", but Lua script is a simple enough language to learn and script in.Decennary
@Thilo: Technically I probably don't have to, but I figured it would be something that might be very useful later on to keep cross compatability between my Android and PC work... But if I could find a scripting language that wasn't too painful to implement into both my C++ editors and my Android engine for now, I guess I'd look into it.Hunger
@Zack: I've never used Lua, I'll have to look into it. I've only used python for sripting... I really wasn't a fan of its formatting... but I'll have a look at Lua anywho.Hunger
In the year of 2012, you don't really want to write your own scripting language. Virtual machines and JIT compilation is almost a must for a modern scripting engine and can be complex thing if you try to implement it for your own language (and you will hit performance issues quite soon with your own implementation). Try Google v8 (JavaScript) engine. As far as I know Google is using v8 for Android so it should be possible to embed it on both PC and Android.Moa
M
5

I've tried looking for tutorials online, but alot require converting things to byte code, virtual machines and such

Yes. This is really the approach, even for a dead simple language. Executing the source code directly will be way more complicated, the way this is done is first you parse the source code and digest it into byte code, then have a virtual machine interpret the byte code.

You may want to look at existing languages to learn about their design.

This tutorial from flipcode builds a simple language and includes all the code, so it might be useful.

You can also take a look at the Lua source code.

Magnien answered 15/1, 2012 at 8:32 Comment(0)
L
4

Part of my job is maintaining a proprietary game scripting language. I'm not aware of any "How-to" books on the subject in its narrow sense. There is an interesting article by Bruce Wilcox on writing such languages. It doesn't discuss implementation detail, but goes into the design process somewhat.

http://www.gamasutra.com/view/feature/1570/reflections_on_building_three_.php

Writing a scripting language is like writing any language and involves all the same principles and design questions. You need to think about what concepts your language should revolve around, you need to define a grammar, then you need to write a compiler or translator and/or an interpreter. Which you choose and the details of their implementations are entirely up to you, and there is no one or best way to accomplish these things.

There are standard ways of thinking when it comes to parsing syntax and defining language grammars. Regular expressions are your friend here. Thankfully, C++11 includes the <regex> library (originally from boost). It might help to pick up a book on compilers to get you started on important concepts, if you really want to get into the subject in depth. When I was in university taking a compilers course, this was my textbook, which I've kept with me to this day, so I recommend it.

http://www.amazon.com/Compilers-Principles-Techniques-Alfred-Aho/dp/0201100886/ref=sr_1_2?ie=UTF8&qid=1326618572&sr=8-2

Writing a language is a wonderful exercise in computer science. However, if this is for a real project or product, then like others who have already commented, my professional advice is: Don't do it if you don't really have to. This is a huge potential time investment that you're considering, and you have you ask yourself: What benefits will I get out of my own language that I can't find in existing free-to-use languages like Lua and Python, and are those benefits worth the months of extra time it's going to take to implement?

And don't forget the tools. If you're writing a language that you intend others to use, you need lots of documentation and tools, tools, tools. You will almost certainly want a source level debugger at least, and writing a good debugger can take as long as writing the language itself!

If all you want is a language for fast-prototyping game logic, then Lua is likely more than suitable for your needs, and comes with lots of existing documentation, literature and tools.

Linseed answered 15/1, 2012 at 9:35 Comment(0)
B
3

A few old articles are still good read, if you can find them:

  • M. Abrash, D. Illowsky, "Roll your own minilanguages with mini-interpreters", Dr. Dobb's Journal 14 (9) (Sep 1989) 52–72.
  • J. Bentley, "Programming pearls: little languages", Communications of the ACM 29 (1986) 711–721.
  • D. Betz, "Embedded languages", Byte 13 #12 (Nov 1988) 409–416.
  • D. Betz, "Your own tiny object-oriented language", Dr. Dobb's Journal 16 (9) (Sep 1991) 26–33.
  • N. Franks, "Adding an extension language to your software", Dr. Dobb's Journal 16 (9) (Sep 1991) 34–43.
  • R. Valdés, "Little languages, big questions", Dr. Dobb's Journal 16 (9) (Sep 1991) 16–25.

See also Bob: A Tiny Object-Oriented Language by Betz. The latest sources for BOB are available at http://www.xlisp.org/.

Bencher answered 17/1, 2012 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.