Programming language PG0 Language specification -- # Introduction PG0 is a programming language designed for learning programming. It focuses on understanding the basic structures essential to programming: sequential, branching, and looping. For simplicity, the language specifications are very straightforward, supporting only one type of branching and looping, and handling only integer data types. # Type The only data type is integer. Integers are 4 bytes, ranging from -2,147,483,648 to 2,147,483,647. Decimals are not supported. a = 1234 // Decimal a = -123 // Negative number # Variables Variables are containers for storing numerical values. They are automatically allocated when used. If not initially assigned, they are initialized to 0. You can explicitly declare variables using 'var'. var i, j = 100 // Multiple variables can be declared with ',' Variables allocated within a block are only valid within that block. Allocating the same variable name within the same block results in an error. var x { var x // A variable x valid only within the inner block x = 100 } y = x // The value of x is 0 Variable names can include alphanumeric characters and underscores, but cannot start with a number. Variable names are case-insensitive. Literal numbers are treated as constants. # Array Arrays are containers that hold variables in a sequence. To use arrays, specify '[]' after the variable. a[0] = 100 // First element a[1] = 200 // Second element b[i] = 10 // Index specified by variable Specifying a number inside '[]' accesses the array index. Indexes start from 0. Arrays automatically expand in size. a[100] // Prepares an array of 101 elements For multidimensional arrays, add '[]' after each dimension. a[1][0] = 300 // Two-dimensional array Leaving the index empty represents the whole array, allowing for array assignment and concatenation. a[] = b[] // Copies array b into a a[] = b[] + c[] // Concatenates arrays b and c into a Assigning an array to an integer type makes it an array, and assigning an integer to an array makes it an integer type. a = b[] // Equivalent to a[] = b[] a[] = 0 // Equivalent to a = 0 Initialize arrays by assigning {value1, value2, value3, ...}. a[] = {1,2,3} // Equivalent to a[0] = 1; a[1] = 2; a[2] = 3; a[] = {1,2,{10,20,{100,200,300}},3} // Multidimensional initialization b = {100, 200}[1] // Assigns 200 to b # Expressions Variables and constants are calculated or compared using expressions. All expressions return a value as a result. The truth value of expressions is defined as follows: * True: Non-zero * False: 0 ## Arithmetic Operators -------+--------------- a + b Addition a - b Subtraction a * b Multiplication a / b Division a % b Modulus -------+--------------- ## Unary Operators ----+---------------------------------------------- +a Positive value -a Negative value !a If a is 0, becomes 1; otherwise, becomes 0 ----+---------------------------------------------- ## Assignment Operator Assigns the value of the right expression to the left variable. a = 5 // a becomes 5 b = a + 5 // b becomes 9 ## Relational Operators Performs comparison and returns 1 or 0. -------+---------------------+----------------------------------------- a == b Equal 1 if a equals b a != b Not equal 1 if a does not equal b a < b Less than 1 if a is less than b a > b Greater than 1 if a is greater than b a <= b Less than or equal 1 if a is less than or equal to b a >= b Greater than or equal 1 if a is greater than or equal to b -------+---------------------+----------------------------------------- For array comparisons, the data inside the arrays are compared. Only '==' and '!=' can be used for array comparisons. ## Logical Operators ------+------------+------------------------------------------ a && b Logical AND 1 if both a and b are true, otherwise 0 a || b Logical OR 1 if either a ------+------------+------------------------------------------ The logical operator is short-circuit evaluated. ## Operator Precedence high () [] ! + - * / % + - < > <= >= == != && || = , low # Basic Structures ## Sequence ### Statements A statement is separated by a newline or ';'. They are executed sequentially. a = 1 // First execution, assigns 1 to a b = 2 // Second execution, assigns 2 to b To write a statement over multiple lines, end the line with an operator. Example: Writing 'a = 1 + 2' over two lines. a = 1 + // Continues to next line 2 ### Blocks Blocks are denoted by { to } and group multiple statements into a single unit. { a = 1 b = 2 } ## Branching ### if The if statement controls the execution of statements based on a condition. The statement following the if statement must be enclosed in a block. if (condition) { statement } The statement is executed if the expression is true; otherwise, it is skipped. if (a > b) { b = a // If a is greater than b, assign a to b a = 0 } Using '=' for assignment in expressions is a syntax error. ### else Specifies the statement to execute when the if statement's expression evaluates to false. If the if statement executes, the else statement is skipped. The statement following else must be in a block. if (a > b) { b = a } else { a = b } ## Looping ### while The while statement performs a loop based on an expression. The statement following while must be in a block. while (condition) { statement } As long as the expression is true, the statement continues to execute. i = 1 while (i <= 10) { // Loop from 1 to 10 i = i + 1 } Using '=' for assignment in expressions is a syntax error. # Other Comments are denoted by // to the end of the line. Use 'exit' to terminate processing. exit 'exit' can have a return value. exit i -- Copyright (C) 1996-2023 by Ohno Tomoaki. All rights reserved. WEB SITE: https://www.nakka.com/ E-MAIL: nakka@nakka.com