Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Where can I learn to create a programming language?

compilers

This topic has been archived. This means that you cannot reply to this topic.
39 replies to this topic

#25 Kreative

Kreative

    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 30 October 2015 - 05:38 AM

@WingedPanther73

Without watching any tutorials to guide me or reading anything, prior to posting this thread I have made a little code stub in java that can read files and successfully breaks them down into tokens, I can imagine how I would expand it to create a calculator that understands operations and can set values to variables. I would find anywhere that there's an equals sign and declare the line of code ended by a semi-colon (my choice) as a variable setting, I would have a hashmap with string and integer types (if my programming language were to accept only integers as variable values), the string would be the variable name. If a hashmap contained a variable that I was setting a value to, I would just update the value, otherwise create a new entry with the new value. If a line of code didn't have an equals sign I could just print the value either from the variable in the hashmap if it's a variable name or the numeric or string value if it wasn't, if it was a variable that doesn't exist in the hashmap I'd just throw an exception.

If it helps you understand my understanding of parsing I can give you the stub of code that breaks down a file into tokens, or I can see if I can complete the calculator if you deem it necessary so you could give me better advice. I haven't started thinking about how I could create classes, instances or methods though I'd imagine all values that would exist would be a class with a hashmap of <String (vaiable name), Itself>.

Edit: How would I add generics to my programming language if my programming language's parser written in java, which, doesn't allow storing generics? :)

Kind Regards,
Kreative


Edited by Kreative, 30 October 2015 - 05:45 AM.


#26 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 30 October 2015 - 09:09 AM

It's about defining the processing. Your compiler's language doesn't have to support the features that are allowed by the language.

Example, most C++ compilers are written in a more limited language, such as C. So what happens is lots of things get aliased into other things. Class methods are converted into function pointers. Classes become structs, etc. There are then additional data structures created to track pieces of information about instances of classes, etc.

Original C++ compilers were just processors that converted it into valid C code. Then they compiled it as C code to get the object code/executables.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#27 Kreative

Kreative

    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 30 October 2015 - 10:09 AM

It's about defining the processing. Your compiler's language doesn't have to support the features that are allowed by the language.

Example, most C++ compilers are written in a more limited language, such as C. So what happens is lots of things get aliased into other things. Class methods are converted into function pointers. Classes become structs, etc. There are then additional data structures created to track pieces of information about instances of classes, etc.

Original C++ compilers were just processors that converted it into valid C code. Then they compiled it as C code to get the object code/executables.

Not what I meant, it's just that sometimes a language's restrictions can limit what the interpreter can interpret or at least degrade efficiency.

Kind Regards,
Kreative



#28 Kreative

Kreative

    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 31 October 2015 - 04:28 AM

This is slightly off the question but for my programming language, how can the SOLE fact that static members are shared across all derived classes be advantageous, not that static members are shared across all instances of a class, but that it's pointed to the same address in the class and every derived class? How can that be advantageous? If anyone can answer that I'd be very thankful, but if you could go a teeny bit further and use your imagination, if there would be a keyword or modifier in C++ that would make members shared across all instances of a class but they could still be overridden or have their own unique implementations, their own unique values, meaning they're not shared across all derived classes. Without using the static keyword anywhere could you emulate a member with this new keyword to make the member just like it would be with a static keyword instead of this new keyword, perhaps by making it private, and making access and assign methods that are final and have this new keyword?

Kind Regards,
Kreative


Edited by Kreative, 31 October 2015 - 04:30 AM.


#29 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 01 November 2015 - 07:06 AM

I'll answer your first question.

 

If you have a property that is shared between the class and all derived class such that they ALL point to the same value, then you can keep a count of how many instances of the parent/derived classes you have. One way to do this is to have member that is a singlton class which is incremented when an object is created and decremented when it is destroyed.

 

Of course, if you know how to create a singleton class, then you don't need any special language features to accomplish this :)


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#30 Kreative

Kreative

    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 01 November 2015 - 07:34 AM

I'll answer your first question.

 

If you have a property that is shared between the class and all derived class such that they ALL point to the same value, then you can keep a count of how many instances of the parent/derived classes you have. One way to do this is to have member that is a singlton class which is incremented when an object is created and decremented when it is destroyed.

 

Of course, if you know how to create a singleton class, then you don't need any special language features to accomplish this :)

When I was questioning how to solve my main issue in java, someone suggested creating a singleton and they demonstrated the use of creating a static method (function) which created an instance within this method and returned the value of the instance method that was abstract (virtual). But then you'd have to create such a method in every derived class and you cannot ensure anyone else who uses your class will make such a method and if you were to cast back to the super class you would be using the super-class' static method which will create a new instance of the super-class without implementing the virtual function.

As for what you said for the second question, if there was such a variable that is shared across all instances but not derived classes, you could just make it private and in the constructor of the class make the private variable increment on every creation, and since you have to call the super-constructor (at least in java) you will not need static. I've started looking a bit more into C++ but it's quite different from java in some aspects which is taking some time, though unfortunately the syntax and some aspects of how it works are ugly to me XD, for example how the static_cast operator is in the written like a function, but that's just me.

Kind Regards,
Kreative



#31 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 01 November 2015 - 11:21 AM

C++ is very, very different from Java in a lot of ways. I actually prefer C++ over Java for those very differences. When you learn Java after C++, you find yourself having to cope with a lot of limitations that feel artificial and constraining. Yes, not having to deal with memory management (much) is nice, but C++ already has plenty of ways to handle that which are almost more graceful.

 

Going from Java to C++, I suspect, is even more painful. You are suddenly forced to manage memory far before you learn how to use auto-pointers to make it simple again. You have implementations go away and the messiness of multiple-inheritance replacing it without the benefits pointed out. I suspect C++ makes it feel like you're doing more work for the same result for quite a while.

 

As you learn C++, hopefully you'll gain a much richer appreciation for how challenging it is to decide to include a feature in a language. C++ and Java made opposite choices on just about every OOP decision possible :)


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#32 Kreative

Kreative

    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 02 November 2015 - 10:05 AM

C++ is very, very different from Java in a lot of ways. I actually prefer C++ over Java for those very differences. When you learn Java after C++, you find yourself having to cope with a lot of limitations that feel artificial and constraining. Yes, not having to deal with memory management (much) is nice, but C++ already has plenty of ways to handle that which are almost more graceful.

 

Going from Java to C++, I suspect, is even more painful. You are suddenly forced to manage memory far before you learn how to use auto-pointers to make it simple again. You have implementations go away and the messiness of multiple-inheritance replacing it without the benefits pointed out. I suspect C++ makes it feel like you're doing more work for the same result for quite a while.

 

As you learn C++, hopefully you'll gain a much richer appreciation for how challenging it is to decide to include a feature in a language. C++ and Java made opposite choices on just about every OOP decision possible :)

Hmm,

Could you write or find an example of code for any somewhat realistic scenario where you can create virtual members that are shared across all instances of a class but not derived classes and accessible without the need for instantiation, so like a Vehicle class with a virtual wheels member and derived classes define what wheels is, and you don't have to create a new instance, you can access it directly or perhaps some form of workaround? Apart from this one issue, even after learning a bit of C++ I prefer java, by the way I don't like how java restricts you to only single inheritance and manages memory for you automatically, and especially the fact that you can't have unsigned numeric types, though I still prefer it.

Kind Regards,
Kreative


Edited by Kreative, 02 November 2015 - 10:05 AM.


#33 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 02 November 2015 - 04:44 PM

What I would probably do is use an abstract method: int Wheels(), which is virtual final in the implementation classes.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#34 Kreative

Kreative

    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 03 November 2015 - 10:05 AM

How would access this method without instantiating the class from its every derived class and its declarative class?

Kind Regards,
Kreative



#35 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 03 November 2015 - 01:50 PM

Virtual in C++ means you don't have to re-instantiate a method in every class. It's different from Java.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#36 Kreative

Kreative

    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 03 November 2015 - 03:32 PM

Virtual in C++ means you don't have to re-instantiate a method in every class. It's different from Java.

In java you do not have to re-implement / re-instantiate a method in every class if it is declared abstract. An abstract method's declaration / definition / creation does not have to implement / instantiate the method body, and it is optional to implement an abstract's methods body in an abstract class that extends another abstract class where the method is declared, and mustn't have an implementation / body. Any concrete class, normal class, that extends an abstract class with an unimplemented / uninstantiated method, whether from another abstract class that it extends and has no implementation for the method or declared within itself.

How I would define an abstract method (function) in java is that it's a method, that can be declared in an abstract class (not-complete class), with no implementation (without a body; not instantiated) within its declaration that needs to be implemented in a normal class (full class). Once implemented in a full class, any derived / sub-classes do not have to re-implement the method, but they can override it, it's optional though, not necessary. What did you mean???????

Kind Regards,
Kreative






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download