Classification of Programming Languages

From TRCCompSci - AQA Computer Science
Revision as of 09:59, 10 March 2017 by Admin (talk | contribs) (Declarative)
Jump to: navigation, search

Low Level Languages

Machine Code

Machine code is the basic binary information required for a computer to perform a function. It is classified as a 1st generation programming language. Each command is binary pattern, ie 100001 might be load, or 100010 might be add or 100011 might be store.

This is essentially writing a program by coding the binary. This provides direct access to the hardware and all programs are converted to machine code at some point.

Assembly Language

Assembly language is a low level, 2nd generation programming language, purposed towards fast execution and complete control over hardware. It replaces a binary pattern with a more readable mnemonic, ie a command word.

One assembly language instruction is equivalent to one machine code instruction, and an assembler is used to convert the assembly language to machine code.

Relationship Between Machine Code & Assembly Language

One machine code instruction will create one assembly language instruction. Remember assembly language is essentially replacing a binary pattern with a command word.

Terms

Mnemonics

An identifiable text label for a particular command (rather than requiring a programmer write instructions in binary).

Source Code

The source code is a program as written by the programmer.

Object Code

The object code is generated by the interpreter, compiler or assembler.

High Level Languages

The main characteristics of a high level language are:

  • It is easier for the programmer to identify what a command does (English words)
  • High level languages need to be translated
  • One command in a high level language represents many lines of code in assembler or machine code
  • They are portable between systems
  • More straight forward to program
  • Wider variety of data structures

Imperative

Imperative, or procedural, programming languages break down processes into functions or methods, which are all given mnemonics. A single statement of an imperative language is converted to multiple machine code instructions by a language translator (such as an interpreter or compiler), which are then executed in sequence. Imperative languages are part of the 3rd generation of programming languages. Examples of imperative languages include C++, Python and Visual Basic.

Object Oriented

OOP languages are based on the emulation of real world objects and their relationships and interactions with each other. The basis of object oriented programming is situated on the re-usability and conservation of large scale code/programming structures; for example, a video game makes extensive use of object oriented programming by defining complex object hierarchies, such as:

Object
Sprite
PlayableSprite
MainCharacter

Note At This Point We Can Also Have Other Classes Inherit From Sprite

EnemySprite
BossMonster
BufferMonster
StaticSprite
Block
TreasureChest

etc...

Note that inheritance isn't just a fancy way to structure classes; OOP also allows for methods, variables, anonymous functions etc. to be passed on down the chain, for example if the base class Sprite is given a default integer (health) and a Method() (Death), any class which inherits from sprite will automatically have these instance members defined at declaration. Furthermore inherited classes can override base methods and variables, and call methods or variables higher up on the inheritance chain (using the base/super method). for Example:

 1 struct Rect {
 2     public Rect(int x, int y, int w, int h) {
 3         this.X = x, this.Y = y, this.width = w, this.height = h;
 4     }
 5 
 6     public Int32 X, Y, width, height;
 7 }
 8 
 9 class Sprite : object { // reference to base "Object" Unnecessary As All Classes Originate From "Object" 
10     public Sprite() { 
11         this.ID = Sprite.spriteCount.ToString().PadLeft(5, '0');
12         Sprite.spriteCount += 1;
13     } // If No ID Given
14 
15     public Sprite(String spriteID) {
16         this.ID = spriteID;
17         Sprite.spriteCount += 1;
18     }
19 
20     public void Dead() {
21         // Do Some Stuff When Dead
22     }
23 
24     public String ID;
25     public Int32 Health = 100;
26     public Rect position;
27     public bool IsDead { get { return (this.Health == 0); } }
28 
29     public static Int32 spriteCount = 0;
30 }
31 
32 class PlayableSprite : Sprite {
33     public PlayableSprite(Rect pos) : base() { // creates new constructor & calls base constructor
34         this.position = pos;
35     }
36 
37     public PlayableSprite(String id, Rect pos) : base(id) {  // calls base constructor with passed parameter
38         this.position = pos;
39     }
40 
41     public void CheckInput(KeyboardState kb) {
42         // Loads of movement code, tied to user keypress
43     }
44 }
45 
46 class EnemySprite : Sprite {
47     public void Move() {
48         // Loads of movement code, tied to mathematical algorithm
49     }
50 }

In The Above example both PlayableSprite & EnemySprite are siblings and possess all the same instance members as Sprite.

1 PlayableSprite protag = new PlayableSprite("00000", new Rect(0, 0, 100, 100)); // calls overridden constructor
2 
3 EnemySprite boss = new EnemySprite("00000") {
4     position = new Rect(1000, 1000, 100, 100); // Set Base Member Instance Variable
5 };
6 
7 while (!protag.IsDead && !boss.IsDead()) { // Accessing Base Member Instance Variable
8     // Game Run Until either player or enemy is dead
9 }

The above example doesn't in any part do anything, but it does showcase the simple and intuitive way of decompressing complex code into simple classes.

Declarative

Often referred to as Fourth Generation Languages, Declarative languages allow the programmer to specify what they want to produce, without having to say how to achieve it.

For example SQL is declarative, you say what you want to select, where to select it from, and what conditions apply. The SQL server will then execute your request according to its coding and provide you with the results.