Scripting Language Syntax Proposal

Started by
118 comments, last by irbrian 19 years, 11 months ago
I''ve spent roughly a week trying to find just the right scripting language for my online game framework project. Many of the languages I''ve looked at have elements I like, but all of them have elements I can''t stand. So yesterday, out of frustration more than ambition, I spent several hours working on syntax examples for what I hope will one day be a new scripting language. Some comments before I give my examples: 1) I wrote all my sample code from a perspective of Game Development, so I don''t know yet how well it would translate to other applications, but it seems pretty flexible to me. 2) Don''t bother whining about how this is just a syntax proposal (don''t even have the BNFs worked out) and its a whole other ballgame to actually build a compiler and/or interpreter. I''m aware of the sheer magnitude of the task. 3) A few words about the thoughts that drove me to certain syntactical decisions: In my recent post, "Choosing a Scripting Language," I said I wanted a language that was C-like, because C++, PHP, and JavaScript are what I am most familiar with. A lot of people whined and moaned and scoffed at that. So I decided to take a slightly different approach -- the actual language usage would be based on C-style, but I''d reluctantly do away with the ;''s and {}''s. In the end, the code looked so neat and clean to me that I had very little trouble adapting. I''m hoping others on both sides of the C-like-code debate will agree. There are two things I''m hoping for in posting this... the first being that people will offer the comments, constructive criticism, and advice on the syntax and style of the language. The second is more of a P.I.T.S... I''m hoping that someone (PeterTarkus, you reading this? ) will actually find the language interesting enough to consider working with me on a compiler/interpreter. The sample scripts will follow.. **************************************** Brian Lacy ForeverDream Studios Comments? Questions? Curious? "I create. Therefore I am."
---------------------------Brian Lacy"I create. Therefore I am."
Advertisement
1   // A Simple Example Script2   3   include ("io.script")4   5   var strHello = "Hello!"6   var nTest = IO.Input()7   print ("Enter a number: ")8   var lenHello = strHello.Length()9   10  if (lenHello / 2 == nTest)11      for (var nI = 0, i < lenHello, i++)12          print (strHello[nI] + "\n")13      end14  else15      print ("Wrong answer.")16  end if17 
OUTPUT (note that []'s indicate user input)

Enter a number: [3]

H
e
l
l
o
!

[edited by - irbrian on April 13, 2004 12:11:11 PM]
---------------------------Brian Lacy"I create. Therefore I am."
While it looks pretty straightforward, there are some interesting things going on here. The first thing to consider is that the 'var' keyword has some very cool properties (imo). The 'var' keyword defines a variable that is not soft-typed, but is dynamically typed. In other words, variables are given a permanent type when they are assigned a value of a specific type.

Consider 'strHello' declared on line 5. When the interpreter reads 'var strHello' it creates a new variable with type unknown . At this point, we could have assigned it an integer value, and it would be an integer. But immediately following, the variable is assigned the value "Hello!" -- a string. So, it assigns the type of strHello to a string. Assigning it an integer value after this would not work.

Now look at line 8. Notice the 'strHello.Length()'? This works because each type is actually a class containing useful functions associated with that type. And like JavaScript, all variables are instances of the object class. In fact, 'var' itself is of the 'unknown' class -- meaning it too has some useful functions. I'll get to that later.

But why bother, you may ask? Why not just specify the type and be done with it? Well, for one thing, I think its a bit easier for new users to pick up on -- they needn't worry about type assignments because its all done automatically. Furthermore, because 'var' objects can be assigned a type dynamically, it may be useful in a script to assign a value without knowing its type, and then ask it later.

An example might be line 6. The IO object on line 6 is an interface object (included on line 3, naturally) which contains a method called Input(). This method gets input from the input stream (specified inside the C++ application, here it would probably be console or keyboard input) and assigns the value, in this case, to nTest. nTest has not been assigned a type yet. The IO.Input() method converts the input to the most appropriate type -- in this case, 3 alone is an integer, so the return type is an integer.

The program now knows that nTest is an integer. But the scripter does not. Well, we can find out by simply using the isInteger() method:
if ( nTest.isInteger() )    // do something integer-esqueend if 
This works because the unknown type, 'var', is the base class of all types, and each type inherits methods from 'var' to determine what type of variable it is.

[edited by - irbrian on April 13, 2004 12:10:39 PM]
---------------------------Brian Lacy"I create. Therefore I am."
I''ll post some more examples later today. My Linux class is starting.
---------------------------Brian Lacy"I create. Therefore I am."
Looks like you''ve pretty much just reinvented Python.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
uhh.. no. I'm don't know much about python, but python's syntax bears little resemblence to what I'm trying to describe here, even though the functionality may be similar in some ways. The syntax style has much more in common with PHP or JavaScript than it does Python.

And I realize that to some of you syntax is irrelevant, but that's not the case for me.

I intended to post more examples much sooner to help demonstrate, but hadn't found the time yet. I'll post some more examples now.. hopefully it will become more evident what I'm going for.
// A MUD or I.F. function that returns the Room ID of// a given entity, such as a character, monster, or// other mobilefunction SearchRooms (var Entity, var StartRoom, var EndRoom)    for (var z = StartRoom, z < EndRoom, z++)        if ( Game.Rooms[z].hasEntity (Entity) )            return z        end if    endend  
The only new thing in the above listing is the use of a general 'function' keyword to specify a function definition block.
// A class to create a simple container.// 'Container' is an abstract class we can use// to simplify the creation of container types.class Jar extends Container        var isOpen    var LidTightness    var MaxLidTightness        // Constructor -- called when new Jar is created    function Jar ()                isOpen = false        LidTightness = 10        MaxLidTightness = 20        MaxWeight = 05.00         // Auto-formatted float?                                  // ...just a thought         MaxContentsSize = 10        ContentsSize = 0        ContentsWeight = 0        EmptyContents ()          // Inherited method to set                                  // the container to empty    end        function Add (var Item)         if (Item.GetSize() < MaxContentSize - ContentSize)             if (Item.GetWeight() < MaxWeight - ContentsWeight)                 if (isOpen)                     Container.Add (Item)                 else                     // Tell player it won't happen...                     // More on Feedback another time                     Game.Feedback (this, "LidClosed")                     return                 end if             else                 Game.Feedback (this, "ItemTooHeavy")                 return             end if         else             Game.Feedback (this, "ItemTooBig")             return         end if                  Game.Feedback (this, "ItemAdded")     endend  


[edited by - irbrian on April 13, 2004 11:35:35 PM]
---------------------------Brian Lacy"I create. Therefore I am."
quote:Original post by irbrian
uhh.. no. I''m don''t know much about python, but python''s syntax bears little resemblence to what I''m trying to describe here, even though the functionality may be similar in some ways. The syntax style has much more in common with PHP or JavaScript than it does Python.

After you''ve learned one language, syntax is the most important thing about it, because it''s the thing you''ve slaved over trying to get right. Once you''ve learned another, the strange and foreign feel of the syntax makes it that language''s most prominent feature, too. Once you''ve learned a dozen or so, you begin to realize that judging a language by its syntax is very much like judging a book by its cover. Trust me on this: good syntax does not make a good language.


"Sneftel is correct, if rather vulgar." --Flarelocke
Don''t get me wrong, I agree wholeheartedly that the overall value of the language is not determined by the syntax.

Think about it this way: Price, performance and capability factors being equal, would you rather drive a Landrover, or an Escalade? Maybe you''d prefer the Landrover for its reputation for rugged outdoor adventuring. On the other hand, I might choose the Escalade, ''cause I like a smooth ride, nice handling, tons of options, and leather seats. Is one choice inherently better or worse than the other? Of course not. But that doesn''t mean I should satisfy myself with driving a Landrover my whole life.

Ultimately, I think syntax is about comfort. You find a style you like and you stick to it when you can. Well, this is about me sticking with a style I like.

If you''re still not convinced it matters, I''ve got a perfectly road-worthy ''96 Ford I can sell ya.
---------------------------Brian Lacy"I create. Therefore I am."
Suggestion: drop the var-keyword. It''s not necessary. If you say var x = y or you say x = y, it''s basically the same thing. You can only assign something to a variable. Just check to see if the variable already exists: if it doesn''t, just create it. Defining a function? function fn(var x, var y) can be replaced by function fn(x, y).
KISS: Keep It Simple, Sydney.

---
tommy online: http://users.pandora.be/tommycarlier
irbrian, i wouldn''t mind helping you out. It seems to me you have some solid ideas, i like that. I agree that syntax doesn''t "make" a language powerful, but I also agree that it
makes a language "comfortable" to a new user. Irbrian, I have been spending the last year researching compiler implementations, VMs, Interpeters, and script overall. I was
working on designing my own scripting language (i won''t go into a lot of details here, but i can send you some). I have a lot
of design down for the compiler\parser\virtual proccessor implemenation, but I am currently just beginging developement,
but, I would like to offer my assistance. I really like your
ideas on this style of syntax for scripting. I think it would
be easy on the designers writing scripts for the first time.
I do have a few suggestions, but I will save that for an email, or another post. I am off.

Elendil!

This topic is closed to new replies.

Advertisement