-
Notifications
You must be signed in to change notification settings - Fork 2
Abstract Syntax Tree
Benjamin Kowarsch edited this page Sep 25, 2020
·
46 revisions
compilationUnit :=
'(' COMPUNIT filenameNode timestampNode digestNode moduleNode ')'
;
(COMPUNIT
(FILENAME ...)
(COMPILED ...)
(DIGEST ...)
(DEFMOD ...))
filenameNode :=
'(' FILENAME quotedLiteral ')'
;
(FILENAME "Foobar.def")
timestampNode :=
'(' COMPILED quotedLiteral ')'
;
(COMPILED "202009242016000+900")
digestNode :=
'(' DIGEST quotedLiteral ')'
;
(DIGEST "9E107D9D372BB6826BD81D3542A419D6")
moduleNode :=
defModNode | impModNode | progNode
;
defModNode :=
'(' DEFMOD idNode importNode* reExportNode* declarationNode* ')'
;
(DEFMOD (ID "Foobar")
(IMPORT ...)
(REEXPORT ...)
(CONST ...)
(VAR ...)
(TYPE ...)
(PROC ...)
(BIND ...))
idNode :=
'(' ID quotedLiteral ')'
;
(ID "Foobar")
importNode :=
'(' IMPORT quotedLiteral+ ')'
;
IMPORT Foo, Bar, Baz;
(IMPORT "Foo" "Bar" "Baz")
reExportNode :=
'(' REEXPORT quotedLiteral+ ')'
;
IMPORT Foo+, Bar+;
(IMPORT "Foo" "Bar")
(REEXPORT "Foo" "Bar")
declarationNode :=
constDeclNode | varDeclNode | typeDeclNode | procDeclNode
;
constDeclNode :=
'(' CONST idNode exprNode typeIdNode? ')'
;
alias typeIdNode = idNode ;
CONST Foo = 42;
CONST Bar : INTEGER = -1;
(CONST (ID "Foo") (EXPR (NUM 42)))
(CONST (ID "Bar") (EXPR (NEG (NUM 1))) (ID "INTEGER"))
varDeclNode :=
'(' VAR idListNode typeIdNode ')'
;
VAR foo : Foo;
VAR i, j : INTEGER;
(VAR (ID "foo") (ID "Foo"))
(VAR (ID "i" "j") (ID "INTEGER"))
idListNode :=
'(' ID quotedLiteral+ ')'
;
(ID "foo" "bar" "baz")
typeDeclNode :=
'(' TYPE idNode typeConstructorNode ')'
;
typeConstructorNode :=
aliasTypeNode | derivedTypeNode | subrangeTypeNode |enumTypeNode |
setTypeNode | arrayTypeNode | recordTypeNode | pointerTypeNode |
opaqueTypeNode | procTypeNode
;
(TYPE (ID "TypeIdent") ...)
aliasTypeNode :=
'(' ALIAS baseTypeNode ')'
;
alias baseTypeNode = typeIdNode ;
TYPE Foo = ALIAS OF Bar;
(TYPE (ID "Foo") (ALIAS (ID "Bar)))
alias derivedTypeNode = typeIdNode ;
TYPE Foo = Bar;
(TYPE (ID "Foo") (ID "Bar))
subrangeTypeNode :=
'(' SUBR baseTypeNode lowerBound upperBound ')'
;
alias lowerBound, upperBound = exprNode ;
TYPE Hour = [0..23] OF INTEGER;
(TYPE (ID "Hour") (ID "INTEGER") (SUBR (EXPR (NUM 0))(EXPR (NUM 23))))
enumTypeNode :=
'(' ENUM valueListNode baseTypeNode? ')'
;
alias valueListNode = idListNode ;
TYPE Colour = ( Red, Green, Blue );
TYPE MoreColour = ( +Colour, Orange, Magenta );
(TYPE (ID "Colour") (ENUM (ID "Red" "Green" "Blue")))
(TYPE (ID "MoreColour") (ENUM (ID "Orange" "Magenta") (ID "Colour")))
setTypeNode :=
'(' SET enumTypeIdNode ')'
;
alias enumTypeIdNode = typeIdNode ;
TYPE ColourSet = SET OF Colour;
(TYPE (ID "ColourSet") (SET (ID "Colour")))
arrayTypeNode :=
'(' ARRAY capacity baseTypeNode ')'
;
alias capacity = exprNode ;
TYPE Str80 = ARRAY 80 OF CHAR;
(TYPE (ID "Str80") (ARRAY (EXPR (NUM 80)) (ID "Colour")))
recordTypeNode :=
'(' RECORD baseTypeNode fieldListNode+ ')'
;
TYPE Point = RECORD ( NIL )
x, y : REAL
END; (*RECORD*)
TYPE ColourPoint = RECORD ( Point )
colour : Colour
END; (*RECORD*)
(TYPE (ID "Point")
(RECORD (ID "NIL")
(FIELD (ID "x" "y") (ID "REAL"))))
(TYPE (ID "ColourPoint")
(RECORD (ID "Point")
(FIELD (ID "colour") (ID "Colour"))))
fieldListNode :=
'(' FIELD idListNode fieldTypeNode ')'
;
fieldTypeNode :=
typeIdNode | arrayTypeNode | pointerTypeNode | procTypeNode
;
pointerTypeNode :=
'(' POINTER targetTypeIdNode ')'
;
alias targetTypeIdNode = typeIdNode;
TYPE FooPtr = POINTER TO Foo;
(TYPE (ID "FooPtr") (POINTER (ID "Foo")))
opaqueTypeNode :=
'(' OPAQUE allocSize? ')'
;
alias allocSize = exprNode ;
TYPE String = OPAQUE POINTER;
TYPE HashKey = OPAQUE [KeyBits DIV 8];
(TYPE (ID "String") (OPAQUE))
(TYPE (ID "HashKey") (OPAQUE (EXPR (DIV (ID "KeyBits") (NUM 8)))))
procTypeNode :=
'(' PROCTYPE formalTypeList returnTypeNode ')'
;
formalTypeList :=
formalTypeNode+ | emptyNode
;
alias returnTypeNode = typeIdNode ;
TYPE BoolFunc = PROCEDURE () : BOOLEAN;
TYPE FooFunc = PROCEDURE ( Foo ) : INTEGER;
TYPE BarProc = PROCEDURE ( Bar; VAR Status );
TYPE WriteProc = PROCEDURE ( CONST ARRAY OF CHAR );
(TYPE (ID "BoolFunc") (PROCTYPE () (ID "BOOLEAN")))
(TYPE (ID "FooFunc") (PROCTYPE (FT (ID "Foo")) (ID "INTEGER")))
(TYPE (ID "BarProc") (PROCTYPE (FT (ID "Bar")) (FT (VARP) (ID "Status"))))
(TYPE (ID "WriteProc") (PROCTYPE (FT (CONSTP) (ARRAYP) (ID "CHAR"))))
formalTypeNode :=
'(' FT attrNode? idListNode+ structNode? typeIdNode ')'
;
attrNode :=
'(' ( CONSTP | VARP ) ')'
;
structNode :=
'(' ( ARRAYP | ARGLIST | CASTSEQ | CASTPTR ) ')'
;
procDeclNode :=
'(' PROC formalParamList returnTypeNode ')'
;
formalParamList :=
formalParamNode+ | emptyNode
;
PROCEDURE isFoo () : BOOLEAN;
PROCEDURE length ( str : String ) : LONGCARD;
PROCEDURE Open ( f : File; VAR s : Status );
PROCEDURE Write ( CONST s : ARRAY OF CHAR );
(PROC (ID "isFoo") () (ID "BOOLEAN"))
(PROC (ID "length") (FP (ID "str") (ID "String")) (ID "LONGCARD"))
(PROC (ID "Open") (FP (ID "f") (ID "File")) (FP (VARP) (ID "s") (ID "Status")))
(PROC (ID "Write") (FP (CONSTP) (ID "s") (ARRAYP) (ID "CHAR")))
formalParamNode :=
'(' FP attrNode idListNode+ structNode typeIdNode ')'
;
bindDeclNode :=
'( BIND idToBind targetToBindTo ')'
;
alias idToBind, targetToBindTo = idNode ;
CONST [TLIMIT] Capacity = 100;
PROCEDURE [LENGTH] length ( str : String ) : LONGCARD;
(CONST (ID "Capacity) (EXPR (NUM 100)))
(BIND (ID "Capacity) (ID "TLIMIT"))
(PROC (ID "length") (FP (ID "str") (ID "String")) (ID "LONGCARD"))
(BIND (ID "length) (ID "LENGTH"))
progNode :=
'(' PROG idNode importNode* declarationNode* blockNode ')'
;
(PROG (ID "Foobar")
(IMPORT ...)
(CONST ...)
(VAR ...)
(TYPE ...)
(PROC ...)
(BIND ...)
(UNQ ...)
(BLOCK ...))
impModNode :=
'(' IMPMOD idNode importNode* declarationNode* blockNode? ')'
;
(IMPMOD (ID "Foobar")
(IMPORT ...)
(CONST ...)
(VAR ...)
(TYPE ...)
(PROC ...)
(BIND ...)
(UNQ ...)
(BLOCK ...))
Copyright © 2015-2018 Modula-2 Software Foundation