Skip to content

AST Generation From Source

Benjamin Kowarsch edited this page Jun 20, 2023 · 85 revisions

Compilation Units

Source File

A file node encodes the filename, checksum (module key) and source code (compilation unit) of a Modula-2 source file. It is the root node of the abstract syntax tree.

Encoding

An interface source file Foobar.def is encoded as

(FILE (FILENAME "Foobar.def") (Key 0xDEADBEEF) (INTERFACE ...))

An implementation source file Foobar.mod is encoded as

(FILE (FILENAME "Foobar.mod") (Key 0xCAFEBABE) (IMPLEMENTATION ...))

A program source file FooProg.mod is encoded as

(FILE (FILENAME "FooProg.mod") (Key 0xCAFED00D) (PROGRAM ...))

API Call

A file node is created using the AST API call

ast_node = m2c_ast_new_node(AST_FILE,
  filename_node, key_node, module_node, NULL);

Interface Module

An interface module node encodes the module identifier, import list, re-export list, definitions and declarations within an interface module.

Encoding

INTERFACE MODULE Foobar;
...
END Foobar.

is encoded as

(INTERFACE (IDENT "Foobar")
  (IMPORT ...)
  (RE-EXPORT ...)
  (DECL-LIST ...))

API Call

An interface module node is created using the AST API call

ast_node = m2c_ast_new_node(AST_INTERFACE,
  id_node, imp_node, rxp_node, def_or_decl_node, NULL);

Implementation Module

An implementation module node encodes the module identifier, import list, definitions and initialisation code of an implementation module.

Encoding

IMPLEMENTATION MODULE Foobar;
...
END Foobar.

is encoded as

(IMPLEMENTATION (IDENT "Foobar")
  (IMPORT ...)
  (DEF-LIST ...)
  (STMTSEQ ...))

API Call

An implementation module node is created using the AST API call

ast_node = m2c_ast_new_node(AST_IMPLEMENTATION,
  id_node, imp_node, def_node, init_node, NULL);

Program Module

A program module node encodes the module identifier, import list, definitions and execution code of a program module.

Encoding

MODULE FooProg;
...
END FooProg.

is encoded as

(PROGRAM (IDENT "FooProg")
  (IMPORT ...)
  (DEF-LIST ...)
  (STMTSEQ ...))

API Call

A program module node is created using the AST API call

ast_node = m2c_ast_new_node(AST_PROGRAM,
  id_node, imp_node, def_node, exec_node, NULL);

Import List

An import list node encodes all imported library identifiers within an interface module in the order they appear in the Modula-2 source.

Encoding

IMPORT Foo, Bar;
IMPORT Baz;

is encoded as

(IMPORT "Foo" "Bar" "Baz")

API Call

An import list node is created using the AST API call

ast_node = m2c_ast_new_terminal_list_node(AST_IMPORT, imp_list);

where imp_list is a FIFO API created object of type fifo_t and the list elements are interned strings of type intstr_t.

Re-Export List

A re-export list node encodes all re-exported library identifiers within an interface module in the order they appear within the Modula-2 source.

Encoding

IMPORT Foo+, Bar;
IMPORT Baz+;

is encoded as

(IMPORT "Foo" "Bar" "Baz")

and

(RE-EXPORT "Foo" "Baz")

API Call

A re-export list node is created using the AST API call

ast_node = m2c_ast_new_terminal_list_node(AST_REEXPORT, rxp_list);

where rxp_list is a FIFO API created object of type fifo_t and the list elements are interned strings of type intstr_t.

Declarations and Definitions

Declaration List

A declaration list node encodes all definitions and declarations within an interface module in the order they appear in the Modula-2 source.

Encoding

CONST Foo = 42;
TYPE Bar = Baz;
PROCEDURE Bam;

is encoded as

(DECL-LIST
  (CONSTDEF (IDENT "Foo") (INTVAL 42))
  (TYPEDEF (IDENT "Bar") (IDENT "Baz"))
  (PROCDECL (PSIG (IDENT "Bam") (EMPTY) (EMPTY))))

API Call

A declaration list node is created using the AST API call

ast_node = m2c_ast_new_list_node(AST_DECLLIST, decl_list);

where decl_list is a FIFO API created object of type fifo_t and the list elements are AST nodes of type m2c_astnode_t.

Definition List

A definition list node encodes all definitions within an implementation or program module in the order they appear in the Modula-2 source.

Encoding

CONST Foo = 42;
TYPE Bar = Baz;
VAR ch : CHAR;

PROCEDURE Bam;
BEGIN
  NOP
END Bam;

is encoded as

(DEF-LIST
  (CONSTDEF (IDENT "Foo") (INTVAL 42))
  (TYPEDEF (IDENT "Bar") (IDENT "Baz"))
  (VARDEF (IDENT "ch") (IDENT "CHAR"))
  (PROCDEF (PSIG (IDENT "Bam") (EMPTY) (EMPTY) (STMTSEQ (NOP)))))

API Call

A definition list node is created using the AST API call

ast_node = m2c_ast_new_list_node(AST_DEFLIST, def_list);

where def_list is a FIFO API created object of type fifo_t and the list elements are AST nodes of type m2c_astnode_t.

CONST Definition

A constant definition node encodes the identifier, optionally a type, and the value expression of a constant definition.

Encoding

CONST Foo = 42;

is encoded as

(CONSTDEF (IDENT "Foo") (EMPTY) (INTVAL 42))

API Call

A constant definition node is created using the AST API call

ast_node = m2c_ast_new_node(AST_CONSTDEF,
  id_node, type_node, expr_node, NULL);

where type_node may be an empty node.

TYPE Declaration

A type declaration node encodes the identifier, and optionally the allocation size of a type declaration within an interface module.

Encoding

TYPE FooPtr = OPAQUE;
TYPE BarRec = OPAQUE [AllocSize];

is encoded as

(TYPEDECL (IDENT "FooPtr") (EMPTY))
(TYPEDECL (IDENT "BarRec") (IDENT "AllocSize"))

API Call

A type declaration node is created using the AST API call

ast_node = m2c_ast_new_node(AST_TYPEDECL,
  id_node, expr_node, NULL);

where expr_node may be an empty node.

TYPE Definition

A type definition node encodes the identifier and type constructor of a type definition within an interface, implementation or program module.

Encoding

TYPE FooPtr = POINTER TO Foo;

is encoded as

(TYPEDEF (IDENT "FooPtr") (POINTER (IDENT "Foo"))

API Call

A type definition node is created using the AST API call

ast_node = m2c_ast_new_node(AST_TYPEDEF,
  id_node, type_node, NULL);

VAR Declaration

A variable declaration node encodes the identifier or identifier list, and type of a variable definition within an interface module.

Encoding

VAR foo, bar : Foobar;

is encoded as

(VARDECL (IDENTLIST "foo" "bar") (IDENT "Foobar"))

API Call

A variable declaration node is created using the AST API call

ast_node = m2c_ast_new_node(AST_VARDECL,
  ident_node, type_node, NULL);

where ident_node may be an identifier or identifier list node, and type_node an identifier or anonymous type constructor node.

VAR Definition

A variable definition node encodes the identifier or identifier list, and type of a variable definition within an implementation or program module.

Encoding

VAR foo, bar : Foobar;

is encoded as

(VARDEF (IDENTLIST "foo" "bar") (IDENT "Foobar"))

API Call

A variable definition node is created using the AST API call

ast_node = m2c_ast_new_node(AST_VARDEF,
  ident_node, type_node, NULL);

where ident_node may be an identifier or identifier list node, and type_node an identifier or anonymous type constructor node.

PROCEDURE Declaration

A procedure declaration node encodes the procedure signature of a procedure declaration within an interface module.

Encoding

PROCEDURE default : Foobar;

is encoded as

(PROCDECL
  (PSIG
    (IDENT "default")   ; procedure identifier
    (EMPTY)             ; formal parameters
    (IDENT "Foobar")))  ; return type

API Call

A procedure declaration node is created using the AST API call

ast_node = m2c_ast_new_node(AST_PROCDECL,
  psig_node, NULL);

PROCEDURE Definition

A procedure definition node encodes the procedure signature and body of a procedure definition within an implementation or program module.

Encoding

PROCEDURE default : Foobar;
BEGIN
  RETURN defaultValue
END default;

is encoded as

(PROCDEF
  (PSIG
    (IDENT "default")   ; procedure identifier
    (EMPTY)             ; formal parameters
    (IDENT "Foobar"))   ; return type
  (STMTSEQ
    (RETURN (IDENT "defaultValue"))))

API Call

A procedure definition node is created using the AST API call

ast_node = m2c_ast_new_node(AST_PROCDEF,
  psig_node, body_node, NULL);

Constant Binding

A constant binding node encodes the binding of a constant definition within an interface module.

Encoding

CONST [TLIMIT] Capacity = 1000;

is encoded as

(CONSTDEF (IDENT "Capacity") (INTVAL 1000))

and

(BIND-CONST (IDENT "Capacity") (IDENT "TypeIdent") (IDENT "TLIMIT"))

API Call

A constant binding node is created using the AST API call

ast_node = m2c_ast_new_node(AST_BINDCONST,
  id_node, type_node, bind_spec_node, NULL);

Procedure Binding

A procedure binding node encodes the binding of a procedure declaration within an interface module.

Encoding

PROCEDURE [LENGTH] length ( str : String ) : LONGCARD ;

is encoded as

 (PROCDECL
   (PSIG
     (IDENT "length")                        ; procedure identifier
     (BYVAL (IDENT "str") (IDENT "String"))  ; formal parameters
     (IDENT "LONGCARD")))                    ; return type

and

(BIND-PROC (IDENT "length") (IDENT "String") (IDENT "LENGTH"))

API Call

A procedure binding node is created using the AST API call

ast_node = m2c_ast_new_node(AST_BINDPROC,
  id_node, type_node, bind_spec_node, NULL);

Formal Parameters Sequence

A formal parameters sequence node encodes the formal parameters of a procedure signature.

API Call

A formal parameters sequence node is created using the AST API call

ast_node = m2c_ast_new_list_node(AST_FPSEQ, node_list);

where node_list is a FIFO API created object of type fifo_t and the list elements are AST nodes of type m2c_astnode_t.

Formal Parameters

A formal parameters node encodes one or more formal parameters of a procedure signature.

There are three kinds:

  • BYVAL : formal value parameters node
  • BYREF : formal var parameters node
  • CONSTREF : formal const parameters node

Formal Value Parameters

A BYVAL formal parameters node encodes one or more formal by-value parameters of a procedure signature.

Encoding

PROCEDURE Foo ( bar, baz : Bam );

is encoded as

(PROCDECL
  (PSIG
    (IDENT "Foo")               ; procedure identifier
    (BYVAL
      (IDENTLIST "bar" "baz")   ; formal parameter identifiers
      (IDENT "Bam"))            ; formal type identifier
    (EMPTY)))                   ; no return type

API Call

A BYVAL formal parameters node is created using the AST API call

ast_node = m2c_ast_new_node(AST_BYVAL,
  id_node, type_node, formal_type_node, NULL);

where id_node may be an identifier or identifier list node.

Formal VAR Parameters

A BYREF formal parameters node encodes one or more formal by-reference parameters of a procedure signature.

Encoding

PROCEDURE Foo ( VAR bar, baz : Bam );

is encoded as

(PROCDECL
  (PSIG
    (IDENT "Foo")               ; procedure identifier
    (BYREF
      (IDENTLIST "bar" "baz")   ; formal parameter identifiers
      (IDENT "Bam"))            ; formal type identifier
    (EMPTY)))                   ; no return type

API Call

A BYREF formal parameters node is created using the AST API call

ast_node = m2c_ast_new_node(AST_BYREF,
  id_node, type_node, formal_type_node, NULL);

where id_node may be an identifier or identifier list node.

Formal CONST Parameters

A CONSTREF formal parameters node encodes one or more formal immutable by-reference parameters of a procedure signature.

Encoding

PROCEDURE Foo ( CONST bar, baz : Bam );

is encoded as

(PROCDECL 
  (PSIG
    (IDENT "Foo")               ; procedure identifier
    (CONSTREF
      (IDENTLIST "bar" "baz")   ; formal parameter identifiers
      (IDENT "Bam"))            ; formal type identifier
    (EMPTY)))                   ; no return type

API Call

A CONSTREF formal parameters node is created using the AST API call

ast_node = m2c_ast_new_node(AST_CONSTREF,
  id_node, type_node, formal_type_node, NULL);

where id_node may be an identifier or identifier list node.

Formal Types

A formal type node encodes a formal type for one or more formal parameters of a procedure signature.

There are four kinds:

  • non-attributed formal type node
  • ARRAYOF : open array formal type node
  • ARGLIST : variadic formal type node
  • CAST : casting formal type node

Open Array Formal Type

An open array formal type node encodes an open array formal type for one or more formal parameters of a procedure signature.

Encoding

PROCEDURE Foo ( bar, baz : ARRAY OF Bam );

is encoded as

(PROCDECL
  (PSIG
    (IDENT "Foo")               ; procedure identifier
    (BYVAL
      (IDENTLIST "bar" "baz")   ; formal parameter identifiers
      (ARRAYOF "Bam"))          ; open array formal type
    (EMPTY)))                   ; no return type

API Call

An open array formal type node is created using the AST API call

ast_node = m2c_ast_new_node(AST_ARRAYOF, base_type_ident_node, NULL);

Variadic Formal Type

A variadic formal type node encodes a variadic formal type for one or more formal parameters of a procedure signature.

Encoding

PROCEDURE Foo ( bar, baz : ARGLIST OF Bam );

is encoded as

(PROCDECL
  (PSIG
    (IDENT "Foo")               ; procedure identifier
    (BYVAL
      (IDENTLIST "bar" "baz")   ; formal parameter identifiers
      (ARGLIST "Bam"))          ; list element type identifier
    (EMPTY)))                   ; no return type

API Call

A variadic formal type node is created using the AST API call

ast_node = m2c_ast_new_node(AST_ARGLIST, base_type_ident_node, NULL);

Casting Formal Type

A casting formal type node encodes a casting formal type for one or more formal parameters of a procedure signature.

Encoding

PROCEDURE Foo ( barPtr : CAST ADDRESS );

is encoded as

(PROCDECL
  (PSIG
    (IDENT "Foo")          ; procedure identifier
    (BYVAL
      (IDENT "barPtr")     ; formal parameter identifier
      (CAST "ADDRESS"))    ; casting type identifier
    (EMPTY)))              ; no return type

and

PROCEDURE Baz ( bam : CAST OCTETSEQ );

is encoded as

(PROCDECL
  (PSIG
    (IDENT "Baz")          ; procedure identifier
    (BYVAL
      (IDENT "bam")        ; formal type identifier
      (CAST "OCTETSEQ"))   ; casting type identifier
    (EMPTY)))              ; no return type

API Call

A casting formal type node is created using the AST API call

ast_node = m2c_ast_new_node(AST_CAST, base_type_ident_node, NULL);

ALIAS Type Constructor

An alias type node encodes the type constructor for an alias type.

Encoding

TYPE Foo = ALIAS OF BAR;

is encoded as

(TYPEDEF (IDENT "Foo")    ; alias type identifier
  (ALIAS (IDENT "Bar"))   ; base type identifier

API Call

An alias type node is created using the AST API call

ast_node = m2c_ast_new_node(AST_ALIAS, base_type_node, NULL);

Subrange Type Constructor

A subrange type node encodes the type constructor for a subrange type.

Encoding

TYPE DecDigit = [0 .. 9] OF CARDINAL;

is encoded as

(TYPEDEF (IDENT "DecDigit")           ; subrange type identifier
  (SUBR
    (IDENT "CARDINAL")                ; base type identifier
    (RANGE (INTVAL 0) (INTVAL 9))))   ; value range

API Call

A subrange type node is created using the AST API call

ast_node = m2c_ast_new_node(AST_SUBR,
  base_type_node, range_node, NULL);

Value Range

TO DO

Enumeration Type Constructor

An enumeration type node encodes the type constructor for an enumeration type.

Encoding

TYPE BaseColour = ( Red, Green, Blue );
TYPE MoreColour = ( +BaseColour, Orange, Yellow, Purple ); 

is encoded as

(TYPEDEF (IDENT "BaseColour")               ; enumeration type identifier
  (EMPTY)                                   ; no base type
  (IDENTLIST "Red" "Green" "Blue"))         ; enumerated value identifiers

(TYPEDEF (IDENT "MoreColour")               ; enumeration type identifier
  (IDENT "BaseColour")                      ; base type identifier
  (IDENTLIST "Orange" "Yellow" "Purple"))   ; enumerated value identifiers

API Call

An enumeration type node is created using the AST API call

ast_node = m2c_ast_new_node(AST_ENUM,
  base_type_node, value_list_node, NULL);

SET Type Constructor

A set type node encodes the type constructor for a set type.

Encoding

TYPE ColourSet = SET OF BaseColour;

is encoded as

(TYPEDEF (IDENT "ColourSet")
  (SET (IDENT "BaseColour"))

API Call

A set type node is created using the AST API call

ast_node = m2c_ast_new_node(AST_SET, base_type_node, NULL);

ARRAY Type Constructor

An array type node encodes the type constructor for an array type.

Encoding

TYPE Str80 = ARRAY 80 OF CHAR;

is encoded as

(TYPEDEF (IDENT "Str80")
  (ARRAY (IDENT "CHAR") (INTVAL 80))

API Call

An array type node is created using the AST API call

ast_node = m2c_ast_new_node(AST_ARRAY,
  base_type_node, capacity_node, NULL);

RECORD Type Constructor

A record type node encodes the type constructor for a record type.

Encoding

TYPE Foo = RECORD
  bar : Bar;
  baz : Baz
END;

is encoded as

(TYPEDEF (IDENT "Foo")
  (RECORD
    (EMPTY)                                     ; no base type
    (FIELD-LIST-SEQ
      (VARDEF (IDENT "bar") (IDENT "Bar")       ; field list definition
      (VARDEF (IDENT "baz") (IDENT "Baz")))))   ; field list definition

and

TYPE Root = RECORD (NIL)
  foo : Foo;
END;

TYPE Extension = RECORD (Root)
  bar : Bar;
  baz, bam : Baz
END;

is encoded as

(TYPEDEF (IDENT "Root")
  (RECORD
    (IDENT "NIL")                                         ; root type indicator
    (FIELD-LIST-SEQ
      (VARDEF (IDENT "foo") (IDENT "Foo"))))              ; field list definition

and

(TYPEDEF (IDENT "Extension")
  (RECORD
    (IDENT "Root")                                        ; base type identifier
    (FIELD-LIST-SEQ
      (VARDEF (IDENT "bar") (IDENT "Bar")                 ; field list definition
      (VARDEF (IDENTLIST "baz" "bam") (IDENT "Baz")))))   ; field list definition

API Call

A record type node is created using the AST API call

ast_node = m2c_ast_new_node(AST_RECORD,
  base_type_node, field_list_seq_node, NULL);

Field List Sequence

A field list sequence node encodes one or more field list definitions within a record type.

API Call

A field list sequence node is created using the AST API call

ast_node = m2c_ast_new_list_node(AST_FIELDLISTSEQ, field_list_seq);

where field_list_seq is a FIFO API created object of type fifo_t and the list elements are AST nodes of type m2c_astnode_t.

Field List

A variable definition node is used to encode a field list definition within a field list sequence node.

See Variable Definition for details.

POINTER Type Constructor

A pointer type node encodes the type constructor for a pointer type.

Encoding

TYPE Foo = POINTER TO Bar;

is encoded as

(TYPEDEF (IDENT "Foo")
  (POINTER (IDENT "Bar"))

API Call

A pointer type node is created using the AST API call

ast_node = m2c_ast_new_node(AST_POINTER, target_type_node, NULL);

OPAQUE Type Constructor

A type declaration node encodes the type constructor for an opaque type.

See Type Declaration for details.

PROCEDURE Type Constructor

A procedure type node encodes the type constructor for a procedure type.

Encoding

TYPE Foo = PROCEDURE ( ...  ) : Bam ;

is encoded as

(TYPEDEF (IDENT "Foo")
  (PROC-TYPE ( ... ) (IDENT "Bam"))

API Call

A procedure type node is created using the AST API call

ast_node = m2c_ast_new_node(AST_PROCTYPE,
  formal_type_list_node, ret_type_node, NULL);

Statements

Statement Sequence

A statement sequence node encodes one or more statements.

Encoding

foo := 0;
bar := 1;
counter++;

is encoded as

(STMTSEQ
  (ASSIGN (IDENT "foo") (INTVAL 0))
  (ASSIGN (IDENT "bar") (INTVAL 1))
  (INCR (IDENT "counter")))

API Call

A statement sequence node is created using the AST API call

ast_node = m2c_ast_new_list_node(AST_STMTSEQ, stmt_list);

where stmt_list is a FIFO API created object of type fifo_t and the list elements are AST nodes of type m2c_astnode_t.

Assignment Statement

An assignment statement node encodes an assignment statement.

Encoding

foo := bar;

is encoded as

(ASSIGN (IDENT "foo") (IDENT "bar"))

API Call

An assignment statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_ASSIGN,
  target_designator_node, expr_node, NULL);

Increment Statement

An increment statement node encodes an increment statement.

Encoding

counter++;

is encoded as

(INCR (IDENT "counter"))

API Call

An increment statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_INCR, designator_node, NULL);

Decrement Statement

A decrement statement node encodes a decrement statement.

Encoding

counter--;

is encoded as

(DECR (IDENT "counter"))

API Call

A decrement statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_DECR, designator_node, NULL);

Procedure Call

A procedure call node encodes a procedure call.

Encoding

Foo;

is encoded as

(PCALL (IDENT "Foo") (EMPTY))

and

Bar(baz);

is encoded as

(PCALL (IDENT "Bar") (IDENT "baz"))

API Call

A procedure call node is created using the AST API call

ast_node = m2c_ast_new_node(AST_PCALL,
  designator_node, arguments_node, NULL);

RETURN Statement

A return statement node encodes a return statement.

Encoding

RETURN foo;

is encoded as

(RETURN (IDENT "foo"))

API Call

A return statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_RETURN, expr_node, NULL);

NEW Statement

A new statement node encodes a new statement.

Encoding

NEW foo;

is encoded as

(NEW (IDENT "foo"))

API Call

A new statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_NEW, designator_node, NULL);

NEW ARGLIST Statement

A new arglist statement node encodes a new statement with initialisation arguments.

Encoding

NEW foo := { bar, baz, bam };

is encoded as

(NEWARG (IDENT "foo") (STRUCT "bar" "baz" "bam"))

API Call

A new arglist statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_NEWARG,
  designator_node, designator_or_expr_node, NULL);

NEW CAPACITY Statement

A new capacity statement node encodes a new statement with capacity argument.

Encoding

NEW foo CAPACITY 1000;

is encoded as

(NEWCAP (IDENT "foo") (INTVAL 1000))

API Call

A new capacity statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_NEWCAP,
  designator_node, expr_node, NULL);

RETAIN Statement

A retain statement node encodes a retain statement.

Encoding

RETAIN foo;

is encoded as

(RETAIN (IDENT "foo"))

API Call

A retain statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_RETAIN, designator_node, NULL);

RELEASE Statement

A release statement node encodes a release statement.

Encoding

RELEASE foo;

is encoded as

(RELEASE (IDENT "foo"))

API Call

A release statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_RELEASE, designator_node, NULL);

IF Statement

An if statement node encodes an if statement.

Encoding

IF n > 0 THEN
  n := 1
END;

is encoded as

(IF
  (GT (IDENT "n") (INTVAL 0))        ; condition expression node
  (ASSIGN (IDENT "n") (INTVAL 1))    ; if branch statement node
  (EMPTY)                            ; no elsif branches
  (EMPTY))                           ; no else branch

and

IF n > 0 THEN
  n := 1
ELSE
  n := 0
END;

is encoded as

(IF
  (GT (IDENT "n") (INTVAL 0))        ; condition expression node
  (ASSIGN (IDENT "n") (INTVAL 1))    ; if branch statement node
  (EMPTY)                            ; no elsif branches
  (ASSIGN (IDENT "n") (INTVAL 0)))   ; else branch statement node

API Call

An if statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_IF,
  expr_node, stmt_seq_node, elsif_seq_node, else_stmt_seq_node, NULL);

ELSIF Sequence

An elsif sequence node encodes one or more elsif branch nodes.

Encoding

IF n > 999 THEN
  exp := 3
ELSIF n > 99 THEN
  exp := 2
ELSIF n > 9 THEN
  exp := 1
ELSE
  exp := 0
END;

is encoded as

(IF (GT (IDENT "n") (INTVAL 999))          ; if branch
  (ASSIGN (IDENT "exp") (INTVAL 3))
  (ELSIFSEQ
    (ELSIF (GT (IDENT "n") (INTVAL 99))    ; first elsif branch
      (ASSIGN (IDENT "exp") (INTVAL 2))   
    (ELSIF (GT (IDENT "n") (INTVAL 9))     ; second elsif branch
      (ASSIGN (IDENT "exp") (INTVAL 1)))   
  (ASSIGN (IDENT "exp") (INTVAL 0)))       ; else branch

API Call

An elsif sequence node is created using the AST API call

ast_node = m2c_ast_new_list_node(AST_ELSIFSEQ, elsif_node_list);

where elsif_node_list is a FIFO API created object of type fifo_t and the list elements are AST nodes of type m2c_astnode_t.

ELSIF Branch

An elsif branch node encodes an elsif branch.

Encoding

IF n > 0 THEN
  n := 1
ELSIF n < 0 THEN
  n := -1
END;

is encoded as

(IF (GT (IDENT "n") (INTVAL 0))              ; if branch
  (ASSIGN (IDENT "n") (INTVAL 1))
  (ELSIF (LT (IDENT "n") (INTVAL 0))         ; elsif branch
    (ASSIGN (IDENT "n") (NEG (INTVAL 1))))
  (EMPTY))                                   ; no else branch

API Call

An elsif statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_ELSIF,
  expr_node, stmt_seq_node, NULL);

CASE Statement

A case statement node encodes a case statement.

TO DO

LOOP Statement

A loop statement node encodes a loop statement.

Encoding

LOOP
  Foo(bar)
END;

is encoded as

(LOOP (PCALL (IDENT "Foo") (IDENT "bar")))

API Call

A loop statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_LOOP, stmt_seq_node, NULL);

WHILE Statement

A while statement node encodes a while statement.

Encoding

WHILE n < 100 DO
  Foo(n);
  n++
END;

is encoded as

(WHILE (LT (IDENT "n") (INTVAL 100))
  (STMTSEQ
    (PCALL (IDENT "Foo") (IDENT "n"))
    (INCR (IDENT "n")))

API Call

A while statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_WHILE,
  expr_node, stmt_seq_node, NULL);

REPEAT Statement

A repeat statement node encodes a repeat statement.

Encoding

REPEAT
  Foo(n);
  n++
UNTIL n = 100;

is encoded as

(REPEAT
  (STMTSEQ
    (PCALL (IDENT "Foo") (IDENT "n"))
    (INCR (IDENT "n"))
  (LT (IDENT "n") (INTVAL 100)))

API Call

A repeat statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_REPEAT,
  stmt_seq_node, expr_node, NULL);

FOR Statement

A for statement node encodes a for statement.

TO DO

EXIT Statement

An exit statement node encodes an exit statement.

Encoding

EXIT;

is encoded as

(EXIT)

API Call

An exit statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_EXIT, NULL);

READ Statement

A read statement node encodes a read statement.

Encoding

READ foo;

is encoded as

(READ (IDENT "STDIN") (IDENT "foo"))

and

READ @file foo;

is encoded as

(READ (IDENT "file") (IDENT "foo"))

API Call

A read statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_READ,
  chan_node, designator_node, NULL);

READ NEW Statement

A read new statement node encodes a read statement with a NEW prefixed designator.

Encoding

READ NEW fooPtr;

is encoded as

(READNEW (IDENT "STDIN") (IDENT "fooPtr"))

and

READ @file NEW fooPtr;

is encoded as

(READNEW (IDENT "file") (IDENT "fooPtr"))

API Call

A read new statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_READNEW,
  chan_node, designator_node, NULL);

WRITE Statement

A write statement node encodes a write statement.

Encoding

WRITE foo;

is encoded as

(WRITE (IDENT "STDOUT") (IDENT "foo"))

and

WRITE @file foo;

is encoded as

(WRITE (IDENT "file") (IDENT "foo"))

API Call

A write statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_WRITE,
  chan_node, designator_node, NULL);

Formatted WRITE Statement

A formatted write statement node encodes a write statement with a formatted argument.

Encoding

WRITE #("5;2", amount);

is encoded as

(WRITEF (IDENT "STDOUT") (QUOTEDVAL "5;2") (IDENT "amount"))

and

WRITE @file #("5;2", amount);

is encoded as

(WRITEF (IDENT "file") (QUOTEDVAL "5;2") (IDENT "amount"))

API Call

A formatted write statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_WRITEF,
  chan_node, format_node, designator_node, NULL);

NOP Statement

A nop statement node encodes an empty statement.

Encoding

NOP;

is encoded as

(NOP)

API Call

A nop statement node is created using the AST API call

ast_node = m2c_ast_new_node(AST_NOP, NULL);

Expressions

Equality

An equality expression node encodes an equality expression.

Encoding

foo = 0

is encoded as

(EQ (IDENT "foo") (INTVAL 0))

API Call

An equality expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_EQ,
  left_sub_expr_node, right_sub_expr_node, NULL);

Inequality

An inequality expression node encodes an inequality expression.

Encoding

foo # 0

is encoded as

(NEQ (IDENT "foo") (INTVAL 0))

API Call

An inequality expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_NEQ,
  left_sub_expr_node, right_sub_expr_node, NULL);

Less-Than

A less-than expression node encodes a less-than expression.

Encoding

foo < 0

is encoded as

(LT (IDENT "foo") (INTVAL 0))

API Call

A less-than expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_LT,
  left_sub_expr_node, right_sub_expr_node, NULL);

Less-Than-Or-Equal

A less-than-or-equal expression node encodes a less-than-or-equal expression.

Encoding

foo <= 0

is encoded as

(LTEQ (IDENT "foo") (INTVAL 0))

API Call

A less-than-or-equal expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_LTEQ,
  left_sub_expr_node, right_sub_expr_node, NULL);

Greater-Than

A greater-than expression node encodes a greater-than expression.

Encoding

foo > 0

is encoded as

(GT (IDENT "foo") (INTVAL 0))

API Call

A greater-than expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_GT,
  left_sub_expr_node, right_sub_expr_node, NULL);

Greater-Than-Or-Equal

A greater-than-or-equal expression node encodes a greater-than-or-equal expression.

Encoding

foo >= 0

is encoded as

(GTEQ (IDENT "foo") (INTVAL 0))

API Call

A greater-than-or-equal expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_GTEQ,
  left_sub_expr_node, right_sub_expr_node, NULL);

Identity

An identity expression node encodes an identity expression.

Encoding

foo == bar

is encoded as

(IDTY (IDENT "foo") (IDENT "bar"))

API Call

An identity expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_IDTY,
  left_sub_expr_node, right_sub_expr_node, NULL);

IN Expression

An in expression node encodes a membership test expression.

Encoding

elem IN set

is encoded as

(IN (IDENT "elem") (IDENT "set"))

API Call

An in expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_IN,
  left_sub_expr_node, right_sub_expr_node, NULL);

Plus Expression

A plus expression node encodes an arithmetic addition or set union expression.

Encoding

foo + 5

is encoded as

(+ (IDENT "foo") (INTVAL 5))

API Call

A plus expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_PLUS,
  left_sub_expr_node, right_sub_expr_node, NULL);

Minus Expression

A minus expression node encodes an arithmetic subtraction expression.

Encoding

foo - 5

is encoded as

(- (IDENT "foo") (INTVAL 5))

API Call

A minus expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_MINUS,
  left_sub_expr_node, right_sub_expr_node, NULL);

Unary Minus Expression

A unary minus expression node encodes an arithmetic negation expression.

Encoding

-1

is encoded as

(NEG (INTVAL 1))

API Call

A unary minus expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_NEG, sub_expr_node, NULL);

Concatenation Expression

A concatenation expression node encodes a concatenation expression.

Encoding

str & "."

is encoded as

(CONCAT (IDENT "str") (QUTEDVAL "."))

API Call

A concatenation expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_CONCAT,
  left_sub_expr_node, right_sub_expr_node, NULL);

Set Difference Expression

A set difference expression node encodes a set difference expression.

Encoding

setA \ setB

is encoded as

(SETDIFF (IDENT "setA") (IDENT "setB"))

API Call

A set difference expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_SETDIFF,
  left_sub_expr_node, right_sub_expr_node, NULL);

OR Expression

An or expression node encodes a logical disjunction expression.

Encoding

foo OR bar

is encoded as

(OR (IDENT "foo") (IDENT "bar"))

API Call

An or expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_OR,
  left_sub_expr_node, right_sub_expr_node, NULL);

Asterisk Expression

An asterisk expression node encodes an arithmetic multiplication or set intersection expression.

Encoding

foo * bar

is encoded as

(* (IDENT "foo") (IDENT "bar"))

API Call

An asterisk expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_STAR,
  left_sub_expr_node, right_sub_expr_node, NULL);

Solidus Expression

A solidus expression node encodes an arithmetic real number division or symmetric set difference expression.

Encoding

foo / bar

is encoded as

(/ (IDENT "foo") (IDENT "bar"))

API Call

A solidus expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_SLASH,
  left_sub_expr_node, right_sub_expr_node, NULL);

DIV Expression

A div expression node encodes an arithmetic integer division expression.

Encoding

foo DIV bar

is encoded as

(DIV (IDENT "foo") (IDENT "bar"))

API Call

A div expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_DIV,
  left_sub_expr_node, right_sub_expr_node, NULL);

MOD Expression

A mod expression node encodes an arithmetic modulus expression.

Encoding

foo MOD bar

is encoded as

(MOD (IDENT "foo") (IDENT "bar"))

API Call

A mod expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_MOD,
  left_sub_expr_node, right_sub_expr_node, NULL);

AND Expression

An and expression node encodes a logical conjunction expression.

Encoding

foo AND bar

is encoded as

(AND (IDENT "foo") (IDENT "bar"))

API Call

An and expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_AND,
  left_sub_expr_node, right_sub_expr_node, NULL);

NOT Expression

A not expression node encodes a logical negation expression.

Encoding

NOT foo

is encoded as

(NOT (IDENT "foo"))

API Call

A not expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_NOT, sub_expr_node, NULL);

Type Conversion Expression

A type conversion expression node encodes a type conversion expression.

Encoding

i :: REAL

is encoded as

(TYPECONV (IDENT "i") (IDENT "REAL"))

API Call

A type conversion expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_TYPECONV,
  sub_expr_node, target_type_node, NULL);

Subscript Expression

A subscript expression node encodes a subscript expression.

Encoding

foo[bar]

is encoded as

(SUBSCR (IDENT "foo") (IDENT "bar"))

API Call

A subscript expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_SUBSCR,
  designator_node, subscript_node, NULL);

Dereference Expression

A dereference expression node encodes a dereference expression.

Encoding

foo^

is encoded as

(DEREF (IDENT "foo"))

API Call

A dereference expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_DEREF, designator_node, NULL);

Field Selection Expression

A field selection expression node encodes a field selection expression.

Encoding

foo.bar

is encoded as

(SELECT (IDENT "foo") (IDENT "bar"))

API Call

A subscript expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_SUBSCR,
  designator_node, field_designator_node, NULL);

Function Call Expression

A function call expression node encodes a function call expression.

Encoding

foo(bar, 10)

is encoded as

(FCALL (IDENT "foo") (EXPRLIST (IDENT "bar") (INTVAL 10)))

API Call

A function call expression node is created using the AST API call

ast_node = m2c_ast_new_node(AST_FCALL,
  designator_node, expr_list_node, NULL);

Insert Designator

An insert designator node encodes an insert designator.

Encoding

foo[..n]

is encoded as

(INSERT (IDENT "foo") (IDENT "n"))

API Call

An insert designator node is created using the AST API call

ast_node = m2c_ast_new_node(AST_INSERT,
  designator_node, index_designator_node, NULL);

Slice Designator

A slice designator node encodes a slice designator.

Encoding

foo[n..m]

is encoded as

(SLICE (IDENT "foo") (RANGE (IDENT "n") (IDENT "m")))

API Call

A slice designator node is created using the AST API call

ast_node = m2c_ast_new_node(AST_SLICE,
  designator_node, range_node, NULL);

Identifier

An identifier node encodes an identifier.

Encoding

foobar

is encoded as

(IDENT "foobar")

API Call

An identifier node is created using the AST API call

ast_node = m2c_ast_new_terminal_node(AST_IDENT, lexeme);

where lexeme is an interned string object of type intstr_t.

Qualified Identifier

A qualified identifier node encodes the components of a qualified identifier.

Encoding

FooLib.barbaz

is encoded as

(QUALIDENT "FooLib" "barbaz")

API Call

A qualified identifier node is created using the AST API call

ast_node = m2c_ast_new_terminal_list_node(AST_QUALIDENT, lexeme1, ... lexemeN, NULL);

where the components lexeme1 ... lexemeN are interned string objects of type intstr_t.

Identifier List

An identifier list node encodes a list of identifiers.

Encoding

foo, bar, baz

is encoded as

(IDENTLIST "foo" "bar" "baz")

API Call

An identifier list node is created using the AST API call

ast_node = m2c_ast_new_terminal_list_node(AST_IDENTLIST, lexeme1, ... lexemeN, NULL);

where the components lexeme1 ... lexemeN are interned string objects of type intstr_t.

Integer Value

An integer value node encodes a whole number value.

Encoding

42

is encoded as

(INTVAL 42)

API Call

An integer value node is created using the AST API call

ast_node = m2c_ast_new_terminal_node(AST_INTVAL, lexeme);

where lexeme is an interned string object of type intstr_t.

Real Value

A real value node encodes a real number value.

Encoding

1.23

is encoded as

(REALVAL 1.23)

API Call

A real value node is created using the AST API call

ast_node = m2c_ast_new_terminal_node(AST_REALVAL, lexeme);

where lexeme is an interned string object of type intstr_t.

Character Value

A character value node encodes a character code point value.

Encoding

0u20

is encoded as

(CHARVAL 0u20)

API Call

A character value node is created using the AST API call

ast_node = m2c_ast_new_terminal_node(AST_CHRVAL, lexeme);

where lexeme is an interned string object of type intstr_t.

Quoted Value

A quoted value node encodes a single- or double quoted character or character string.

Encoding

"The quick brown fox"

is encoded as

(QUOTEDVAL "The quick brown fox")

API Call

A quoted value node is created using the AST API call

ast_node = m2c_ast_new_terminal_node(AST_QUOTEDVAL, lexeme);

where lexeme is an interned string object of type intstr_t.

Clone this wiki locally