Skip to content

Syntax and coding style

Paolo Angeli edited this page Sep 22, 2019 · 27 revisions

A tiny taste of the language

At the very first beginning, the Jay language was somewhat similar to C with some funny constructs. Now that is more mature a lot of weird syntaxes started to appear so as a first impact you can get a bit confused.

Comments

// Simple one line comment
/* Multi line comment */
/* A strange /* nested */ comment */ 

Constants

RED    :: 0x04586;
PI     :: 3.14;
PLAYER :: "Unnamed";

Style: prefer using all upper case naming convention;

Variables

// Unassigned declarations will be initialized to default values unless specified
counter     : int;    // Signed 32 bit integer
key         : s64;    // Signed 64 bit integer
flags       : u8;     // Unsigned 8 bit integer 
// Declarations and assignements
mode        :- 10;                     // Type int automatically inferred by the compiler
area        : float - r * r * PI;      // Single precision 32 bit floating point
temp        : string - "Convert to:";  // A standard native string

Style: Prefer using all lower case naming convention;

Procedures

// Define a procedure with arguments
do_something :: (what: string){
  printf("I'm doing: %", what); 
}

// Another procedure without arguments
main_loop :: (){
  while !should_exit {
    get_user_input();
    simulate();
    render();
    post_process();
  }
}

Style: prefer using snake case naming convention for all the compound terms

Functions

// Define a simple function
square :: (val: float) -> float {
  return val * val;
}

// Define a function with multiple results
circle_area:: (radious: float) -> float, bool {
  return square(radious) * PI, radious > 0 ;
}

Aggregated data type

// define a new struct type
Spring :: struct {
  relaxed_length : float; // millimeters
  theta          : float; // N/m
  force          : float; // N
  current_length : float; // mm
}

Style: prefer using capitalization for the first letter of the user-defined type.

// declare a new spring allocate it on the stack and initialize to zeroes
a_spring : Spring;

// use dot notation to access the fields
a_spring.relaxed_length - 100;

// or use the using keyword
using a_spring {
  theta - 1000.0;
  force - 25;
  current_length - theta / relaxed_length * force + relaxed_lenght; 
}


Types, constants and variables

  • Variables and assignments
  • Language data types
  • Simple user-defined data types
  • Expressions and operators
  • Type-casting
  • Pointers

Flow control

Procedures and functions

  • Declarations
  • Arguments / Parameters
  • Return values
  • Overloading / Polymorhism
  • Advanced features
  • Lambdas

Aggregated data types

  • Arrays
  • Strings
  • Composition of Structs

Advanced features

Clone this wiki locally