Skip to content

Specifications

Shachar Shemesh edited this page Aug 3, 2019 · 17 revisions

Table of Contents

Lexical Analysis

Literals

Literals are values given explicitly in the source code.

Numeric Literals

All integer numeric literals may have any number of underscore characters (_) in them to space out the digits. The only exception is that the underscore may not be the first character of the literal.[1]

Decimal Integers

Decimal integers are denoted as a sequence of one or more decimal digits (in the range 0-9) or the underscore character. The first character of the literal may not be an underscore. The first character of the literal may only be the digit 0 if the literal contains no more digits[2].

Hexadecimal Integers

Hexadecimal integers start with 0x or 0X, followed by one or more hexadecimal digits (0-9 or a-f in either cases). The digits MAY have any number of underscores between them, but MAY NOT begin with an underscore or have an underscore between the leading 0 and the x.

All of the following are legal hexadecimal literals:

  • 0x0
  • 0x___12
  • 0xA
  • 0X12_
The following are not legal hexadecimal literals:
  • 0x___ (no digits)
  • _0x12 (a legal identifier)
  • 0_x12 (underscore between 0 and x)
  • 0xcovfefe ("o" and "v" are not hexadecimal digits).

Binary Integers

Binary integers start with 0b or 0B, followed by one or more binary digits (0 or 1). The digits MAY have any number of underscores between them, but the literal MAY NOT begin with an underscore or have an underscore between the leading 0 and the b.

All of the following are legal binary literals:

  • 0b0
  • 0b_0010_1101__1011_1000
  • 0b10_
The following are not legal binary literals:
  • 0b___ (no digits)
  • _0b11 (a legal identifier)
  • 0_b11 (underscore between 0 and b)
  • 0b12 (2 is not a binary digit)

Octal Integers

Practical has no octal literal integers built into the language's syntax. A TBD library function will be used to specify octal literals.

String Literals

TBD

Compound Literals

Array Literals

TBD

Identifiers

Parser

MODULE -> GLOBAL_EXPRESSIONS_LIST

GLOBAL_EXPRESSIONS_LIST -> ϵ

GLOBAL_EXPRESSIONS_LIST -> GLOBAL_EXPRESSIONS_LIST GLOBAL_EXPRESSION

GLOBAL_EXPRESSION -> FUNC_DEF

FUNC_DEF -> reserved_def FUNC_DECL_BODY COMPOUND_EXPRESSION

FUNC_DECL_BODY -> IDENTIFIER ( FUNC_DECL_ARGS ) FUNC_DECL_RET

FUNC_DECL_ARGS -> ϵ

FUNC_DECL_ARGS -> FUNC_DECL_ARGS_NONEMPTY

FUNC_DECL_ARGS_NONEMPTY -> FUNC_DECL_ARG

FUNC_DECL_ARGS_NONEMPTY -> FUNC_DECL_ARG , FUNC_DECL_ARGS_NONEMPTY

FUNC_DECL_ARG -> IDENTIFIER : TYPE

FUNC_DECL_RET -> ϵ

FUNC_DECL_RET -> '->' TYPE

STATEMENT -> EXPRESSION ;

STATEMENT -> VARIABLE_DEFINITION ;

EXPRESSION -> COMPOUND_EXPRESSION

EXPRESSION -> LITERAL

EXPRESSION -> IDENTIFIER

EXPRESSION -> EXPRESSION ( FUNC_CALL_ARGUMENTS )

EXPRESSION -> EXPRESSION op EXPRESSION

EXPRESSION -> EXPRESSION op

EXPRESSION -> op EXPRESSION[3]

EXPRESSION -> cast_type ! TEMPLATE_PARAM ( EXPRESSION )

TEMPLATE_PARAM -> id

TEMPLATE_PARAM -> ( EXPRESSION )

LITERAL -> literal_string

LITERAL -> literal_int

LITERAL -> literal_fp

COMPOUND_EXPRESSION -> { STATEMENT_LIST }

COMPOUND_EXPRESSION -> { STATEMENT_LIST EXPRESSION }

STATEMENT_LIST -> ϵ

STATEMENT_LIST -> STATEMENT_LIST STATEMENT

TYPE -> IDENTIFIER

VARIABLE_DEFINITION -> reserved_def VARIABLE_DECL_BODY

VARIABLE_DEFINITION -> reserved_def VARIABLE_DECL_BODY = EXPRESSION

VARIABLE_DECL_BODY -> IDENTIFIER : TYPE

IDENTIFIER -> identifier

FUNC_CALL_ARGUMENTS -> ϵ

FUNC_CALL_ARGUMENTS -> FUNC_CALL_ARGUMENTS_NONEMPTY

FUNC_CALL_ARGUMENTS_NONEMPTY -> EXPRESSION

FUNC_CALL_ARGUMENTS_NONEMPTY -> EXPRESSION , FUNC_CALL_ARGUMENTS_NONEMPTY

Rational

  1. ^ Otherwise it is not possible to know whether this is an integer literal or an identifier.
  2. ^ This is done to avoid confusion with C's octal literals.
  3. ^ For list of actual operators and their precedence, see https://github.com/Practical/practical-sa/wiki/Operators

Clone this wiki locally