• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

classes and arrays of structures?

 
Ranch Hand
Posts: 287
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wasn't sure whether to post this in the beginners section or here - my apologies if it's in the wrong place.

I'm writing a compiler for a small language that allows you to produce screens for platform games. Everything works OK but I wanted to structure my program a little better with the intention of perhaps turning it into a project others can work on. I want to have a class that can take the program as a string and return an array of structures back (I might have my terms wrong here).

The input might be something like:

loop y 5
platform at 100, 100 * y
ladder at 100 + rnd 100, 100 * y
end


The structure might be something like this:
  • String name_of_item
  • int x_pos
  • int y_pos
  • boolean affected_by_gravity
  • ...

  • How do I return an array of this structure from a class?
    Is there a better/simpler way to do this?
    Thanks for reading

    Mike

     
    Saloon Keeper
    Posts: 5602
    214
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Mich (Mike?),

    you could do this in several ways.

    One way is to directly implement your example. So you would have a class, say GameObject, with
    the following definition:

    and when your parser has decoded one of the lines of your input, you simply create a new GameObject.
    You would also have, in your parser-method, a List<GameObject>, to which you add the new GameObject.
    And at the end you return this List. It's not an array, though, but that would be easy to fix.

    Another way is to define different gameobject classes, like GameLadder, GamePlatform, all derived from
    some basic abstract class or interface "GameObject". Upon reckognizing, say, a platform in your input, create a platform-object
    and add it to List<GameObject>.

    Well, just two possibilities that come to mind.

    Greetings,
    Piet
     
    Ranch Hand
    Posts: 159
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can use arrays for that, but you might consider using a collection from the collection framework (e.g. a java.util.List).
    You can make a simple class, Structure, that holds your 4 variables of your structure (name of the item, x pos, y pos and affected by gravity).

    In some other class you can make a method like:
     
    Marshal
    Posts: 80441
    451
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What's a structure? Are you thinking about a struct? That exists in C/C++ but doesn't exist in Java®. Whenever you want to put several pieces of data together in an object‑oriented language, you think of a class to put them in. Each individual collection of data is an instance of that class.
     
    Mich Robinson
    Ranch Hand
    Posts: 287
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the replies!

    Classes/Structures:
    I normally have just one huge class and stuff everything into it. I'd like to do it better on this project but wasn't sure how to go about things. I need some sort of structure that holds various types ie integers, some strings and a few boolean values. I just want to pass the program "string" to the compiler bit and get an array of these structures back. At the moment I just have a set of individual arrays that are accessed by the different classes but I wondered if there was a better way. The order of these arrays/structures is important as this defines what is drawn over what ie you want the background to be drawn first rather than last.

    GameObject:
    Unfortunately the program can define new objects at any time so a platform is defined as

    All this stuff works at the moment but I just wanted to clean up the internal data structures.

    Method:
    I already have a working example but it's not written well and I'd like to split it up into various classes such as :
  • the compiler that turns the program string into the array "structure"
  • the part that runs the "array" as a game on screen and move all the images around, moves your player and provides the simple physics needed
  • I'd like to add an editing/development window but I haven't done forms before
  • I'd like to provide a back-end database so people can play each others screens and vote on what's best

  • Any suggestions on:
  • man animation - at the moment I have a simple stick man that runs about but he's very badly drawn (I did it).
  • forms - I've only ever done games programs that paint to screen so I have to work out how to do an editing screen with a few buttons.
  • new images - at the moment the images are in the jar but this means a user can't add new images - no idea how to solve this at the moment

  • The end goal:
    (which is quite a way off yet) is to provide an easy language that youngsters (or indeed anyone) can program in and see quick results on screen. The various screens will be tied together by the program so it just appears like you're playing one large game. The feedback part adds a little bit of competitiveness to everything and also allows me to get rid of bad screens without having to look through everything myself.
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mich Robinson wrote:I normally have just one huge class and stuff everything into it.


    Well that's a really bad idea for a start. Java is an Object-Oriented language, so the idea is to have lots of objects that can work together. And for something as complex as this, if you end up with less than twenty classes, I'll be surprised. You're creating a language, man.

    I need some sort of structure that holds various types ie integers, some strings and a few boolean values. I just want to pass the program "string" to the compiler bit and get an array of these structures back.


    Sorry, but that's all very "woolly". What exactly do you need?

    GameObject: Unfortunately the program can define new objects at any time so a platform is defined as
    [code=java]define platform
    image brick.png
    tile 10 by 1
    action stand


    It might be, but what you've got there is HOW you want to do it.

    Back up: What is a "platform"? And don't answer in Java (or your "game language") terms. What is it? As far I'm concerned, it's something that you stand on, but it's YOU that has to decide. Imagine you had to explain it to one of the kids who is hopefully going to play your game. Tell him or her what it is, and explain it in English (or your native language).

    In fact, I suspect you need to go back to square 1 and write out WHAT your game does, exactly, in English and in detail.
    And when you're doing it, don't use any Java terms, or any other specialist terms that you haven't already explained. You seem to be fixated on code right now, and code is all about HOW; Java classes and objects are about WHAT.

    You may find the WhatNotHow (←click) page useful to read, because it's a very important aspect of Java programming. You will never be able to write classes if you don't know what they're there for; and you won't be able to do that until you have a detailed requirements spec in English.

    About the only other thing I can say is that this is NOT a simple problem.

    HIH

    Winston
     
    Mich Robinson
    Ranch Hand
    Posts: 287
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:

    Mich Robinson wrote:I normally have just one huge class and stuff everything into it.


    Well that's a really bad idea for a start.

    I know, hence my question


    Winston Gutkowski wrote:for something as complex as this, if you end up with less than twenty classes, I'll be surprised. You're creating a language, man.

    I've created quite a few languages and the compilers or interpreters to go with them. They're not as difficult as you think and if you're the one designing the language then you can make the language easier to process.

    Winston Gutkowski wrote:

    I need some sort of structure that holds various types ie integers, some strings and a few boolean values. I just want to pass the program "string" to the compiler bit and get an array of these structures back.


    Sorry, but that's all very "woolly". What exactly do you need?

    The user's program is just a long string of commands. The compiler parses this string and currently turns it into a set of parallel arrays that mostly contain integers (but there are some string arrays as well). Another aspect of the program uses these arrays to draw the users game on screen and allow the user to play it. I'd like to kow if I can combine these arrays into a class and then have a method that can pass back this "structure" to the calling program.

    Winston Gutkowski wrote:What is a "platform"?

    There were a class of games called platform games - here's an example of one called Donkey Kong.

    Winston Gutkowski wrote:Imagine you had to explain it to one of the kids who is hopefully going to play your game.

    My program allows them to write the game and then allow their mates to play it as well. The aim is to give kids a way of creating games simply and hopefully getting them interested in programming. My younger son (13) is quite capable of knocking out games using the language.

    Winston Gutkowski wrote:You may find the WhatNotHow (←click) page useful to read

    I'm at a stage where most of this works but I'm trying to change things so I can make the Java program more understandable by other Java programmers. The idea being that I might share the project with others. It's difficult knowing where to split the program into classes. Obviously I'd like to do this if it makes the program better but my first attempt at splitting the compileGame class out of the main program is proving difficult. I'm currently trying to split the program into the following classes:
  • compileGame
  • editGame
  • playGame
  • userFeedback


  • Winston Gutkowski wrote:About the only other thing I can say is that this is NOT a simple problem.

    I have no problems designing the language or writing the "compiler". In fact it works fine on my PC. My issue is just how to pass the various arrays back to the calling program and how to split things into classes. This part is proving quite difficult for me to understand

    If it helps to picture the language then this is what I have on my help screen:
    Obviously if anyone has any comments/observations/criticisms regards the language then I'd like to hear them as well.

    Sorry for such a long post but I wanted to try to answer all your questions.
     
    Ranch Hand
    Posts: 679
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mich Robinson wrote:I'm currently trying to split the program into the following classes:

  • compileGame
  • editGame
  • playGame


  • That sounds wrong. I would think it is more likely you want a class called Game that contains all the information about the game and that class should have methods called edit, compile and play.*

    Mich Robinson wrote:My issue is just how to pass the various arrays back to the calling program and how to split things into classes. This part is proving quite difficult for me to understand


    In that case you should stop programming now and do some study on object oriented design/programming. Spending some time on that now will make you are a far better programmer in the long term (assuming you're using an object oriented language) and will also save you time in the future when you have to maintain the code you've written


    *Note that this is just one possible approach based on the names you have provided. Your design may point towards a better approach.
     
    Mich Robinson
    Ranch Hand
    Posts: 287
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stuart A. Burkett wrote:That sounds wrong. I would think it is more likely you want a class called Game that contains all the information about the game and that class should have methods called edit, compile and play.*

    Oddly enough, that sounds exactly like the way the program was organised before ;)

    Stuart A. Burkett wrote:In that case you should stop programming now and do some study on object oriented design/programming. Spending some time on that now will make you are a far better programmer in the long term (assuming you're using an object oriented language) and will also save you time in the future when you have to maintain the code you've written

    It's funny, no matter how I read your comment it just comes across as a little bit insulting. I was going to respond with a simple 3 letter acronym but instead I'll suggest you have a look at a few of the apps I have in my signature and let you decide for yourself whether you've done anything better.
     
    Piet Souris
    Saloon Keeper
    Posts: 5602
    214
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Mich,

    keep calm. All comments here are made with the best intentions.

    I think what I suggested, sort of 1-1 translation of your objects (the ones after the 'define' keyword is not too bad for a start.
    There must be quite some code to transform your input file into a working game. But you managed that, so getting that
    to work with such simple classes that I refer to, should be straightforward.

    I have a remark on your language specification, and one general question.

    The remark: to make the language specification better readable, I suggest to put the key-words between angled brackets,
    to make the meaning more clear. Are you familiar with the Backus--Naur notation? Something like that.
    So you would get, for instance:

    That makes it far more clear what is literal text and what are the key-words.

    A question I have is more oo-orientated.
    If you have defined a platform and say a ladder, or a ball, or whatever, what are the
    similarities and what are the specific differences between these? Can you define different classes
    with different properties, or are they only treated different in the game code?

    Greetings,
    Piet
     
    Mich Robinson
    Ranch Hand
    Posts: 287
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piet Souris wrote:to make the language specification better readable, I suggest to put the key-words between angled brackets,
    to make the meaning more clear. Are you familiar with the Backus--Naur notation?

    This is my attempt. Please correct me where I've made mistakes.



    Piet Souris wrote:If you have defined a platform and say a ladder, or a ball, or whatever, what are the
    similarities and what are the specific differences between these?

    Both are just entries in the array "structure". They would both have different images associated with them while the ladder is likely to be tiled vertically and the action associated with it would be "climb". The ball might have the action "die" which would mean the player might want to keep out the way of any balls. So a definition of ladder and a bouncing killer ball might be:


    Piet Souris wrote:Can you define different classes with different properties, or are they only treated different in the game code?

    Everything that appears in the game are just entries in the arrays. The user can create new definitions like the ladder above but this is still an entry in the arrays. There are no new classes created as that would mean compiling the script into a java program and then compiling that - which is quite a bit more complex. In fact some of the "keywords" that appear in the BN above are actually only defined in an initial script that's run before the users script ie

     
    Stuart A. Burkett
    Ranch Hand
    Posts: 679
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mich Robinson wrote:It's funny, no matter how I read your comment it just comes across as a little bit insulting.


    It wasn't meant to be. I've been programming for 30 odd years and I'm always looking for ways to make myself a better programmer.
    Your posts in this thread give the impression you are new to object oriented programming, so I was just suggesting you might want to get some background knowledge on the subject before diving into a java project.
    If this is not the case, feel free to ignore the advice.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mich Robinson wrote:I'm at a stage where most of this works but I'm trying to change things so I can make the Java program more understandable by other Java programmers.


    Then I'm afraid you need to follow the advice you've been given rather than reacting defensively to it.

    What you have written - almost certainly - is a procedure that compiles in Java, rather than a proper Java program; so I'm afraid your work is FAR from done, even if it does work. I'm not saying that you won't be able to re-use some of the code you've already written, but you will certainly have to redesign it - something you seem to be aware of already - along Object-Oriented lines.

    The FIRST task in that process is WhatNotHow, and your problem is likely to be that you're so far down the "how" road that you'll have difficulty starting out your thinking process with a clean slate. One possibility might be to think about how you described the game to your son when you first taught him to use it - or maybe even get him to describe to you WHAT it does.

    But what you absolutely must do is write this down; and not in Java.

    One simple technique - once you have a basic "what" description (otherwise known as a requirements spec) - is grammatical analysis:
  • Go through the spec and pick out all the verbs and all the major nouns - the nouns will often become classes, and the verbs methods of those classes.

  • Examples of classes might include Board (your "game board"), Platform, Sprite and Position; examples of methods might include move() (which might involve another class: Direction), pickUp() (I'm thinking of Donkey Kong ), score() and dimensions().

    It's by no means complete, but it's often a good place to start; and you absolutely must have a working RS in order to do it.
    My worry is that you are no longer the right person to write that spec because what you've already written (ie, your code) is going to colour your thinking.

    You're thinking of this as a simple rewrite: Well it's not, it's a re-design; and design is usually the FIRST thing you do.

    Winston
     
    Mich Robinson
    Ranch Hand
    Posts: 287
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stuart A. Burkett wrote:

    Mich Robinson wrote:It's funny, no matter how I read your comment it just comes across as a little bit insulting.


    It wasn't meant to be

    OK

    Winston Gutkowski wrote:What you've written..

    Perhaps you can just pretend that I fully agree with you and I'll pretend that you're fully experienced with building compilers ;)
    Joel wrote a couple of articles that may be of interest (this one and this one)
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mich Robinson wrote:Perhaps you can just pretend that I fully agree with you and I'll pretend that you're fully experienced with building compilers ;)


    Right, then plainly you know it all; which begs the question why you asked for help.

    I'll say it one last time for the peanut gallery: You plainly know how to solve this problem, but you don't know what its components are. And until you do, you will never write an Object-Oriented program; you will write procedures in an Object-Oriented language.

    Winston
     
    Mich Robinson
    Ranch Hand
    Posts: 287
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I am after help but being told to totally completely rewrite your (working) code is not helpful.
    Doing this takes far too much time and will just introduce bugs.
    The end result may be easier for you to read but it will be more difficult for me.
    Being told I should stop programming until I've learnt a little more also gets pretty short thrift from me.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mich Robinson wrote:I am after help but being told to totally completely rewrite your (working) code is not helpful.
    Doing this takes far too much time and will just introduce bugs.


    Why should it? If you'd actually read my previous post instead of reacting to it, you'll notice that I said that you probably CAN re-use some of your code - maybe even quite a bit.

    The end result may be easier for you to read but it will be more difficult for me.


    Then plainly you're not ready for the jump to Object-Orientation.

    If you're only happy in the world of "do this...then do this", however modularised it might be, then I'm afraid that cooperative objects are not on your current horizon. And believe me, I understand the problem because I was a procedural programmer for nearly 20 years before I was introduced to C++ and then Java; and it took me 8 years to finally "get it".

    All I can say is that it was worth the wait, and if you can put your ego aside for a bit and try to accept advice - and we don't give it out just to hear ourselves speak - you might be able to break out of the "procedural" cycle.

    Being told I should stop programming until I've learnt a little more also gets pretty short thrift from me.


    Then you'll probably need to make do with what you've got.

    However, I don't understand your reaction, since it was you who seems to understand that what you've got is not where you want to be.
    If your only goal is to make sure that other Java programmers understand it, then you may be able to get away with good documentation; but if you want to know why they might rewrite it, then you need to start learning about Object-Orientation.

    It's a tough lesson, because we all put something of ourselves in our code, and it's hard to hear that we're doing it wrong; but believe me, my only goal in being blunt was to help you to answer your own question (which is NOT a simple one).

    Winston
     
    Mich Robinson
    Ranch Hand
    Posts: 287
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:

    Mich Robinson wrote:Doing this takes far too much time and will just introduce bugs.


    Why should it? If you'd actually read my previous post instead of reacting to it, you'll notice that I said that you probably CAN re-use some of your code - maybe even quite a bit.

    It takes time to re-code. All coding introduces bugs. The end benefit of your proposed re-write offers no discernible functional improvement over what I have and actually makes it worse for me personally to maintain. My point is put far better in this piece by Joel Spolsky.

    However, I don't understand your reaction, since it was you who seems to understand that what you've got is not where you want to be.

    Nearly all programs can do with improvement but not all need a complete rewrite just to keep some architecture astronaut happy.

    PS I just wanted some help on a particular problem. I can see that isn't going to happen. Can someone close this thread?


     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mich Robinson wrote:Can someone close this thread?


    Done.

    And as to the "architecture astronauts" quip; it was you who decided to use Java. If you wanted to write C code, why didn't you just use the right tool?

    [Edit: Loved the Spolsky article so much I've bookmarked it; but I think you've missed the whole point of it]

    Winston
     
    Piet Souris
    Saloon Keeper
    Posts: 5602
    214
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Just before closing this topic:

    very disappointing, the outcome. I was looking forward to gain some experience in a topic
    I am totally unfamiliar with. Hoping to extract the characteristics of all objects appearing
    in a game and how that could result in nice classes, with data and methods, and how that
    would relate to compilers.

    No, I do not know an answer to that, but my hope was to learn on the fly.

    But we started in a rather fundamentalistic way about OO, endig in what was called "blunt",
    but in my view rude, discussions.

    Thinking before you begin, of course, but there comes a moment when you must stop
    thinking and start coding, or you might never get off.

    Disappointing.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piet Souris wrote:very disappointing, the outcome. I was looking forward to gain some experience in a topic I am totally unfamiliar with. Hoping to extract the characteristics of all objects appearing in a game and how that could result in nice classes, with data and methods, and how that
    would relate to compilers.


    Then I fear you've come to the wrong place. And furthermore, your expectations are way beyond what any single thread can do.

    This is a Java forum, designed to help people (especially beginners) to write good Java code. Mich is clearly not new to the business of programming, but - at least from what I can gather - he is new to the business of Object-Orientation. My initial posts (I can't speak for anybody else) were trying to explain that what he has is NOT a Java program; it's a C program written in Java. He chose not to take the advice; and there's not much I can do about that.

    Winston
     
    Mich Robinson
    Ranch Hand
    Posts: 287
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:This is a Java forum, designed to help people (especially beginners) to write good Java code.

    I couldn't see where it states this - I tried clicking the FAQ a couple of times but it crashed each time. My impression was simply that the idea was to vilify anyone that doesn't code in the same manner as you.

    Winston Gutkowski wrote:Object-Orientation

    I'll be the 1st to admit that I'm not an OO programmer. I tend to identify data structures first and then program around those. It works for me and I've managed to create a fair number of interesting applications (though most aren't Java based). I don't know if this approach has a name and nor do I try to force this methodology on anyone else. It simply works for me.

    I find it difficult picturing classes for a compiler - the suggestions for classes so far are just items or types of things that a user "could" create in their program but they're certainly not understood by the language as it stands. It's probably worth trying to picture what classes you'd have when writting a Java compiler for the 1st time. Using classes is fine and allows a programmer to build on those classes but keep in mind a programmer is required to expand those classes. In a compiler you'd want to build immediatley on any definition a user creates - my suspicion is that this is easier to do by putting the definitions into data which by it's very nature is extensible.

    Winston Gutkowski wrote:Loved the Spolsky article so much I've bookmarked it; but I think you've missed the whole point of it

    Glad you liked it. He wrote a very influential book called Joel on Software which is well worth reading. The important bit in that particular article was "the single worst strategic mistake that any software company can make: They decided to rewrite the code from scratch". Which I believe is what you were suggesting.

    EDIT: spelling
     
    Campbell Ritchie
    Marshal
    Posts: 80441
    451
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I haven't followed this whole discussion, but I think you are trying to write procedural code. Porting the code to Java would entail a complete redesign, so to follow Spolsky's suggestion, I suggest you consider keeping the code in a procedural language, e.g. C.
     
    Mich Robinson
    Ranch Hand
    Posts: 287
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I haven't followed this whole discussion, but I think you are trying to write procedural code. Porting the code to Java would entail a complete redesign, so to follow Spolsky's suggestion, I suggest you consider keeping the code in a procedural language, e.g. C.

    The code is already written, it's currently in Java and works fine. I have better things to do than to rewrite it all in C.
    Perhaps you can suggest some nice Java classes for my compiler?
     
    Campbell Ritchie
    Marshal
    Posts: 80441
    451
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mich Robinson wrote: . . . The code is already written, it's currently in Java and works fine. . . .

    If it ain't broke, don't fix it.

    I suggest you try a combination of JFlex and CUP for a compiler. Or ANTLR.
     
    Mich Robinson
    Ranch Hand
    Posts: 287
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Mich Robinson wrote: . . . The code is already written, it's currently in Java and works fine. . . .

    If it ain't broke, don't fix it.

    I suggest you try a combination of JFlex and CUP for a compiler. Or ANTLR.

    I'm a bit confused by your post.

    My compiler works fine at present so I'm guessing I shouldn't fix it or do you suggest I look at changing all the code to use JFlex and CUP etc? looking at the example code I'd say the resultant code is far more complex than what I have at present. I have used Lex and Yacc in the past but I'll admit I found them more of a hindrance than a help.

    Should I still look at creating classes for the compiler and, if so, what classes?
     
    Campbell Ritchie
    Marshal
    Posts: 80441
    451
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry, I was getting confused. If it already works, forget about JFlex and CUP.
     
    Mich Robinson
    Ranch Hand
    Posts: 287
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    no problem
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mich Robinson wrote:I couldn't see where it states this - I tried clicking the FAQ a couple of times but it crashed each time. My impression was simply that the idea was to vilify anyone that doesn't code in the same manner as you.


    Well, I'm glad we're back to a rational discussion, because I think we (me included) were heading down the road of punch-counterpunch, which is rarely productive. If you got an "Aaargh" message when you tried the FAQ pages, then I suspect you were just unlucky. The poor guys do have to do maintenance sometimes . Normally, I never have any problems.

    I'll be the 1st to admit that I'm not an OO programmer. I tend to identify data structures first and then program around those. It works for me and I've managed to create a fair number of interesting applications (though most aren't Java based). I don't know if this approach has a name and nor do I try to force this methodology on anyone else. It simply works for me.


    I think "data-directed" probably comes close. Personally, I never particularly liked it - although I understand the rationale - because it seemed to me to be too focused on one particular aspect of the problem. It was only when I started reading about Object-Orientation (back in the mid-'90's) that it made sense; however, it took me (as I said above) another 8 years after I started using OO languages to really learn how to think "objectively".

    And, strange as it may seem, it often involves "inverting" the problem, so that you're looking at it from the object's point of view.
    Unfortunately, like most "light-bulb" moments, it's very difficult to explain to someone who hasn't been through it. And I don't mean to make it sound 'mystical' - it isn't - it's simply one of those 're-wiring' things that happens when you finally "get" the solution to a problem that needs a bit of lateral thought. I've tried to explain it in a short essay, which I'd be more than happy to send to you if you want. And, you may be pleased to hear, it's all about parsing. [Edit: see "PS" below]

    I find it difficult picturing classes for a compiler - the suggestions for classes so far are just items or types of things that a user "could" create in their program but they're certainly not understood by the language as it stands.


    And that's probably because we (or I) have been focusing on the things we'd be trying to map if we were writing the game. Correct me if I'm wrong, but you're trying to write a language that allows people to create their own games; so you're into the business of meta-data - ie, understanding what makes up those "items".

    And there's that word "what" again. There are probably billions of ways to actually DO it (most of which are probably foolish, but nevertheless out there), but unless you understand WHAT you're trying to achieve, you can get lost in the business of HOW.

    Fortunately, an awful lot of languages have already been written, so nouns like "class" and "method" and "field" and "instance" are now part of the lexicon of someone trying to design a language. Your task, Mr. Phelps, should you choose to accept it, is to try and rationalise those things for what you want to do.

    If you've only ever (or indeed spent a lot of time) writing compilers, then your thinking is likely to be constrained down very narrow lines (albeit complex ones), because a compiler IS a procedure. It does a very specific task: convert source code to executable (or byte code). In Java terms you have one object - Compiler - which does everything - and your only worry is how to get it to do that task.

    There are actually several "objective" compilers around now, and maybe looking at some of those might help you to understand the thought process.

    my suspicion is that this is easier to do by putting the definitions into data which by it's very nature is extensible.


    Spot on. But you actually have to use that power: if you're simply duplicating a C program - or the C 'thought process' - in Java, you're missing out on an awful lot.

    He wrote a very influential book called Joel on Software which is well worth reading. The important bit in that particular article was "the single worst strategic mistake that any software company can make: They decided to rewrite the code from scratch". Which I believe is what you were suggesting.


    Then you misunderstood, or probably I didn't explain it right. What I was trying to say is that you need to change your approach. The article actually cites a re-factoring that he was doing that did re-use a pile of code that was already written.

    You've plainly written that code. All you need to do is rearrange it.

    Winston

    PS: Just in case you thought that "thinking objectively" is an absolute must; it isn't. 8 years is a long time, and I managed to write some pretty good classes in the meantime by simply using procedural "tools of the trade" (modularity, loose coupling, etc).
     
    Mich Robinson
    Ranch Hand
    Posts: 287
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Apologies for the really late reply.

    I'd certainly like to read your essay but please don't expect any major sea changes from me - can you PM it?

    Can you suggest any classes that might go into such my (or any) compiler? I just wondered please keep in mind that the users program isn't being compiled into java and that the concept of platforms, ladders etc is just something that I created in order to illustrate the language (it's not a part of the language per se). Can you just (very) briefly point out how the compiler would be better with classes as well? You mention that some compilers have been written in an OO fashion but I'm guessing that if OO gave a major advantage to their construction then wouldn't all compilers would be done that way?
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mich Robinson wrote:I'd certainly like to read your essay but please don't expect any major sea changes from me - can you PM it?


    Sure, but it's currently a Word doc, so I'll have to massage it a bit. Give me a day or two.

    Can you suggest any classes that might go into such my (or any) compiler? I just wondered please keep in mind that the users program isn't being compiled into java and that the concept of platforms, ladders etc is just something that I created in order to illustrate the language (it's not a part of the language per se).


    Well, like I said, the "things" that you want to define probably fall under the general heading of "objects", and each one is going to have a specific type (or class). And in a language like Java, objects usually contain data (fields) and methods. So there's three right there: Class (or Type), Field and Method.

    Beyond that, you need to ask yourself how involved you want this compiler to be. Can objects contain other objects? If so, then a Field should probably be a reference to another object; if not, then it could simply be the equivalent of a Java String or primitive (ie, a numeric or boolean).

    I'm also not sure what you meant by "isn't being compiled into Java". I understand that the declarative part is your own language, but wasn't your original idea to compile the source into something that would be executed - or used - by a Java program (your "game")?

    Can you just (very) briefly point out how the compiler would be better with classes as well? You mention that some compilers have been written in an OO fashion but I'm guessing that if OO gave a major advantage to their construction then wouldn't all compilers would be done that way?


    Well, I'm on shaky ground here, since I've never written a compiler (although I have written many parsers), but the major reason for using OO is that it manages the change process better than procedural code.
    If, for example, you decide to change your game later on to include weapons, or objects to be collected, or add "levels", I think you'll find the whole process a lot easier if your compiler is designed to allow new classes to be built.

    And getting back to your "language", it appears to be very specific to your game, so I suspect there will be quite a lot of stuff that can be pre-built - like the "Game Board". I don't know how you're looking at this, but it sounds to me a bit like a glorified game of snakes & ladders, except without dice; so I'd imagine the "board" as a grid of squares that starts out empty, but can be added to with things like platforms and ladders (which I assume will come from your compiler module). I suspect also that many of the "methods" of the objects you define with your compiler can also be pre-defined, perhaps with a static utility class for each major type. Then the objects you create simply forward calls to those methods using their own fields as parameters.

    Other classes like 'Direction' (an 8-point compass?) and 'Position' (row, column) sound like they could also be pre-built, since they're unlikely to change within the context of a Board. I suspect also that once the board is built, you'll need some sort of Controller to actually manage the game for you and react to input commands.

    However, I'm not a "gamer", nor indeed a GUI expert, so this is all guesswork on my part. Hope it gives you somewhere to start, though.

    I'll send you the essay as soon as I've rearranged it for a PM.

    Winston
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mich Robinson wrote:I'd certainly like to read your essay but please don't expect any major sea changes from me - can you PM it?


    I've gone one better: it's now a page on this site called MomentOfClarity. It's still going through a bit of an "editorial phase" at the moment, so it may change a bit in the coming days; but I'd be interested in what you think.

    Winston
     
    Mich Robinson
    Ranch Hand
    Posts: 287
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Interesting reading. You asked for comments so here's a few that came to mind while I was reading it :
  • Your code illustrates the compactness of the overall design but doesn't have the actual logic and rules - surely this bit is quite important.
  • How do you decide what rules can be applied to a given file - I assume by file type but I don't see this logic either.
  • Would all the rules be repeated for languages that share common quoting conventions etc?
  • I didn't think you could nest comments in Java - does you system cope with that?
  • Could you supply the original awk program for us to compare against?
  • I'm reasonably competent with awk and must admit I'd of used it for this problem - why didn't you?
  • It seems like a fairly interesting problem - was it done for work or personal interest?


  •  
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mich Robinson wrote:Interesting reading. You asked for comments so here's a few that came to mind while I was reading it :

  • Your code illustrates the compactness of the overall design but doesn't have the actual logic and rules - surely this bit is quite important.

  • Oddly enough, not as important as you'd think, because Class and object logic is about (and I'm sorry if I sound like a broken record here) what it does, not HOW it does it. If I can prove that a Rule - let's say a Rule for a "/* ... */" comment - works, then I can be pretty sure that it will work for a '"..."' quotation, or a "// ..." comment, or "(...)" brackets. The importance of the solution is not in its implementation code, but in the model; and in the fact that you hand text to a Rule object, rather than applying a bunch of "rule states" to a piece of text - ie, the logic is centred on the Rule rather than the text.

  • How do you decide what rules can be applied to a given file - I assume by file type but I don't see this logic either.

  • Well, that would depend on what you're parsing; but if you have a Rule interface that deals with text between specified start and end points, then you can implement it to deal with whatever bounds you want. In fact it's highly likely that you could write a generic skeleton class that simply takes the bounds it's supposed to work with.

  • Would all the rules be repeated for languages that share common quoting conventions etc?

  • Only if you write it that way. There'd be nothing to stop you creating, for example, a public enum for common constructs ('/*...*/', '"..."' and '//...' spring to mind). But again, I think you're focusing too much on the mechanics rather than the design.

  • I didn't think you could nest comments in Java - does you system cope with that?

  • I think you're right, but another language I know (Progress) that has the "/*...*/" comment style does allow them to be nested.

  • Could you supply the original awk program for us to compare against?

  • Ooof. That would probably double the size of the page. If I find it (it's in an old archive somewhere), I'll send it to you. Suffice to say that it's structure is pretty much like my first (procedural) effort in Java.

  • I'm reasonably competent with awk and must admit I'd of used it for this problem - why didn't you?

  • Erm ... I did. That's how the whole "Eureka" business came about to begin with.

  • It seems like a fairly interesting problem - was it done for work or personal interest?

  • A bit of both. I needed a way of getting the execution trees for any script in a Unix system, and this was just part of that requirement.

    But thanks very much for your comments. I'll see if I can incorporate a few of your points.

    Winston
     
    reply
      Bookmark Topic Watch Topic
    • New Topic