tranintf.pas

Переключить прокрутку окна
Загрузить этот исходный код

{
    Этот исходный текст является частью Продвинутого векторного транслятора.

    Copyright © 2017 Малик Разработчик

    Это свободная программа: вы можете перераспространять её и/или
    изменять её на условиях Стандартной общественной лицензии GNU в том виде,
    в каком она была опубликована Фондом свободного программного обеспечения;
    либо версии 3 лицензии, либо (по вашему выбору) любой более поздней версии.

    Эта программа распространяется в надежде, что она может быть полезна,
    но БЕЗО ВСЯКИХ ГАРАНТИЙ; даже без неявной гарантии ТОВАРНОГО ВИДА
    или ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЁННЫХ ЦЕЛЕЙ. Подробнее см. в Стандартной
    общественной лицензии GNU.

    Вы должны были получить копию Стандартной общественной лицензии GNU
    вместе с этой программой. Если это не так, см.
    <http://www.gnu.org/licenses/>.
}

unit TranIntf;

{$MODE DELPHI,EXTENDEDSYNTAX ON}

interface

uses
    Lang, IOStream;

{$ASMMODE INTEL,CALLING REGISTER,INLINE ON,GOTO ON}
{$H+,I-,J-,M-,Q-,R-,T-}

type
    Pool = interface;
    PoolInt = interface;
    PoolLong = interface;
    PoolUltra = interface;
    PoolReal = interface;
    PoolXVector = interface;
    PoolString = interface;
    Lexer = interface;
    TypeDescriptor = interface;
    TypePrimitive = interface;
    TypeArray = interface;
    TypeFunction = interface;
    TypeStructure = interface;
    NamedObject = interface;
    NameAndType = interface;
    LocalVariable = interface;
    FunctionArgument = interface;
    StructureField = interface;
    NamespaceEntry = interface;
    GlobalVariable = interface;
    GlobalConstant = interface;
    GlobalException = interface;
    GlobalFunction = interface;
    Namespace = interface;
    Tree = interface;
    Node = interface;
    NodeEnumerator = interface;
    BuilderOfNamespaces = interface;
    BuilderOfTrees = interface;
    BuilderOfTargetCode = interface;
    UnicodeTextEncoder = interface;
    CompileError = class;

    Lexer_Array1d = array of Lexer;

    TypeDescriptor_Array1d = array of TypeDescriptor;

    FunctionArgument_Array1d = array of FunctionArgument;

    StructureField_Array1d = array of StructureField;

    NamespaceEntry_Array1d = array of NamespaceEntry;

    GlobalConstant_Array1d = array of GlobalConstant;

    Namespace_Array1d = array of Namespace;

    Node_Array1d = array of Node;

    Pool = interface(_Interface) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA60}']
        procedure clear();
        procedure delete(index: int);
        function getLength(): int;
    end;

    PoolInt = interface(Pool) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA61}']
        procedure setValue(index: int; const value: int);
        function getValue(index: int): int;
        function indexAcquire(const value: int): int;
        function indexOf(const value: int): int;
    end;

    PoolLong = interface(Pool) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA62}']
        procedure setValue(index: int; const value: int64);
        function getValue(index: int): int64;
        function indexAcquire(const value: int64): int;
        function indexOf(const value: int64): int;
    end;

    PoolUltra = interface(Pool) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA63}']
        procedure setValue(index: int; const value: ultra);
        function getValue(index: int): ultra;
        function indexAcquire(const value: ultra): int;
        function indexOf(const value: ultra): int;
    end;

    PoolReal = interface(Pool) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA64}']
        procedure setValue(index: int; const value: real);
        function getValue(index: int): real;
        function indexAcquire(const value: real): int;
        function indexOf(const value: real): int;
    end;

    PoolXVector = interface(Pool) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA65}']
        procedure setValue(index: int; const value: xvector);
        function getValue(index: int): xvector;
        function indexAcquire(const value: xvector): int;
        function indexOf(const value: xvector): int;
    end;

    PoolString = interface(Pool) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA66}']
        procedure setValue(index: int; const value: UnicodeString);
        function getValue(index: int): UnicodeString;
        function indexAcquire(const value: UnicodeString): int;
        function indexOf(const value: UnicodeString): int;
    end;

    Lexer = interface(_Interface) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA6F}']
        procedure clear();
        procedure trimToSize();
        procedure append(lexType, lexValue, lexLine, lexChar: int);
        procedure delete(index: int);
        procedure insert(index, lexType, lexValue: int);
        procedure setData(index, lexType, lexValue: int);
        procedure split(placeComments: boolean; const sourceCode: UnicodeString);
        function getLength(): int;
        function getLinesCount(): int;
        function getLine(index: int): int;
        function getChar(index: int): int;
        function getType(index: int): int;
        function getValue(index: int): int;
        function getValueInt(index: int): int;
        function getValueLong(index: int): int64;
        function getValueReal(index: int): real;
        function getValueAString(index: int): AnsiString;
        function getValueUString(index: int): UnicodeString;
        function getPoolOfInt(): PoolInt;
        function getPoolOfLong(): PoolLong;
        function getPoolOfReal(): PoolReal;
        function getPoolOfString(): PoolString;
        function getSourceName(): UnicodeString;
        function getSourceCode(): UnicodeString;
        function getSourceLine(index: int): UnicodeString;
        function getSourceFragment(beginLine, beginChar, endLine, endChar: int): UnicodeString;
        function lexemeToString(index: int): UnicodeString;
        property poolOfInt: PoolInt read getPoolOfInt;
        property poolOfLong: PoolLong read getPoolOfLong;
        property poolOfReal: PoolReal read getPoolOfReal;
        property poolOfString: PoolString read getPoolOfString;
        property sourceName: UnicodeString read getSourceName;
        property sourceCode: UnicodeString read getSourceCode;
    end;

    TypeDescriptor = interface(_Interface) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA70}']
        function possibleCastTo(typeForTest: TypeDescriptor): boolean;
        function possibleAssignTo(typeForTest: TypeDescriptor): boolean;
        function isMatchForConstant(): boolean;
        function isMatchForVariable(): boolean;
        function isMatchForReturnType(): boolean;
        function getAlignedSize(operandSize: int): int;
        function getKind(): int;
        function getSize(): int;
        property kind: int read getKind;
        property size: int read getSize;
    end;

    TypePrimitive = interface(TypeDescriptor) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA71}']
        function possibleAssignLong(value: long): boolean;
    end;

    TypeArray = interface(TypeDescriptor) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA72}']
        function getDimensions(): int;
        function getCellType(): TypeDescriptor;
        function getElementType(): TypeDescriptor;
        property dimensions: int read getDimensions;
        property cellType: TypeDescriptor read getCellType;
        property elementType: TypeDescriptor read getElementType;
    end;

    TypeFunction = interface(TypeDescriptor) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA73}']
        function getArgumentsSize(): int;
        function getArgumentsCount(): int;
        function getReturnType(): TypeDescriptor;
        function getArgument(index: int): FunctionArgument;
        function findArgument(const argName: UnicodeString): FunctionArgument;
        function addArgument(argType: TypeDescriptor;
                const argName: UnicodeString): FunctionArgument;
        property argumentsSize: int read getArgumentsSize;
    end;

    TypeStructure = interface(TypeDescriptor) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA74}']
        procedure clear();
        procedure updateSize();
        function isInheritedFrom(ancestorForTest: TypeStructure): boolean;
        function getStructureSize(): int;
        function getFieldsCount(): int;
        function getAncestor(): TypeStructure;
        function getField(index: int): StructureField;
        function findField(const fldName: UnicodeString; findInAncestors: boolean): StructureField;
        function addField(fldPublic: boolean; fldType: TypeDescriptor;
                const fldName: UnicodeString): StructureField;
        property structureSize: int read getStructureSize;
        property ancestor: TypeStructure read getAncestor;
    end;

    NamedObject = interface(_Interface) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA78}']
        procedure setDeclarePosition(declarePosition: int);
        procedure setStartPosition(startPosition: int);
        function getDeclarePosition(): int;
        function getStartPosition(): int;
        function getName(): UnicodeString;
        property declarePosition: int read getDeclarePosition write setDeclarePosition;
        property startPosition: int read getStartPosition write setStartPosition;
        property name: UnicodeString read getName;
    end;

    NameAndType = interface(NamedObject) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA79}']
        function getDataType(): TypeDescriptor;
        property dataType: TypeDescriptor read getDataType;
    end;

    LocalVariable = interface(NameAndType) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA7A}']
    end;

    FunctionArgument = interface(LocalVariable) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA7B}']
    end;

    StructureField = interface(NameAndType) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA7C}']
        function isPublic(): boolean;
        function getOffset(): int;
        function getOwner(): TypeStructure;
        property publicObject: boolean read isPublic;
        property offset: int read getOffset;
        property owner: TypeStructure read getOwner;
    end;

    NamespaceEntry = interface(NamedObject) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA80}']
        function isPublic(): boolean;
        function getOwner(): Namespace;
        property publicObject: boolean read isPublic;
        property owner: Namespace read getOwner;
    end;

    GlobalVariable = interface(NamespaceEntry) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA81}']
        function getDataType(): TypeDescriptor;
        property dataType: TypeDescriptor read getDataType;
    end;

    GlobalConstant = interface(GlobalVariable) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA82}']
        function getBooleanValue(): boolean;
        function getCharValue(): int;
        function getByteValue(): int;
        function getShortValue(): int;
        function getIntValue(): int;
        function getLongValue(): int64;
        function getUltraValue(): ultra;
        function getRealValue(): real;
        function getXVectorValue(): xvector;
        property booleanValue: boolean read getBooleanValue;
        property charValue: int read getCharValue;
        property byteValue: int read getByteValue;
        property shortValue: int read getShortValue;
        property intValue: int read getIntValue;
        property longValue: int64 read getLongValue;
        property realValue: real read getRealValue;
    end;

    GlobalException = interface(NamespaceEntry) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA83}']
        function isInheritedFrom(exceptionForTest: GlobalException): boolean;
        function getAncestor(): GlobalException;
        property ancestor: GlobalException read getAncestor;
    end;

    GlobalFunction = interface(NamespaceEntry) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA84}']
        function isInterrupt(): boolean;
        function isAssembler(): boolean;
        function isPureAssembler(): boolean;
        function getFunctionType(): TypeFunction;
        property functionType: TypeFunction read getFunctionType;
    end;

    Namespace = interface(NamedObject) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA8F}']
        procedure clear();
        procedure addImport(import: Namespace);
        function isImported(const namespaceName: UnicodeString): boolean;
        function isVisible(entry: NamespaceEntry): boolean; overload;
        function isVisible(field: StructureField): boolean; overload;
        function getImportsCount(): int;
        function getEntriesCount(): int;
        function getImport(index: int): Namespace;
        function getEntry(index: int): NamespaceEntry;
        function getLexemes(): Lexer;
        function addVariable(entryPublic: boolean; const entryName: UnicodeString;
                varType: TypeDescriptor): GlobalVariable;
        function addConstant(entryPublic: boolean; const entryName: UnicodeString;
                constType: TypeDescriptor; const constValue: real): GlobalConstant;
        function addConstantUltra(entryPublic: boolean; const entryName: UnicodeString;
                constType: TypeDescriptor; const constValue: ultra): GlobalConstant;
        function addConstantXVector(entryPublic: boolean; const entryName: UnicodeString;
                constType: TypeDescriptor; const constValue: xvector): GlobalConstant;
        function addException(entryPublic: boolean; const entryName: UnicodeString;
                ancestorException: GlobalException): GlobalException;
        function addStructure(entryPublic: boolean; const entryName: UnicodeString;
                ancestorStructure: TypeStructure): TypeStructure;
        function addStructureExt(entryPublic: boolean; const entryName: UnicodeString;
                ancestorStructure: TypeStructure): NamespaceEntry;
        function addFunction(entryPublic: boolean; const entryName: UnicodeString;
                codeType: int; returnType: TypeDescriptor): GlobalFunction;
        function findEntry(const entryName: UnicodeString; findInImports: boolean): NamespaceEntry;
        property lexemes: Lexer read getLexemes;
    end;

    Tree = interface(_Interface) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA90}']
        procedure deleteChildrens(parent: Node);
        procedure deleteNode(sibling: Node);
        function addChildLast(parent: Node): Node;
        function addChildFirst(parent: Node): Node;
        function addNodeAfter(sibling: Node): Node;
        function addNodeBefore(sibling: Node): Node;
        function getRoot(): Node;
        property root: Node read getRoot;
    end;

    Node = interface(_Interface) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA91}']
        procedure setIndex(index: int);
        procedure setParent(parent: Node; index: int);
        function isParent(parentForTest: Node): boolean;
        function getIndex(): int;
        function getChildrensCount(): int;
        function getChild(index: int): Node;
        function getParent(): Node;
        function getOwner(): Tree;
        function enumerateChildrens(): NodeEnumerator;
        property index: int read getIndex write setIndex;
        property owner: Tree read getOwner;
        property parent: Node read getParent;
    end;

    NodeEnumerator = interface(_Interface) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA92}']
        function nextChild(): Node;
        function nextSibling(): Node;
    end;

    BuilderOfNamespaces = interface(_Interface) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA98}']
        procedure clear();
        procedure addLexer(lexemes: Lexer);
        procedure buildNamespaces();
        function getNamespacesCount(): int;
        function getNamespace(index: int): Namespace;
        function getSystemNamespace(): Namespace;
        function getEntryPoint(): GlobalFunction;
        function findNamespace(const namespaceName: UnicodeString): Namespace;
        property systemNamespace: Namespace read getSystemNamespace;
        property entryPoint: GlobalFunction read getEntryPoint;
    end;

    BuilderOfTrees = interface(BuilderOfNamespaces) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA99}']
        function buildSyntaxTree(forFunction: GlobalFunction): Tree;
    end;

    BuilderOfTargetCode = interface(_Interface) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA9A}']
        procedure setUnicodeTextEncoder(encoder: UnicodeTextEncoder);
        procedure buildTargetCode(programme: BuilderOfTrees; stream: Output);
    end;

    UnicodeTextEncoder = interface(_Interface) ['{2187DBA7-8D82-42DE-95E7-6B43015BBA9B}']
        function encode(const text: UnicodeString): AnsiString;
    end;

    CompileError = class(Exception)
    public
        constructor create(const message: String; lexemes: Lexer; srcLine, srcChar: int); overload;
        constructor create(const message: String; lexemes: Lexer; lexIndex: int); overload;
    strict private
        fieldSourceName: UnicodeString;
        fieldSourceLine: int;
        fieldSourceChar: int;
    published
        property message;
        property sourceName: UnicodeString
                read fieldSourceName
                write fieldSourceName
                stored true;
        property sourceLine: int
                read fieldSourceLine
                write fieldSourceLine
                default 0;
        property sourceChar: int
                read fieldSourceChar
                write fieldSourceChar
                default 0;
    end;

implementation

{ CompileError }

constructor CompileError.create(const message: String; lexemes: Lexer; srcLine, srcChar: int);
begin
    inherited create(message);
    if lexemes <> nil then begin
        self.fieldSourceName := lexemes.sourceName;
    end else begin
        self.fieldSourceName := '';
    end;
    self.fieldSourceLine := srcLine;
    self.fieldSourceChar := srcChar;
end;

constructor CompileError.create(const message: String; lexemes: Lexer; lexIndex: int);
begin
    inherited create(message);
    if lexemes <> nil then begin
        self.fieldSourceName := lexemes.sourceName;
        if (lexIndex >= 0) and (lexIndex < lexemes.getLength()) then begin
            self.fieldSourceLine := lexemes.getLine(lexIndex);
            self.fieldSourceChar := lexemes.getChar(lexIndex);
        end else begin
            self.fieldSourceLine := 0;
            self.fieldSourceChar := 0;
        end;
    end else begin
        self.fieldSourceName := '';
        self.fieldSourceLine := 0;
        self.fieldSourceChar := 0;
    end;
end;

end.